• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Tic Tac Toe is this good solution

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am practicing programming logic a little. And I read head first Java so I decided for start to create simple console Tic Tac Toe game, and I have created a class to represent a game. It works fine but I wonder is this solution good, can this be done better.
Three same character in line gives a point to player. Board size is configurable. I made method to check point in all directions.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vladimir,
The code is fine and as you observed it works. Most of these comments are how you would improve it to be more "industrial strengh".

1) The following two are equivalent



I prefer the second because it is a bit easier to read/recognize the pattern.

2) It is traditional to only use one character variable names for loop variables. What are "f", "s" and "t" ? In the real world, someone left to maintain the program would be left scratching his/her head.

3) I see some duplication in checkXXX. Consider passing f, s and t to a common method which does the logic for three in a row.

4) Why do you catch Exception? Are you expecting something to go wrong?

Also, you said the board size is configurable. If the board size is 5x5, do they need three in a row or five in a row?
 
Vladimir Vucetic
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jeanne ,
Thank you for answer


Also, you said the board size is configurable. If the board size is 5x5, do they need three in a row or five in a row?


No, point is only three guess in a row. Each guess is one point to player.


4) Why do you catch Exception? Are you expecting something to go wrong?


for example if mark is on location 0,0 and when it checks up, left or up-left that is out of array. I use exception to catch this situation, but if there is better solution I would like to hear.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Throwing and catching an exception is a very expensive operation (i.e., it takes a long time -- hundreds or even thousands of CPU cycles.) It's considered very bad style to allow exceptions to happen as a matter of course like this. It would be much better to add code that checked whether the indexes were in bounds before looking in the arrays. This is such bad style, in fact, and so unusual that I think that's why Jeanne didn't see at first what you were doing!
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ernest Friedman-Hill wrote: that I think that's why Jeanne didn't see at first what you were doing!


Fair enough. In hindsight it is is obvious. Not a good idea, but obvious.
 
Vladimir Vucetic
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok friends, thanks for some direction I fixed code a little bit, is it better
My point of this post is to learn to write valid quality code. I didn't know that exceptions are so heavy operation, thanks for that tip
reply
    Bookmark Topic Watch Topic
  • New Topic