猜测数字游戏程序无法正常运作

时间:2017-07-26 02:39:55

标签: actionscript-3 flash actionscript flash-cs5 flash-cs6

在我的程序中,用户设置一系列数字供计算机猜测。然后,用户必须猜测计算机选择的数字,从5开始猜测的限制。在我的功能程序中有几个问题,我不明白如何修复。这些错误包括:

- 剩下的猜测数始终保持为0.每次单击btnCheck按钮时,它不会从5开始减1。

- 无论何时我点击btnCheck按钮获取新的猜测数字,如果您猜到太高或太低,该陈述仍保持不变。

- 当我按btnNewGame时,我插入低值和高值文本输入的值将不会被清除。

- 计算机如何根据我设置的数字范围生成随机整数?

非常感谢下面修改我的代码。

// 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 个答案:

答案 0 :(得分:2)

回答你的问题......

  1. 您已在GuessesLeft内声明checkCorrectGuess(),这意味着每次调用该函数时都会重新定义一个本地变量。此外,因为您正在传递var FirstGuess:String;(未初始化的,未引用的string变量),(g1 != computerGuess)返回false,答案始终为0.
  2. GuessesLeft - 1;未将结果保存回变量。您需要使用GuessesLeft = GuessesLeft - 1之类的赋值运算符,或者只需键入GuessesLeft--,如果您想要减少的话。您还可以编写GuessesLeft -= 1从左侧减去右侧,并将值分配给左侧的变量。 See AS3 Operators...
  3. 您之前已经为这些TextFields分配了值;只需使用newGame()(与高位相同)
  4. 重复txtinLow.text = ""内的处理
  5. 使用您的变量。您之前在checkGuess() UserGuessLowValueHighValue
  6. 中定义了它们

    请注意,如果可能在其他地方调用该段代码,则只需将功能拆分为单独的功能。否则,堆栈上的每个函数都会产生更多内存和性能命中。 checkCorrectGuess()属于该类别,因此是不必要的。

    此外,您要在newGame()函数中向用户打印反馈,而不是checkGuess()。这似乎是一种疏忽。

    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);
    }