JS CLI RPS 游戏问题

JS CLI RPS game issue

向 Stack Overflow 的爱心人士致以问候。 我最近才开始学习 JS,现在正在尝试制作 CLI 石头剪刀布游戏。是的,我知道我是第 100 个提出问题的人,但我无法通过其他人的代码找到 'no-make-sense' 代码。所以只求各位热心帮助新手的朋友帮忙!

情况是这样的:一个熟悉的游戏最常见的规则,但我无法让计数器工作,加上有时我输入的文字无法识别(我认为输入比较功能的原因). 另外,我确定我的功能范围已损坏,但我无法找出或跟踪它的位置和原因。

function computerPlay() {
  let anyPick = Math.random();
  if (anyPick < 0.3) {
    return "Rock";
  }
  if (anyPick > 0.3 && anyPick < 0.6) {
    return "Paper";
  }
  if (anyPick > 0.6) {
    return "Scissors";
  }
}

let aiScore = 0;
let playerScore = 0;
function battleRound(playerSelection, computerSelection) {
  if (playerSelection === computerSelection) {
    return "None of you are win or lose, cuz it is an equal!";
  }
  if (playerSelection === "rock" && computerSelection === "Scissors") {
    playerScore += 1;
    return "You Win! Rock beats Scissors";
  }
  if (playerSelection === "rock" && computerSelection === "Paper") {
    aiScore += 1;
    return "You Loose! Paper beats Rock";
  }
  if (playerSelection === "paper" && computerSelection === "Scissors") {
    aiScore += 1;
    return "You Loose! Scissors cut Paper";
  }
  if (playerSelection === "paper" && computerSelection === "Rock") {
    playerScore += 1;
    return "You Win! Paper beats Rock";
  }
  if (playerSelection === "scissors" && computerSelection === "Paper") {
    playerScore += 1;
    return "You Win! Scissors cut Paper";
  }
  if (playerSelection === "scissors" && computerSelection === "Rock") {
    aiScore += 1;
    return "You Loose! Rock beats Scissors";
  } else return "U misspelled, try again";
}

function scores(aiScore, playerScore) {
  if (aiScore > playerScore) {
    return "CONGRAS, AI IS SMARTER THEN YOU!";
  }
  if (playerScore > aiScore) {
    return "CONGRAS, YOU ARE SMARTER THEN AI";
  }
}

for (let i = 0; i < 5; i++) {
  const playerSelection = prompt(
    "Choose: Rock, Paper or Scissors",
    " "
  ).toLowerCase();
  const computerSelection = computerPlay();
  console.log("AI choose: " + computerSelection);
  console.log(battleRound(playerSelection, computerSelection));
  console.log("AI score: " + aiScore, "Player score: " + playerScore);
}

请指出代码中的明显问题以及我需要替换的确切内容。 非常感谢任何提示!

您的代码有两个主要问题可能会导致此问题,

  • 您正在将默认提示值设置为空 space。 通过使用:
prompt("Choose: Rock, Paper or Scissors", " ")

默认提示值为空 space,这样更容易输入错误值。

为了解决这个问题,我删除了默认值并添加了 .trim() 方法来从输入的开头和结尾删除白色 space。

  • 您正在进行不可能的比较。您在代码中进行了比较:
if (playerSelection === computerSelection) {
     return ("None of you are win or lose, cuz it is an equal!");
}

这个条件永远不会是 true,因为 playerSelection 总是转换为小写,例如 "rock",但 computerSelection 的第一个字母总是大写,例如"Rock".

我在比较之前把.toLowerCase()方法加到computerSelection的末尾。

完整代码:

function computerPlay() {
  let anyPick = Math.random();
  if (anyPick < 0.3) {
    return "Rock";
  }
  if (anyPick > 0.3 && anyPick < 0.6) {
    return "Paper";
  }
  if (anyPick > 0.6) {
    return "Scissors";
  }
}

let aiScore = 0;
let playerScore = 0;

function battleRound(playerSelection, computerSelection) {
  if (playerSelection === computerSelection.toLowerCase()) {
    return "None of you are win or lose, cuz it is an equal!";
  }
  if (playerSelection === "rock" && computerSelection === "Scissors") {
    playerScore += 1;
    return "You Win! Rock beats Scissors";
  }
  if (playerSelection === "rock" && computerSelection === "Paper") {
    aiScore += 1;
    return "You Loose! Paper beats Rock";
  }
  if (playerSelection === "paper" && computerSelection === "Scissors") {
    aiScore += 1;
    return "You Loose! Scissors cut Paper";
  }
  if (playerSelection === "paper" && computerSelection === "Rock") {
    playerScore += 1;
    return "You Win! Paper beats Rock";
  }
  if (playerSelection === "scissors" && computerSelection === "Paper") {
    playerScore += 1;
    return "You Win! Scissors cut Paper";
  }
  if (playerSelection === "scissors" && computerSelection === "Rock") {
    aiScore += 1;
    return "You Loose! Rock beats Scissors";
  } else return "U misspelled, try again";
}

function finalScores() {
  if (aiScore > playerScore) {
    return "CONGRATS, AI IS SMARTER THEN YOU!";
  }
  if (playerScore > aiScore) {
    return "CONGRATS, YOU ARE SMARTER THEN AI";
  }
}

for (let i = 0; i < 5; i++) {
  const playerSelection = (prompt("Choose: Rock, Paper or Scissors") ?? "")
    .trim()
    .toLowerCase();
  const computerSelection = computerPlay();
  console.log("AI choose: " + computerSelection);
  console.log(battleRound(playerSelection, computerSelection));
  console.log("AI score: " + aiScore, "Player score: " + playerScore);
}

console.log(finalScores())