0

I'm working on a checkers game in java. I'm representing my board like this:

Object[][] board = new Object[8][8];

I want to be able to place a Checker object or a null value in the 2D Object Array.

I have a method that assigns Checker objects to locations in the 2D object array.

My problem is, when I go to write my test for the method:

    public void testPopulateCheckers() {
        assertEquals("Red", game.board[0][4].color)
    }

The color method (which is a method for my Checker class) does not show up. I've tried casting it as a Checker,but it only lists the Object methods available.

4
  • @KugathasanAbimaran It is obvious, color is a field.
    – Eng.Fouad
    Nov 2, 2011 at 15:46
  • @Eng.Fouad : I know it, but it's mentioned as a method in the question! Nov 2, 2011 at 15:48
  • @KugathasanAbimaran Oh, you are right
    – Eng.Fouad
    Nov 2, 2011 at 15:49
  • color is a field. Sorry for the lack of clarity Nov 2, 2011 at 15:56

3 Answers 3

5
// This should work
public void testPopulateCheckers() {
    assertEquals("Red", ((Checker) game.board[0][4]).color)
}

However, if all you are storing are Checker objects or null, change your declaration to this so you do not have to cast.

Checker[][] board = new Checker[8][8];
2
  • My hesitation with declaring it as a 2D Checker array is that it doesn't fit the idea that a "board" object "holds" Checker objects, that the spaces certainly are not Checker objects. In my game engine I depend on certain locations of the board being nil (or null, this a a Ruby to Java translation) Nov 2, 2011 at 15:50
  • This works for my situation, thanks. I'm still a little uneasy about leaving the concept that the board should be more of a generic container, but your advice, and others who gave similar advice, did fix my current problem. Thank you. Nov 2, 2011 at 16:09
3

You should use Checker to declare your matrix if you expect to call a method like color() on it.

Checker[][] board = new Checker[ 8 ][ 8 ];

and of course make sure that you initialize all of the 64 Checkers since only the board is initialized by the line above.

1
  • I don't want the possibility of calling Checker methods on "void" spaces or non-occupied spaces. Is there a way to safeguard this? Nov 2, 2011 at 15:52
2

Define your matrix like this..

Object[][] board = new Checker[8][8];

public void testPopulateCheckers() {
    assertEquals("Red", ((Checker) game.board[0][4]).color)
}

You need to cast it.

EDIT : method added!

5
  • This wouldn't help, since the type of the variable is still Object[][]. Nov 2, 2011 at 15:48
  • @Joeri Hendrickx : He tries to cast it, so, placed like that. Nov 2, 2011 at 15:51
  • Tried this and Eclipse still only lists the Object methods available for game.board[0][4], not recognizing it as a Checker object. Nov 2, 2011 at 15:58
  • @Joeri Hendrickx will you please remove the down vote..? Since your point isn't valid & required. Nov 2, 2011 at 16:14
  • With the cast, it could work. But still, it makes a lot more sense to declare it as Checker[][]. Answer below is better. Nov 3, 2011 at 8:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.