• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Tic-Tac-Toe Game not diplaying correctly

 
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:
 
Sheriff
Posts: 13570
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • 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()?
 
Marshal
Posts: 65113
247
  • 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.
 
Sheriff
Posts: 6128
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • 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!