-1

in my program, the user sets a range of numbers for the computer to guess. The user then has to guess which number the computer chose with a limit of guesses starting at 5. There are several problems in my functioning program in which I do not understand how to fix. These errors include:

-The number of guesses left always remains at 0. It won't start at 5 and decrease by 1 each time I click the btnCheck button.

-Whenever I click the btnCheck button for a new guessing number, the statement if you've guessed too high or too low remains the same.

-When I press btnNewGame, the values I insert in my low value and my high value text inputs will not be cleared.

-How can the computer generate a random whole number based on what I set as the number range?

Revising my code down below will be much appreciated.

// This line makes the button, btnCheckGuess wait for a mouse click
// When the button is clicked, the checkGuess function is called
btnCheckGuess.addEventListener(MouseEvent.CLICK, checkGuess);

// This line makes the button, btnNewGame wait for a mouse click
// When the button is clicked, the newGame function is called
btnNewGame.addEventListener(MouseEvent.CLICK, newGame);

// Declare Global Variables
var computerGuess:String;   // the computer's guess
var Statement:String;   // Statement based on your outcome


// This is the checkGuess function
// e:MouseEvent is the click event experienced by the button
// void indicates that the function does not return a value
function checkGuess(e:MouseEvent):void
{  
    var LowValue:Number;    // the user's low value
    var HighValue:Number;    // the user's high value
    var UserGuess:Number;     // the user's guess
    var CorrectGuess:int;       // the correct number
    var FirstGuess:String; //the user's guess

    // get the user's range and guess
    LowValue = Number(txtinLow.text);
    HighValue = Number(txtinHigh.text);
    UserGuess = Number(txtinGuess.text);


    // determine the number of the user
    GuessesLeft = checkCorrectGuess(FirstGuess);
    lblNumber.text = GuessesLeft.toString();
    lblStatement.text = "You have guessed " + Statement.toString() + "\r";

  }

// This is function checkColoursCorrect
// g1– the user's guess
function checkCorrectGuess(g1:String):int
{
    var GuessesLeft:int = 5; // How many guesses are left

    if (g1 != computerGuess)
    {
       GuessesLeft - 1;
    }

    else
    {
        GuessesLeft = 0;
    }

    return GuessesLeft;
}


// This is the newGame function
// e:MouseEvent is the click event experienced by the button
// void indicates that the function does not return a value
function newGame(e:MouseEvent):void
{
    var Guess1:int;     // computer's guess in numbers
    var UserGuess1:int;     // user's guess in numbers
    Guess1 = randomWholeNumber(100,1); //It is not (100,1). How do I change this to the range the user put?
    UserGuess1 = randomWholeNumber(100,1); //It is not (100,1). How do I change this to the range the user put?


if (Guess1 > UserGuess1) {
    Statement = "TOO HIGH";
} else if (Guess1 < UserGuess1) {
    Statement = "TOO LOW";
} else if (Guess1 == UserGuess1) {
    Statement = "CORRECTLY";
} 

txtinGuess.text = "";
lblStatement.text = "";
}
// This is function randomWholeNumber
// highNumber – the maximum value desired
// lowNumber – the minimum value desired
// returns – a random whole number from highNumber to lowNumber inclusive
function randomWholeNumber(highNumber:int,lowNumber:int):int //How do I make a whole random number based on the range the user made?
{
    return Math.floor((highNumber - lowNumber + 1) * Math.random() + lowNumber);
}

1 Answers1

2

To answer your questions...

  1. You've declared GuessesLeft inside checkCorrectGuess() which means its a local variable that's being redefined every time you call the function. Futhermore, because you're passing in var FirstGuess:String; (an uninitialized, non-referenced string variable), (g1 != computerGuess) is returning false, and the answer is always 0.
  2. GuessesLeft - 1; is not saving the result back to the variable. You need to use an assignment operator such as GuessesLeft = GuessesLeft - 1 or simply type GuessesLeft-- if all you want is to decrement. You could also write GuessesLeft -= 1 which subtracts the right from the left, and assigns the value to the variable on the left. See AS3 Operators...
  3. You've already assigned values to these TextFields earlier; simply repeat the process inside of newGame() with a txtinLow.text = "" (same with high)
  4. Use your variables. You defined them earlier in checkGuess() as UserGuess, LowValue, and HighValue

Be mindful that you only need to split out functionality into separate functions if that piece of code is likely to be called elsewhere. Otherwise, every function on the stack incurs more memory and performance hits. checkCorrectGuess() falls into that category and is therefore unnecessary.

Also, you are printing your feedback to the user in the newGame() function instead of checkGuess(). It seemed like an oversight.

btnCheckGuess.addEventListener(MouseEvent.CLICK, checkGuess);
btnNewGame.addEventListener(MouseEvent.CLICK, newGame);

// Global Variables
var computerGuess:int;
var remainingGuesses:int;

newGame();

function newGame(e:MouseEvent):void {
    // Reset our guess limit
    remainingGuesses = 5;

    // Generate a new number
    computerGuess = random(int(txtinLow.text), int(txtinHigh.text));

    // Reset our readouts.
    txtinGuess.text = "";
    lblStatement.text = "";
}

function checkGuess(e:MouseEvent):void {
    var guess:int = int(txtinGuess.text);
    var msg:String;

    if (guess == computerGuess) { // Win
        remainingGuesses = 0; // Zero our count
        msg = "CORRECT";
    } else { // Missed
        remainingGuesses--; // Decrement our count

        if (guess > computerGuess) {
            msg = "TOO HIGH";
        } else if (guess < computerGuess) {
            msg = "TOO LOW";
        }
    }

    lblNumber.text = remainingGuesses.toString();
    lblStatement.text = "You have guessed " + msg;
}

function random(low:int, high:int):int {
    return Math.floor((high - low + 1) * Math.random() + low);
}
Atriace
  • 2,572
  • 1
  • 14
  • 27