使用C++怎么编写一个开心消消乐游戏-创新互联

今天就跟大家聊聊有关使用C++怎么编写一个开心消消乐游戏,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

创新互联专注为客户提供全方位的互联网综合服务,包含不限于网站设计制作、成都网站建设、灵石网络推广、微信平台小程序开发、灵石网络营销、灵石企业策划、灵石品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们大的嘉奖;创新互联为所有大学生创业者提供灵石建站搭建服务,24小时服务热线:13518219792,官方网址:www.cdcxhl.com

用C++实现的开心消消乐主要分成一个一个模块去实现的,较少代码的耦合性,在这里用了一个xiaoxiaogame类去实现,其中构造函数中对数组和变量的初始化 xiaoxiaogame(int row1, int col1); 用void display();这样一个函数实现显示,用bool isvalid(int x, int y);来判断一个坐标所在的位置能不能消除, 用bool isgameover();判断游戏有没有结束,用void remove(int x, int y, int target);来消除方块,然后用void adjustment()去调试消除方块后的位置 用void playgame();来执行游戏。


代码如下:

#include
#include
#include
#include
using namespace std;

class xiaoxiaogame
{
public:
 //构造函数中对数组和变量的初始化
 xiaoxiaogame(int row1, int col1);
 //显示
 void display();
 //判断一个坐标所在的位置能不能消
 bool isvalid(int x, int y);
 //判断游戏有没有结束
 bool isgameover();
 //用深度遍历去执行消除功能
 void remove(int x, int y, int target);
 //消除方块后剩余方块的摆放位置的调整
 void adjustment();
 //执行游戏
 void playgame();
private:
 //存放游戏开心消消乐的二维数组
 vector>nums;
 //记录存在的状态
 vector>state;
 //记录分数
 int score;
 //连在一起的相同数字的个数
 int cnt;
 //开心消消乐的行
 int row;
 //开心消消乐的列
 int col;
};
xiaoxiaogame::xiaoxiaogame(int row1, int col1)
{
 row = row1;
 col = col1;
 score = 0;
 cnt = 0;
 srand(time(0));
 vector>tmp(row1,vector(col1,0));
 vector>temp(row1, vector(col1, false));
 state = temp;
 for (int i = 0; i < row; i++)
 {
 for (int j = 0; j < col; j++)
 {
  tmp[i][j] = rand() % 3;
 }
 }
 nums = tmp;
 display();
}
void xiaoxiaogame::display()
{
 for (int i = 0; i < row; i++)
 {
 for (int j = 0; j < col; j++)
 {
  if (!state[i][j])
  cout << nums[i][j] << " ";
  else cout << " ";
 }
 cout << endl;
 }
 cout << "your score is :" << score << endl;
}
bool xiaoxiaogame::isvalid(int x, int y)
{
 if (x < 0 || x >= row || y < 0 || y >= col || state[x][y])return false;
 return true;
}
bool xiaoxiaogame::isgameover()
{
 for (int i = 0; i < row; i++)
 {
 for (int j = 0; j < col; j++)
 {
  int target = nums[i][j];
  int x = i;
  int y = j;
  if (!isvalid(i, j))return false;
  if ((isvalid(x + 1, y) && nums[x + 1][y] == target) || (isvalid(x - 1, y) && nums[x - 1][y] == target) || \
  (isvalid(x, y + 1) && nums[x][y + 1] == target) || (isvalid(x, y - 1) && nums[x][y - 1] == target))
  return false;
 }
 }
 return true;
}
void xiaoxiaogame::remove(int x, int y, int target)
{
 if (!isvalid(x, y))return;
 if (nums[x][y] != target)return;
 state[x][y] = true;
 cnt++;
 remove(x + 1, y, target);
 remove(x - 1, y, target);
 remove(x, y + 1, target);
 remove(x, y - 1, target);
}
void xiaoxiaogame::adjustment()
{
 for (int j = 0; j < col; j++)
 {
 vectortmp;
 for (int i = row - 1; i >= 0; --i)
 {
  if (!state[i][j])tmp.push_back(nums[i][j]);

 }
 int r = row - 1;
 for (int i = 0; i < tmp.size(); i++)
 {
  nums[r][j] = tmp[i];
  state[r][j] = false;
  r--;
 }
 for (; r >= 0; r--)
 {
  state[r][j] = true;
 }
 }
}
void xiaoxiaogame::playgame()
{
 int x, y;
 while (cin >> x >> y)
 {
 if (!isvalid(x, y))continue;
 int target = nums[x][y];
 cnt = 0;
 if ((isvalid(x + 1, y) && nums[x + 1][y] == target) || (isvalid(x - 1, y) && nums[x - 1][y] == target) || \
  (isvalid(x, y + 1) && nums[x][y + 1] == target) || (isvalid(x, y - 1) && nums[x][y - 1] == target))
  remove(x, y, target);
 score += target*cnt;
 adjustment();
 display();
 if (isgameover())
 {
  cout << "gameover" << endl;
  break;
 }
 }
}
int main()
{
 xiaoxiaogame t(10, 10);
 t.playgame();
 cin.get();
 return 0;
}

看完上述内容,你们对使用C++怎么编写一个开心消消乐游戏有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。


分享文章:使用C++怎么编写一个开心消消乐游戏-创新互联
分享路径:http://lszwz.com/article/dejeds.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款