this is what I clobbered together for the O'Reilly Java 2 Course, final assignment. There may well be several issues still with this code, my main grief at the moment is: The mouseReleased does not react (it did in the assignment I lifted it from and made only minor changes). No matter where I click on the grid, nothing happens.
Could someone please give me a hint what to do? Thanks in advance, and this is the code:
I have a hunch the mouseAdapter in TicTacBox is not correct, but it's just a hunch.
You're not using it as a MouseListener. Line 121 of TicTacToe would apparently add that, but it's commented out.
Be aware that both ClickableBox and TicTacBox define the method mouseReleased, without the subclasss referencing the method in the superclass, so ClickableBox.mouseReleased will never be executed.
Thanks Ulf, I thought it would be something simple... ARGH! OK; I corrected this, now I have the problem that the counter is not passed on and every click I make generates an "Ö". Oh well... Back to coding. Again, hints are appreciated.
I think in line 232 you want "} else if (...) {". As it is, the first "if" increments the counter from 1 to 2, and then the second "if" kicks in immediately, and sets it back to 1.
OK, I edited the TicTacBox Class and the TicTacToe class as such (the clickable box is still the same as above):
If you try it, you see it sort of works, but I have to click a box twice for the O to appear and there will always be an X first. I have been unable to correct this. Any hints? (I know there are a few more flaws, but this is my main concern at the moment).
Lines 53-58 of TicTacBox seem to implement exactly the behavior you describe (click one -> X, click again -> O). So if something else should happen, that looks like a starting point.
OK, I solved all the other issues (like the faulty gameLogic() method), but all attempts to get the program to draw Xs and Os alternately into different boxes have failed (I also experimented with an instantiated TicTacToe in the TicTacBox Class and a few other things.
So I am actually asking for some concrete code now, since I am really stuck. Oh the shame!
You have logic to alternate drawing X and O so if it isn't alternating correctly, the counter value must not be changing correctly. I suggest you add some print statements to the code that sets the counter value to see how it is being changed.