actionscript 3 - Guessing Number Game Program not Functioning Correctly -


in program, user sets range of numbers computer guess. user has guess number computer chose limit of guesses starting @ 5. there several problems in functioning program in not understand how fix. these errors include:

-the number of guesses left remains @ 0. won't start @ 5 , decrease 1 each time click btncheck button.

-whenever click btncheck button new guessing number, statement if you've guessed high or low remains same.

-when press btnnewgame, values insert in low value , high value text inputs not cleared.

-how can computer generate random whole number based on set number range?

revising code down below appreciated.

// line makes button, btncheckguess wait mouse click // when button clicked, checkguess function called btncheckguess.addeventlistener(mouseevent.click, checkguess);  // line makes button, btnnewgame wait mouse click // when button clicked, newgame function called btnnewgame.addeventlistener(mouseevent.click, newgame);  // declare global variables var computerguess:string;   // computer's guess var statement:string;   // statement based on outcome   // checkguess function // e:mouseevent click event experienced button // void indicates function not return value function checkguess(e:mouseevent):void {       var lowvalue:number;    // user's low value     var highvalue:number;    // user's high value     var userguess:number;     // user's guess     var correctguess:int;       // correct number     var firstguess:string; //the user's guess      // user's range , guess     lowvalue = number(txtinlow.text);     highvalue = number(txtinhigh.text);     userguess = number(txtinguess.text);       // determine number of user     guessesleft = checkcorrectguess(firstguess);     lblnumber.text = guessesleft.tostring();     lblstatement.text = "you have guessed " + statement.tostring() + "\r";    }  // function checkcolourscorrect // g1– user's guess function checkcorrectguess(g1:string):int {     var guessesleft:int = 5; // how many guesses left      if (g1 != computerguess)     {        guessesleft - 1;     }      else     {         guessesleft = 0;     }      return guessesleft; }   // newgame function // e:mouseevent click event experienced button // void indicates function not return 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 not (100,1). how change range user put?     userguess1 = randomwholenumber(100,1); //it not (100,1). how change range 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 = ""; } // function randomwholenumber // highnumber – maximum value desired // lownumber – minimum value desired // returns – random whole number highnumber lownumber inclusive function randomwholenumber(highnumber:int,lownumber:int):int //how make whole random number based on range user made? {     return math.floor((highnumber - lownumber + 1) * math.random() + lownumber); } 

to answer questions...

  1. you've declared guessesleft inside checkcorrectguess() means local variable that's being redefined every time call function. futhermore, because you're passing in var firstguess:string; (an uninitialized, non-referenced string variable), (g1 != computerguess) returning false, , answer 0.
  2. guessesleft - 1; not saving result variable. need use assignment operator such guessesleft = guessesleft - 1 or type guessesleft-- if want decrement. write guessesleft -= 1 subtracts right left, , assigns value variable on left. see as3 operators...
  3. you've assigned values these textfields earlier; repeat process inside of newgame() txtinlow.text = "" (same high)
  4. use variables. defined them earlier in checkguess() userguess, lowvalue, , highvalue

be mindful need split out functionality separate functions if piece of code called elsewhere. otherwise, every function on stack incurs more memory , performance hits. checkcorrectguess() falls category , therefore unnecessary.

also, printing feedback user in newgame() function instead of checkguess(). seemed 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 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; // 0 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); } 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -