• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Tic Tac Toe Game

 
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.

 
Rancher
Posts: 43011
76
  • 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.
 
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!
 
Ranch Hand
Posts: 116
2
Eclipse IDE PHP Java
  • 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,
 
please buy my thing and then I'll have more money:
create, convert, edit or print DOC and DOCX in Java
https://products.aspose.com/words/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!