Win a copy of Programmers Guide to Apache Thrift this week in the Open Source 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
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

2 Player Tic Tac Toe GUI Issue  RSS feed

 
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: 3353
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: 64473
225
  • 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.
 
Marshal
Posts: 5980
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • 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?
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!