• Post Reply Bookmark Topic Watch Topic
  • New Topic

Tic-Tac-Toe Game not diplaying correctly  RSS feed

 
Rebecca Berry
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all!  So I have written a basic tic-tac-toe game and all seems to be well except the board display.  It should display as follows:
1 2 3
4 5 6
7 8 9
But is coming out 123456789.
For the life of me I can not figure out what I did wrong. Any hints or pointers would be appreciated greatly. Thanks!
Here is my code:

And here is the outcome when I run it:
 
Junilu Lacar
Sheriff
Posts: 11164
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look carefully at your boardDisplay method and understand what it's doing. For example, when would it start another line? Do you have code that does that? And why are you printing an empty string after the array element? An empty string does nothing to affect your display, so what's the point of including that in the expression passed to println()?
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest you don't want those switch statements, which contain much repeated code. You only need to write one lot of code to find the square. Also using one lot of code is much less error‑prone. You use i % 3 to find the column and i / 3 to find the row, but that will only work if the top left square is No 0. If you use 1 for top left, you will have to use (i − 1) % 3 for column and (i − 1) / 3 for row instead. If you miss out the () it will go horribly wrong.

Why have you made all your methods static?
 
Rebecca Berry
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been referencing the class book for most of this code.  So that is why the static methods. It is just what the book shows.  I have never used Java before and this is the 5th week of my course. So everything is still pretty new to me. Thanks for the help!  It is appreciated.
 
Knute Snortum
Sheriff
Posts: 4078
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you get your code to work?  If so, repost your code in a reply.
 
Rebecca Berry
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did get it to work the way I needed it too.  Thanks again for the help.  Here is the final code:
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!