2014-02-25 58 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