【问题标题】:C++ TicTacToe errorClass problems [closed]C++井字错误类问题[关闭]
【发布时间】:2014-12-12 18:04:54
【问题描述】:

我正在尝试学习 C++,并且我制作了 litte tictactoe 游戏,但出现了问题。我试图制作一个错误布尔值。但它有些不对劲。我已经尝试了 2 个小时,但我无法找到解决问题的方法。怎么了?当我输入 2 2 时,这应该可以工作,错误消息弹出并显示无效移动。但 X 或 O 仍会在板上弹出 代码如下:

#include <iostream>

const int rows = 3;
const int elements = 3;

char SetPlayer;
char SetEnemyPlayer;

char chooseTurn = 0;

char board[rows][elements];

void Clear()
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < elements; j++)
        {
            board[i][j] = ' ';
        }
    }
}

void Show()
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < elements; j++)
        {
            std::cout << " " << board[i][j] << " |";
        }
        std::cout << std::endl;
        std::cout << "------------" << std::endl;
    }
}

void StartTurn()
{
    std::cout << "Which character would you like to be? (X or O) "; std::cin >> chooseTurn;
        switch (chooseTurn){
        case 'O':
            std::cout << "You have choosen O" << std::endl << std::endl;
            chooseTurn = 'O';
            break;    

        case 'X':
            std::cout << "You have choosen X" << std::endl << std::endl;
            chooseTurn = 'X';
            break;
        default:
            std::cout << "Enter a valid character" << std::endl;
            StartTurn();    

    }
}

bool PlayerAttack(int x, int y, char PlayerAttackChar)
{
    if (board[x][y] == ' ')
    {
        board[x][y] = PlayerAttackChar;
        return true;
    }
    return false;
}

bool EnemyAttack(int x, int y, char PlayerAttackChar)
{
    if (board[x][y] == ' ')
    {
        board[x][y] = PlayerAttackChar;
        return true;
    }
    return false;
}    

bool OWinner()
{
    if (board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O' ||
        board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] == 'O' ||
        board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O' ||
        board[0][1] == 'O' && board[1][1] == 'O' && board[2][1] == 'O' ||
        board[0][2] == 'O' && board[1][2] == 'O' && board[2][0] == 'O' ||
        board[0][2] == 'O' && board[1][2] == 'O' && board[2][2] == 'O' ||
        board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == 'O' ||
        board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == 'O')
    {
        std::cout << "You won!" << std::endl;
        return true;
    }
    return false;
}    

bool XWinner()
{
        if (board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X' ||
            board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X' ||
            board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X' ||
            board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X' ||
            board[0][2] == 'X' && board[1][2] == 'X' && board[2][0] == 'X' ||
            board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X' ||
            board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X' ||
            board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X')
        {
            std::cout << "You won!" << std::endl;
            return true;
        }
        return false;
}

bool error(int x, int y)
{
    if (board[x][y] != ' ')
    {
        return true;
    }
}    

int main()
{
    Clear();
    StartTurn();
    Show();
    if (chooseTurn == 'X')
    {
        SetPlayer = 'X';
        SetEnemyPlayer = 'O';
    }
    else
    {
        SetPlayer = 'O';
        SetEnemyPlayer = 'X';
    }
    int pos1 = 0;
    int pos2 = 0;
    bool Xwinner = false;
    bool Owinner = false;
    int PlayerTurn = 0;
    while (!Xwinner && !Owinner)
    {
        while (PlayerTurn == 0)
        {

            bool yourAttack = false;
            PlayerTurn++;
            std::cout << SetPlayer << " turn. Please input a coordinate: "; std::cin >> pos1 >> pos2; std::cout << std::endl;
            pos1 -= 1;
            pos2 -= 1;
            if (error(pos1, pos2))
            {
                std::cout << "Please enter a valid position (1 to 3)" << std::endl;
                PlayerTurn = 0;
            }
            PlayerAttack(pos1, pos2, SetPlayer);
            Show();

        }
        while (PlayerTurn != 0)
        {
            PlayerTurn = 0;
            std::cout<< SetEnemyPlayer << " turn. Please input a coordinate: "; std::cin >> pos1 >> pos2; std::cout << std::endl;
            pos1 -= 1;
            pos2 -= 1;
            if (error(pos1, pos2))
            {
                std::cout << "Please enter a valid position (1 to 3)" << std::endl;
                PlayerTurn = 1;
            }
            EnemyAttack(pos1, pos2, SetEnemyPlayer);
            Show();
            // AI
        }
        Xwinner = XWinner();
        Owinner = OWinner();
    }
}

【问题讨论】:

  • “但它有问题” 没有提供足够的信息。您应该提供示例输入和输出。你想让它做什么,它正在做什么。

标签: c++ boolean


【解决方案1】:

如果移动有效,您的错误函数将不会返回任何内容,并导致未定义的行为。你想添加一个 return false;在 if 语句之后。您还想确保 x 和 y 在数组的范围内。

bool error(int x, int y)
{
    if(x > 2 || y > 2 || board[x][y] != ' ') 
    {
         return true;
    }

    return false;

}

此外,您可能希望 if(error) 检查处于循环中。所以游戏在玩家输入有效动作之前不会继续。

while (error(pos1, pos2))
{
    std::cout << "Please enter a valid position (1 to 3)" << std::endl;
    PlayerTurn = 0;
}

【讨论】:

  • @user2589195 你得到的答案是,除了自我修复之外,这对于学习其他东西很有用:1)总是调试你的代码。如果您无法找出问题所在,请逐步进行。 2)编译器警告是你的朋友,不要忽视它们。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多