-1

I am writing a tic-tac-toe game and I am finding it hard to make function that checks when a space is filled by either the noughts or crosses

Here is the full code (sorry a bit long):

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

The problem is that checkbord function works sometimes or not at all. if the player plots the noughts/cross in a place that is already taken then it will replace it and its not supposed to do that

edit: I am not getting an error but feel the problem is with the checkbord function and I am out of ideas

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Be specific. At what point you are getting error? Nobody is going to take your code, debug it and find what is logically getting wrong. – Mangesh Jan 07 '16 at 05:55
  • Welcome to Stack Overflow. The ability to prepare a [minimal complete example](http://stackoverflow.com/help/mcve) is a very important skill, much more valuable than a single bug-fix. If I run your code I might see the error, or I might not. You can make my job much easier by reducing the code until it is very simple but still produces the error. – Beta Jan 07 '16 at 06:17
  • its been edited sorry – Yamarai Akizuki Jan 07 '16 at 06:26
  • Are you absolutely sure that `'X' + 'X' + 'X'` is 3? And that `'O' + 'O' + 'O'` is also 3? Looks like you need a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Jan 07 '16 at 06:29

1 Answers1

0

Look at these functions:

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

So setboard(1,2) sets the value of board[2][1], but checkbord(1,2,...) checks the value of board[1][2].

There are other problems in this code, but you can start with this one.

Beta
  • 96,650
  • 16
  • 149
  • 150
  • thanks i have fixed it! you said there are other problems, could you pls point them out – Yamarai Akizuki Jan 07 '16 at 18:31
  • @YamaraiAkizuki, here are three: 1) you use `goto`, which is deprecated and inelegant, 2) as moldbnilo has pointed out, you add `char` variables, when you should be using "==", and 3) your `main` function is too long and contains too much repetitive code. – Beta Jan 08 '16 at 02:12