2011-11-08 8 views
0

if文は奇妙です。なんらかの理由で、IF文でif (choosePositionX)が他に移動した場合、他のプレイヤーがすでに移動したことをユーザーに知らせる場合は、if (choosePositionO)の場合は次のプレイヤーに移動し、そのプレイヤーのelseを出力します。どのようにしてそれが他の人に行くかは、どちらかといえば、それがちょうど崩壊し続けていくというifステートメントの中でどのようにすることができますか? break;が動作しないようです。if文のジャンプ

基本的に、最初に使用されたelseは、2番目のif文の中の秒になります。どうして?

#include <iostream> 
using namespace std; 

// Initialize Array to 9 elements. 
// Set array elements 1 to 9 to specify locations for placement of X and O. 
char ticTacBoard[9] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'}; 

// gameState will return 1 to 4. #1. Player X has won, #2. Player Y has won, #3. Draw, #4. Game Not Finished. 
int gameState = 0; 
int choosePositionX = 0; 
int choosePositionO = 0; 
int totalMoves = 0; 

// Functions declared to run game. 
// Checks for winners, draw and if game should continue. 
int checkGameState (int gameState); 
// Takes in users moves. 
void makeMove(); 
// Resets board when game finished. 
void reset(); 
// Prints the board to the user. 
void printBoard(); 


int main() 
{ 
    cout << "Welcome to Tic Tac Toe!" << endl << endl 
     << "The goal of Tic Tac Toe is to be the first player to get three in a row on the grid." << endl << endl 
     << "Please select 1 through 9 to select placement for your turn."<< endl << endl; 

    int gameState = 4; 

    printBoard(); 

    while (gameState == 4) 
    { 
     cout << "Player X please choose a position: "; 
     cin >> choosePositionX; 
     cout << endl; 
     makeMove(); 
     totalMoves = totalMoves + 1; 
     printBoard(); 
     gameState = checkGameState(gameState); 
     if (gameState != 4) 
     { 
      if (gameState == 1) 
      { 
       cout << "Player X has won the game!" << endl << endl; 
       break; 
      } 
      if (gameState == 2) 
      { 
       cout << "Player Y has won the game!" << endl << endl; 
       break; 
      } 
      if (gameState == 3) 
      { 
       cout << "The game is a DRAW!" << endl << endl; 
       break; 
      } 
     } 
     cout << "Player O please choose a position: "; 
     cin >> choosePositionO; 
     cout << endl; 
     makeMove(); 
     totalMoves = totalMoves + 1; 
     printBoard(); 
     gameState = checkGameState(gameState); 
     if (gameState != 4) 
     { 
      if (gameState == 1) 
      { 
       cout << "Player X has won the game!" << endl << endl; 
       break; 
      } 
      if (gameState == 2) 
      { 
       cout << "Player Y has won the game!" << endl << endl; 
       break; 
      } 
      if (gameState == 3) 
      { 
       cout << "The game is a DRAW!" << endl << endl; 
       break; 
      } 
     } 
    } 

    char playAgain; 

    cout << "Would you like to play again? y/n: "; 
    cin >> playAgain; 
    cout << endl; 

    if (playAgain == 'y') 
    { 
     reset(); 
    } 
    else if (playAgain == 'n') 
    { 
     cout << endl; 
     cout << "Thanks for playing!!"; 
     return 0; 
    } 

    return 0; 
} 

void reset() 
{ 
    for (int i = 0; i < 9; i++) 
    { 
     ticTacBoard[i] = 49 + i; 
    } 

    gameState = 0; 
    choosePositionX = 0; 
    choosePositionO = 0; 
    totalMoves = 0; 

    main(); 
} 

int checkGameState(int gameState) 
{ 

////////////// CHECK FOR X ///////////////// 

    // Check Rows 
    if(ticTacBoard[0] == 'X' && ticTacBoard[1] == 'X' && ticTacBoard[2] == 'X') 
    { 
     return 1; 
    } 
    if(ticTacBoard[3] == 'X' && ticTacBoard[4] == 'X' && ticTacBoard[5] == 'X') 
    { 
     return 1; 
    } 
    if(ticTacBoard[6] == 'X' && ticTacBoard[7] == 'X' && ticTacBoard[8] == 'X') 
    { 
     return 1; 
    } 

    // Check Columns 
    if(ticTacBoard[0] == 'X' && ticTacBoard[3] == 'X' && ticTacBoard[6] == 'X') 
    { 
     return 1; 
    } 
    if(ticTacBoard[1] == 'X' && ticTacBoard[4] == 'X' && ticTacBoard[7] == 'X') 
    { 
     return 1; 
    } 
    if(ticTacBoard[2] == 'X' && ticTacBoard[5] == 'X' && ticTacBoard[8] == 'X') 
    { 
     return 1; 
    } 

    // Check Diagonally 
    if(ticTacBoard[0] == 'X' && ticTacBoard[4] == 'X' && ticTacBoard[8] == 'X') 
    { 
     return 1; 
    } 
    if(ticTacBoard[2] == 'X' && ticTacBoard[4] == 'X' && ticTacBoard[6] == 'X') 
    { 
     return 1; 
    } 

////////////// CHECK FOR O ///////////////// 

    // Check Rows 
    if(ticTacBoard[0] == 'O' && ticTacBoard[1] == 'O' && ticTacBoard[2] == 'O') 
    { 
     return 2; 
    } 
    if(ticTacBoard[3] == 'O' && ticTacBoard[4] == 'O' && ticTacBoard[5] == 'O') 
    { 
     return 2; 
    } 
    if(ticTacBoard[6] == 'O' && ticTacBoard[7] == 'O' && ticTacBoard[8] == 'O') 
    { 
     return 2; 
    } 

    // Check Columns 
    if(ticTacBoard[0] == 'O' && ticTacBoard[3] == 'O' && ticTacBoard[6] == 'O') 
    { 
     return 2; 
    } 
    if(ticTacBoard[1] == 'O' && ticTacBoard[4] == 'O' && ticTacBoard[7] == 'O') 
    { 
     return 2; 
    } 
    if(ticTacBoard[2] == 'O' && ticTacBoard[5] == 'O' && ticTacBoard[8] == 'O') 
    { 
     return 2; 
    } 

    // Check Diagonally 
    if(ticTacBoard[0] == 'O' && ticTacBoard[4] == 'O' && ticTacBoard[8] == 'O') 
    { 
     return 2; 
    } 
    if(ticTacBoard[2] == 'O' && ticTacBoard[4] == 'O' && ticTacBoard[6] == 'O') 
    { 
     return 2; 
    } 

    // After 9 moves we call a Draw. Since we should have a winner before that. 
    if (totalMoves >= 9) 
    { 
     return 3; 
    } 

    // Otherwise CONTINUE 
    else 
     return 4; 
} 

void makeMove() 
{ 
    if (choosePositionX) 
    { 
     if (ticTacBoard[choosePositionX - 1] != 'X' && ticTacBoard[choosePositionX - 1] != 'O') 
     { 
      ticTacBoard[choosePositionX - 1] = 'X'; 
     } 
     else 
     { 
      cout << "Player O has already made this move" << endl << endl; 
      // keep same moves for counter draw 
      totalMoves = totalMoves - 1 + 1; 
     } 
    } 

    if (choosePositionO) 
    { 
     if (ticTacBoard[choosePositionO - 1] != 'X' && ticTacBoard[choosePositionO - 1] != 'O') 
     { 
      ticTacBoard[choosePositionO - 1] = 'O'; 
     } 
     else 
     { 
      cout << "Player X has already made this move" << endl << endl; 
      // keep same moves for counter draw 
      totalMoves = totalMoves - 1 + 1; 
     } 
    } 
} 

void printBoard() 
{ 
    for (int y = 0; y < 3; y++) 
    { 
     for (int x = 0; x < 3; x++) 
     { 
      cout << ticTacBoard[3 * y + x] << " "; 
     } 

     cout << endl; 
    } 
    cout << endl; 
} 
+0

を置くことができる場合は、2番目を作ります時間? –

+0

私はそれを考え出しました。特定のものを0に設定しなければならなかったのです。 – mystycs

+0

@mystycs - choosePositionXは常にtrueであるからです。あなたはいつかfalseに設定しようとする必要があります。 –

答えて

7

あなたは2つのif Sを接続elseを忘れてしまいました。あなたは使用する必要があります。

else if (choosePositionO) 
+1

+1スピードラウンドで最初にブザーを鳴らすために+1 – Lou

+3

+1質問を理解するために+1 – Mranz

+0

それでは、それは全く選択しないか、または選択肢に全く入りません。 – mystycs

4

使用else if

if (condition1) 
{ 
    if (subcond1) { /* ... */ } 
    else { /* ... */ } 
} 
else if (condition2) 
{ 
    if (subcond2) { /* ... */ } 
    else { /* ... */ } 
} 
else 
{ 
    // ... 
} 
+0

これで条件2は私のために実行されません – mystycs

+0

いいえ - 私はあなたがそれを望んでいないと思った?あなたが望むものをより正確に言うことができますか? –

1

choosePositionXがtrueの場合checkPositionOはチェックされませんので、else ifif秒にします。

相互排他的な場合は、1つの変数を使用するだけのようです。あるいはさらに良いenumと:

enum Player { 
    PlayerX 
    , PlayerO 
}; 

enum Player choosePosition; 
1

`choosePositionX`と`同じで真choosePositionO`されている理由(choosePosition)

else if(choosePositionO) 

は、あなたがブレーク