我在这里有一个tic tac toe游戏,但我一直收到这些错误:
[错误]预期声明之前'}'令牌
[错误]期望在数字常量
之前的非限定id[错误]预期'}'在数字常量之前
[错误]数字常量之前的预期标识符
#include <iostream>
#include <string>
using std::string;
enum SquareState{ blank = ' ', X = 'X', 0 = '0'};
class Board{
private:
const int HEIGHT;
const int WIDTH;
int* gboard;
public:
Board() : WIDTH(3), HEIGHT(3)
{
gboard = new int[9];
for (int i = 0; i < 9; i++)
*(gboard+i) = blank;
}
Board() {delete[] gboard;}
void setX(int h, int w);
void set0(int h, int w);
bool isTaken(int h, int w);
SquareState isLine();
void draw();
};
void Board::setX(int h, int w)
{
*(gboard + h*HEIGHT + w) = X;
}
void Board::set0(int h, int w)
{
*(gboard + h*HEIGHT + w) = 0;
}
bool Board::IsTaken(int h, int w)
{
return *(gboard + h*HEIGHT + w) != ' ';
}
SquareState Board::IsLine()
{
if(*gboard==X && *(gboard +1)==X && *(gboard +2)==X)
return X;
if(*gboard==0 && *(gboard +1)==0 && *(gboard +2)==0)
return 0;
if(*(gboard+3)==X && *(gboard +4)==X && *(gboard +5)==X)
return X;
if(*(gboard+3)==0 && *(gboard +4)==0 && *(gboard +5)==0)
return 0;
if(*(gboard+6)==X && *(gboard +7)==X && *(gboard +8)==X)
return X;
if(*(gboard+6)==0 && *(gboard +7)==0 && *(gboard +8)==0)
return 0;
if(*gboard==X && *(gboard +3)==X && *(gboard +6)==X)
return X;
if(*gboard==0 && *(gboard +3)==0 && *(gboard +6)==0)
return 0;
if(*(gboard+1)==X && *(gboard +4)==X && *(gboard +7)==X)
return X;
if(*(gboard+1)==0 && *(gboard +4)==0 && *(gboard +7)==0)
return 0;
if(*(gboard+2)==X && *(gboard +5)==X && *(gboard +8)==X)
return X;
if(*(gboard+2)==0 && *(gboard +5)==0 && *(gboard +8)==0)
return 0;
if(*gboard==X && *(gboard +4)==X && *(gboard +8)==X)
return X;
if(*gboard==0 && *(gboard +4)==0 && *(gboard +8)==0)
return 0;
if(*(gboard+2)==X && *(gboard +4)==X && *(gboard +6)==X)
return X;
if(*(gboard+2)==0 && *(gboard +4)==0 && *(gboard +6)==0)
return 0;
return blank;
}
void Board::draw()
{
using std::cout;
cout << "\n"
for (int i = 0; i <HEIGHT; i++){
cout << (char)*(gameboard + i*HEIGHT);
for (int c = 1; c < WIDTH; c++);
cout << " | " << (char)*(gameboard + i*WIDTH + c);
cout << "\n" << "------" << "/n";
}
class Game
{
public:
Board* doInput(string player, gboard * gb);
bool inRange (int test);
};
Board* Game::doInput(string player, Board* gb)
{
using std::cout;
using std::cin;
gb->draw();
string letter;
if(player.compare("one")==0)
letter = "X";
else if (player.compare("two")==0)
letter = "0";
else return gb;
int input1, input2;
do{
do{
cout << "\nplayer" << player.c_str()
<< ", choose a row to put an "
<< letter.c_str() << ": ";
cin >> input1;
}while(!inRange(input1));
do{
cout << "\nplayer" << player.c_str()
<< ", choose a column to put an "
<< letter.c_str() << ": ";
cin >> input2;
}while(!inRange(input2));
}while (gb->isTaken(input1, input2));
if (player.compare("one")==0)
gb->setX(input1, input2);
else
gb->set0(input1, input2);
return gb;
}
bool Game::inRange(int test)
{
return test > -1 && test < 3;
}
int main(void)
{
using std::cout;
using std::cin;
Board* gb = new Board;
Game g;
string player1, player2;
cout<< "Let's play some Triple-T"
<<"\nplayer one, introduce yourself: ";
cin >> player1;
cout << "\nplayer two, introduce yourself: ";
cin >> player2;
while (gb->isLine()== ' ')
{
gb = g.doInput("one", gb);
gb = g.doInput("two", gb);
}
gb->draw();
if (gb->isLine()==X)
cout<< "\nplayer one has prevailed\n";
else
cout<< "\nplayer one has prevailed\n";
return 0;
}//end main
答案 0 :(得分:3)
;
cout << "\n"
遗失了void Board::draw()
在您的枚举中,变量名称不能是关键字/常量 - 0
答案 1 :(得分:0)
enum SquareState{ blank = ' ', X = 'X', 0 = '0'};
使用0作为常量标识符?我不确定这是可能的。
答案 2 :(得分:0)
错误在于:
enum SquareState{ blank = ' ', X = 'X', 0 = '0'};
^
枚举值的名称必须遵循与变量名称相同的规则 - 您不能以数字开头,只包含数字。
答案 3 :(得分:0)
enum SquareState{ blank = ' ', X = 'X', 0 = '0'};
0 = '0'
似乎是罪魁祸首,因为0不是有效的变量名。