#Need help with boolean methods and algorithms

1 messages · Page 1 of 1 (latest)

desert stream
#

I'm currently making a tictactoe program in java but I'm having trouble making boolean algorithms for the program

cold valeBOT
#

Hey, @desert stream!
Please remember to /close this post once your question has been answered!

desert stream
#

I can provide the code i have so far.

#

commented out code is what I need to fix.

boreal pier
#

you need help with the win methods?

desert stream
#

yeah

boreal pier
#

I assume the "player" symbol is the player argument?

desert stream
#

Player is actually char X & O

#

which go into the gameboard

boreal pier
#

what is the second argument then

desert stream
desert stream
#

else its player 2's turn

boreal pier
#

I'm talking about this

desert stream
#

and once the char is added count++ so it's player 2's turn

boreal pier
#

what is the player char

#

just X and O

desert stream
#

yeah

boreal pier
#

alright

desert stream
#

essentially

#

can we have to use the parameter given

boreal pier
#

so just have a loop and then focus on one column at a time and check if they're all player

desert stream
#

we cant leave it out

desert stream
boreal pier
#

then the same idea for the horizontal one

#

ah I see

desert stream
#

and also with the "isBoardFull" method I tried iterating through the 2d array checking if all the elements were filled up but it didnt work

#

so i also need help with that :/

boreal pier
#

okay so this is for a vertical win

String playerStr = Character.toString(player);
boolean win = false;
for (int i = 0; i < array.length; ++i) { // this loops thru the rows
  if (array[0][i].equals(playerStr)
      && array[1][i].equals(playerStr)
      && array[2][i].equals(playerStr) { // if all of them are the same for a column
    win = true;
    break;
  }
}  

return win;
#

I did Character.toString(player) for equals bc it's not a string by defaul

#

and this is for a horizontal win

String playerStr = Character.toString(player);
boolean win = false;
for (int i = 0; i < array[0].length; ++i) { // this loops thru the columns
  if (array[i][0].equals(playerStr)
      && array[i][1].equals(playerStr)
      && array[i][2].equals(playerStr) { // if all of them are the same for a column
    win = true;
    break;
  }
}  

return win;
#

very similar

#

Also this assumes that the array is a 3x3

#

if it's null then u can just do:

for (int i = 0; i < array.length; ++i) {
  for (int j = 0; j < array[0].length; ++j) {
    if ("".equals(array[i][j])) { // this happens when there is an empty spot
      return false;
    }
  } 
}
return true;
desert stream
#

okay the winHorizontal and winVertical works

#

but now the isBoardFull isn't working

boreal pier
#

oh

#

what is it the error

boreal pier
desert stream
#

nvm fixed it

boreal pier
#

what was it

desert stream
#

i did ""

#

instead of " "

#

lol

boreal pier
#

ahh

#

yeah

desert stream
#

thanks man

boreal pier
#

np

#

gl

desert stream
#

or maam

#

love you

boreal pier
#

man

#

hah ur good