2014-02-25 44 views
0

所以我正在制作一个Tic Tac Toe游戏,但我陷入了一个地步,如果已经采取了位置=>让用户进入另一个位置。这里是我正在谈论的:如何循环,直到== true

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主()

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; 
    } 
} 

哪里是我的错误,我该如何解决呢?有任何想法吗 ?使用

+6

您在while循环中只使用一个equals,它将始终返回true,因为它只是分配值 –

+0

最好提供一个[SSCCE](http://www.sscce.org/)。 –

+1

除了上述==/=的问题。只要pos [x] [y]等于1,代码就会重复循环。你只需阅读一个新的价值。但据我所见,你不更新x/y,因此一旦你进入循环,你应该在那里堆叠。 –

回答

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; 

} 
+0

我先试了这个,但它没有为我工作,但它修复了一些错误后,现在工作。谢谢 – PhysicalPhantom

+0

我想你可能会有其他错误,因为计数变量用于检查你的渣滓剩余多少个槽 –

4

==代替=
前者相比,后者的任务

+0

我已经试过了。同样的结果。 – PhysicalPhantom

+1

我不能相信。你可能有其他错误,但==肯定会帮助解决这个问题 – deviantfan

+1

据我所知,pos [y] [x]的值在你的while循环中没有改变。一旦它的值是1,你有一个无限循环。 – tgmath

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