I'm literally brand new to Java and I'm having trouble with declaring this 2d array in my code. If I do this, my code will work as expected:
class game{
String player = "x";
String[][] board = {{" ", " ", " "}, {" ", " ", " "}, {" ", " ", " "}};
game(){
}
public void make_move(int square){
board[square/3][square%3] = player;
player = (player.equals("x")) ? "O" : "x";
}
So in this situation would board and player be instance variables since I didn't use the static keyword and then each instance of game will have a separate board and player?
Coming from python, I'm used to defining variables inside the constructor and using words like self.whatever for instance attributes. So I tried to do the same thing here.
class game{
game(){
String[][] this.board = {{" ", " ", " "}, {" ", " ", " "}, {" ", " ", " "}};
String this.player = "x";
}
public void make_move(int square){
board[square/3][square%3] = player;
player = (player.equals("x")) ? "O" : "x";
}
This didn't work at all and I was getting multiple errors. "Array initializer is not allowed here" and "Cannot resolve symbol 'player' ." Could anyone tell me what's going on here and point me in the right direction? Thanks.