0

通过查看以下解决方案,有人可以告诉我如何实现检查游戏是否结束的功能吗?我通过创建“int checkEndGame”来尝试此操作,该“int checkEndGame”循环通过棋盘并返回 1 或 0,但它不起作用。

标题:

#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;
}
4

2 回答 2

2

你要这个:

int checkEndGame()
{
    for (int j = 0; j < boardHeight; ++j)
    {
        for (int i = 0; i < boardWidth; ++i)
        {
            if (board[i][j] == alive)
                return 0;
        }
    }
    return 1;
}

您的函数在看到一个单元格的那一刻返回,这是不正确的。如果您看到一个活细胞,您可以中止。但除此之外,您需要检查每个单元格。

还:

        checkEndGame();
    }
    while (checkEndGame == 0);

被打破。checkEndGame永远不会为零,因为它是指向函数的指针。你要:

}
while (checkEndGame() == 0);
于 2013-04-04T10:37:50.547 回答
0

a 你的功能应该是这样的:

int checkEndGame()
 {
 for (int j = 0; j < boardHeight; ++j)
  {
    for (int i = 0; i < boardWidth; ++i)
    {
        if (board[i][j] == alive){return 0; }
    }
  }

 return 1;
}

因为你要么找到“活着”并说游戏还没有结束,要么必须穿过洞板然后说游戏已经结束,因为你还没有找到“活着”。

如前所述:

while(checkEndGame==0) 

不运行函数来查看返回值是什么,这样做只是检查函数的内存地址是否为 0(除非您分配它,否则它肯定永远不会是)。

要运行该函数并查看您必须编写的返回值:

while(checkEndGame()==0)

(注意末尾的“()”)

于 2013-04-04T10:40:03.730 回答