首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >石头,纸,剪刀游戏-重复5回合

石头,纸,剪刀游戏-重复5回合
EN

Stack Overflow用户
提问于 2018-09-19 11:03:37
回答 2查看 2.7K关注 0票数 1

我正在建造石头,剪刀的任务。在我的代码只工作1轮的那一刻。我不知道我如何才能保持这个分数,同时重复5轮。我的印象是,我需要一个for循环,至少在各个回合中是这样的,大致如下:

代码语言:javascript
复制
for(i=0; i<5;i++);

但我不知道该把它插到我的代码里。我在网上环顾了一下,我找不到足够简单的资源,不需要使用开关方法或任何其他更高级的代码来构建游戏。任何帮助都将不胜感激。谢谢。

代码语言:javascript
复制
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);

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-09-19 11:12:58

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

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

让playerSelection =提示符(“选择一个移动”); const computerSelection = computerPlay() console.log(playRound(playerSelection,computerSelection)) Console.log(“您的分数=”+ userScore); Console.log(“计算机分数=”+ computerScore);

代码语言:javascript
复制
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);
}

票数 4
EN

Stack Overflow用户

发布于 2018-09-19 11:15:18

试试下面的代码:

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

它允许用户玩5次。

使用递归

代码语言:javascript
复制
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();

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52404412

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档