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?
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.
The best ideas are the crazy ones. If you have a crazy idea and it works, it's really valuable.—Kent Beck
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.