Jump to content
  • Advertisement
Sign in to follow this  
Stuart_Mastrgamr

help making tictactoe in Java

This topic is 2455 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

Share this post


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

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="#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 ...

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

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!