繁体   English   中英

如何防止控制台在JavaScript游戏中打印?

[英]How can I prevent console from printing in JavaScript game?

我一直在用JavaScript练习游戏,想知道当用户做出错误定义时如何防止控制台打印?

这是代码:

var user = prompt("Do you choose rock, paper or scissors?");
var computer = Math.random();
if (computer < 0.34) {
    computer = "rock";
}
else if (computer <= 0.67) {
    computer = "paper";
}
else {
    computer = "scissors";
}
console.log("Computer Chooses: " + computer);
console.log("User Chooses: " + user);
var compare = function (computer, user) {
    if (computer === "rock") {
        if (user === "scissors") {
            return "Computer wins by choosing rock!";
        }
    }
    else if (computer === "scissors") {
        if (user === "paper") {
            return "Computer wins by choosing scissors!";
        }
    }
    else if (computer === "paper") {
        if (user === "rock") {
            return "Computer wins by choosing paper!"
        }
    }
    if (computer === user) {
        return ("It is a tie!")
    }
    else if (user === "paper") {
        if (computer === "rock") {
            return ("You win by choosing paper!")
        }
    }
    else if (user === "rock") {
        if (computer === "scissors") {
            return ("You win by choosing scissors!")
        }
    }
    else if (user === "scissors") {
        if (computer === "paper") {
            return ("You win by choosing scissors!")
        }
    }
    ***if (user !== "rock" && user !== "paper" && user !== "scissors") {
        confirm(user + " is an invalid entry.");
    }***
};
compare(computer, user);

最后,我摘录了一些代码,向用户表明他输入了错误的字符。 我想知道的是:

一旦有人输入了错误的输入,如何使任何内容都不显示在控制台上?

在一开始就对用户输入进行验证。 如果用户仅通过显示代码,则显示错误。

var user = prompt("Do you choose rock, paper or scissors?");
if(user !== "rock" && user !== "paper" && user!== "scissors") { 
    confirm(user + " is an invalid entry."); 
} else {
    // code for process
}

一种选择是一直询问用户有效输入,直到给出有效输入为止:

while (user != "rock" && user != "paper" && user != "scissors") {
    user = prompt("Do you choose rock, paper or scissors?")
    if (user == null) {
        break;
    }
};

if (user != null) {
...
}

http://jsfiddle.net/2w3pt5yy/3/

您可以将此代码添加到您的js代码之上

var console = {};
console.log  = function(){};

if语句多于必要; 您可以简化逻辑。

var getComputerMove = function () {
    // equivalent of Math.floor
    // aka it grabs the integer component of `Math.random() * 3`
    // either 0, 1, or 2
    return (Math.random() * 3) | 0;
}

var playRockPaperScissors = function () {
    var moves = [ "rock", "paper", "scissors" ]
      , user = ""
      , computer = getComputerMove();

    // while `user` is not in the `moves` array
    // `Array.indexOf` returns -1 if an element is not in the array
    // prompt the user for his move
    while (moves.indexOf(user.toLowerCase()) == -1) {
        user = prompt("Rock, paper, or scissors?");
    }

    // this is where you can save yourself all that typing and think out
    // the possible movesets:
    // item:    rock < paper < scissors < rock
    // index:   0    < 1     < 2        < 0
    // so, the computer wins if its choice is greater than the user's,
    // or if the computer chose "rock" and the user chose scissors
    // we can translate this to
    // user < computer || (computer == 0 && user == 2)


    var userIndex = moves.indexOf(user.toLowerCase());

    // uncomment, if you want to see the moves
    // console.log("user:", user, "computer:", moves[computer]);

    if (userIndex < computer || (userIndex == 2 && computer == 0) {
        alert("Computer wins!");
    } else {
        alert("User wins!");
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM