0

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.

Jonathan
  • 199
  • 1
  • 9
  • Possible duplicate of [How do I declare and initialize an array in Java?](https://stackoverflow.com/questions/1200621/how-do-i-declare-and-initialize-an-array-in-java) – user7 Nov 25 '18 at 07:32
  • @user7 It is more of a scope question than how to initialize an array. I'm sure there would be possible duplicates for this question, but the one you posted is not the one. – Vineeth Chitteti Nov 25 '18 at 07:39
  • Please start with a book or tutorial that provides a basic introduction to Java before posting questions like this. This is basic syntax that any introductory text would cover. – Mark Rotteveel Nov 25 '18 at 09:16

2 Answers2

2

1: You are correct in saying that game and board are instance variables.

2: You have to create a game object in order to actually use the make_move method, since it is a non-static method. You also don't need this in the this.board statement; simply saying board=whatever is fine because it is an instance variable. However, you must initialize these instance variables before the constructor, otherwise the constructor doesn't know what variables it is changing. You could do either

class game {
String[][] board;
String player;


game() {
    board = new String[][]{{" ", " ", " "}, {" ", " ", " "}, {" ", " ", " "}};
    player = "x";
}

public void make_move(int square) {
    board[square / 3][square % 3] = player;
    player = (player.equals("x")) ? "O" : "x";

} }

Which uses a constructor to define variables initialized by the class, or the simpler

class game {
String[][] board= new String[][]{{" ", " ", " "}, {" ", " ", " "}, {" ", " ", " "}};
String player="x";



public void make_move(int square) {
    board[square / 3][square % 3] = player;
    player = (player.equals("x")) ? "O" : "x";

}}

which allows you to skip a constructor if you want all instances of game to have the same initial value for board and player, probably not what you want. In the future, learn the java scope rules, and remember to always define instance variables within a class definition, not the constructor.

  • Ahh thank you. So in the first option you posted, are the variables board and player equipped with class level scope because you declared them outside of a method first? If you modified the first to just be String[][] board = new String[][]{{" ", " ", " "}, {" ", " ", " "}, {" ", " ", " "}} without first declaring the variable under the class, then it would just be defined inside the constructor? – Jonathan Nov 25 '18 at 08:25
  • Yeah, they have class level scope if they are declared inside the class definition. You are also correct on the second point: if you do that, you can't use the board variable outside the constructor (so it's basically useless, unless you use it to define other instance variables) unless you make the constructor return String[][], something you probably don't want. – Daniel Felsenthal Nov 25 '18 at 08:44
1
class Game{
String player;
String[][] board = null;

 Game(String playerName){ 
     this.player = playerName; 
     this.board = new String[][]{{" ", " ", " "}, {" ", " ", " "}, {" ", " ", " "}}`
 }

 public void makeMove(int square){
     board[square/3][square%3] = player;
     player = (player.equals("x")) ? "O" : "x";

 }

You need to understand the concept of scope of variables in java as you're coming from Python. Good place to start: https://www.geeksforgeeks.org/variable-scope-in-java/

Game(){
         String[][] this.board = new String[][]{{" ", " ", " "}, {" ", " ", " "}, {" ", " ", " "}};
         String this.player = "x";
     }

the player variable and the board variable you defined here exists in the scope of this function and gets deleted as soon as the function's execution is complete unless you're returning the value.

Since constructors do not return the variable you must declare the variable at the class level, and initialize them in the constructor if the need be.

Vineeth Chitteti
  • 1,274
  • 2
  • 12
  • 26
  • The code you posted won't resolve the error. Will still get "Array initializer is not allowed here" – Jonathan Nov 25 '18 at 07:46
  • @Jonathan Corrected. On a side note, Class names should be in Pascal Case and method names should be in Camel case. And Constructor though a method name should be in Pascal Case. – Vineeth Chitteti Nov 25 '18 at 09:37