Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Tic Tac Toe logic method not working  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there, I have been scratching my head for a while now as to why my checkWin() logic method does not work for a simple Tic Tac Toe game for my Java class. The method is provided and must be integrated as is. Any help is greatly appreciated.

 
Rancher
Posts: 2454
80
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Moe,

welcome to the Ranch and enjoy the stay!

You write that the method is provided and must be integrated. What exactly do you mean by that? The code can be improved quite a lot. If you like some suggestions, lt us know..

You have the JLabels cg, cn, ..., lg defined as fields. That is okay. But look at the method 'initialize()':

What is wrong with this:

1) cg is now a local variable to the method 'initialize' and is not visible outside this method. It is therefore NOT the field 'cg' that you initialize. The field 'cg' is still null, leading to a NullPointerException in the checkWin() method.

2) but even if you leave out the 'JButton', so that you refer to the field as it should be, you get a compiler error. The field is defined as a JLabel, so that you cannot initialze it with 'new JButton()';

So, work to be done! The easiest way is to change the field definitions from JLabel to JButton, and in the initialize method, just use 'cg = new JButton(""); (so without a foregoing JButton cg = ...').

I made these changes to your code, and the 'checkWin()' seems to perform as it should. Think about what should be done when a win is detected. Currently, the game simply goes on.
 
Moe Rashid
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Piet,

Thanks for the prompt reply. The community here is awesome, glad I stumbled onto it!
)
I tried your suggestions but it seems to still not work for some reason. The victory message does not show up. Kindly check my code below. What I meant by my earlier post is that the code for the checkWin() method is provided to us and should not be modified.

 
Rancher
Posts: 2862
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are too many repeated nearly identical methods that should be merged into a single method.

Do the names of the JButton variables have a meaning? Having so many names is a poor coding technique.  What if the game were in a 5x5 grid?  Would you write 25 different variables?
Normally there would be an array to hold references to the buttons being displayed. 

it seems to still not work


Does the code compile and execute without error?  If there are errors, please copy the full text of the error message and paste it here.

If there are logic errors, how are you trying to debug the code?
Add lots of print statements to the code to show the values of the variables as they are changed and used.  If you know what the values should be, the print out will show you where the logic is wrong.
 
Moe Rashid
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply Norm. The names of the JButton variables are preset by the checkWin() method which is provided to us and must not be modified. The code compiles fine, no errors shown. However, when a player wins no victory message displays as per the checkWin() method. The JButton variables seem to return a null value and thats where the issue lies. I'm not sure why they do so after the modifications suggested.
 
Norm Radder
Rancher
Posts: 2862
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The JButton variables seem to return a null value


Which one(s) have the null value?  Find them and then check to see why they do not have valid value(s).
Use a print statement that prints their values to find which one(s) are null.
 
Piet Souris
Rancher
Posts: 2454
80
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the initialize method. Did you really remove ALL JButton in 'JButton xx = new JButton()'?
 
Moe Rashid
Greenhorn
Posts: 4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, silly mistake. Thank you for your help. Works like a charm now!
 
Piet Souris
Rancher
Posts: 2454
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome!
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!