我正在写一个井字游戏,我发现很难做出一个功能,可以检查空间是否由空白或十字架填充
这是完整的代码(抱歉有点长):
#include <iostream>
using namespace std;
const int kingsize = 3;
char board[kingsize][kingsize];
bool p1 = false;
int p2 = -1;
char empty = 0;
/*this functions dispays the board*/
void bord()
{
cout <<
" ";
for (int iX = 0; iX < kingsize; iX++)
cout << " " << iX << " ";
cout << endl;
cout << " ";
for (int iX = 0; iX < kingsize; iX++)
cout << "+---";
cout << "+" << endl;
for (int iY = 0; iY < kingsize; iY++)
{
cout << " " << iY << " ";
for (int iX = 0; iX < kingsize; iX++)
cout << "|" << board[iY][iX] << " ";//this is the space to be filled by Os or Xs
cout << "|" << endl;
cout << " ";
for (int iX = 0; iX < kingsize; iX++)
cout << "+---";
cout << "+" << endl;
}
}
/*when this function is called it will show a blank board*/
void resetBoard()
{
for (int iY = 0; iY<kingsize; iY++)
{
for (int iX = 0; iX<kingsize; iX++)
{
board[iY][iX] = ' ';
}
}
}
/*this funtion is to plot the symbols in the grid based on the column and row*/
bool setboard(int iX, int iY, char cval)
{
if (iX >= kingsize || iY >= kingsize)
return false;
board[iY][iX] = cval;
return true;
}
/*this checks if there space is filled by one of the symbols*/
bool checkbord(int x, int z, char val)
{
bool fill = false;
if (board[x][z] != ' ')
{
fill = true;
}
return fill;
}
bool win(int tir)
{
int win = 3;
return (board[0][0] + board[0][1] + board[0][2] == win) // row 0
|| (board[1][0] + board[1][1] + board[1][2] == win) // chicking row 1
|| (board[2][0] + board[2][0] + board[2][2] == win) // checking row 2
|| (board[0][0] + board[1][1] + board[2][0] == win) // checking column 0
|| (board[0][1] + board[1][1] + board[2][1] == win) // column 1
|| (board[0][2] + board[1][2] + board[2][2] == win) // column 2
|| (board[0][0] + board[1][1] + board[2][2] == win) // diagonal
|| (board[2][0] + board[1][1] + board[0][2] == win) ; // checking diagonal
}
int main()
{
const int r = 3, c = 3;
char sym1 = 'X';
char sym2 = 'O';
char sym = 'O' || 'X';
cout << "TIC TAC TOE" << endl;
cout << endl;
cout << "How To Play" << endl;
cout << "to plot noughts and crosses you write the row number first " << endl;
cout << "then the number of the column" << endl;
cout << "Example :" << endl;
cout << endl;
resetBoard();
bord();
cout << endl;
cout << "plotting row 2 column 0" << endl;
cout << endl;
setboard(0, 2, 'X');
bord();
resetBoard();
cout << endl;
cout << "OK LET'S PLAY!!!" << endl;
bord();
int y, z;
int t=0;
do
{
loop:
cout << "first player to plot X:" << endl;
cout << "col No." << endl;
cin >> y;
cout << "row No." << endl;
cin >> z;
t++;
if (y > 2 || z > 2)//if the player enters numbers higher than too it will show this error message
{
cout << "error enter number between 0-2" << endl;
t--;
goto loop;
}
if (checkbord(y, z, sym) == 1)//if the player plots their symbol in a space that is already filled it will show this error message
{
cout << "spot alreay filled, please try again" << endl;
cout << endl;
t--;
goto loop;
}
setboard(y, z, sym1);//will plot the symbol based on what the player enetered
bord();
loop2:
cout << "second player plot O:" << endl;
cout << "col No." << endl;
cin >> y;
cout << "row No." << endl;
cin >> z;
if (y > 2 || z > 2)
{
cout << "error enter number between 0-2" << endl;
goto loop2;
}
if (checkbord(y, z, sym) == 1)
{
cout << "spot alreay filled" << endl;
t--;
goto loop2;
}
setboard(y, z, sym2);
bord();
} while (t <4);
loop3:
cout << "first player to plot X:" << endl;
cout << "col No." << endl;
cin >> y;
cout << "row No." << endl;
cin >> z;
t++;
if (y > 2 || z > 2)
{
cout << "error enter number between 0-2" << endl;
t--;
goto loop3;
}
if (checkbord(y, z, sym) == 1)
{
cout << "spot alreay filled" << endl;
t--;
goto loop3;
}
setboard(y, z, sym1);
bord();
return 0;
}
问题是checkbord功能有时或根本不工作。如果玩家在一个已经被占用的地方绘制了noughts / cross,那么它将取代它并且它不应该这样做
编辑:我没有收到错误,但感觉问题出在checkbord函数上,而且我没有想法
答案 0 :(得分:0)
看看这些功能:
bool setboard(int iX, int iY, char cval)
{
...
board[iY][iX] = cval;
...
}
bool checkbord(int x, int z, ...)
{
bool fill = false;
if (board[x][z] != ' ')
{
fill = true;
}
return fill;
}
因此setboard(1,2)
设置board[2][1]
的值,但checkbord(1,2,...)
会检查board[1][2]
的值。
此代码中还有其他问题,但您可以从这个问题开始。