#Need help with boolean methods and algorithms
1 messages · Page 1 of 1 (latest)
Hey, @desert stream!
Please remember to /close this post once your question has been answered!
you need help with the win methods?
yeah
I assume the "player" symbol is the player argument?
what is the second argument then
well i set it up as if count is % 2 = 0 its player one's turn
else its player 2's turn
I'm talking about this
and once the char is added count++ so it's player 2's turn
alright
so just have a loop and then focus on one column at a time and check if they're all player
we cant leave it out
yeah idk what that loop would look like because im pretty sure i made mine wrong
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 :/
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;
okay the winHorizontal and winVertical works
but now the isBoardFull isn't working
this assumes that an empty tile is "" or null
nvm fixed it
what was it
thanks man