所以我正在制作一个Tic Tac Toe游戏但我陷入了如果已经采取了位置的地步=>让用户进入另一个位置。这就是我在说的:
int pos[3][3]=
{
{0,0,0},
{0,0,0},
{0,0,0}
};
bool CheckForPos(int x,int y)
{
if (pos[y][x] == 0) return true;
return false;
}
这是我检查位置是否已输入的矩阵。 以及 int main():
中的代码if( CheckForPos(y,x) )
{
pos[y][x] = 1;
chess[y][x] = 'X';
}
else
{
while (pos[y][x] = 1)
{
cout << "Spot already taken." << endl;
cin>>get;
}
}
我的错误在哪里?我该如何解决?有什么想法吗?
答案 0 :(得分:4)
使用==
代替=
前者是比较,后者是作业
答案 1 :(得分:1)
做这样的事......
if( CheckForPos(y,x))
{
pos[y][x] = 1;
chess[y][x] = 'X';
//take one count variable for counting that how much position has be finished like if
count value is greater than your array size than terminate it
count++;
}
else
{
//simply print your message and again get your position
cout << "Spot already taken." << endl;
cin>>get;
}
答案 2 :(得分:0)
while (pos[y][x] = 1)
{
cout << "Spot already taken." << endl;
cin>>get;
}
与
相同while (pos[y][x] == 1) // == instead of = !
{
cout << "Spot already taken." << endl;
cin>>get;
}
是无限循环,因为while语句总是返回true