• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Tic Tac Toe Code

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello guys,

I tried to write a Tic Tac toe game today and I will hopefully learn to set up a GUI for it soon.
I did it all in one class - assinged numbers from one to nine and im letting the user pass the numbers the players want to occupy into Array Lists.
If one of the ArrayLists assinged to a player (either cross or circle) containts certian numbers (meaning if the player occupies certian squares 3 in a row) one player wins.

I do have a while loop for the progress of the game that does work, but it just doesnt feel practical to jam the definition for my variable "winCheck" 5 times there.



Without those definitions the second part with moveCirclePlayer(*) method is called one time too much.
Depending on how the game plays out, the "winCheck" definitions are required on different places.

Do you have an idea on how I can structure it so that the moveCirclePlayer(*) method doesnt execute again?
 
Ranch Hand
Posts: 185
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can check winCheck again in an if statement, before executing parts which you don't want to execute if that check is true.
 
Thomas Anderz
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But isnt that exactly what im doing already? With this
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Program code that reads like a story is more readable. Names like winCheck and isWinConditionMet, while they may sound "technical", are actually less readable.

Compare this:

The names used here make the code more conversational sounding, like someone speaking in plain English rather than "tech-speak"
 
Junilu Lacar
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem you sense with the repeated use of the winCheck variable is caused mostly by not abstracting ideas enough and reducing the logic down to more cohesive chunks of ideas.

For example, there are only two conditions for ending the game: when someone has won with the most recent move or when the most recent move results in a draw at which point you are assured that the number of moves was 9. You are getting caught up in the details (moves == 9) instead of focusing on the idea (the game has ended in a draw)

Clarify your intent and try to reduce the logic down to the fewest possible conditions to check.
 
Junilu Lacar
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note also that it's poor form to compare a boolean variable with true or false.

Comparing a boolean variable to true or false is redundant and unnecessary. If you chose a good name for the variable, the code will read more naturally, as seen in the above example.

Also, it's easy to accidentally type = instead of ==, which usually results in a logical bug even though the compiler will accept the code.
 
Marshal
Posts: 80214
423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thomas Anderz wrote:. . . I did it all in one class  . . .

That sounds a very bad idea. Nobody plays a game as one class; you would want different classes for the players nd the board. You would also probbly want a class for a tournament (multiple games). Junilu called those classes logical chunks of ideas.
 
This cake looks terrible, but it tastes great! Now take a bite out of this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic