尝试使用OOP C ++制作BASIC TicTacToe游戏
我得到的错误是: 第74行意外的不合格ID在'while'之前(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)**
答案 0 :(得分:3)
您在此处结束了board
功能:
cout << " | | " << endl << endl;
} // and for some reason this one (3) <----PROBLEM is this Closing Brace
while (i==-1) // **this one (1)**
答案 1 :(得分:0)
you forgot to write ; at the end of a decleration:
while(i == -1) // **this one (1)**
{
if (player % 2)
player == 1
else
AND
player == 2 //You forgot to write ;
cout << "Please enter 1-3 for row: ";
cin >> ch1;
cout << "Please enter 1-3 for coumns: ";
cin >> ch2;
this makes probems later in the code when stuff isn't found!