• 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 Game

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 28
1
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

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

 
Brain Pedigo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have added the for loop that saved me a lot of lines of code thanks!
 
Ranch Hand
Posts: 116
2
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
reply
    Bookmark Topic Watch Topic
  • New Topic