• 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;
    }
  • 相关阅读:
    洛谷——P1518 两只塔姆沃斯牛 The Tamworth Two
    VIjos——V 1782 借教室 | | 洛谷——P1083 借教室
    POJ——T3160 Father Christmas flymouse
    洛谷——P1155 双栈排序
    CODEVS——T1052 地鼠游戏
    python(8)- python基础数据类型
    张槎地铁站定位综合枢纽 与多条重要轨道交汇
    【佛山】地铁2号线“海口站”改“张槎站”设7换乘站
    绝对干货:保险公司决策分析系统建设方案
    android的helloworld工程目录学习
  • 原文地址:https://www.cnblogs.com/mahaitao/p/5820826.html
Copyright © 2020-2023  润新知