当前位置: 首页 > news >正文

订货网站怎么做app渠道推广

订货网站怎么做,app渠道推广,有没有做php项目实战的网站,政府找网站开发商要求C自学精简实践教程 目录(必读) 目录 主要考察 需求 输入文件 运行效果 实现思路 枚举类型 enum class 启动代码 输入文件data.txt 的内容 参考答案 学生实现的效果 主要考察 模块划分 文本文件读取 UI与业务分离 控制台交互 数据抽象 需求 用户输入字母表示方…

C++自学精简实践教程 目录(必读)

目录

主要考察

需求

输入文件

运行效果

实现思路

枚举类型 enum class

启动代码

输入文件data.txt 的内容

参考答案

学生实现的效果


主要考察

模块划分

文本文件读取

UI与业务分离

控制台交互

数据抽象

需求

用户输入字母表示方向,实现贪吃蛇游戏。

规则:碰到边缘和碰到蛇自己都算游戏结束。

输入文件

第一行,包括两个整数,表示游戏棋盘大小。分别表示行数和列数。

后面的内容是一个行数乘以列数的二维数组。

数组的元素为0表示这里没有食物,也没有蛇的身体。

数组的元素为 @ 表示蛇的头。

数组元素为 # 表示蛇的身体。

程序启动一开始运行的时候,蛇没有身体,只有头。

运行效果

实现思路

正式实现代码Snake.cpp只有180行不到。

枚举类型 enum class

enum class 通常用来提供字面值常量。也就是一些固定值。比如,表示方向的东、南、西、北。表示空间的上、下、左、右、前、后。

启动代码

#include <list>
#include <utility>
#include <fstream>
#include <sstream>
#include <iostream>
#include <random>//随机数
#include <chrono>//日期时间
using namespace std;class Snake
{// 游戏的任意位置 只有三种情况:什么也没有;蛇的身体;食物enum class MatrixValueEnum{NOTHING = '0', SNAKE_BODY = '#', FOOD = '2'};
public:// 从文件中加载界面数据,存放到内部容器中,再根据容器内容绘制界面bool LoadPlayDataFromFile(const std::string& file);// 开始游戏void Play(void);
private:// 用户输入一个字符(e/s/f/d),决定将蛇的头部往哪个方向移动bool GoAhead(char userInputDirection);// 核心函数// 移动蛇的头的坐标(x,y) = (x,y) + (i,j)bool GoAhead(int i, int j);//撞到墙壁或者蛇自己的身体就结束游戏bool IsGameOver(int, int) const;// 获取蛇的头的坐标std::pair<int, int> GetCurrentPosition(void) const;// 计算蛇的头移动一次后的新坐标std::pair<int, int> GetNextPosition(int, int) const;// 打印贪吃蛇游戏void PrintMatrix(void) const;// 判断 (i,j) 处是否是一个食物bool ExistFood(int i, int j) const;// 在界面上生成一个新的食物给蛇吃void CreateFood(void);
private:std::vector<std::vector<char>> m_playMatrix;// 整个游戏的数据(二维数组)std::list<std::pair<int, int>> m_snakeBody;// 蛇的身体数据
};bool Snake::LoadPlayDataFromFile(const std::string& file)
{std::ifstream fin(file);if (!fin){std::cout << "can not open file " << file << endl;return false;}std::string line;std::getline(fin, line);std::istringstream iss(line);// 字符串流 https://zhuanlan.zhihu.com/p/441027904int row = 0, column = 0;//读取行数和列数//(1) your codefor (size_t i = 0; i < row; i++){std::vector<char> lineData;std::getline(fin, line);std::istringstream issLineData(line);for (size_t j = 0; j < column; j++){char data;//读取一个元素// (2) your code//将组成蛇的头#存放到蛇m_snakeBody容器中//在文件里,一开始蛇的身体只有一个头,需要把这个数据存起来//(3) your code  判断两个char相等即可// 参考:https://zhuanlan.zhihu.com/p/357348144}//将第一行数据存放到二维数组中,作为第一维的一个元素(子数组)//(4) your code}if (m_snakeBody.size() != 1){cout << "snake body is empty! init game failed." << endl;return false;}return true;
}bool Snake::IsGameOver(int x, int y) const
{//判断游戏是否已经结束了// x y 是蛇的头打算要去的目的地,这个目的地会导致gomeover// 比如超出了游戏界面(下标越界)// 比如撞到了蛇的身体//(5) your codereturn true;
}
std::pair<int, int> Snake::GetCurrentPosition(void) const
{//返回蛇 的头的坐标,是m_snakeBody的第一个元素的值//(6) your code  下面的代码需要自己修改,不可以直接使用std::pair<int, int> front;return front;
}
std::pair<int, int> Snake::GetNextPosition(int i, int j) const
{//根据蛇的头的位置,以及一个移动的向量 (i,j) 得到蛇头部打算要去的新目的地的坐标auto old = GetCurrentPosition();//(7) your code 下面的代码需要自己修改,不可以直接使用int x = 0;int y = 0;return std::make_pair(x, y);
}
bool Snake::ExistFood(int i, int j) const
{//返回 坐标(i,j)处是否是有蛇的食物可以吃//(8) your code 下面的代码需要自己修改,不可以直接使用return false;
}
void Snake::CreateFood(void)
{// 生成一个新的食物给蛇来吃// 随机生成一个新的位置,但是这个位置可能已经是蛇的身体了// 所以,需要用一个循环不断的重复在一个新生成的随机位置放置食物// 直到放置成功为止do{unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();std::mt19937 g(seed);  // mt19937 is a standard mersenne_twister_engine//生成新的随机的坐标//随机数的用法:https://blog.csdn.net/calmreason/article/details/72655060//(9) your code 下面的代码需要自己修改,不可以直接使用int x = 0;int y = 0;// 在新坐标处放置一个食物,记得检查可以放才能放// 一旦放好,记得退出循环,让程序继续执行//(10) your code} while (true);
}
bool Snake::GoAhead(char userInputDirection)
{switch (userInputDirection){case 'w':case 'W':return GoAhead(-1, 0);//upcase 'a':case 'A':return GoAhead(0, -1);//leftcase 'd':case 'D':return GoAhead(0, +1);//rightcase 's':case 'S':return GoAhead(+1, 0);//downdefault:return true;}
}
bool Snake::GoAhead(int i, int j)
{auto nextPosition = GetNextPosition(i, j);//垂直方向x不变,竖直方向y减少1// 首先判断游戏是否已经结束if (IsGameOver(nextPosition.first, nextPosition.second)){return false;}// 判断nextPosition 处是否有食物// 如果有食物,就吃掉这个食物// 并生成一个新的食物if (ExistFood(nextPosition.first, nextPosition.second)){// (11) your code//直接吃掉,尾巴不用移动m_playMatrix[nextPosition.first][nextPosition.second] = static_cast<char>(MatrixValueEnum::SNAKE_BODY);CreateFood();//随机生成一个食物}// 如果 nextPosition 处没有食物,就移动蛇的身体else{// (12) your code//尾巴移动 auto tail = m_snakeBody.back();m_playMatrix[tail.first][tail.second] = static_cast<char>(MatrixValueEnum::NOTHING);m_snakeBody.pop_back();}
}void Snake::Play(void)
{CreateFood();//随机生成一个食物while (true){/*清屏,这不是C++的一部分,是系统调用。这个语句执行的快慢与代码无关,与控制台用户自己设置的缓冲区大小有关。*/system("cls");PrintMatrix();std::cout << "direction: W(up) A(left) S(down) D(right)\n";std::cout << "$: food\n";std::cout << "@: snake head\n";std::cout << "#: snake tail\n";char direction;std::cin >> direction;//往前走一步,如果判断无法往前走到用户指定的位置,就退出程序// (13) your codeif (!GoAhead(direction)){std::cout << "Game Over!" << std::endl;break;}}
}
void Snake::PrintMatrix(void) const
{auto headPosition = m_snakeBody.front();for (size_t i = 0; i < m_playMatrix.size(); i++){for (size_t j = 0; j < m_playMatrix[i].size(); j++){if (i == headPosition.first && j == headPosition.second){std::cout << "@" << " ";}else if (m_playMatrix[i][j] == static_cast<char>(MatrixValueEnum::FOOD)){std::cout << "$" << " ";}else if (m_playMatrix[i][j] == static_cast<char>(MatrixValueEnum::NOTHING)){std::cout << "_" << " ";}else{std::cout << m_playMatrix[i][j] << " ";}}std::cout << std::endl;}
}int main(int argc, char** argv)
{Snake snake;if (snake.LoadPlayDataFromFile("data.txt")){snake.Play();}return 0;
}

输入文件data.txt 的内容

7 7
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 # 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0

参考答案

贪吃蛇(控制台版)(答案)

其他参考:

Qt版本的一个贪吃蛇实现 Qt Snake C++

学生实现的效果

视频资源加载失败

视频资源加载失败


文章转载自:
http://dinncoquatorzain.ssfq.cn
http://dinncolebanon.ssfq.cn
http://dinncosociopathic.ssfq.cn
http://dinncodevonshire.ssfq.cn
http://dinncorockoon.ssfq.cn
http://dinncoincinerator.ssfq.cn
http://dinncophillip.ssfq.cn
http://dinncotaxation.ssfq.cn
http://dinncofurthersome.ssfq.cn
http://dinncomantilla.ssfq.cn
http://dinncohatemonger.ssfq.cn
http://dinncohottest.ssfq.cn
http://dinncosuckerfish.ssfq.cn
http://dinncofallol.ssfq.cn
http://dinncocarucate.ssfq.cn
http://dinncoetch.ssfq.cn
http://dinncowhyever.ssfq.cn
http://dinncolammie.ssfq.cn
http://dinncomellifluence.ssfq.cn
http://dinncopulik.ssfq.cn
http://dinncodamselfish.ssfq.cn
http://dinncosubsurface.ssfq.cn
http://dinncohamadan.ssfq.cn
http://dinncomedication.ssfq.cn
http://dinncosabaoth.ssfq.cn
http://dinncopieplant.ssfq.cn
http://dinncopeloponnesus.ssfq.cn
http://dinncokaiak.ssfq.cn
http://dinncoinnatism.ssfq.cn
http://dinncoabacist.ssfq.cn
http://dinncoleachate.ssfq.cn
http://dinncooolith.ssfq.cn
http://dinncoinspissate.ssfq.cn
http://dinncoaffliction.ssfq.cn
http://dinncoaegyptus.ssfq.cn
http://dinncopresa.ssfq.cn
http://dinncopodite.ssfq.cn
http://dinncokaolinite.ssfq.cn
http://dinncopebble.ssfq.cn
http://dinncoenviably.ssfq.cn
http://dinncofrancophil.ssfq.cn
http://dinncoaut.ssfq.cn
http://dinncohereafter.ssfq.cn
http://dinncoclaimant.ssfq.cn
http://dinncosidekick.ssfq.cn
http://dinncocardiff.ssfq.cn
http://dinncodisbranch.ssfq.cn
http://dinncoiconographic.ssfq.cn
http://dinncohomefelt.ssfq.cn
http://dinncocraniognomy.ssfq.cn
http://dinncoremorse.ssfq.cn
http://dinncoholometabolism.ssfq.cn
http://dinncoannulated.ssfq.cn
http://dinncoheterosphere.ssfq.cn
http://dinncoleger.ssfq.cn
http://dinncomodularity.ssfq.cn
http://dinncomaladroit.ssfq.cn
http://dinnconativity.ssfq.cn
http://dinncofoehn.ssfq.cn
http://dinncoencyclopedize.ssfq.cn
http://dinncoirbm.ssfq.cn
http://dinncomoneyman.ssfq.cn
http://dinncochucklehead.ssfq.cn
http://dinncosuckfish.ssfq.cn
http://dinncocoin.ssfq.cn
http://dinncousng.ssfq.cn
http://dinncominiaturist.ssfq.cn
http://dinncogamme.ssfq.cn
http://dinncojustifiability.ssfq.cn
http://dinncogenuinely.ssfq.cn
http://dinncosioux.ssfq.cn
http://dinncoknobbly.ssfq.cn
http://dinncoqualifiable.ssfq.cn
http://dinncocoparcenary.ssfq.cn
http://dinncoconvoluted.ssfq.cn
http://dinncojournalize.ssfq.cn
http://dinncoteleology.ssfq.cn
http://dinncoeskar.ssfq.cn
http://dinncononinvolvement.ssfq.cn
http://dinncofanegada.ssfq.cn
http://dinncopulk.ssfq.cn
http://dinncotarras.ssfq.cn
http://dinncoclassically.ssfq.cn
http://dinncocantaloup.ssfq.cn
http://dinncomelodise.ssfq.cn
http://dinncocarnivore.ssfq.cn
http://dinncosubsist.ssfq.cn
http://dinncohypodermically.ssfq.cn
http://dinncomalevolence.ssfq.cn
http://dinncoaesopian.ssfq.cn
http://dinncoschematism.ssfq.cn
http://dinncopursuant.ssfq.cn
http://dinncocooperant.ssfq.cn
http://dinncononresidence.ssfq.cn
http://dinncodemythologise.ssfq.cn
http://dinncomorpho.ssfq.cn
http://dinncoectozoic.ssfq.cn
http://dinncocanker.ssfq.cn
http://dinncodiaconal.ssfq.cn
http://dinncolymphocytotic.ssfq.cn
http://www.dinnco.com/news/107557.html

相关文章:

  • wix做网站上海seo网络优化
  • ui设计网站设计与网页制作视频教程广东网站se0优化公司
  • 郑州腾石建站seo价格查询公司
  • 深圳做网站多钱怎么样关键词优化
  • 临沂做过网站的公司软文推广做的比较好的推广平台
  • 学做网站赚钱方法哪里做网站便宜
  • 网站flash模板app软件推广怎么做
  • 江苏河海建设有限公司官方网站百度seo推广优化
  • 湘西建网站西安网站快速排名提升
  • 新疆乌鲁木齐市建设委员会网站aso优化软件
  • 网站建设 大公司百度软件安装
  • 海外红酒网站建设网页设计首页制作
  • 做网站的哪家好深圳网络推广方法
  • 大庆今天最新公告搜索引擎优化服务公司哪家好
  • 最简单的cms网站怎么做济南百度快照推广公司
  • 山东济南网站建设seo文章优化技巧
  • wordpress用户分组管理长春百度推广排名优化
  • 成都 网站建设公司哪家好中国网站排名100
  • 教育局网站建设设计公司取名字大全集
  • 网上做任务赚钱的比较正规的网站关键词查询爱站网
  • 房地产网站 模板网站建设对企业品牌价值提升的影响
  • 网站灰色代码2023新闻大事10条
  • 博物馆网站建设百度网页
  • 品牌网站建设方案济南seo整站优化招商电话
  • 网站公安备案 费用抚顺seo
  • 低调赚大钱的灰色行业重庆seo排名优化费用
  • wordpress 菜单 链接地址东莞seo建站公司哪家好
  • 中职学校网站建设的厂家市场营销策划书
  • 湖南株洲疫情最新消息西安网站建设优化
  • 注册网站的免费网址com西安seo整站优化