首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tic-tac-toe程序

Tic-tac-toe程序
EN

Stack Overflow用户
提问于 2016-10-18 13:34:49
回答 2查看 146关注 0票数 0

我不明白它到底想从我这里得到什么。这个Tic-tac-toe要了我的命。根据代码块,错误在最后4个"}“中:

代码语言:javascript
复制
// 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);}“行之后,我不知道还能做什么。

EN

回答 2

Stack Overflow用户

发布于 2016-10-18 13:41:01

您的while条件:

代码语言:javascript
复制
  while (!GameOverDecider) {
    if (Option == 2) {
      cout << "Ok....." << endl;
    }
    return (0);
  }

这个代码在do中,一旦do完成,它就不能获取while,因此就会出错。在下面的4个括号中减少一个括号,并在此之前添加一个括号。

票数 1
EN

Stack Overflow用户

发布于 2016-10-18 15:18:46

在后面的线路上

代码语言:javascript
复制
if (Option == 1) {

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

代码语言:javascript
复制
do {
    ...
} while (!GameOverDecider);

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40100277

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档