C语言风格实现的开心消消乐

把整个代码分成很多小模块,初始化模块,显示模块,调整模块,运行模块。还望大牛能给出进一步优化的建议和改进。代码如下:

#include<iostream>
#include<string>
#include<vector>
#include<ctime>
using namespace std;
#define row 10
#define col 10
int score = 0;
int cnt = 0;
vector<vector<bool>>state(row, vector<bool>(col, false));
vector<vector<int>>nums(row, vector<int>(col, 0));
void 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;
}
void initgame()
{
    srand(time(0));
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            state[i][j] = false;
            nums[i][j] = rand() % 3;
        }

    }
    display();
}
bool isvalid(int x, int y)
{
    if (x < 0 || x >= row || y < 0 || y >= col || state[x][y])return false;
    return true;
}
bool 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 dfs(int x, int y,int target)
{
    if (!isvalid(x, y))return;
    if (nums[x][y] != target)return;
    state[x][y] = true;
    cnt++;
    dfs(x+1,y,target);
    dfs(x-1,y,target);
    dfs(x,y+1,target);
    dfs(x,y-1,target);
}
void 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 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))
        dfs(x,y,target);
        score += target*cnt;
        adjustment();
        display();
        if (isgameover())
        {
            cout << "gameover" << endl;
            break;
        }
    }
}
int main()
{
    
    initgame();
    playgame();
    cin.get();
    return 0;
}

 

转载于:https://www.cnblogs.com/mahaitao/p/5820826.html

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值