This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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: 43027
76
  • 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,
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic