2016-10-18 162 views
0

最后我无法弄清楚自己想要什么。这个井字游戏正在杀死我。该错误是在过去4“}”,根据代码块:“返回(0);}”之后Tic-tac-toe编

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

哦,你真的不应该像你一样用括号来初始化变量。用这种风格,你很可能会打到[最令人头疼的解析](https://en.wikipedia.org/wiki/Most_vexing_parse)的方式。相反,例如'char s1 ='1';' –

+0

欢迎来到Stack Overflow!请参阅[问]和[mcve]。 – Mat

回答

1

你而条件:

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

这个人是do里面,一旦你完成它不能同时获得,因此错误。减少下面4个括号中的一个括号,并在此之前添加1个大括号。

+0

@JoachimPileborg有两个do,一个do在另一个do内。这个''do'里面有'while(!ValidTurn)','对于外部的'',这个'while(!GameOverDecider)'在里面并且应该在外面。 –

0

上线后

if (Option == 1) { 

你有do {。但是在该块的末尾没有whileuntil条件。它看起来像它应该是

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

然后是错误while循环内的代码不应该在侧,它应该是在同一水平作为第一if