2016-10-18 15 views
0

最後に私が望むものがわからない。このTic-tac-toeが私を殺しています。エラーがコードブロックに応じて「}」の最後の4である:Tic-tac-toe prog

// Author: Aaron Yi 
// Date:  17 October 2016 
// Contact: [email protected] /347-570-5723 
// MAC 125-3005/Lab 03 
# 
include <iostream> 
using namespace std; 
int main() { 
    int Option; 
    char s1('1'); 
    char s2('2'); 
    char s3('3'); 
    char s4('4'); 
    char s5('5'); 
    char s6('6'); 
    char s7('7'); 
    char s8('8'); 
    char s9('9'); 

    int PlayerTurn(1); 
    bool GameOverDecider(true); 

    cout << "\tActivating T^3" << endl; 
    cout << endl << endl; 
    cout << "\t(1)Begin!" << endl; 
    cout << "\t(2)Quit" << endl; 
    cout << endl; 
    cout << "Choose 1 or 2:"; 
    cin >> Option; 

    if (Option == 1) { 
     do { 
      int PlayerTurn(1); 
      bool GameOverDecider(true); 
      cout << "  " << s1 << " | " << s2 << " | " << s3 << endl; 
      cout << "  -----+-----+-----" << endl; 
      cout << "  " << s4 << " | " << s5 << " | " << s6 << endl; 
      cout << "  -----+-----+-----" << endl; 
      cout << "  " << s7 << " | " << s8 << " | " << s9 << endl; 
      cout << "  -----+-----+-----" << endl; 
      char PlayerMarker; 
      if (PlayerTurn = 1) { 
       PlayerMarker = 'X'; 
      } else { 
       PlayerMarker = 'O'; 
      } 
      bool ValidTurn; 
      do { 
       char CurrentMove; 
       cout << "Player" << PlayerTurn << "'s turn, set move on what square: " << endl; 
       cin >> CurrentMove; 
       ValidTurn = true; 

       if (CurrentMove == '1' && s1 == '1') { 
        s1 = PlayerMarker; 
       } else if (CurrentMove == '2' && s1 == '2') { 
        s2 = PlayerMarker; 
       } else if (CurrentMove == '3' && s1 == '3') { 
        s3 = PlayerMarker; 
       } else if (CurrentMove == '4' && s1 == '4') { 
        s4 = PlayerMarker; 
       } else if (CurrentMove == '5' && s1 == '5') { 
        s5 = PlayerMarker; 
       } else if (CurrentMove == '6' && s1 == '6') { 
        s6 = PlayerMarker; 
       } else if (CurrentMove == '7' && s1 == '7') { 
        s7 = PlayerMarker; 
       } else if (CurrentMove == '8' && s1 == '8') { 
        s8 = PlayerMarker; 
       } else if (CurrentMove == '9' && s1 == '9') { 
        s9 = PlayerMarker; 
       } else { 
        cout << "Invalid Move, make another one:" << endl; 
        ValidTurn = false; 
       } 
      } while (!ValidTurn); 

      GameOverDecider = false; 
      bool WinGame = true; 
      if (s1 != '1') { 
       if (s2 == s1 && s3 == s1) { 
        GameOverDecider = true; 
       } 
       if (s4 == s1 && s7 == s1) { 
        GameOverDecider = true; 
       } 
      } 
      if (s1 != '9') { 
       if (s3 == s9 && s6 == s9) { 
        GameOverDecider = true; 
       } 
       if (s7 == s9 && s8 == s9) { 
        GameOverDecider = true; 
       } 
      } 
      if (s1 != '5') { 
       if (s1 == s5 && s9 == s5) { 
        GameOverDecider = true; 
       } 
       if (s2 == s5 && s8 == s5) { 
        GameOverDecider = true; 
       } 
       if (s4 == s5 && s6 == s5) { 
        GameOverDecider = true; 
       } 
       if (s3 == s5 && s7 == s5) { 
        GameOverDecider = true; 
       } 
      } 
      if (s1 != '1' && 
       s2 != '2' && 
       s3 != '3' && 
       s4 != '4' && 
       s5 != '5' && 
       s6 != '6' && 
       s7 != '7' && 
       s8 != '8' && 
       s9 != '9' && 
       !GameOverDecider) { 
       GameOverDecider = true; 
       WinGame = false; 
       if (GameOverDecider) { 
        if (WinGame) 

         { 
          cout << "Player " << PlayerTurn << " totally wins!" << endl; 
         } 
        cout << "  " << s1 << " | " << s2 << " | " << s3 << endl; 
        cout << "  -----+-----+-----" << endl; 
        cout << "  " << s4 << " | " << s5 << " | " << s6 << endl; 
        cout << "  -----+-----+-----" << endl; 
        cout << "  " << s7 << " | " << s8 << " | " << s9 << endl; 
        cout << "  -----+-----+-----" << endl; 
        cout << "\tGame Over!" << endl; 
        cout << "\tAgain?(Y/N)?: "; 

        char PlayAgain; 
        cin >> PlayAgain; 
        if (PlayAgain = 'y') { 
         GameOverDecider = false; 
         s1 = '1'; 
         s2 = '2'; 
         s3 = '3'; 
         s4 = '4'; 
         s5 = '5'; 
         s6 = '6'; 
         s7 = '7'; 
         s8 = '8'; 
         s9 = '9'; 
        } 

        PlayerTurn = 1; 
       } else { 
        if (PlayerTurn == 1) { 
         PlayerTurn = 2; 
        } else { 
         PlayerTurn = 1; 
        } 
       } 
      } 
      while (!GameOverDecider) { 
       if (Option == 2) { 
        cout << "Ok....." << endl; 
       } 
       return (0); 
      } 
     } 
    } 
} 

エラーが正しい「リターン(0);}」の後に発生するライン、と私は何をする他に何かわかりません。

+0

ビルドエラーに関する質問を投稿する場合は、完全なエラー出力を未編集、可能な情報メモ、*をテキスト*として含めてください。質問を編集し、ビルドの出力をコピーして質問に貼り付けてください。 –

+0

ああ、実際には変数の初期化に括弧を使用しないでください。このスタイルでは、あなたはおそらく[最も厄介な構文解析](https://en.wikipedia.org/wiki/Most_vexing_parse)の方法で早くにヒットします。代わりに、たとえば'char s1 = '1';' –

+0

ようこそスタックオーバーフロー! [ask]と[mcve]を参照してください。 – Mat

答えて

1

あなたのwhile条件は:

while (!GameOverDecider) { 
    if (Option == 2) { 
     cout << "Ok....." << endl; 
    } 
    return (0); 
    } 

これはdo内にあり、一度それがしばらくので、エラーを取得することはできません終了します。下にある4つの括弧の中の1つの括弧を減らし、この間に1つの括弧を追加します。あなたはdo {を持って

if (Option == 1) { 

後行で

+0

@JoachimPileborg 2つのdoがあり、もう1つdoは別のdoの内部にあります。内側にある 'do'は' while(!ValidTurn); while 'while(!GameOverDecider)'の内側にあり、外側にはありません。 –

0

。しかし、そのブロックの最後にはwhileまたはuntilという条件はありません。それは

do { 
    ... 
} while (!GameOverDecider); 

する必要があり、その誤ったwhileループ内のコードが側にそれてはならないように、それは最初ifと同じレベルにする必要がありそうです。

関連する問題