Where do you think players might cheat? Where is there a vulnerability? Please tell us the details, and when you have told us the problem, have you any ideas what might sort it out?
Joined: May 08, 2009
For example let say the user clicks on the button and gets an X the user can change it to and O and the user can keep on doing this. I hope that was clear.
I was thinking of something like this:
Or maybe use a boolean array, but i'm not sure how to implement the boolean arrary into my code.
Joined: Oct 13, 2005
That won't work; it sets the button to X regardless. What you want is to set the buttons enabled, or otherwise, maybe using a suitable method from the JButton API. Put a call to whichever method inside the actionPerformed method, then it becomes impossible to click the same button twice.
This sort of thing is usually discussed on the Swing forum, so I shall move you there.
One simple solution: only allow the button to be set if its text equals "?":
Having said that, note that this is a somewhat fragile solution in that it breaks if you ever decide to change the default text of the buttons. If this were my project, I'd use it to practice creating a MVC program, and just have my model set in such a way that it won't change the state of the grid item if it has already been set.
Are you having the user play tic-tac-toe against the computer?
I didn't look too closely at your code, but I would probably use some kind of Boolean variable to determine whose turn it is. As soon as one side makes a move, the value of the variable changes. Then, whatever method you have that responds to the user's clicks, have that method check the value of the Boolean variable, and if it is not the user's turn then do nothing in response to the click.
Joined: Oct 13, 2005
Two other useful suggestions which I hadn't thought about
Joined: May 13, 2009
Even if you weren't playing against the computer, you could use a Boolean in the same way so that an X could only be followed by an O
Would setting the buttons to unenabled (setEnabled(false)) help at all? You could still see the X or O on the button but it would no longer respond to being clicked. You can still get values from the buttons, though. Not sure if this will help, I just gave the code a quick once over.