Life Game生命遊戲的C++實現
阿新 • • 發佈:2018-11-24
因為上了鮑老師的課,每週都要做一次課堂練習。所以大三才想起來開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;
}
執行結果就是這樣了
(這個圖片是怎麼回事,擠扁了好可憐= =)