• Post Reply Bookmark Topic Watch Topic
  • New Topic

Tic Tac Toe Game  RSS feed

 
Brain Pedigo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to JAVA and I am trying to make a simple Tic Tac Toe game to learn the language better. The below code works! It uses a Jframe and the computer isn't too easy! However, the code is getting long and I think I might be missing some steps on how to make it shorter and more concise. I was thinking about creating multiple class and importing them but I am not sure. If you have any ideas on how to make it more compact please let me know.

 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

There's a nine-fold code duplication in the actionPerformed method that should be easy to get rid of. At a quick glance, the only thing that differs between those code blocks is the number 0...8 in 3 places - instead, you can use a loop from 0...8.
 
Anuj Sharma R
Greenhorn
Posts: 28
1
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Brain.

Have a look at the below threads where this has been discussed earlier at CodeRanch. Hopefully the discussion in these threads will guide you to a solution.

http://www.coderanch.com/t/620503/java/java/Tic-tac-toe-Design
http://www.coderanch.com/t/539935/Game-Development/java/TicTacToe-game
http://www.coderanch.com/t/609934/java/java/tic-tac-toe-nought-crosses

 
Brain Pedigo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have added the for loop that saved me a lot of lines of code thanks!
 
Scott Winterbourne
Ranch Hand
Posts: 116
2
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent, I'm glad you were able to implement the for loop.

Would you mind posting your updated code? Others that have similar questions might find it very useful to see the before and after.

Thanks,
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!