Sign in to follow this  
Stuart_Mastrgamr

help making tictactoe in Java

Recommended Posts

[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.[/size][/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.[/size][/font]

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

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

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

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

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

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

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

Share this post


Link to post
Share on other sites
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][color="#666600"][[/color][color="#000000"]space[/color][color="#666600"]][/color] [color="#666600"]=[/color][color="#000000"] player[/color][color="#666600"].[/color][color="#000000"]getLetter[/color][color="#666600"]();[/color][/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.

Share this post


Link to post
Share on other sites
[quote name='frob' timestamp='1317239948' post='4866924']
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][color="#666600"][[/color][color="#000000"]space[/color][color="#666600"]][/color] [color="#666600"]=[/color][color="#000000"] player[/color][color="#666600"].[/color][color="#000000"]getLetter[/color][color="#666600"]();[/color][/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.
[/quote]

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.

[u] What I dont understand is why my array returns a NullPointer, when it's not null (from the way I understand it)...[/u]
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

[b]From how I understand my code:[/b]
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 ...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote name='rip-off' timestamp='1317240896' post='4866935']
The problem is that main.main.Game.Player.player is null.
[/quote]

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

[quote name='rip-off' timestamp='1317240896' post='4866935']
Player should not extend Game. Such a relationship does make sense in the real world - a player is not a game.
[/quote]
makes total sense.

[quote name='rip-off' timestamp='1317240896' post='4866935']
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.
[/quote]

So basically I'd have to rewrite half my project.... guess I did it to myself

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this