gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:41:31 27 4
gpt4 key购买 nike

尝试使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com