• Post Reply Bookmark Topic Watch Topic
  • New Topic

Simple TicTacToe game  RSS feed

 
Adin Cebic
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there,
I am trying to make a very simple TicTacToe game.
I created a grid layout and put 9 buttons into that layout.
I would like to make those 9 button to show X when first clicked and O when second clicked.



From what I know, I should use the action listeners and respond to the mouse click on the button
I do not need this game to determine the winner or terminate the game when someone wins, just need to draw X when clicked first, and when clicked on the another button to show the O.

 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you should add an ActionListener to each button.
the initial label for each button should be a sincle blank (' ') instead of a numeral. Then in the listener's actionPerformed() method, set the text of that button to an 'X' or an 'O' as appropriate. Note that you may have to worry about the size of the buttons changing, especially if you do not use a fixed font.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you trying to draw a GUI? You shoul‍d write the logic first, in separate classes, and add the GUI later.
 
Adin Cebic
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Why are you trying to draw a GUI? You shoul‍d write the logic first, in separate classes, and add the GUI later.



Here is what I have done



Problem is that I can not say
button1.setText

for some reason it does not let me do it like that. I am not sure why?
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are getting an error you should post it here and tell us which line in your posted code it is occurring on.
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are declaring button1 in line 29 inside of a method, so it is a local variable.  Local variables cannot be accessed outside of the block (in this case, method) that they're declared in.  To solve this, declare button1 outside of any method like you've done with WIDTH and HEIGHT, but not static or final.
 
Adin Cebic
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:You are declaring button1 in line 29 inside of a method, so it is a local variable.  Local variables cannot be accessed outside of the block (in this case, method) that they're declared in.  To solve this, declare button1 outside of any method like you've done with WIDTH and HEIGHT, but not static or final.


Ok I did it, thank you!
Now there is another problem



See? When I click it first time, it shows the X as I wanted, but when I click  it next time, it does not show O.
Maybe I have the wrong logic?
When I click on another button, I want to display "O".
How do I make it work?

Thank you all for your help!
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use == false or == true, which are both poor style and error‑prone.
Not
if (b == false) ...
but
if (!b) ...
Not
if (b == true) ...
but
if (b) ...

Why are you checking for !check, then two lines later trying to find !check (the same thing) inside an else? How is that going to work? I think you shou‍ld actually use the ?: operator for that sort of thing.
 
Adin Cebic
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Don't use == false or == true, which are both poor style and error‑prone.
Not
if (b == false) ...
but
if (!b) ...
Not
if (b == true) ...
but
if (b) ...

Why are you checking for !check, then two lines later trying to find !check (the same thing) inside an else? How is that going to work? I think you shou‍ld actually use the ?: operator for that sort of thing.


The thing is that I am a beginer and really trying to solve this.
If you can, please provide me with more concreete answer as this one does not explain a lot.

Thank you!
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adin Cebic wrote:


But where are you changing the value of 'check'?

Using ternary operators:
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your actionPerformed() method should not refer to button1. You have added the same ActionListener to each of the buttons, so  that method will be called when you click on any of the buttons, not just button1.
The event passed to the method can be used to get the source of the click:


Note also that each button has three states: unclicked, clicked to add an 'X', and clicked to add an 'O'. You should not allow the state to change unless its current state is unchecked (i.e., if it is currently an 'X', you cannot click on it to change it to an 'O' or to change it back to a blank (except to start a new game, when all are reset to show a blank).
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adin Cebic wrote:. . . If you can, please provide me with more concreete answer as this one does not explain a lot.

Thank you!
Using ctrl‑F find everywhere you wrote == true and delete it. Using ctrl‑F find everywhere you wrote == false and delete it and add the not operator ! before the name of the boolean. If it is an expression you will have to add () too.
You are setting the value if not‑check and later using else if not check. Please work out how often the else if block will ever run.
Carey Brown has provided a nice example of the use of ?: as I suggested.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:. . . The event passed to the method can be used to get the source of the click: . . .
I still think that is bad design; you end up with a big block of else ifs which contains repeated code and cannot easily be extended; try changing that so you use a 4×4 board. I think every button shou‍ld have a different listener object.

This shows what bad practice it is to work from the display to the game. It would have been much better to start with the game without a GUI and then add the GUI later.
 
Adin Cebic
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Fred Kleinschmidt wrote:. . . The event passed to the method can be used to get the source of the click: . . .
I still think that is bad design; you end up with a big block of else ifs which contains repeated code and cannot easily be extended; try changing that so you use a 4×4 board. I think every button shou‍ld have a different listener object.

This shows what bad practice it is to work from the display to the game. It would have been much better to start with the game without a GUI and then add the GUI later.


Ok, I finaly got this working. But I am wondering how could I make an array of those buttons so I do not have to make 9 objects and 9 actionListeners.
The question is: how to create an array of those buttons like a matrix?

here is my code:
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Adin Cebic
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all,
thank you all for your help, it's been incredibly helpful.

Now, if someone could explain me, how can I go through the matrix of buttons and check for the winner
I need to check horisontally, vertically and diagonaly.
I would not like to create another array and update it each time the button gets clicked. I think that there should be some more efficient way to go through the matrix and check who won.

 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adin Cebic wrote:
Now, if someone could explain me, how can I go through the matrix of buttons and check for the winner
I need to check horisontally, vertically and diagonaly.

As Campbell warned you very early on, you really should have started with that logic before plunging right into creating a GUI for your program. The logic for checking a winner in this game really has nothing to do with buttons on the GUI. Rather, it should be the other way around: your GUI should just be a reflection of the internal state of the game object.

This is not the first time TicTacToe has been discussed in these forums and this thread: https://coderanch.com/t/671943/java/ticTacToe-learn-object-oriented-programming goes into detail on how you can write code to check for a winner.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adin Cebic wrote:. . . how can I go through the matrix of buttons and check for the winner . . .
This is what happens if you don't think and plan from the beginning. You don't have to go through the matrix of buttons, nor the matrix of O/X at all.
I shall let you think what you really have to do. You ought to have had that information before trying any coding. And as I said earlier, it ought to be in a Game class not the GUI.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!