新学习Javascript,因此我正在学习将函数存储在对象中。当前正在创建一个简单的剪刀石头布游戏。我陷入困境,我需要getWinner函数来确定获胜者,并在该函数中添加了3个条件:平局,赢局或输局。现在的问题是,它总是会吸引我。有人可以帮忙吗?
const startGameBtn = document.getElementById('start-game-btn');
let ROCK = "ROCK";
let PAPER = "PAPER";
let SCISSORS = "SCISSORS";
let RESULT_DRAW = "It's a draw";
let RESULT_PLAYER_WINS = "Player Wins";
let RESULT_COMPUTER_WINS = "Player Wins";
let GAME_IS_RUNNING = false;
let getPlayerChoice = function () {
let selection = prompt(`${ROCK},${PAPER}, or ${SCISSORS}? `, '').toUpperCase();
if (selection !== ROCK &&
selection !== PAPER &&
selection !== SCISSORS) {
alert ("Invalid choice, defaulted to Rock");
selection = ROCK;
}
return selection
}
const getComputerChoice = function() {
const randomValue = Math.floor(Math.random() * 2);
if (randomValue === 0) {
return ROCK;
} else if (randomValue === 1) {
return PAPER;
} else if (randomValue === 2) {
return SCISSORS;
};
}
const getWinner = function (cChoice, pChoice) {
if (cChoice === pChoice) {
return RESULT_DRAW;
} else if (cChoice === ROCK && pChoice === PAPER ||
cChoice === PAPER && pChoice === SCISSORS ||
cChoice === SCISSORS && pChoice === ROCK
) {
return RESULT_PLAYER_WINS;
} else {
return RESULT_COMPUTER_WINS;
}
}
startGameBtn.addEventListener('click', function () {
if (GAME_IS_RUNNING) {
return
}
GAME_IS_RUNNING = true;
console.log("Game is starting....");
let playerChoice = getPlayerChoice();
console.log(playerChoice);
let computerChoice = getComputerChoice();
console.log(computerChoice);
let winner = getWinner(computerChoice, playerChoice);
console.log(winner);
});
<button id="start-game-btn">Start</button>
答案 0 :(得分:3)
仅解决计算机永远无法选择剪刀的问题,它可以正常运行。也许您只是幸运抽奖。
const startGameBtn = document.getElementById('start-game-btn');
let ROCK = "ROCK";
let PAPER = "PAPER";
let SCISSORS = "SCISSORS";
let RESULT_DRAW = "It's a draw";
let RESULT_PLAYER_WINS = "Player Wins";
let RESULT_COMPUTER_WINS = "Player Wins";
let GAME_IS_RUNNING = false;
let getPlayerChoice = function() {
let selection = prompt(`${ROCK},${PAPER}, or ${SCISSORS}? `, '').toUpperCase();
if (selection !== ROCK &&
selection !== PAPER &&
selection !== SCISSORS) {
alert("Invalid choice, defaulted to Rock");
selection = ROCK;
}
return selection
}
const getComputerChoice = function() {
const randomValue = Math.floor(Math.random() * 3); // <----
if (randomValue === 0) {
return ROCK;
} else if (randomValue === 1) {
return PAPER;
} else if (randomValue === 2) {
return SCISSORS;
};
}
const getWinner = function(cChoice, pChoice) {
if (cChoice === pChoice) {
return RESULT_DRAW;
} else if (cChoice === ROCK && pChoice === PAPER ||
cChoice === PAPER && pChoice === SCISSORS ||
cChoice === SCISSORS && pChoice === ROCK
) {
return RESULT_PLAYER_WINS;
} else {
return RESULT_COMPUTER_WINS;
}
}
startGameBtn.addEventListener('click', function() {
if (GAME_IS_RUNNING) {
return
}
GAME_IS_RUNNING = true;
console.log("Game is starting....");
let playerChoice = getPlayerChoice();
console.log(playerChoice);
let computerChoice = getComputerChoice();
console.log(computerChoice);
let winner = getWinner(computerChoice, playerChoice);
console.log(winner);
});
<button id="start-game-btn">START-STOP</button>