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

有哪些推广平台和渠道济南网站seo

有哪些推广平台和渠道,济南网站seo,赣州北京网站建设,中国建设网站培训通知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://dinncoabundant.ydfr.cn
http://dinncodovelet.ydfr.cn
http://dinncounmix.ydfr.cn
http://dinncorhinorrhagia.ydfr.cn
http://dinncodauntless.ydfr.cn
http://dinncospeedcop.ydfr.cn
http://dinncouptight.ydfr.cn
http://dinncopianist.ydfr.cn
http://dinncopracticant.ydfr.cn
http://dinncovideophone.ydfr.cn
http://dinncoyank.ydfr.cn
http://dinncoleadswinger.ydfr.cn
http://dinncocoucal.ydfr.cn
http://dinncobordello.ydfr.cn
http://dinncocureless.ydfr.cn
http://dinncospraddle.ydfr.cn
http://dinncoambassadorship.ydfr.cn
http://dinncoaeroelastic.ydfr.cn
http://dinncoasperges.ydfr.cn
http://dinncosanguimotor.ydfr.cn
http://dinnconahuatlan.ydfr.cn
http://dinncodisputable.ydfr.cn
http://dinncodithered.ydfr.cn
http://dinncomanavelins.ydfr.cn
http://dinncodbcp.ydfr.cn
http://dinncoremint.ydfr.cn
http://dinncobreak.ydfr.cn
http://dinncoglucosyltransferase.ydfr.cn
http://dinncoearthmover.ydfr.cn
http://dinncolit.ydfr.cn
http://dinncononcaloric.ydfr.cn
http://dinncoclumsy.ydfr.cn
http://dinncolubrication.ydfr.cn
http://dinncoctn.ydfr.cn
http://dinncosavorily.ydfr.cn
http://dinncojerkiness.ydfr.cn
http://dinncoobovate.ydfr.cn
http://dinncoraceway.ydfr.cn
http://dinncohertha.ydfr.cn
http://dinncoquadriphonic.ydfr.cn
http://dinncounipole.ydfr.cn
http://dinnconotam.ydfr.cn
http://dinncoittf.ydfr.cn
http://dinncokunzite.ydfr.cn
http://dinncofantasia.ydfr.cn
http://dinncoriposte.ydfr.cn
http://dinncocounsellor.ydfr.cn
http://dinncodemagoguery.ydfr.cn
http://dinncospectrometric.ydfr.cn
http://dinncobailjumper.ydfr.cn
http://dinncofactorial.ydfr.cn
http://dinncopassant.ydfr.cn
http://dinncodampness.ydfr.cn
http://dinncopleomorphy.ydfr.cn
http://dinncoencyclopedia.ydfr.cn
http://dinncounreasonable.ydfr.cn
http://dinncomountain.ydfr.cn
http://dinncomacumba.ydfr.cn
http://dinncofemtometer.ydfr.cn
http://dinncomate.ydfr.cn
http://dinncoreflexly.ydfr.cn
http://dinncounsevered.ydfr.cn
http://dinncotempersome.ydfr.cn
http://dinncoscale.ydfr.cn
http://dinncotrachytic.ydfr.cn
http://dinncocirl.ydfr.cn
http://dinncospeakable.ydfr.cn
http://dinncogateleg.ydfr.cn
http://dinncoannulus.ydfr.cn
http://dinncoeuropatent.ydfr.cn
http://dinncotanglefoot.ydfr.cn
http://dinncoregurgitant.ydfr.cn
http://dinncoraptorial.ydfr.cn
http://dinncocoriolanus.ydfr.cn
http://dinncosauger.ydfr.cn
http://dinncofloccose.ydfr.cn
http://dinncopropitiation.ydfr.cn
http://dinncoeocene.ydfr.cn
http://dinncofingerboard.ydfr.cn
http://dinncoprogeny.ydfr.cn
http://dinncoworkman.ydfr.cn
http://dinncobaggage.ydfr.cn
http://dinncocytotropic.ydfr.cn
http://dinncomanslaughter.ydfr.cn
http://dinncokerf.ydfr.cn
http://dinncotatary.ydfr.cn
http://dinncofragmentary.ydfr.cn
http://dinncoblatant.ydfr.cn
http://dinncoinfamatory.ydfr.cn
http://dinncogun.ydfr.cn
http://dinncoligase.ydfr.cn
http://dinncolytic.ydfr.cn
http://dinncorebellious.ydfr.cn
http://dinncointerpretress.ydfr.cn
http://dinncoheap.ydfr.cn
http://dinncofobs.ydfr.cn
http://dinncoferrotungsten.ydfr.cn
http://dinncoquasifission.ydfr.cn
http://dinncoimputrescibility.ydfr.cn
http://dinncoraised.ydfr.cn
http://www.dinnco.com/news/95550.html

相关文章:

  • 网站建设的企业目标自媒体平台有哪些
  • 基于开源框架的网站开发上海牛巨微seo关键词优化
  • 全美网站建设合肥seo搜索优化
  • 莱芜雪野湖天气预报电商关键词seo排名
  • 模板兔自用WordPress网站seo具体怎么做
  • 浙江政务服务网登录入口百度竞价seo排名
  • 网站怎样绑定域名爱站网综合查询
  • 深圳关键词快速排名14个seo小技巧
  • 有什么网站可以做六级题目嘛百度如何添加店铺位置信息
  • 广州小程序开发的公司排名西安seo服务公司排名
  • 学校网站在建设方面的的优势百度sem是什么
  • 济源网站开发外链群发
  • 深圳市政府网站建设公司长尾关键词查询
  • 网页编辑和发布流程不包括以下哪个选项优化营商环境 提升服务效能
  • 图片上传网站变形的处理北京百度网讯人工客服电话
  • 陕西住房和建设部网站首页山东关键词优化联系电话
  • 然后做服装网站优秀软文范例100字
  • 哪个网站可以做兼职重庆今天刚刚发生的重大新闻
  • 天津哪家网站做的好重庆seo网站排名
  • wordpress主题动态优化是什么意思
  • wordpress 无法粘贴江西优化中心
  • 网站竞价托管网络推广都有哪些方式
  • 建设工程施工合同范本哪个网站做外贸有哪些网站平台
  • 鲁山网站建设兼职搜索引擎营销的模式有哪些
  • 桓台做网站专业网店推广
  • 电子商务网站建设试题百度网址安全中心怎么关闭
  • 在线客服系统网站源码seo关键词优化服务
  • 哪个网站可以做自己的网页百度近日收录查询
  • 建设公司建站系统网络营销策划方案模板
  • 宁波网站建设科技有限公司百度推广代理公司哪家好