• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

X and O game

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does it not do properly?
 
James Mousseau
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
James Mousseau
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!

EDIT:

It works like a charm!!!
[ June 06, 2008: Message edited by: James Mousseau ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic