1
class Board {
    Direction[][] board;
    Integer width;
    Integer height;

    Board(Integer width, Integer height) {
        this.width = width;
        this.height = height;

        board = new Direction[width][height];

        for (Integer y = 0; y < height; y++) {
            for (Integer x = 0; x < width; x++) {
                board[x][y] = Direction.Down;
            }
        }   
    }

    Direction direction(Integer x, Integer y) {
        return board[x][y];
    }

    void update(Integer x, Integer y) {
        board[x][y].next();
    }
}

The enumerated values in Direction are: Up or Down

Firstly, I want to initialise my board so that all the initial values at every place of the board are set to Down.

Secondly, I want to retrieve the value at a particular (x,y) position of of the board

Thirdly, I want to be able to update the value at that particular element of the board. The next() function is designed to change the value from Down to Up or vice versa. This is declared separately.

I believe I am doing something wrong as the code compiles, however my program doesn't work as it should.

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
maclunian
  • 7,893
  • 10
  • 37
  • 45
  • "however my program doesn't work as it should." Could you specify? – toto2 Jun 27 '11 at 00:23
  • Your "board" does not initialize correctly? – Kal Jun 27 '11 at 00:25
  • @toto Basically I get a blank screen with the relative background color to `Down` defined elsewhere, when I try to run the program. Nothing else occurs. Similarly, If I set to `Direction.Up` above, the other colour appears but nothing happens. – maclunian Jun 27 '11 at 00:26
  • I think the `board` does initialise correctly @Kal I think the `void update(Integer x, Integer y){...}` function may be the issue. – maclunian Jun 27 '11 at 00:27
  • 1
    You need to be more specific about what is happening because from your description this sounds like a UI problem. Which implies that the code is not relevant to the problem. – drekka Jun 27 '11 at 00:29
  • @Derek So the code is ok then? – maclunian Jun 27 '11 at 00:30
  • @maclunian I guess you are using a GUI. Then your most likely problem is that your data is not synchronized to the Swing event dispatch thread (EDT) (the board in memory for the Swing thread is the same as the modified board). [EDIT: answers below about the use of `next` make sense too.] – toto2 Jun 27 '11 at 00:31
  • 2
    Is there a reason you are using *int* instead of *Integer*? See [this post](http://stackoverflow.com/questions/2509025/when-to-use-primitive-and-when-reference-types-in-java) that describes the difference between primitive types and reference types. – Jay Elston Jun 27 '11 at 00:47
  • @maclunian: Please do not remove the code, without it the answers can't be understood. – Paŭlo Ebermann Jun 27 '11 at 01:21

2 Answers2

3

If the problem is your update method:

Values of an enum type should be immutable. Calling the next() method on it could change the enum object (depending on its implementation), but as you are using the same object in all of your array positions, all of those would change, too.

You might want to use something like this:

board[x][y] = board[x][y].next();

with

enum Direction {

    UP, DOWN;

    public Direction next() {
        if (this == UP)
           return DOWN;
        else
           return UP;
    }
}
Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
  • or even simpler: `return this == UP ? DOWN : UP;` – Bohemian Jun 27 '11 at 00:38
  • @Bohemian I've got similar to what @Paŭlo Ebermann has, except it doesn't say public at the beginning. Would that make any difference? – maclunian Jun 27 '11 at 00:40
  • @maclunian: The problem is not the `next` method, but how you call it. You need to use the result somehow, by assigning it to the right position of the board array. – Paŭlo Ebermann Jun 27 '11 at 01:19
1

Most likely, you want to change your update code to be:

board[x][y] = board[x][y].next();

Of course, I'm assuming a lot about what's not working with your code, but it's a common mistake I've seen before.

Ben Hocking
  • 7,790
  • 5
  • 37
  • 52