Life Game生命游戏的C++实现

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37717751/article/details/84438408

因为上了鲍老师的课,每周都要做一次课堂练习。所以大三才想起来开C++的坑= =

代码是课上一个半小时写出来的。难免有考虑不周的地方。如果有什么问题,请各位不吝赐教~

首先解释一下Life Game。开局一张棋盘,随便放几个棋子,然后每回合棋盘做一次更新,对棋盘的每个格子来说,看它周围的9个邻居在旧棋盘中的状态,有两个棋子则这个格子下回合是有棋的,有三个棋子则这个格子下回合状态不变,其余情况这个格子下回合没有棋子。(鲍老师语:三活两不变其余死)

实现思路嘛,数组开一张棋盘(注意边界问题,可以多开两行两列作为边界这样判断起来方便一些),之后每个回合,先另开一个棋盘保存下旧棋盘,按旧棋盘的状态去更新棋盘。

下面上代码

Life.h

#ifndef LIFE_H_INCLUDED
#define LIFE_H_INCLUDED

#define maxRow 20
#define maxCol 60

class Life {
public:
    void initialize();  // 创建棋盘
    void show();  // 打印棋盘
    void judge();  // 判断并更新下一轮的情况

private:
    int board[maxRow + 2][maxCol + 2];  // 棋盘留出外面一圈作为边界。0为无棋,1为有棋
};

#endif // LIFE_H_INCLUDED

Life.cpp

#include "Life.h"
#include <iostream>

using namespace std;

void Life::initialize()
{
    int i, j;
    bool flag = false;

    // 初始化棋盘,让所有元素先等于0
    for (i = 0; i < maxRow + 1; i++) {
        for (j = 0; j < maxCol + 1; j++) {
            board[i][j] = 0;
        }
    }

    int tmpRow, tmpCol;
    cout << "请输入初始棋子的坐标,以x y形式输入,以-1 -1结束" << endl;
    cin >> tmpRow >> tmpCol;

    // 判断输入合法性
    while (tmpRow != -1 && tmpCol != -1) {
        if (tmpRow >= 1 && tmpRow <= maxRow - 2) {
            if (tmpCol >= 1 && tmpCol <= maxCol - 2) {
                board[tmpRow][tmpCol] = 1;
                flag = true;
            }
            else {
                cout << "列数输入错误。请重新输入" << endl;
                flag = false;
            }
        }
        else {
            cout << "行数输入错误。请重新输入" << endl;
            flag = false;
        }
        if (flag) {
            cout << "继续输入或退出输入" << endl;
            cin >> tmpRow >> tmpCol;
        }
        else {
            cin >> tmpRow >> tmpCol;
        }
    }
}

void Life::show()
{
    int i, j;
    cout << endl;
    for (i = 1; i < maxRow + 1; i++) {
        for (j = 1; j < maxCol + 1; j++) {
            if (board[i][j] == 1) {
                cout << "*";
            }
            else {
                cout << "-";
            }
        }
        cout << endl;
    }
    cout << endl;
}

void Life::judge()
{
    int i, j;
    int ii, jj;
    int newBoard[maxRow + 2][maxCol + 2];  // 用一个新的棋盘来存放更新后的情况,之后将新棋盘赋值给旧棋盘
    int tmpCount;  // 计算邻居数

    for (i = 1; i < maxRow + 1; i++) {
        for (j = 1; j < maxCol + 1; j++) {
            newBoard[i][j] = 0;
        }
    }

    for (i = 1; i < maxRow + 1; i++) {
        for (j = 1; j < maxCol + 1; j++) {
            tmpCount = 0;

            // 计算邻居数
            for (ii = i - 1; ii <= i + 1; ii++) {
                for (jj = j - 1; jj <= j + 1; jj++) {
                    if (board[ii][jj] == 1) {
                        tmpCount++;
                    }
                }
            }

            tmpCount -= board[i][j];  // 去掉自己

            // 计算棋子生死
            if (tmpCount == 2) {
                newBoard[i][j] = board[i][j];
            }
            else if (tmpCount == 3) {
                newBoard[i][j] = 1;
            }
            else {
                newBoard[i][j] = 0;
            }
        }
    }

    // 将新棋盘赋给旧棋盘
    for (i = 1; i < maxRow + 1; i++) {
        for (j = 1; j < maxCol + 1; j++) {
            board[i][j] = newBoard[i][j];
        }
    }
}

main.cpp

#include "Life.h"
#include "iostream"

using namespace std;

int main(void)
{
    Life l;
    char tmp;
    cout << "Welcome to Li Qingquan's game of life." << endl;
    cout << "This game uses a grid of size 20 by 60 in witch" << endl;
    cout << "each cell can either be occupied by an organism or not." << endl;
    cout << "according to the number of neighbouring cells which are alive." << endl;
    cout << "List the coordinates for living cells." << endl;
    l.initialize();
    l.show();
    cout << "请输入是否继续(Y/N)?" << endl;
    cin >> tmp;
    while (tmp != 'Y' && tmp != 'y' && tmp != 'N' && tmp != 'n') {
        cout << "输入错误,请重新输入(Y/N)" << endl;
        cin >> tmp;
    }
    while (tmp == 'Y' || tmp == 'y') {
        l.judge();
        l.show();
        cout << "请输入是否继续(Y/N)?" << endl;
        cin >> tmp;
        while (tmp != 'Y' && tmp != 'y' && tmp != 'N' && tmp != 'n') {
            cout << "输入错误,请重新输入(Y/N)" << endl;
            cin >> tmp;
        }
    }
    cout << "感谢试玩!" << endl;
    return 0;
}

运行结果就是这样了

(这个图片是怎么回事,挤扁了好可怜= =)

猜你喜欢

转载自blog.csdn.net/m0_37717751/article/details/84438408
今日推荐