• 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

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: 3785
34
  • 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: 69418
276
  • 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: 7108
184
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?
 
Where does a nanny get ground to air missles? Protect this tiny ad:
Try Free Java/.NET Libraries for Word Excel PowerPoint and PDF
htttp://www.e-iceblue.com/free-apis.html
    Bookmark Topic Watch Topic
  • New Topic