c++ - 'while' 之前的意外不合格 ID

标签 c++ oop compiler-errors tic-tac-toe

尝试使用 OOP C++ 制作 BASIC TicTacToe 游戏

我得到的错误是: 第 74 行在“while”之前出现意外的不合格 ID (1) 第 139 行错误:输入末尾应为“}”(2) 第 77 行错误:输入末尾预期的不合格 ID (3)

我不知道这些括号怎么会错...提前谢谢您! 这是我的代码:

#include <iostream>
#include <stdlib.h>
using namespace std;

class TicTacToe
{
private:
int player=1, cw , ch1, ch2; //ch= choice for rows and columns
char pick, grid[10]= {' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
public:
int checkWin()
{
  if (grid[1] == grid[2] && grid[2] == grid[3])

    return 1;
else if (grid[4] == grid[5] && grid[5] == grid[6])

    return 1;
else if (grid[7] == grid[8] && grid[8] == grid[9])

    return 1;
else if (grid[1] == grid[4] && grid[4] == grid[7])

    return 1;
else if (grid[2] == grid[5] && grid[5] == grid[8])

    return 1;
else if (grid[3] == grid[6] && grid[6] == grid[9])

    return 1;
else if (grid[1] == grid[5] && grid[5] == grid[9])

    return 1;
else if (grid[3] == grid[5] && grid[5] == grid[7])

    return 1;
else if (grid[1] != '1' && grid[2] != '2' && grid[3] != '3'
                && grid[4] != '4' && grid[5] != '5' && grid[6] != '6'
              && grid[7] != '7' && grid[8] != '8' && grid[9] != '9')

    return 0;
else
    return -1;
}//check for winner
char mark()
{
    if(player==1)
       return 'X';
    else
       return 'O';
}
void board()
{
    system("cls");
cout << "\n\n\tTic Tac Toe\n\n"; //learned that \t is to tab it in instead    of using spaces

cout << "Player 1 = X    Player 2 = O" << endl << endl;
cout << endl;

cout << "   1     2    3  ";
cout <<"\n";
cout << "     |     |     " << endl;
cout << "1 " << grid[1] << "  |  " << grid[2] << "  |  " << grid[3] << endl;
cout << "     |     |     " << endl;
cout << "_____|_____|_____" << endl;
cout << "     |     |     " << endl;

cout << "2 " << grid[4] << "  |  " << grid[5] << "  |  " << grid[6] << endl;
cout << "     |     |     " << endl;
cout << "_____|_____|_____" << endl;
cout << "     |     |     " << endl;

cout << "3 " << grid[7] << "  |  " << grid[8] << "  |  " << grid[9] << endl;
cout << "     |     |     " << endl;
cout << "     |     |     " << endl << endl;

} // and for some reason this one (3)

while (i==-1) // **this one (1)**
{
    if(player %2)
        player==1
    else
        player==2
    cout<< "Please enter 1-3 for row: ";
    cin>> ch1;
    cout<< "Please enter 1-3 for coumns: ";
    cin>>ch2;

    mark();

    if(ch1=1 && ch2 ==1)
        mark = grid[1];
    else if (ch1=2 && ch2== 1)
        mark = grid[2];
    else if (ch1=3 && ch2== 1)
        mark = grid[3];
    else if (ch1=1 && ch2== 2)
        mark = grid[4];
    else if (ch1=2 && ch2== 2)
        mark = grid[5];
    else if (ch1=3 && ch2== 2)
        mark = grid[6];
    else if (ch1=1 && ch2== 3)
        mark = grid[7];
    else if (ch1=2 && ch2== 3)
        mark = grid[8];
    else if (ch1=3 && ch2== 3)
        mark = grid[9];
    else
    {
        cout<<" Move is invalid";
        player--; //so player can retake turn
        //cin.ignore(); //ignore what was input
        //cin.get(); // get answers
    }
       cw= checkwin();
}
board();
if(i==1)

    cout<<"\aPlayer "<<--player<<" win "; // a makes a beep!
else
    cout<<"\aGame draw";

//cin.ignore();
//cin.get();
return 0;


};

int main()
{
cout<<" \tWelcome to TicTacToe!";
TicTacToe game;
return 0;

} // **issue with this one (2)**

最佳答案

你在这里结束了你的board功能:

cout << "     |     |     " << endl << endl;

} // and for some reason this one (3) <----PROBLEM is this Closing Brace

while (i==-1) // **this one (1)**

关于c++ - 'while' 之前的意外不合格 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28052295/

相关文章:

c++ - 从容器中获取元素的常量资格的通用方法

c++ - 1 个带有 cmake 的体系结构 x86_64 的重复符号

ruby - 在Ruby中, `self.i`和 `@i`之间有区别吗?

c++ - 如何在switch case语句中一起解决char和number?

java - Hashsets的ArrayList!编译错误

c++ - Python Pandas 与 C++ 文本 CSV 数据导入解决方案的性能对比

c++ - 使用 move 语义在构造函数中初始化类成员

java - 如何在 Java 中正确建模战舰游戏

oop - UML - 对象方法返回一个集合

java - 如何使用具体参数进行子类化