【问题标题】:Rock Paper scissors logic issues石头剪刀布逻辑问题
【发布时间】:2021-07-15 12:56:14
【问题描述】:

刚开始通过 Odin 项目学习 Javascript。构建石头剪刀布,我正在尝试构建我自己的逻辑,但有点卡住了。

我有电脑和玩家的输入。但是,一个返回一个字符串,而另一个返回一个数字。解决办法?

另外,我认为我的游戏逻辑有缺陷。

const rps = ["ROCK", "PAPER", "SCISSORS"];

let computerPlay =  Math.floor(Math.random() * rps.length);

console.log(rps[computerPlay]);

let playerSelection = window.prompt("ROCK, PAPER, or SCISSORS?")

console.log(playerSelection, computerPlay);
/* Need to change both to strings? */


/* Game Logic*/

if (playerSelection === "ROCK") {
    if (computerPlay === "ROCK")
        return "TIE: Both selected ROCK";
    else if (computerPlay === "PAPER")
        return "You Lose! PAPER beats ROCK";
    else (computerPlay === "SCISSORS")
        return "You Won! ROCK beats SCISSORS";
} 
else if (playerSelection === "PAPER") {
    if (computerPlay === "PAPER")
        return "TIE: Both selected PAPER";
    else if (computerPlay === "ROCK")
        return "You Won! PAPER beats ROCK";
    else (computerPlay === "SCISSORS")
        return "You Lose! SCISSORS beats PAPER";
} else if (playerSelection === "SCISSORS") {
    if (computerPlay === "SCISSORS")
        return "TIE: Both selected SCISSORS";
    else if (computerPlay === "PAPER")
        return "You Won! SCISSORS beats PAPER";
    else (computerPlay === "ROCK")
        return "You Lose! ROCK beats SCISSORS";}```

【问题讨论】:

  • 您需要在某个时候将computerPlay 设置为rps[computerPlay],或者引入另一个变量。您正在比较计算机选择的 index 而不是值。

标签: javascript logic


【解决方案1】:

在基本侦察之后,您在computerPlay 中获得了随机数,并且您试图将其与完全错误的用户输入进行比较。您需要将rps[computerPlay] 与用户响应进行比较。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-10
    • 2018-04-06
    相关资源
    最近更新 更多