help making tictactoe in Java

Started by
3 comments, last by Stuart_Mastrgamr 12 years, 6 months ago
[font="Arial,"][size="4"]Im jumping back into java from a 3 month break and making a tictactoe game to reinforce some stuff i learned from an Intro to Java Class.[/font]

[font="Arial,"][size="4"]I created an array for the spaces of the tic tac toe (char)board, initialized the array indices to '-', have the player pick a space from 1-9, if its occupied return a false boolean and choose again, when it returns true I change the value of the space in the array.[/font]

[size=2]Can someone please take a look at my code uploaded at Google Code: http://code.google.com/p/java-console-tictactoe/source/browse/#svn%2Ftrunk%2Ftictactoe%2Fsrc[size=2]


[font="Arial,"][size="4"]I get the Error:[/font]



[font="Arial,"][size="4"] Exception in thread "main" java.lang.NullPointerException[/font]



[font="Arial,"][size="4"] at Game.setSpace(Game.java:47)[/font]



[font="Arial,"][size="4"] at Player.turn(Player.java:19)[/font]



[font="Arial,"][size="4"] at Game.run(Game.java:32)[/font]



[font="Arial,"][size="4"] at main.main(main.java:6)[/font]

[size="4"]-- [size="4"]Stuart, [size="2"]Currently: Messing with LibGDX (Java)
aspiring video game programmer. (follow @mastrgamr)
Programming on and off since 2008 in C++, and C# (XNA).
Attending college as a Computer Science major.
Advertisement
Without wanting to do your homework or learning experiment for you...

Stop it on the debugger on that line.

[font="Monaco,"][color="#000000"]board[color="#666600"][[color="#000000"]space[color="#666600"]] [color="#666600"]=[color="#000000"] player[color="#666600"].[color="#000000"]getLetter[color="#666600"]();[/font]
[font="Monaco,"] [/font]
Inspect the values and observe the null.

You can add some assertions before use:

assert player!=null;
assert board!=null;
assert space>=0;
assert space<9; // Fix your magic numbers. Use a named constant.

That should cause your debugger to stop at the right place. You can then use the callstack / backtrace to figure out why your expectations are incorrect. It should be pretty obvious once you look at the stack.

Good code will have lots of assertions in it to verify assumptions. Even better code will detect those conditions when they happen in production and give a safe and predictable failure behavior in addition to the assert.

Without wanting to do your homework or learning experiment for you...

Stop it on the debugger on that line.

[font="Monaco,"][color="#000000"]board[color="#666600"][[color="#000000"]space[color="#666600"]] [color="#666600"]=[color="#000000"] player[color="#666600"].[color="#000000"]getLetter[color="#666600"]();[/font]
[font="Monaco,"] [/font]

Assuming a current version of Java, add some assertions before use:

assert player!=null;
assert board!=null;
assert space>0;
assert space<9; // Fix your magic numbers. Use a named constant.

That should cause your debugger to stop at the right place. You can then use the callstack / backtrace to figure out why your expectations are incorrect. It should be pretty obvious once you look at the stack.


I understand what you're saying, I assure you it's not homework, or any school work (you may take that statement with a grain of salt but I'm serious)
I'm not looking for an answer.

What I dont understand is why my array returns a NullPointer, when it's not null (from the way I understand it)...
I can run my setSpace class with no problem in the Game class, but when I run it in Player, BOOM error. Player should have automatic access to all of Game's statements, I've made the private variables accessible through the get/sets

From how I understand my code:
player is != null, it extends the Game class
board is != to null, I instatiated and declared the values of all chars in the board array before the game even begins
space is > 0
space is <9 in my debugging process, where it asks the user for input.

I've also put 'debugging' statements before each step in the game so far. I'll try using your suggestion though ...
[size="4"]-- [size="4"]Stuart, [size="2"]Currently: Messing with LibGDX (Java)
aspiring video game programmer. (follow @mastrgamr)
Programming on and off since 2008 in C++, and C# (XNA).
Attending college as a Computer Science major.
The problem is that main.main.Game.Player.player is null. Remember, you have two "Game" instances, the one in main(), and the one that comprises part of the player. Player should not extend Game. Such a relationship does make sense in the real world - a player is not a game.

I think a more typical model is to have a Board class, and a Player enumeration/class. The board would not handle I/O, the calling classes would do that.

The problem is that main.main.Game.Player.player is null.


Yes I found this out just now, heh, thanks for the explaination


Player should not extend Game. Such a relationship does make sense in the real world - a player is not a game.

makes total sense.


I think a more typical model is to have a Board class, and a Player enumeration/class. The board would not handle I/O, the calling classes would do that.


So basically I'd have to rewrite half my project.... guess I did it to myself
[size="4"]-- [size="4"]Stuart, [size="2"]Currently: Messing with LibGDX (Java)
aspiring video game programmer. (follow @mastrgamr)
Programming on and off since 2008 in C++, and C# (XNA).
Attending college as a Computer Science major.

This topic is closed to new replies.

Advertisement