• 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
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Tic-Tac-Toe Game not diplaying correctly  RSS feed

 
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: 13454
222
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()?
 
Marshal
Posts: 64483
225
  • 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.
 
Marshal
Posts: 5982
155
Chrome Eclipse IDE Java Postgres Database Ubuntu 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:
 
Normally trees don't drive trucks. Does this tiny ad have a license?
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!