功能不起作用-原因

Function is not working - why?

本文关键字:原因 不起作用 功能      更新时间:2023-10-16

我正在写一个井字游戏,我发现很难制作一个函数来检查空间何时被零填充或穿过

这是完整的代码(抱歉有点长):

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

问题是checkboard函数有时工作,或者根本不工作。如果玩家在一个已经被占领的地方绘制零/十字,那么它将替换它,而不应该这样做

edit:我没有收到错误,但觉得问题出在checkboard函数上,我对

没有想法

看看这些函数:

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]的值。

此代码中还有其他问题,但您可以从这个问题开始。