0
我正在製作一個雙人遊戲,玩家在1-9網格中輸入一個數字。玩家X用變量Xp輸入所需的位置。玩家O也一樣。但是我不能做一個if語句來檢查玩家是否輸入了已經採用的網格點。檢查數組中的元素是否已被佔用
#include <iostream>
using namespace std;
void showBoard(char board[], int turn, int Xp, int Op);
void winning(char board[]);
int main()
{
int Xp=0, Op=0, turn=0;
char board[9] = {'1','2','3','4','5','6','7','8','9'};
showBoard(board, turn, Xp, Op);
return 0;
}
void showBoard(char board[], int turn, int Xp, int Op)
{
cout << "Please enter a number on the board that is the spot you wish to use" << endl;
cout << "Board:\n";
cout << board[0] << " " << board[1] << " " << board[2] << endl;
cout << board[3] << " " << board[4] << " " << board[5] << endl;
cout << board[6] << " " << board[7] << " " << board[8] << endl;
do
{
for (turn=1; turn<10; turn++)
if (!(turn % 2) == 0)
{
cout << "\nPlayer X's turn." << endl;
cin >> Xp;
if(Xp=Op) //HERE IS THE CHECKER
{
cout << "Spot is taken! Enter new spot: ";
cin >> Xp;
board[Xp-1] = 'X';
}
else
{
board[Xp-1] = 'X';
} //WHERE IT ENDS
cout << "Current Board:\n";
cout << board[0] << " " << board[1] << " " << board[2] << endl;
cout << board[3] << " " << board[4] << " " << board[5] << endl;
cout << board[6] << " " << board[7] << " " << board[8] << endl;
winning(board);
}
else
{
cout << "\nPlayer O's turn." << endl;
cin >> Op;
board[Op-1] = 'O';
cout << "Current Board:\n";
cout << board[0] << " " << board[1] << " " << board[2] << endl;
cout << board[3] << " " << board[4] << " " << board[5] << endl;
cout << board[6] << " " << board[7] << " " << board[8] << endl;
winning(board);
}
}while(turn<10);
cout << "No one won.\n";
return;
}
數組'在初始化一切「0'',那麼如果它是一個'」 0''你知道這不是招」沒有被採取。 –
'if(Xp = Op)'我相當肯定不會做你認爲的事。嘗試'==' – WhozCraig
另外,嘗試爲兩個玩家使用相同的代碼。 – Deduplicator