Tuesday, March 9, 2010

Idiotic Guessing Game in ActionScript 3.0

I've spent time I should have spent contemplating the beauty of the human form or drawing trees creating a computer guessing game. It's sure to thrill. Here's a link to the full code in a .txt file. If you really like to rock to funky beats, you can download the .fla file and the .as file right here. Anyhow, let me know if you manage to break it.

Here's the main code for getting the guessing down. Initially, I came up with many hare-brained formulas that didn't work. Finally I resorted to digging around on the net, saw a post on limits and the light went on at last. Here's the code for the "no" button. The minLimit variable is initialized as 1 and the maxLimit variable is initialized at 101. As the computer guesses, its guess becomes either the new max or the new min limit. The computerPlay function just runs the animation and keeps track of the number of guesses.

function onNoButtonClick(event:MouseEvent):void
{
output_txt.text = interestingMessage;
if (computerGuess < userNumber) { minLimit = computerGuess; computerGuess = computerGuess + (maxLimit-minLimit)/2; computerPlay(); } else if (computerGuess > userNumber)
{
maxLimit = computerGuess;
computerGuess = computerGuess - (maxLimit-minLimit)/2;
computerPlay();
}
else
{
output_txt.text = lieMessage;
}
}

2 comments:

Audrey Sayle said...

its gets it in six each time. It always starts at 50... but how does it know whether to ascend or descend!? I'll tell ya how! Its a CHEAT, thats how!!!

Miles Inada said...

I thought about just making it cheat. That would be kind of awesome, actually.