作为我的课堂作业之一,我的任务是在repl.it网站上修复损坏的代码。 repl.it rock, paper scissors。尽管这个话题已经被提出很多次了,但是我找不到具有与此类似代码的线程。它们都非常不同。
我知道主要问题是:
提示符提示即使用户赢了,计算机也总是赢。我添加了用户输入逻辑,以便为赢得用户的结果返回结果。
游戏会忽略“ Rock”作为用户输入。我添加了toLowerCase()方法来删除大写字符。
计算机始终忽略选择“ rock”。我将Math.ceil更改为Math.floor,以便函数randomFrom还包括computerChoices数组的0索引。
正确读取和存储用户输入(除了创建输入变量外,我不知道该如何进行这项工作。我已对此进行了控制台记录,但到目前为止还没有运气。
这是原始代码:
let computerChoices = ["rock", "paper", "scissors"];
function randomFrom(array) {
return array[(Math.ceil(Math.random()*3)) ];
console.log(array);
}
function checkInput(input, computerChoices) {
if (input == "quit") {
return true;
}
let computerChoice = randomFrom(computerChoices);
if(computerChoice === "rock" && input === "scissors"){
alert("Computer wins!");
return true;
} else if (computerChoice === "scissors" && input === "paper"){
alert("Computer wins!");
return true;
} else if (computerChoice === "paper" && input === "rock"){
alert("Computer wins!");
return true;
}
alert("Computer wins!");``
return false
}
function start(gameOver, computerChoices) {
while (!gameOver){
let playerInput = '';
prompt("Hi! Enter rock/paper/scissors to play, or quit to stop playing.");
gameOver = checkInput(playerInput, computerChoices);
playerInput = "paper";
}
}
let gameOver = false;
start(gameOver, computerChoices)
我也将代码包含在注释中。我不太了解这里发生的一切。我知道这也是一个范围问题,但我只是没有看到。我并没有要求任何人为我解决这个问题,但至少要让我知道我是否朝着正确的方向发展,或者我是否使代码比开始时做得差。如果您确实解决了该问题,请添加更多评论。谢谢!
//Is gameOver defined correctly? I'm not sure.
let gameOver = false;
let computerChoices = ["rock", "paper", "scissors"];
let computerChoice = randomFrom(computerChoices);
console.log(computerChoice);
// //change Math.ceil to Math.floor in order to include the [0] index
function randomFrom(array) {
return array[(Math.floor(Math.random()*3))];
}
//Changed computerChoices to computerChoice
function checkInput(input, computerChoice) {
if (input === "quit") {
return true;
}
//Changed computerChoices to computerChoice
function startGame(gameOver, computerChoice) {
while (!gameOver){
let playerInput = '';
//Added input in order to change user input to lowercase characters
let input = playerInput.toLowerCase();
prompt("Hi! Enter rock/paper/scissors to play, or quit to stop playing.");
}
if(computerChoice === "rock" && input === "scissors"){
alert("Computer wins!");
return true;
} else if (computerChoice === "scissors" && input === "paper"){
alert("Computer wins!");
return true;
} else if (computerChoice === "paper" && input === "rock"){
alert("Computer wins!");
return true;
//Added user outcomes since program results in the computer always winning
} else if(computerChoice === "rock" && input === "paper"){
alert("Player wins!");
return true;
} else if (computerChoice === "scissors" && input === "rock"){
alert("Player wins!");
return true;
} else if (computerChoice === "paper" && input === "scissors"){
alert("Computer wins!");
return true;
} else if (computerChoice === input) {
alert("we have a draw");
} else {
console.log("error");
}
}
//I'm calling the startGame() function in order to get it to run
return startGame();
}
答案 0 :(得分:0)
这里有些事
input
函数外部定义了startGame()
以使其可在checkInput()
内部使用-将input
作为参数传递也很好startGame()
内的无效和不必要的while循环startGame()
在checkInput()
内部,因此它从未被调用,因此游戏从未开始
//Is gameOver defined correctly? I'm not sure.
let gameOver = false;
let input;
let computerChoices = ["rock", "paper", "scissors"];
let computerChoice = randomFrom(computerChoices);
console.log(computerChoice);
// //change Math.ceil to Math.floor in order to include the [0] index
function randomFrom(array) {
return array[(Math.floor(Math.random() * 3))];
}
//Changed computerChoices to computerChoice
function startGame() {
//Added input in order to change user input to lowercase characters
input = prompt("Hi! Enter rock/paper/scissors to play, or quit to stop playing.").toLowerCase();
checkInput();
}
//Changed computerChoices to computerChoice
function checkInput() {
if (input === "quit") {
return;
}
if (computerChoice === "rock" && input === "scissors") {
alert("Computer wins!");
} else if (computerChoice === "scissors" && input === "paper") {
alert("Computer wins!");
} else if (computerChoice === "paper" && input === "rock") {
alert("Computer wins!");
//Added user outcomes since program results in the computer always winning
} else if (computerChoice === "rock" && input === "paper") {
alert("Player wins!");
} else if (computerChoice === "scissors" && input === "rock") {
alert("Player wins!");
} else if (computerChoice === "paper" && input === "scissors") {
alert("Computer wins!");
} else if (computerChoice === input) {
alert("we have a draw");
} else {
console.log("error");
}
}
//I'm calling the startGame() function in order to get it to run
startGame();
请记住,如果您希望游戏持续一轮以上,那么就需要循环。
第二,这是一个有效的示例,因为SO决定显示除堆栈片段以外的其他地方,我在测试时未遇到的一些错误:http://jsfiddle.net/zvao25ny/1/