// 定义变量,记录双方出拳
let playerChoice = "";
let computerChoice = "";
// 定义函数,用于电脑出拳
function computerPlay() {
// 随机生成整数 0、1、2 分别代表剪刀、石头、布
const choices = ["scissors", "rock", "paper"];
const randomIndex = Math.floor(Math.random() * 3);
return choices[randomIndex];
}
// 定义函数,用于开始游戏
function playTurn(choice) {
playerChoice = choice;
computerChoice = computerPlay();
const iconsHTML = `
${emojiOf(playerChoice)}
${emojiOf(computerChoice)}
`;
const outcomeMessage = determineOutcome(playerChoice, computerChoice);
document.getElementById("choices").innerHTML = iconsHTML;
document.getElementById("outcome").innerHTML = outcomeMessage;
const playerIcon = document.querySelector("#player-icon div");
playerIcon.style.color = "#3498db";
playerIcon.style.fontSize = "6rem";
const computerIcon = document.querySelector("#computer-icon div");
computerIcon.style.color = "#e74c3c";
computerIcon.style.fontSize = "6rem";
}
// 定义函数,用于将出拳转为 Emoji 显示
function emojiOf(choice) {
switch (choice) {
case "scissors":
return "✌️";
case "rock":
return "✊";
case "paper":
return "🖐️";
default:
return "";
}
}
function determineOutcome(playerChoice, computerChoice) {
if (playerChoice === computerChoice) {
return "打平了!";
} else if (
(playerChoice === "scissors" && computerChoice === "paper") ||
(playerChoice === "rock" && computerChoice === "scissors") ||
(playerChoice === "paper" && computerChoice === "rock")
) {
return "你赢了!";
} else {
return "你输了!";
}
}