虽然循环太过分了

时间:2015-10-23 18:47:21

标签: c++ while-loop tic-tac-toe

所以我遇到的问题是:当满足胜利条件时,程序应该停止while循环并只显示获胜/失败/平局消息,而是允许游戏的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;
}

1 个答案:

答案 0 :(得分:3)

我已经明白了......如果您要使用playerChar检查isMoveValid(),我会在每次X检查后更改OX在检查胜利者之后进行简单的更改。这将给出正确的结果。

最初,当player1提供O位置时,您正在检查获胜行动以获得#include <cmath> #include <cstdlib> #include <iostream> using namespace std; void PizzaMenu(); void SizePrices(); int main() { double personal = 10.00; double medium = 14.50; double large = 19.00; double xlarge = 23.50; double FlavorChoice=0; int SizeChoice; int PizzaCountP=(cin >> PizzaCountP, PizzaCountP); int PizzaCountM = (cin >> PizzaCountM, PizzaCountM); int PizzaCountL = (cin >> PizzaCountL, PizzaCountL); int PizzaCountXL = (cin >> PizzaCountXL, PizzaCountXL); double orderTotal = (personal * PizzaCountP) + (medium * PizzaCountM) + (large * PizzaCountL) + (xlarge * PizzaCountXL); cout << "Welcome to Joes pizza place!" << endl; do{ PizzaMenu(); cout << "\nPlease chose a pizza from the menu(1-6): "; cin >> FlavorChoice; SizePrices(); cin >> SizeChoice; if (SizeChoice > 0 && SizeChoice < 5) { switch (SizeChoice) { case 1: cout << "How many personal pizzas? "; cin >> PizzaCountP; break; case 2: cout << "How many medium pizzas?"; cin >> PizzaCountM; break; case 3: cout << "How many large pizzas?"; cin >> PizzaCountL; break; case 4: cout << "How many extra large pizzas?"; cin >> PizzaCountXL; break; default: cout << "please enter a choice (1-4)"; cin >> SizeChoice; break; } } if (PizzaCountP > 0 || PizzaCountM > 0 || PizzaCountXL > 0 || PizzaCountL > 0) { printf("Your total is: %a", orderTotal); } } while (FlavorChoice != 6); cout << "Thank you for visiting Joes place pizza! "<<endl; } void PizzaMenu() { cout << "\nSpecialty Pizza Menu" << endl; cout << "\n1)Pizza 1" << endl << "\n2)Pizza 2" << endl << "\n3)Pizza 3" <<endl << "\n4)Pizza 4" << endl << "\n5)Pizza 5" << endl << "\n6)Pizza 6" << endl; } void SizePrices() { cout << "1) 10'' Personal" << "\t" << "- $10.00" << endl; cout << "2) 14'' Medium" << "\t" << "- $14.50" << endl; cout << "3) 16'' Large" << "\t" << "- $19.00" << endl; cout << "4) 18'' Extra Large" << "\t" << "- $23.50" << endl; cout << "Your choice (1-4)? "; } ,而在以后的情况下,这种情况也会发生.. 这就是为什么&## 39;得到错误。