I am making a tic tac toe game. I have a class to implement few methods here is the code:
package test;
public class test {
private String[][] board = new String[3][3];
public test() {
}
public void move(int x, int y) {
String turn = "X";
if (turn.equals("X")) {
turn = "O";
}else {
turn = "X";
}
board[x][y] = turn;
}
public void display() {
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println();
}
}
}
My problem is the code always puts "O" in the array position below is the test code use to run the class:
package test;
public class RunTest {
public static void main(String[] args) {
test game = new test();
game.move(1, 2);
game.move(1, 1);
game.move(0, 0);
game.display();
}
}
This is what is displayed
O null null
null O O
null null null
How can I get the code to alternate starting with "X"
then switching to "O"
?