虽然循环太过分了

While loop is going one too far

所以我遇到的问题是:当满足获胜条件时,程序应该停止 while 循环并只显示 win/lose/draw 消息,但它允许游戏的 X 和 O再转一圈,我不知道为什么。任何帮助将不胜感激。

#include <iostream>
#include <cmath>
#include <string>
#include <cstdlib>
#include <ctime>
const int yCoordMax = 6;
const int xCoordMax = 2;

int xCoord;
int yCoord;
int square = 0;
const char PLAYER1 = 'X';
const char COMPUTER = 'O';
const int MAXTURN = 9;
char playerChar ; //the current turn's player's symbol
const std::string WIN = "You won! How nice.\n";
const std::string LOSE = "You lost.\n";
const std::string DRAW = "It's a draw.\n";
const std::string PLAY = "You will be the X's against the computer O's\n\n";
const std::string INSTRUCTIONS = "Enter the number of the square you wish to mark\nwith 1 being top left and 9 being bottom right.\n\n";
const std::string INVALIDSQUARE = "Please enter a correct square number between 1 and 9.\n";
const std::string SQUAREISFULL = "That square is already marked. Choose another.\n";
bool gameOver = false;
bool isGameDraw = false;

char boardChoices[3][3] = {{'1', '2', '3'},{'4', '5', '6'},{'7', '8', '9'}};

void drawBoard(void);
void playGame(void);
bool checkForWinner(void);
void isMoveValid(void);

int main()
{
    std::srand(time(0)); //sets the seed for computer only once
    std::cout << PLAY;
    std::cout << INSTRUCTIONS;
    playerChar = PLAYER1;
        while(!gameOver)
            {
            drawBoard();
            playGame();
            isMoveValid();
            gameOver = checkForWinner();
            }
    if (playerChar == 'O' && !isGameDraw)
        {
            drawBoard();
            std::cout << std::endl << std::endl << "Player 1 [X] Wins! Game Over!\n";
        }
        else if (playerChar == 'X' && !isGameDraw)
        {
            drawBoard();
            std::cout << std::endl << std::endl << "Player 2 [O] Wins! Game Over!\n";
        }
        else
        {
            drawBoard();
            std::cout << std::endl << std::endl << "It's a draw! Game Over!\n";
        }
return 0;
}

void drawBoard()
{
std::cout << std::endl << std::endl << "gameover says " << gameOver << std::endl;
std::cout << "+----" << "+----+" << "----+" << std::endl; // 0
std::cout << "|  " << boardChoices[0][0] << " " << "|  " << boardChoices[0][1] << " |" << "  " << boardChoices[0][2] << " |" << std::endl; // 1 input here only [1][0], [1][1], [1][2]
std::cout << "+----" << "+----+" << "----+" << std::endl; // 2
std::cout << "|  " << boardChoices[1][0] << " " << "|  " << boardChoices[1][1] << " |" << "  " << boardChoices[1][2] << " |" << std::endl; // 3 input here only [3][0], [3][1], [3][2]
std::cout << "+----" << "+----+" << "----+" << std::endl; // 4
std::cout << "|  " << boardChoices[2][0] << " " << "|  " << boardChoices[2][1] << " |" << "  " << boardChoices[2][2] << " |" << std::endl; // 5 input here only [5][0], [5][1], [5][2]
std::cout << "+----" << "+----+" << "----+" << std::endl;
}

void playGame()
{
    std::cout << std::endl;

    if(playerChar == PLAYER1)
        {
      std::cout << "X's turn :: ";
      std::cin >> square;
        }
    else if(playerChar == COMPUTER)
        {
        square = rand() % 9 + 1;
        std::cout << "O's turn:: " << square << std::endl << std::endl;
        }
      if (square == 1)
         {yCoord = 0;
         xCoord = 0;}
      if (square == 2)
         {yCoord = 0;
         xCoord = 1;}
      if (square == 3)
         {yCoord = 0;
         xCoord = 2;}
      if (square == 4)
         {yCoord = 1;
         xCoord = 0;}
      if (square == 5)
         {yCoord = 1;
         xCoord = 1;}
      if (square == 6)
         {yCoord = 1;
         xCoord = 2;}
      if (square == 7)
         {yCoord = 2;
         xCoord = 0;}
      if (square == 8)
         {yCoord = 2;
         xCoord = 1;}
      if (square == 9)
         {yCoord = 2;
         xCoord = 2;}
}

void isMoveValid()
{
    if(playerChar == PLAYER1 && boardChoices[yCoord][xCoord] != PLAYER1 && boardChoices[yCoord][xCoord] != COMPUTER)
    {
        boardChoices[yCoord][xCoord] = playerChar;
        playerChar = COMPUTER;
    }
    else if(playerChar == COMPUTER && boardChoices[yCoord][xCoord] != PLAYER1 && boardChoices[yCoord][xCoord] != COMPUTER)
    {
        boardChoices[yCoord][xCoord] = COMPUTER;
        playerChar = PLAYER1;
    }
    else
    {
        if(playerChar == PLAYER1)
        std::cout << SQUAREISFULL;
    }

}

bool checkForWinner()
{
    std::string victoryOrDefeat;
    if(playerChar == COMPUTER)
    {
        victoryOrDefeat = LOSE;
    }
    else if(playerChar == PLAYER1)
    {
        victoryOrDefeat = WIN;
    }
   if(boardChoices[0][0] == playerChar && boardChoices[0][1] == playerChar && boardChoices[0][2] == playerChar) // Horizontal
      {std::cout << victoryOrDefeat;
      return true;}
   if(boardChoices[1][0] == playerChar && boardChoices[1][1] == playerChar && boardChoices[1][2] == playerChar) // Horizontal
      {std::cout << victoryOrDefeat;
      return true;}
   if(boardChoices[2][0] == playerChar && boardChoices[2][1] ==  playerChar && boardChoices[2][2] == playerChar) // Horizontal
      {std::cout << victoryOrDefeat;
      return true;}

   if(boardChoices[0][0] == playerChar && boardChoices[1][0] == playerChar && boardChoices[2][0] == playerChar) // Vertical
      {std::cout << victoryOrDefeat;
      return true;}
   if(boardChoices[0][1] == playerChar && boardChoices[1][1] == playerChar && boardChoices[2][1] == playerChar) // Vertical
      {std::cout << victoryOrDefeat;
      return true;}
   if(boardChoices[0][2] == playerChar && boardChoices[1][2] ==  playerChar && boardChoices[2][2] == playerChar) // Vertical
      {std::cout << victoryOrDefeat;
      return true;}

   if(boardChoices[0][0] == playerChar && boardChoices[1][1] == playerChar && boardChoices[2][2] == playerChar) // Diagonal
      {std::cout << victoryOrDefeat;
      return true;}
   if(boardChoices[0][2] == playerChar && boardChoices[1][1] ==  playerChar && boardChoices[2][0] == playerChar) // Diagonal
      {std::cout << victoryOrDefeat;
      return  true;}

  for (int i = 0; i < 3; i++)//Check for draw
    {
        for (int j = 0; j < 3; j++)
        {
            if (boardChoices[i][j] != 'X' && boardChoices[i][j] != 'O')
            {
                return false;
            }
        }
    }
    isGameDraw = true;
    return true;
}

我知道了..你在每次 isMoveValid() 检查后更改 playerChar 结果当你应该检查 X 你正在检查 O ..简单地在检查获胜者后进行此更改..这将给出正确的结果。

最初,当玩家 1 给出 X 位置时,您正在检查是否给出 O 的获胜动作,在以后的情况下也会发生这种情况.. 这就是为什么得到错误。