以下の解決策を見て、ゲームが終了したかどうかを確認する機能を実装する方法を教えてください。ボードをループして 1 または 0 を返す「int checkEndGame」を作成してこれを試みましたが、機能しません。
ヘッダ:
#include <iostream>
#include <string>
#include <time.h>
int generations;
int boardWidth;
int boardHeight;
char** board;
char** boardTmp;
char deadOrAlive[] = {' ', 'x'};
char dead = deadOrAlive[0];
char alive = deadOrAlive[1];
char lookLeft(int i, int j)
{
if ( i == 0)
{
return dead;
}
return board[i - 1][j];
}
char lookRight(int i, int j)
{
if (i == boardWidth - 1)
{
return dead;
}
return board[i + 1][j];
}
char lookUp(int i, int j)
{
if (j == 0)
{
return dead;
}
return board[i][j - 1];
}
char lookDown(int i, int j)
{
if (j == boardHeight - 1)
{
return dead;
}
return board[i][j + 1];
}
char lookUpLeft(int i, int j)
{
if (i == 0 || j == 0)
{
return dead;
}
return board[i - 1][j - 1];
}
char lookUpRight(int i, int j)
{
if(i == boardWidth - 1 || j == 0)
{
return dead;
}
return board[i + 1][j + 1];
}
char lookDownLeft(int i, int j)
{
if (j == boardHeight - 1 || i == 0)
{
return dead;
}
return board[i - 1][j + 1];
}
char lookDownRight(int i, int j)
{
if (j == boardHeight - 1 || i == boardWidth - 1)
{
return dead;
}
return board[i + 1][j + 1];
}
char ans;
void init();
void setBoard();
void showBoard();
void verifyDeadOrAlive();
int getNeighbors(int i, int j);
void swap();
void sleep(unsigned int mseconds);
int checkEndGame();
void playGame();
c++:
#include "stdafx.h"
#include "gameoflife.h"
using namespace std;
void init()
{
cout << "How many generations would you like to cycle through? ";
cin >> generations;
cout << "Specify a width for the board: ";
cin >> boardWidth;
cout << "Specify a height for the board: ";
cin >> boardHeight;
}
void setBoard()
{
srand(time(0));
board = new char*[boardWidth];
boardTmp = new char*[boardWidth];
for (int i = 0; i < boardWidth; ++i)
{
board[i] = new char[boardHeight];
boardTmp[i] = new char[boardHeight];
for (int j = 0; j < boardHeight; ++j)
{
board[i][j] = deadOrAlive[rand() % generations];
boardTmp[i][j] = ' ';
}
}
}
void showBoard()
{
system("cls");
for (int j = 0; j < boardHeight; ++j)
{
for (int i = 0; i < boardWidth; ++i)
{
cout << board[i][j];
}
cout << endl;
}
}
void verifyDeadOrAlive()
{
for (int j = 0; j < boardHeight; ++j)
{
for (int i = 0; i < boardWidth; ++i)
{
int neighbors = getNeighbors(i, j);
if (board[i][j] == alive)
{
if (neighbors < 2 || neighbors > 3)
{
boardTmp[i][j] = dead;
}
else
{
boardTmp[i][j] = alive;
}
}
else
{
if (neighbors == 3)
{
boardTmp[i][j] = alive;
}
else
{
boardTmp[i][j] = dead;
}
}
}
}
swap();
}
int getNeighbors(int i, int j)
{
int x = 0;
if (lookLeft(i, j) == alive)
{
x++;
}
if (lookRight(i, j) == alive)
{
x++;
}
if (lookUp(i, j) == alive)
{
x++;
}
if (lookDown(i, j) == alive)
{
x++;
}
if (lookUpLeft(i, j) == alive)
{
x++;
}
if (lookUpRight(i, j) == alive)
{
x++;
}
if (lookDownLeft(i, j) == alive)
{
x++;
}
if (lookDownRight(i, j) == alive)
{
x++;
}
return x;
}
void swap()
{
char** tmp = board;
board = boardTmp;
boardTmp = tmp;
}
void sleep(unsigned int mseconds)
{
clock_t goal = mseconds + clock();
while (goal > clock());
}
int checkEndGame()
{
for (int j = 0; j < boardHeight; ++j)
{
for (int i = 0; i < boardWidth; ++i)
{
if (board[i][j] == dead)
{
return 1;
}
else
{
return 0;
}
}
}
}
void playGame()
{
init();
setBoard();
do
{
showBoard();
sleep(500);
verifyDeadOrAlive();
checkEndGame();
}
while (checkEndGame == 0);
}
int main()
{
do
{
playGame();
cout << "Would you like to play again? y/n: ";
cin >> ans;
system("cls");
}
while (ans == 'Y' || ans == 'y');
return 0;
}