1. 程式人生 > 程式設計 >C++語言實現開心消消樂

C++語言實現開心消消樂

本文例項為大家分享了C++實現開心消消樂的具體程式碼,供大家參考,具體內容如下

用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<iostream>
#include<string>
#include<vector>
#include<ctime>
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 target);
 //消除方塊後剩餘方塊的擺放位置的調整
 void adjustment();
 //執行遊戲
 void playgame();
private:
 //存放遊戲開心消消樂的二維陣列
 vector<vector<int>>nums;
 //記錄存在的狀態
 vector<vector<bool>>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<vector<int>>tmp(row1,vector<int>(col1,0));
 vector<vector<bool>>temp(row1,vector<bool>(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 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,target);
 remove(x,y + 1,y - 1,target);
}
void xiaoxiaogame::adjustment()
{
 for (int j = 0; j < col; j++)
 {
  vector<int>tmp;
  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,y - 1) && nums[x][y - 1] == target))
   remove(x,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++經典小遊戲彙總

python經典小遊戲彙總

python俄羅斯方塊遊戲集合

JavaScript經典遊戲 玩不停

javascript經典小遊戲彙總

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。