2014-03-27 91 views
0

好吧,現在當我嘗試確定有效輸入再次播放時,它會在顯示無效輸入後再次顯示遊戲結果,然後詢問用戶再次輸入「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; 
} 

回答

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; 
} 
0

嘗試移動標記Display game board - PRINTPlayer wins - OUTPUT外(前)while環路的部分。

1

將您的輸入驗證碼放入循環中。如果選擇Y或N,則斷開循環。

P.S.在代碼中添加coorrections,break;現在處於不利地位。

// 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; 
      } 

     } 

} 
+0

我不能得到這個工作,它不接受我的意見。 – tinywolves

+0

請檢查大括號。 (Y/N)「' – nondefaultname

0

謝謝你的建議,但出於某種原因,他們無法正常工作,所以我結束了做這這裏是完整的代碼:

// 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; 
} 
+0

其實這並不能完全解決問題,我必須輸入yes或no兩次,爲什麼?並且非默認名稱提示的方法不起作用。 – tinywolves