File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes TicTacToe! Stop user from changing input Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "TicTacToe! Stop user from changing input" Watch "TicTacToe! Stop user from changing input" New topic
Author

TicTacToe! Stop user from changing input

Castulo Ruiz
Greenhorn

Joined: May 08, 2009
Posts: 12
Basically I have this code almost done,but i'm not sure how to prevent the user from cheating. Which is from changing the X's and O's I need a little guidance.


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
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?
Castulo Ruiz
Greenhorn

Joined: May 08, 2009
Posts: 12
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
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.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
Two other useful suggestions which I hadn't thought about
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
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
W Pearce
Ranch Hand

Joined: Jan 06, 2009
Posts: 32
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.


SCJP 6, SCWCD 5
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Would setting the buttons to unenabled (setEnabled(false)) help at all?

it would work, but the gui probably wouldn't have a great visual appeal, without more code,
e.g. changing the disabled text color

the OP already has his answer - checking if the button's text is "?"
if it is-proceed, if not-do nothing

although a tic-tac-toe with a bunch of '?' would look silly to me - I'd prefer blank (empty) buttons,
the the check would be if the button's text was empty .equals("")
 
 
subject: TicTacToe! Stop user from changing input