script.js 1.7 KB
Newer Older
6
62ba7085b5587201978beaa0 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
// 定义变量,记录双方出拳
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 = `
    <div>${emojiOf(playerChoice)}</div>
    <div>${emojiOf(computerChoice)}</div>
  `;
  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 "你输了!";
  }
}