【问题标题】:Tic Tac Toe game - How to not have results print after invalid entry井字游戏 - 无效输入后如何不打印结果
【发布时间】:2014-03-27 05:41:13
【问题描述】:

好的,所以现在当我尝试确定要再次玩的有效输入时,它会在显示无效输入后立即再次显示游戏结果,然后要求用户再次输入 "y" 或 "n" 。我上传了一张图片给你看。我一辈子都想不通。

图片如下: http://imgur.com/SRsMo4P

// GAME OVER
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      while (bGameOver)
      {
            // Display game board - PRINT
            for (int i = 0; i<ROW; ++i)
            {
                cout << " " << board[i][0] << " | " << board[i][1] << " | " << board[i][2] << endl;
                if (i==0 || i==1)
                {
                    cout << " - + - + -" << endl;
                }
            }

            // Player wins - OUTPUT
            if(bWinGame)
            {
                cout << endl;
                cout << " Player "<< playerTurn << " wins! HOORAH! " << endl << endl;
                cout << " Written by: Karolina Sabat - CPSC 1103 - Section: S11" << endl << endl;
            }

            // Play again - OUTPUT & USER INPUT
            //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            // VARIABLES
            char again;         // Play again = "Y" or "N"
            cout << endl;
            cout << " Want to play again? ( Y / N )" << endl << endl;
            cout << " "; cin >> again; cout << endl;

            // Play again - if YES        
            if (again == 'y' || again == 'Y')
            {
                bGameOver=false;        // Reset game state
                bWinGame=true;          // Reset assumption

                board[0][0] = '*';      // Rest game board - Array
                board[0][1] = '*';
                board[0][2] = '*';
                board[1][0] = '*';
                board[1][1] = '*';
                board[1][2] = '*';
                board[2][0] = '*';
                board[2][1] = '*';
                board[2][2] = '*';
            }

            // Play again - if NO
            else if (again == 'n' || again == 'N')
            {
                cout << " Awe oh well, thanks for playing. " << endl << endl;
                cout << " Written by: Karolina Sabat - CPSC 1103 - Section: S11" << endl << endl;
                break;
            }

            else
            {
                cout << " INVALID ENTRY: Please input \"Y\" or \"N\" " << endl << endl;
            }
      }

      // Game play continue
      if (!bGameOver)
      {
          // Switch player turn
          if (playerTurn == 1)
          {
              playerTurn = 2;
          }

          else
          {
              playerTurn = 1;
          }

      }  

}



cout << " "; return 0;
}

【问题讨论】:

    标签: c++ arrays loops output


    【解决方案1】:

    你在这个else声明中没有break

    else
    {
        cout << " INVALID ENTRY: Please input \"Y\" or \"N\" " << endl << endl;
    }
    

    代码将在没有break 的情况下继续运行,从而导致奇怪的行为。试试这个:

    else
    {
        cout << " INVALID ENTRY: Please input \"Y\" or \"N\" " << endl << endl;
        break;
    }
    

    【讨论】:

      【解决方案2】:

      将您的输入验证代码放入循环中。如果选择 Y 或 N,则中断循环。

      P.S.在代码中添加更正,break; 现在在 wrigth 位置。

      // GAME OVER
      // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            while (bGameOver)
            {
                  // Display game board - PRINT
                  for (int i = 0; i<ROW; ++i)
                  {
                      cout << " " << board[i][0] << " | " << board[i][1] << " | " << board[i][2] << endl;
                      if (i==0 || i==1)
                      {
                          cout << " - + - + -" << endl;
                      }
                  }
      
                  // Player wins - OUTPUT
                  if(bWinGame)
                  {
                      cout << endl;
                      cout << " Player "<< playerTurn << " wins! HOORAH! " << endl << endl;
                      cout << " Written by: Karolina Sabat - CPSC 1103 - Section: S11" << endl << endl;
                  }
      
                  // Play again - OUTPUT & USER INPUT
                  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                  // VARIABLES
      
                  bool bInvalidInput = true;
                  while(bInvalidInput)
                  {
                          char again;         // Play again = "Y" or "N"
                          cout << endl;
                          cout << " Want to play again? ( Y / N )" << endl << endl;
                          cout << " "; cin >> again; cout << endl;
      
                          // Play again - if YES        
                          if (again == 'y' || again == 'Y')
                          {
                              bGameOver=false;        // Reset game state
                              bWinGame=true;          // Reset assumption
                              bInvalidInput = false; //stop dialog
      
                              board[0][0] = '*';      // Rest game board - Array
                              board[0][1] = '*';
                              board[0][2] = '*';
                              board[1][0] = '*';
                              board[1][1] = '*';
                              board[1][2] = '*';
                              board[2][0] = '*';
                              board[2][1] = '*';
                              board[2][2] = '*';
                          }
      
                          // Play again - if NO
                          else if (again == 'n' || again == 'N')
                          {
                              cout << " Awe oh well, thanks for playing. " << endl << endl;
                              cout << " Written by: Karolina Sabat - CPSC 1103 - Section: S11" << endl << endl;
                              bInvalidInput = false; //stop dialog
                              // break; //need no more
                          }
      
                          else
                          {
                              cout << " INVALID ENTRY: Please input \"Y\" or \"N\" " << endl << endl;
                          }
                  }
                  if (bGameOver == true)
                  {
                          break; //get here only if bInvalidInput is false and N pressed
                  }
            }
      
            // Game play continue
            if (!bGameOver)
            {
                // Switch player turn
                if (playerTurn == 1)
                {
                    playerTurn = 2;
                }
      
                else
                {
                    playerTurn = 1;
                }
      
            }  
      
      }
      

      【讨论】:

      • 我无法让它工作,它不接受我的输入。
      • 请检查大括号。你有没有在控制台中得到这个字符串" Want to play again? ( Y / N )"
      【解决方案3】:

      尝试将标记为Display game board - PRINTPlayer wins - OUTPUT 的部分移到while 循环之外(之前)。

      【讨论】:

        【解决方案4】:

        感谢您的建议,但由于某种原因它们无法正常工作,所以我最终这样做了,这是完整的代码:

        // FILE: Tic Tac Toe.cpp
        // PROGRAMMER: Karolina Sabat   CPSC 1103   Section: S11
        
        // ~~ TWO PLAYER TIC TAC TOE GAME ~~
        // Asks the users (player 1, followed by player 2) to select a row and column by 
        entering the corresponding row and column number.
        // The program will substitute the player's selection with either an "X" or "O".
        // A horizontal, vertical or diagonal row of either X's or O's results in a win or 
        otherwise game ends in a draw. 
        // For subsequent plays, player 1 and player 2 alternate going first. 
        
        #include<iostream>              // For cin, cout
        using namespace std;
        
        // MAIN FUNCTION
        int main()
        {
        // VARIABLES
        int playerTurn = 1;                         // Player's turn - Player 1
        const int ROW=3;                            // Table - Number of rows - For array
        const int COL=3;                            // Table - Number of columns - For array
        bool bGameOver= false;                      // Game state - True = Game over, False = Continue play
        char again;                                 // Play again = "Y" or "N"
        char board[ROW][COL] = { {'*', '*', '*'},
                                 {'*', '*', '*'},
                                 {'*', '*', '*'} }; // Game board - Array
        
        
        // TITLE
        cout << endl;
        cout << " TIC TAC TOE" << endl;
        cout << " ____________________________________________________" << endl;
        cout << " A two player game." << endl;
        cout << endl;
        
        // Game state = Continue play - Game is NOT over.
        // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        // Continue play - LOOP
        while (!bGameOver)
        {
              // Game board - Array - PRINT
              for (int i = 0; i < ROW; ++i)
              {
                  cout << " " << board[i][0] <<" | " << board[i][1] <<" | " << board[i][2] << endl;
                  if (i==0 || i==1)
                  {
                          cout << " - + - + -" << endl;
                  }
              }
        
              // Set player mark ( "X" or "O" )
              // VARIABLES
              char playerMark;
              if (playerTurn == 1)          // Player 1 = "X"
              {
                  playerMark = 'X';     
              }
              else
              {
                  playerMark = 'O';         // Player 2 = "O"
              }
        
              // Player move - USER INPUT
              // VARIABLES
              int move_r;                   // Row position - USER INPUT
              int move_c;                   // Column position - USER INPUT
              bool validMove = false;       // Bool ValidMove - Assume false 
              cout << endl;
              cout << " Player " << playerTurn << " , please pick a row and column to place "<< playerMark << "." << endl;
              cout << " Separate the row and column number with a space and press ENTER." << endl;
        
              while (!validMove) // DETERMINE VALIDITY - Check play move entries
              {
                  validMove = true;
                  cout << endl;
                  cout << " "; cin >> move_r >> move_c; cout << endl;
                  // Row 1, Column 1  
                  if(move_r == 1 && move_c == 1 && board[0][0] == '*')
                  {
                      board[0][0] = playerMark;
                  }
                  // Row 1, Column 2 
                  else if(move_r == 1 && move_c == 2 && board[0][1] == '*')
                  {
                      board[0][1] = playerMark;
                  }
                  // Row 1, Column 3 
                  else if(move_r == 1 && move_c == 3 && board[0][2] == '*')
                  {
                      board[0][2] = playerMark;
                  }
                  // Row 2, Column 1 
                  else if(move_r == 2 && move_c == 1 && board[1][0] == '*')
                  {
                      board[1][0] = playerMark;
                  }
                  // Row 2, Column 2 
                  else if(move_r == 2 && move_c == 2 && board[1][1] == '*')
                  {
                      board[1][1] = playerMark;
                  }
                  // Row 2, Column 3 
                  else if(move_r == 2 && move_c == 3 && board[1][2] == '*')
                  {
                      board[1][2] = playerMark;
                  } 
                  // Row 3, Column 1 
                  else if(move_r == 3 && move_c == 1 && board[2][0] == '*')
                  {
                      board[2][0] = playerMark;
                  } 
                  // Row 3, Column 2 
                  else if(move_r == 3 && move_c == 2 && board[2][1] == '*')
                  {
                      board[2][1] = playerMark;
                  }
                  // Row 3, Column 3 
                  else if(move_r == 3 && move_c == 3 && board[2][2] == '*')
                  {
                      board[2][2] = playerMark;
        
                  } 
        
                  // INVALID ENTRY
                  else 
                  {
                      cout << " Invalid Move, please try again!" << endl << endl;
                      // Will clear characters
                      cin.clear();
                      cin.ignore(numeric_limits<streamsize>::max(), '\n');
                      validMove = false;
                  }
        
              }
              // Check if game over conditions are met
              // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
              // If row 0 or column 0 = same playerMark
              if (board[0][0] != '*')
              {
                    if (board[0][0] == board[0][1] && board[0][0] == board[0][2])   // Check row 0
                    {
                        bGameOver=true;
                    }
                    if (board[0][0] == board[1][0] && board[0][0] == board[2][0])   // Check column 0
                    {
                        bGameOver=true;
                    }
              }
        
              // If row 1 or column 1 = same playerMark
              if (board[1][1] != '*')
              {
        
                    if(board[1][1] == board[1][0] && board[1][1] == board[1][2])    // Check row 1
                    {
                        bGameOver=true;
                    }
        
                      if(board[1][1] == board[0][1] && board[1][1] == board[2][1])  // Check column 1
                    {
                        bGameOver=true;
                    }
        
                    if(board[1][1] == board[0][0] && board[1][1] == board[2][2])    // Diagonals - Check if 1,1 and 3,3 are equal to 2,2
                    {
                        bGameOver=true;
                    }
                    if(board[1][1] == board[2][0] && board[1][1] == board[0][2])    // Diagnonals - Check if 1,3 and 3,1 are equal to 2,2
                    {
                        bGameOver=true;
                    }
              }
        
              // If row 2 or column 2 = same playerMark
              if (board[2][2] != '*')
              {
                    if (board[2][2] == board[2][1] && board[2][2] == board[2][0])   // Check row 2
                    {
                        bGameOver=true;
                    }
                    if (board[2][2] == board[1][2] && board[2][2] == board[0][2])   // Check column 2
                    {
                        bGameOver=true;
                    }
              }
        
              // Check if DRAW
              bool bWinGame=true; // ASSUMPTION - A player won
              if (board[0][0] !='*' && board[0][1] !='*' && board[0][2] !='*' && 
                  board[1][0] !='*' && board[1][1] !='*' && board[1][2] !='*' &&
                  board[2][0] !='*' && board[2][1] !='*' && board[2][2] !='*' && !bGameOver)
              {
                  bGameOver=true;
                  bWinGame=false;
                  // Tie - OUTPUT
                  cout << " Oh, won't you look at that, it's a TIE!"<< endl << endl;
                  cout << " Written by: Karolina Sabat - CPSC 1103 - Section: S11" << endl << endl;
              }
        
              // GAME OVER
              // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
              while (bGameOver)
              {
                    // Display game board - PRINT
                    for (int i = 0; i<ROW; ++i)
                    {
                        cout << " " << board[i][0] << " | " << board[i][1] << " | " << board[i][2] << endl;
                        if (i==0 || i==1)
                        {
                            cout << " - + - + -" << endl;
                        }
                    }
        
                    // Player wins - OUTPUT
                    if(bWinGame)
                    {
                        cout << endl;
                        cout << " Player "<< playerTurn << " wins! HOORAH! " << endl << endl;
                        cout << " Written by: Karolina Sabat - CPSC 1103 - Section: S11" << endl << endl;
                    }
        
                    // Play again - OUTPUT & USER INPUT
                    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                    // VARIABLES
                    bool bValidInput = false;
        
                    while (bValidInput != true)
                    {
        
                        cout << endl;
                        cout << " Want to play again? ( Y / N )" << endl << endl;
                        cout << " ";
                        // Will clear characters
                        cin.clear();
                        cin.ignore(numeric_limits<streamsize>::max(), '\n');
                        cin >> again; cout << endl;
        
                        // Play again - if YES        
                        if (again == 'y' || again == 'Y')
                        {
                            bValidInput = true;
                            bGameOver=false;        // Reset game state
                            bWinGame=true;          // Reset assumption
        
        
                            board[0][0] = '*';      // Rest game board - Array
                            board[0][1] = '*';
                            board[0][2] = '*';
                            board[1][0] = '*';
                            board[1][1] = '*';
                            board[1][2] = '*';
                            board[2][0] = '*';
                            board[2][1] = '*';
                            board[2][2] = '*';
                        }
        
                        // Play again - if NO
                        else if (again == 'n' || again == 'N')
                        {
                            bValidInput = true; // Assumes 
                            cout << " Awe oh well, thanks for playing. " << endl << endl;
                            cout << " Written by: Karolina Sabat - CPSC 1103 - Section: S11" << endl << endl;
                            cout << " "; return 0;
                        }
        
                        // Play again - INVALID ENTRY
                        else
                        {
                            cout << " INVALID ENTRY: Please input \"Y\" or \"N\" " << endl << endl;
                            bValidInput = false;
                        }
                    }   
        
              }
        
              // Game play continue
              if (!bGameOver)
              {
                  // Switch player turn
                  if (playerTurn == 1)
                  {
                      playerTurn = 2;
                  }
        
                  else
                  {
                      playerTurn = 1;
                  }
        
              }  
        
        }
        
        // EXIT PROGRAM
         cout << " "; return 0;
        }
        

        【讨论】:

        • 其实这并不能完全解决问题,我必须输入两次yes或no,为什么?并且建议的方法 nondefaultname 不起作用。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-12
        相关资源
        最近更新 更多