Tic-tac-toe prog

时间:2016-10-18 05:34:49

标签: c++ codeblocks

我最后无法弄清楚它对我的要求。这个Tic-tac-toe正在杀了我。根据代码块,错误在最后4“}”中:

// Author:   Aaron Yi
// Date:     17 October 2016
// Contact:  ay88ya@gmail.com /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);
            }
        }
    }
}

错误发生在“return(0);}”行之后,我不知道还能做什么。

2 个答案:

答案 0 :(得分:1)

你的条件:

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

这一个在do内部,一旦完成就无法获得,因此错误。减少下面4个括号中的一个括号,并在此之前添加1个括号。

答案 1 :(得分:0)

之后的行上
if (Option == 1) {

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

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

然后错误的while循环中的代码不应该在它旁边,它应该与第一个if处于同一级别。

相关问题