不合格的id错误和井字棋板不能正常工作
我正在尝试编写一个C++井字棋游戏。但是我不断收到两个错误。 1.在while循环之后的每个我的两个if状态之前,如果出现'',我得到错误“unqualified-id before”。我不知道为什么。 2.当我运行程序时,automaticaly表示无效移动,它不会用x或o来替换板上的空间。请帮助,这里是我的代码:不合格的id错误和井字棋板不能正常工作
/*
Sophia Ali
Template for TicTacToe.cpp (CS-509 Assignment 5)
can't get program to store x and o in the board
*/
#include<iostream>
#include <cstdlib>
using namespace std;
/*
Game status enumeration
*/
enum Status { WIN, DRAW, CONTINUE, QUIT };
/*
Function prototypes
*/
// showBoard: Show current state of board
void showBoard(const char board[], int boardSize);
// checkGameState: Returns WIN or CONTINUE
Status checkGameState(const char board[]);
int getHumanSquare(const char board[]);
int getComputerSquare(const char board[]);
// checkBadSquare: Checks to see if a chosen square is already taken; returns
// true if already taken; used by getHumanSquare and
// getComputerSquare functions above.
bool checkBadSquare(const char board[], int squareNum);
int getrandint(int min, int max);
int main()
{
char board[] = "123456789"; // 10 element char board
const int boardSize = 10;
Status gameState = CONTINUE;
int gametype, squareChoice, turnNum = 0;
char currentSymbol; // 'o' or 'x'
cout << "\n This is a Tic Tac Toe program. Choose the type of game: "
<< "\n (1) human o vs. human x (2) human o vs. dumb computer x"
<< "\n\n -> ";
cin >> gametype;
/* Show the current state of Tic Tac Toe board. */
cout << gameState;
cout << "\n\n";
/*
Main game loop
*/
while (gameState == 2)
{
/* Increment turnNum by 1. */
turnNum++;
/* If turnNum equal to 10
Set gameState to DRAW.
Break out of while loop. */
if (turnNum == 10)
{
gameState = DRAW;
break;
}
/* If we are on an odd-numbered turn
Print "It's o's turn."
Set currentSymbol to 'o'.
Call getHumanSquare function to get squareChoice.*/
if (turnNum%2 != 0)
{
cout << "It's o's turn.";
currentSymbol = 'o';
cout << "\n\n";
getHumanSquare(board);
showBoard(board, boardSize);
}
/* Else (we are on an even-numbered turn)
Print "It's x's turn."
Set currentSymbol to 'x'. */
else
{
cout << "It's x's turn.";
currentSymbol = 'x';
cout << "\n\n";
showBoard(board, boardSize);
}
/* If the gametype is 1 (human vs. human)
Call getHumanSquare function to get squareChoice.*/
if (gametype == 1)
{
getHumanSquare(board);
}
/* Else (gametype is 2 (human vs. computer))
Call getComputerSquare function to get squareChoice. */
else
{
getComputerSquare(board);
}
/* If squareChoice is -1 (human player quit)
Set gameState to QUIT.*/
if (squareChoice == -1)
{
gameState = QUIT;
}
/* Else
Insert currentSymbol into board at (squareChoice - 1).
Show the current state of the Tic Tac Toe board.
Call checkGameState function to determine the gameState. */
if (squareChoice >= 0 && squareChoice <= 8 && board[squareChoice-1] == '0' + squareChoice)
{
board[squareChoice-1] = currentSymbol;
}
else
{
cout <<"Invalid move";
turnNum--;
cout << "\n\n";
}
checkGameState(board);
}
return 0;
}/* end while*/
/* If gameState is WIN
print "Player " currentSymbol " is the winner." */
if (gameState == WIN)
cout << "Player" << currentsymbol << "is the winner.";
/* If gameState is DRAW
print "It's a draw." */
if (gameState = DRAW)
cout <<"It's a draw.";
/////////////////////////////////////////////////////////////////////
void showBoard(const char board [], int size)
{
cout << endl;
for (int i = 0; i < size ; i++)
{
cout << board[ i ] << " ";
if ((i + 1) % 3 == 0)
cout << endl;
}
cout << endl;
}
/////////////////////////////////////////////////////////////////////
Status checkGameState(const char board[])
{
// Board Array
//
// 1 2 3 0 1 2
// 4 5 6 --> 3 4 5
// 7 8 9 6 7 8
//
// Diagonal winners
if (board[ 0 ] == board[ 4 ] && board[ 0 ] == board[ 8 ])
return WIN;
else if (board[ 2 ] == board[ 4 ] && board[ 4 ] == board[ 6 ])
return WIN;
// Horizontal winners
else if (board[ 0 ] == board[ 1 ] && board[ 1 ] == board[ 2 ])
return WIN;
else if (board[ 3 ] == board[ 4 ] && board[ 4 ] == board[ 5 ])
return WIN;
else if (board[ 6 ] == board[ 7 ] && board[ 7 ] == board[ 8 ])
return WIN;
// Vertical winners
else if (board[ 0 ] == board[ 3 ] && board[ 3 ] == board[ 6 ])
return WIN;
else if (board[ 1 ] == board[ 4 ] && board[ 4 ] == board[ 7 ])
return WIN;
else if (board[ 2 ] == board[ 5 ] && board[ 5 ] == board[ 8 ])
return WIN;
else
// No one has won yet
return CONTINUE;
}
/////////////////////////////////////////////////////////////////////
int getHumanSquare(const char board[])
{
int squareNum;
cout << "\n Input the number of an empty square: (-1 to quit) ";
cin >> squareNum;
while (checkBadSquare(board, squareNum) == true)
{
cout << "\n Bad input. Choose another square: ";
cin >> squareNum;
}
return squareNum;
}
/////////////////////////////////////////////////////////////////////
int getComputerSquare(const char board[])
{
int squareNum;
squareNum = getrandint(1, 9);
while (checkBadSquare(board, squareNum) == true)
{
squareNum = getrandint(1, 9);
}
return squareNum;
}
/////////////////////////////////////////////////////////////////////
bool checkBadSquare(const char board[], int squareNum)
{
int realSquareNum = squareNum - 1; // count from 0
if (squareNum == -1)
return false; // Let quit code pass as a valid square
else if (squareNum > 9)
return true; // Square numbers out of range are invalid
else if (board[ realSquareNum ] == 'o' || board[ realSquareNum ] == 'x')
return true; // Already taken squares are invalid
else
return false; // Valid square number
}
/////////////////////////////////////////////////////////////////////
int getrandint(int min, int max)
{
int scale, shift;
scale = max - min + 1;
shift = min;
return rand() % scale + shift;
}
在这里你的第一条评论是不正确的。
}
return 0;
}/* end while*/
/* If gameState is WIN
print "Player " currentSymbol " is the winner." */
if (gameState == WIN)
cout << "Player" << currentsymbol << "is the winner.";
你叫什么/* end while */
实际上是main()
末。您在上面看到的}
是实际结束您的while
循环。
错误告诉你,你正在编写函数外的代码。
但我认为这是错误的地方评论.. –
@GrijeshChauhan德鲁建议你需要弄清楚你的牙套有什么问题。可执行代码(如if语句)必须在函数内。由于编译器认为'main()'结束了注释“end while”的位置,因此if语句会导致错误。 –
+对于好的建议,,,我误解了你的答案:) –
语法问题
if (gameState == WIN)
cout << "Player" << currentSymbol << "is the winner.";
/* If gameState is DRAW
print "It's a draw." */
if (gameState = DRAW)
cout <<"It's a draw.";
应该是内部主..(我认为)现在它是出在全球范围内。
逻辑问题
与您的代码可能是上线
if (squareChoice >= 0 && squareChoice <= 8 && board[squareChoice-1] == '0' + squareChoice)
这需要适当括号。你是什么意思
&& board[squareChoice-1] == '0' + squareChoice
请评论。
by && board [squareChoice-1] =='0'+ squareChoice 我的意思是如果squarechoice -1等于0 + squareChoice。因为squareChoice-1是框的编号,0 + squareChoice将是用户选择的内容。 – user2085224
''0'+ squareChoice'不是你想要的。也许'0 + squareChoice'但这只是'squareChoice' – 2013-04-01 17:01:42
会更容易,如果我用这个代替: else { if(squareChoice == 1 && board [1] =='1') board [1 ] = currentSymbol; else if(squareChoice == 2 && board [2] =='2') board [2] = currentSymbol; else if(squareChoice == 3 && board [3] =='3') board [3] = currentSymbol; else if(squareChoice == 4 && board [4] =='4') board [4] = currentSymbol; else if(squarechoice == 5 && board [5] =='5') 等等每个板号? – user2085224
只是一个建议。正确缩进你的代码。如果你能看到为什么第一个错误很容易出现**。其次要注意信箱。 – 2013-04-01 16:11:57
@stardust_如果(squareChoice> = 0 && squareChoice user2085224
它没有工作意味着它没有给出预期的答案?或者它根本不编译? – 2013-04-01 16:26:28