Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

2 Player Tic Tac Toe GUI Issue  RSS feed

 
Rebecca Berry
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.

 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • 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)

 
Campbell Ritchie
Marshal
Posts: 55682
162
  • 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.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database 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?
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!