无法从if和if else传递数据

时间:2013-04-03 13:23:10

标签: c++

我正在尝试创建一个简单的tic tac toe程序作为一些功课的一部分,但很难检查游戏是否已经获胜。一旦我启动该程序并输入一个数字,它立即说“玩家2胜!”我看不出我做错了什么。

#include "stdafx.h"
#include "tictactoe.h"

using namespace std;

void clearBoard(char gameBoard[])
{
    for (int i = 0; i < board_size; i++)
    {
        gameBoard[i] = free_char;
    }
}

void printBoard(char gameBoard[])
{
    system("cls");

    //for (int i = 0; i < board_width; i++)
    //{
        //cout << gameBoard[i] + 1 << " " << gameBoard[i] + 1 << " " << gameBoard[i] + 1 << endl;
    //}

    cout << gameBoard[0] << " " << gameBoard[1] << " " << gameBoard[2] << endl;
    cout << gameBoard[3] << " " << gameBoard[4] << " " << gameBoard[5] << endl;
    cout << gameBoard[6] << " " << gameBoard[7] << " " << gameBoard[8] << endl;
}

int askMove(int player)
{
    cout << "Player " << player << ", Select a square [1-9]: ";
    cin >> choice;

    return 0;
}

bool verifyMakeMove(char gameBoard[], int move, int player)
{
    if (player == 1)
    {
        mark = p1_char;
    }
    else
    {
        mark = p2_char;
    }

    if (move < 1 || move > 9)
    {
        cout << "You did not enter a number between 1 and 9. Try again." << endl;
    }

    if (move == 1 && gameBoard[0] == free_char)
    {
        gameBoard[0] = mark;
    }
    else if (move == 2 && gameBoard[1] == free_char)
    {
        gameBoard[1] = mark;
    }
    else if (move == 3 && gameBoard[2] == free_char)
    {
        gameBoard[2] = mark;
    }
    else if (move == 4 && gameBoard[3] == free_char)
    {
        gameBoard[3] = mark;
    }
    else if (move == 5 && gameBoard[4] == free_char)
    {
        gameBoard[4] = mark;
    }
    else if (move == 6 && gameBoard[5] == free_char)
    {
        gameBoard[5] = mark;
    }
    else if (move == 7 && gameBoard[6] == free_char)
    {
        gameBoard[6] = mark;
    }
    else if (move == 8 && gameBoard[7] == free_char)
    {
        gameBoard[7] = mark;
    }
    else if (move == 9 && gameBoard[8] == free_char)
    {
        gameBoard[8] = mark;
    }
    else
    {
        cout << "That move cannot be made. Try again." << endl;
    }

    return 0;
}

int checkEndGame(char gameBoard[])
{
    if (gameBoard[0] == gameBoard[1] && gameBoard[1] == gameBoard[2])
    {
        return 1;
    }
    else if (gameBoard[3] == gameBoard[4] && gameBoard[4] == gameBoard[5])
    {
        return 1;
    }
    else if (gameBoard[6] == gameBoard[7] && gameBoard[7] == gameBoard[8])
    {
        return 1;
    }
    else if (gameBoard[0] == gameBoard[3] && gameBoard[3] == gameBoard[6])
    {
        return 1;
    }
    else if (gameBoard[1] == gameBoard[4] && gameBoard[4] == gameBoard[7])
    {
        return 1;
    }
    else if (gameBoard[2] == gameBoard[5] && gameBoard[5] == gameBoard[8])
    {
        return 1;
    }
    else if (gameBoard[0] == gameBoard[4] && gameBoard[4] == gameBoard[8])
    {
        return 1;
    }
    else if (gameBoard[2] == gameBoard[4] && gameBoard[4] == gameBoard[6])
    {
        return 1;
    }
    else if (gameBoard[0] != free_char && gameBoard[1] != free_char && gameBoard[2] != free_char && gameBoard[3] != free_char && gameBoard[4] != free_char && gameBoard[5] != free_char && gameBoard[6] != free_char && gameBoard[7] != free_char && gameBoard[8] != free_char)
    {
        return -1;
    }
    else
    {
        return 0;
    }
}

int playGame()
{
    char gameBoard[10];

    int player = 1;

    int winner;

    clearBoard(gameBoard);

    do
    {
    printBoard(gameBoard);

    askMove(player);

    verifyMakeMove(gameBoard, choice, player);

    checkEndGame(gameBoard);

    if (player == 1)
    {
        player = 2;
    }
    else
    {
        player = 1;
    }
    } while (checkEndGame(gameBoard) == 0);

    if (checkEndGame(gameBoard) == 1)
    {
        cout << "Player " << player << " wins!" << endl;
    }
    else
    {
        cout << "It's a draw!" << endl;
    }

    return 0;
}

int main()
{
    playGame();

    return 0;
}

2 个答案:

答案 0 :(得分:2)

在游戏开始时,董事会是空白的。所有九个方块都具有相同的值,因此它们彼此相等。特别是,某些行的所有三个方格都是相等的,所以根据你的逻辑,有人赢了比赛。您需要添加其他逻辑以避免出现这种结论。

答案 1 :(得分:1)

显而易见的问题是,在checkEndGame中,当您查找三行时,您不会检查空方块。测试应该更像

if (gameBoard[0] != 0 && gameBoard[0] == gameBoard[1] && gameBoard[1] == gameBoard[2])
    ^^^^^^^^^^^^^^^^^^^^

可能存在其他问题,您可以通过使用调试器逐步执行代码来找到它。