C++語(yǔ)言實(shí)現(xiàn)開心消消樂
本文實(shí)例為大家分享了C++實(shí)現(xiàn)開心消消樂的具體代碼,供大家參考,具體內(nèi)容如下
用C++實(shí)現(xiàn)的開心消消樂主要分成一個(gè)一個(gè)模塊去實(shí)現(xiàn)的,較少代碼的耦合性,在這里用了一個(gè)xiaoxiaogame類去實(shí)現(xiàn),其中構(gòu)造函數(shù)中對(duì)數(shù)組和變量的初始化 xiaoxiaogame(int row1, int col1); 用void display();這樣一個(gè)函數(shù)實(shí)現(xiàn)顯示,用bool isvalid(int x, int y);來判斷一個(gè)坐標(biāo)所在的位置能不能消除, 用bool isgameover();判斷游戲有沒有結(jié)束,用void remove(int x, int y, int target);來消除方塊,然后用void adjustment()去調(diào)試消除方塊后的位置 用void playgame();來執(zhí)行游戲。
代碼如下:
#include<iostream>
#include<string>
#include<vector>
#include<ctime>
using namespace std;
class xiaoxiaogame
{
public:
//構(gòu)造函數(shù)中對(duì)數(shù)組和變量的初始化
xiaoxiaogame(int row1, int col1);
//顯示
void display();
//判斷一個(gè)坐標(biāo)所在的位置能不能消
bool isvalid(int x, int y);
//判斷游戲有沒有結(jié)束
bool isgameover();
//用深度遍歷去執(zhí)行消除功能
void remove(int x, int y, int target);
//消除方塊后剩余方塊的擺放位置的調(diào)整
void adjustment();
//執(zhí)行游戲
void playgame();
private:
//存放游戲開心消消樂的二維數(shù)組
vector<vector<int>>nums;
//記錄存在的狀態(tài)
vector<vector<bool>>state;
//記錄分?jǐn)?shù)
int score;
//連在一起的相同數(shù)字的個(gè)數(shù)
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 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++)
{
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 - 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;
}
更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專題,分享給大家:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++中成員函數(shù)和友元函數(shù)的使用及區(qū)別詳解
大家好,本篇文章主要講的是C++中成員函數(shù)和友元函數(shù)的使用及區(qū)別詳解,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01
C++利用easyx圖形庫(kù)實(shí)現(xiàn)創(chuàng)意天天酷跑小游戲
這篇文章主要為大家詳細(xì)介紹了C++如何利用easyx圖形庫(kù)實(shí)現(xiàn)創(chuàng)意小游戲——天天酷跑,文中的示例代碼講解詳細(xì),快跟隨小編一起了解一下吧2023-03-03
C語(yǔ)言 棧的表示和實(shí)現(xiàn)詳細(xì)介紹
這篇文章主要介紹了C語(yǔ)言 棧的表示和實(shí)現(xiàn)詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2016-12-12
C++實(shí)現(xiàn)猜數(shù)小游戲的實(shí)現(xiàn)
這篇文章主要介紹了C++實(shí)現(xiàn)猜數(shù)小游戲的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
C++ stack與queue模擬實(shí)現(xiàn)詳解
這篇文章主要給大家介紹了關(guān)于c++stack與queue模擬實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-08-08
簡(jiǎn)單了解設(shè)計(jì)模式中的裝飾者模式及C++版代碼實(shí)現(xiàn)
這篇文章主要介紹了簡(jiǎn)單了解設(shè)計(jì)模式中的裝飾者模式及C++版代碼實(shí)現(xiàn),ConcreteComponent的引用(指針)也可以達(dá)到修飾的功能,需要的朋友可以參考下2016-03-03

