File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes X and O game Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "X and O game" Watch "X and O game" New topic

X and O game

James Mousseau

Joined: May 28, 2008
Posts: 22
The logic seems correct to me, but for some reason the program does not want to funtion properly, could you take a look at the program and give me some input, and I have a few variables not used in the program, even though they are defined because this is the part I need done in order to use those variables.

I would really appreciate the help on this one, its been bugging me for a few days now.
[ June 05, 2008: Message edited by: James Mousseau ]

When in doubt, get the hammer!
Rob Spoor

Joined: Oct 27, 2005
Posts: 20274

What does it not do properly?

How To Ask Questions How To Answer Questions
James Mousseau

Joined: May 28, 2008
Posts: 22
Through all the if statements, it should output an X first, then an O, but for some reason, its ONLY outputting X's through all the buttons (1-9), I added a system.out.print in there for the p1turn and p2turn, and p1turn seems to always add 2 to it, and stay at the value 3, but p2turn stays at 2, as if the p1turn = p1turn + 2, and the p2turn = p2turn + 2 dont seem to count, only counts once and to p1turn.

The program pretty much runs as its supposed to until it gets to:

I was just wondering if there is anything there I cant seem to notice, like brackets, where the "counter" (p1turn, p2turn) is placed.

And Id like to thank you guys for helping me out, im learning more and more everyday
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

In your program p1Turn, p2Turn, and turn are local variables declared in the actionPerformed() method. That means each time actionPerformed is called -- i.e., each time a button is pressed -- these variables are all recreated from scratch, and initialized to 1, 2, and 0, respectively. If you want those variables to persist between calls to this method -- and you do, since they represent the state of the game -- then they should be declared at class level, outside of any method. Then they are called instance variables, and they're initialized just once when an instance of your class is created.

Make sense?

[Jess in Action][AskingGoodQuestions]
James Mousseau

Joined: May 28, 2008
Posts: 22
kind of, I now know that their placement is off, and that makes sence since it seems to repeat the step of adding 2 to p1turn, and it always gets resetted and repeating step 1, Ill try that out...

I think you saved me hours of struggle

Thanks a bunch!


It works like a charm!!!
[ June 06, 2008: Message edited by: James Mousseau ]
I agree. Here's the link:
subject: X and O game
It's not a secret anymore!