用于检查游戏是否已结束的功能

时间:2013-04-04 10:33:53

标签: c++

通过查看以下解决方案,有人可以告诉我如何实现一项功能来检查游戏是否已经结束?我尝试通过创建“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;
}

2 个答案:

答案 0 :(得分: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;
}

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

此外:

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

坏了。 checkEndGame永远不会为零,因为它是指向函数的指针。你想要:

}
while (checkEndGame() == 0);

答案 1 :(得分:0)

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

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)

(注意结尾处的“()”)