石头,剪刀剪游戏-重复5轮

时间:2018-09-19 11:03:38

标签: javascript loops recursion

我正在修建石头剪刀布任务。目前,我的代码只适用于1轮。我不确定如何在保持5个回合的同时保持得分的状态。我的印象是,至少在以下几轮中,我需要一个for循环:

for(i=0; i<5;i++);

但是我不知道将其插入我的代码中的位置。我在网上四处张望,找不到足够简单的资源来理解并没有开始使用切换方法或任何其他更高级的代码来构建游戏。任何帮助,将不胜感激。谢谢。

function computerPlay() {
  let random = Math.random();
  if (random <= 0.3333) {
    return "paper";
  } else if (random >= 0.6666) {
    return "rock";
  } else {
    return "scissors";
  }
}


function playRound(playerSelection, computerSelection) {
  if (playerSelection.toLowerCase() === "rock") {
    if (computerSelection === "paper") {
      computerScore++;
      return lose;
    } else if (computerSelection === "rock") {
      return tie;
    } else {
      userScore++;
      return win;
    }
  }

  if (playerSelection.toLowerCase() === "scissors") {
    if (computerSelection === "paper") {
      userScore++;
      return win;
    } else if (computerSelection === "rock") {
      computerScore++;
      return lose;
    } else {
      return tie;
    }
  }

  if (playerSelection.toLowerCase() === "paper") {
    if (computerSelection === "paper") {
      return tie;
    } else if (computerSelection === "rock") {
      userScore++;
      return win;
    } else {
      computerScore++;
      return lose;
    }
  }
}


let userScore = parseInt(0);
let computerScore = parseInt(0);
let win = "You win"
let lose = "You lose"
let tie = "It is a tie"
let playerSelection = prompt("Pick a move");
const computerSelection = computerPlay()
console.log(playRound(playerSelection, computerSelection))
console.log("your score = " + userScore);
console.log("Computer's score = " + computerScore);

2 个答案:

答案 0 :(得分:4)

我已经编辑了您的代码片段,希望它能够满足您的需求:)

只需将下面的代码放入for循环

  

let playerSelection =提示(“提举”);
  const computerSelection = computerPlay()
  console.log(playRound(playerSelection,computerSelection))
  console.log(“您的分数=” + userScore);
  console.log(“计算机分数=” + computerScore);

function computerPlay() {
  let random = Math.random();
  if (random <= 0.3333) {
    return "paper";
  } else if (random >= 0.6666) {
    return "rock";
  } else {
    return "scissors";
  }
}


function playRound(playerSelection, computerSelection) {
  if (playerSelection.toLowerCase() === "rock") {
    if (computerSelection === "paper") {
      computerScore++;
      return lose;
    } else if (computerSelection === "rock") {
      return tie;
    } else {
      userScore++;
      return win;
    }
  }

  if (playerSelection.toLowerCase() === "scissors") {
    if (computerSelection === "paper") {
      userScore++;
      return win;
    } else if (computerSelection === "rock") {
      computerScore++;
      return lose;
    } else {
      return tie;
    }
  }

  if (playerSelection.toLowerCase() === "paper") {
    if (computerSelection === "paper") {
      return tie;
    } else if (computerSelection === "rock") {
      userScore++;
      return win;
    } else {
      computerScore++;
      return lose;
    }
  }
}


let userScore = parseInt(0);
let computerScore = parseInt(0);
let win = "You win"
let lose = "You lose"
let tie = "It is a tie"

for(var i=0;i<5;i++){
  let playerSelection = prompt("Pick a move");
  const computerSelection = computerPlay()
  console.log(playRound(playerSelection, computerSelection))
  console.log("your score = " + userScore);
  console.log("Computer's score = " + computerScore);
}

答案 1 :(得分:3)

尝试以下代码:

循环不是一个好方法,在这里阅读:

它允许用户播放5次。

使用递归

function computerPlay() {
    let random = Math.random();
    if (random <= 0.3333) {
        return "paper";
    } else if (random >= 0.6666) {
        return "rock";
    } else {
        return "scissors";
    }
}


function playRound(playerSelection, computerSelection) {
    if (playerSelection.toLowerCase() === "rock") {
        if (computerSelection === "paper") {
            computerScore++;
            return lose;
        } else if (computerSelection === "rock") {
            return tie;
        } else {
            userScore++;
            return win;
        }
    }

    if (playerSelection.toLowerCase() === "scissors") {
        if (computerSelection === "paper") {
            userScore++;
            return win;
        } else if (computerSelection === "rock") {
            computerScore++;
            return lose;
        } else {
            return tie;
        }
    }

    if (playerSelection.toLowerCase() === "paper") {
        if (computerSelection === "paper") {
            return tie;
        } else if (computerSelection === "rock") {
            userScore++;
            return win;
        } else {
            computerScore++;
            return lose;
        }
    }
}


let userScore = parseInt(0);
let computerScore = parseInt(0);
let win = "You win"
let lose = "You lose"
let tie = "It is a tie"

var i = 0;
const play = () => {
    let playerSelection = prompt("Pick a move");
    const computerSelection = computerPlay()
    console.log(playRound(playerSelection, computerSelection))
    console.log("your score = " + userScore);
    console.log("Computer's score = " + computerScore);
    i++;
    if (i !== 5) {
        play();
    } else {
        alert("Game Over=> User("+userScore+") vs Computer("+computerScore+")");
    }
}

play();