#Tic Tac Toe game not detecting win

75 messages · Page 1 of 1 (latest)

hollow granite
#

Hello, I've been trying to work on this Tic Tac Toe game for my school assignment, but it's not detecting any win when any player gets 3 in a row in any way.
I have 2 functions that checks the board for a win and one that checks if a player did win:

// check for win
void checkWin(char b[][NUM_COL], Player player[], bool& activeGame, int activePlayer)
{
   /* the code */
}

// checks the board
void checkBoard(char b[][NUM_COL], Player player[], bool& activeGame, string playerName, char playerSymbol)
{
/* the code */
}

The two functions are at the very bottom.
The Hastebin: https://hastebin.com/biwazumegu.cpp

stone mossBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

calm wigeon
#
void checkWin(char b[][NUM_COL], Player player[], bool& activeGame, int activePlayer)
{
    char playerSymbol;
    string playerName;

    if (activePlayer == 1)
    {
        playerSymbol = player[0].symbol;
        playerName = player[0].name;
        checkBoard(b, player, activeGame, playerName, playerSymbol);
    }
    else if (activePlayer == 2)
    {
        playerSymbol = player[1].symbol;
        playerName = player[1].name;
        checkBoard(b, player, activeGame, playerName, playerSymbol);
    }
}```
#

// checks the board
void checkBoard(char b[][NUM_COL], Player player[], bool& activeGame, string playerName, char playerSymbol)
{
    if (b[0][0] == playerSymbol && b[0][1] == playerSymbol && b[0][2] == playerSymbol)
    {
        cout << playerName << " Wins!" << endl;
        activeGame = false;
    }
    if (b[1][0] == playerSymbol && b[1][1] == playerSymbol && b[1][2] == playerSymbol)
    {
        cout << playerName << " Wins!" << endl;
        activeGame = false;
    }
    if (b[2][0] == playerSymbol && b[2][1] == playerSymbol && b[2][2] == playerSymbol)
    {
        cout << playerName << " Wins!" << endl;
        activeGame = false;
    }
    if (b[0][0] == playerSymbol && b[1][0] == playerSymbol && b[2][0] == playerSymbol)
    {
        cout << playerName << " Wins!" << endl;
        activeGame = false;
    }
    if (b[0][1] == playerSymbol && b[1][1] == playerSymbol && b[2][1] == playerSymbol)
    {
        cout << playerName << " Wins!" << endl;
        activeGame = false;
    }
    if (b[0][2] == playerSymbol && b[1][2] == playerSymbol && b[2][2] == playerSymbol)
    {
        cout << playerName << " Wins!" << endl;
        activeGame = false;
    }
    if (b[0][0] == playerSymbol && b[1][1] == playerSymbol && b[2][2] == playerSymbol)
    {
        cout << playerName << " Wins!" << endl;
        activeGame = false;
    }
    if (b[0][2] == playerSymbol && b[1][1] == playerSymbol && b[2][0] == playerSymbol)
    {
        cout << playerName << " Wins!" << endl;
        activeGame = false;
    }
}```
calm wigeon
#

and even better make activePlayer 0 or 1

#

What is the error exactly?

#

yeah you really need to use variables in array access

#

cuz your code is litereally flooded with duplicate code

#

like

#
if(activePlayer==1){
  std::cout<<player[0].name;
}else {
  std::cout<<player[1].name;
}
#

can be replaced with

#
std::cout<<player[activePlayer-1].name```
#

    // determine who gets which
    randN = rand() % 30 + 1;
    if (randN % 2 == 0) // if random number is even
    {
        player[0].symbol = 'X'; // player 1 is x
        player[1].symbol = 'O'; // player 2 is o
    }
    else // if random number is odd
    {
        player[0].symbol = 'O'; // player 1 is o
        player[1].symbol = 'X'; // player 2 is x
    }
}``` you also have this
#

but you don't actually base your turns on this

#

so this only has the effect that it is random what symbol player 1 gets

#

not in what order they get played

#

And you are also passing the activeGame to the function

#

activeGame is could always be assumed to be true so that wouldn't be necessary

#

And i think i finally found it

#

placeMark changes the turn

#

The turn gets changed before the check

hollow granite
#

would i have to check for the win before the change?

calm wigeon
#

Let's say that x wins

#

then it would change to O's turn before we check if k has won

#

And it won't detect a win because O hasn't won yet

#

did my tips make sense?

hollow granite
#

i am trying to understand
where would i check for the win?
would it be within the placeMark function or does it have to be outside the function

calm wigeon
#

I don't think that placemark should be responsible for changing the turn

hollow granite
#

should it instead be a separate function?

calm wigeon
#

nah just do it in the game loop

hollow granite
#

like this?

do
            {
                system("cls");
                displayGame();
                cout << "Active Player: " << activePlayer << endl;
                placeMark(theBoard, player, activePlayer);
                if (activePlayer == 0)
                {
                    activePlayer = 1;
                }
                else
                {
                    activePlayer = 0;
                }
            } while (activeGame = true);
calm wigeon
#

do you want to add support for three players in the future?

#

and also your while is wrong

#

and you don't need the = sign

#

cuz you can just do

#
while (activeGame)```
hollow granite
calm wigeon
#

because you could do something like

#
activePlayer = 1-activePlayer;```
#

You didn't need to remove the checkwin though

hollow granite
#

so would checkwin would be placed here?

placeMark(theBoard, player, activePlayer);
checkWin(theBoard, player, activePlayer);
  if (activePlayer == 0)
  {
  activePlayer = 1;
  }
  else
  {
  activePlayer = 0;
  }

something like this?

calm wigeon
#

think so

#

does it work?

hollow granite
#

it still happens

calm wigeon
#

did you change your if's to what i suggested above

#

in the checkwin function

#

because currently it would just skip that code

#

because active Player is 0

#

does it detect a win if you enter one last entry

#

Imma gonna go to bed now

hollow granite
#

aight gn
ty for helping i'll try to your suggestion

hollow granite
stone mossBOT
#

@hollow granite Has your question been resolved? If so, run !solved :)

hollow granite
#

this is what i changed it to

void checkWin(char board[][NUM_COLS], Player player[], int activePlayer)
{
    char playerSymbol;
    string playerName;

    playerSymbol = player[activePlayer].symbol;
    playerName = player[activePlayer].name;
    checkBoard(board, player, playerName, playerSymbol);
}

my main now:

do
{
  system("cls");
  displayGame();
  placeMark(theBoard, player, activePlayer);
  checkWin(theBoard, player, activePlayer);

  if (activePlayer == 0)
  {
    activePlayer = 1;
  }
  else
  {
    activePlayer = 0;
  }
} while (activeGame);
hollow granite
stone mossBOT
#

@hollow granite Has your question been resolved? If so, run !solved :)

calm wigeon
#

why do you have a nonactive player?

#

update stats doesn't work with ties

#

you never change the ie variable

#

also your row and column should be changed because you are currently entering in the y x format

#

which is really unintuitive to me

#

and also as a general tipuse more local variables

#

yes

#

The while loop is cheking with activeplayer

#

but activeplayer

#

hasn't won yet

#

because the last function that was called was nextPlayer

#

You also seem to be really relient on functions

#

you could really just use

#

arithmetic or thingsl such as nextPlayer

#

It is simple enough to do so and you don't have to go deep einto every function to figure out if it changes your variables with a reference

hollow granite
#

i think what ill do is start over with the actual game portion and work my way up till it starts working as intended since everything is getting more complicated the more i code 💀💀
ill keep the functions but make any changes that are necessary
ill keep ur tips n stuff in mind
tysm

#

!solved