Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

2 Player Tic Tac Toe GUI Issue

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello again!  I am having an issue with my 2 Player Tic Tac Toe Game. When I run it the board comes up like it should, however, nothing happens when I click on a space to make a move. When I click on the space it should mark it with a 'X' and then state it is O's turn and so on. What am I missing?  Thanks as always for the pointers.

 
Rancher
Posts: 3409
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you debugging the code?  You need to see what is in s on line 113 to be sure the logic that follows is right.  Add a print statement to see.
 
Rebecca Berry
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I went ahead and put a System.out.println(s); in. Now when I run the program this is what I get when I click on the spaces. (I clicked top row to bottom row, left to right)

 
Marshal
Posts: 64998
246
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using addActionListener(this)? As you will see if you search my posts for that phrase in the Swing forum, I believe that is an abomination against the principles of object‑oriented programming; you should add a separate action listener to each button. Also: Check very carefully which listener you are setting the action command on. It is easy to buy books which teach bad programming and unfortunately beginners don't know they are being taught badly.
You have some confusing features. Your indentation is still inconsistent, so it is difficult to see that line 60 is the beginning of a new method. You shou‍ld leave one empty line between successive methods, so you can see where the method starts. That method uses a parameter (board) which shadows a field of the same class. That can cause confusion, too.
Can you run the game without the GJI? It is usually bad design to put anything inside a GUI component class except displays. Classes like JFrame, JButton and JPanel exist to make things visible. If you do things to their objects, they call methods of other objects (listeners) and those listeners actuate the logic of the actual app. The app shou‍ld be in separate classes. So you can develop a command‑line/terminal game app which might only print…but that app is sufficient to build a GUI over.
 
Sheriff
Posts: 6107
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rebecca Berry wrote:I went ahead and put a System.out.println(s); in. Now when I run the program this is what I get when I click on the spaces. (I clicked top row to bottom row, left to right)


So is s.length() ever == 3?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!