循环一个石头剪刀游戏

时间:2015-11-04 03:28:01

标签: javascript

好的,所以这应该很容易回答,但由于某种原因,我有一个问题。(可能是因为我非常新的编程)我已经创建了一个针对CPU的摇滚,纸张,剪刀游戏。现在我想询问用户是否想要再次播放,如果他们回答Y,那么每次都要经过循环。如果他们输入" N"然后游戏将结束。我遇到的主要问题是,一旦你输入Y再次玩,它就会给你上一场比赛的结果。任何指针都会有所帮助。

这就是我所拥有的:

var userChoice = "";
var userChoice = prompt("Choose rock, paper, or scissors");
var playagain = "Y";
var computerChoice = Math.random();

if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if (computerChoice <= 0.67) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
}

choice1 = userChoice;
choice2 = computerChoice;

while (playagain == "Y") {
    function compare(choice1, choice2) {

        if (choice1 == choice2) {
            return ("It's a tie!");
        }
        if (choice1 == "rock") {
            if (choice2 == "scissors") {
                return ("You win!");

            } else {
                return ("computer wins!");
            }
        }
        if (choice1 == "paper") {
            if (choice2 == "rock") {
                return ("you win!");
            } else {
                return ("computer wins!");
            }
        }
        if (choice1 == "scissors") {
            if (choice2 == "rock") {
                return ("computer wins!");
            } else {
                return ("you win!");
            }
        }
    }

    document.write(compare(choice1, choice2));
    document.write("<br>");
    playagain = prompt("Do you want to play again, Y or N");
    userChoice = prompt("Choose rock, paper, or scissors");
}

4 个答案:

答案 0 :(得分:2)

您唯一需要的是在while循环下移动所有游戏逻辑:

function compare(choice1, choice2) {
    if (choice1 == choice2) {
        return ("It's a tie!");
    }
    if (choice1 == "rock") {
        if (choice2 == "scissors") {
            return ("You win!");
        } else {
            return ("computer wins!");
            }
        }
        if (choice1 == "paper") {
            if (choice2 == "rock") {
                return ("you win!");
            } else {
                return ("computer wins!");
            }
        }
        if (choice1 == "scissors") {
            if (choice2 == "rock") {
                return ("computer wins!");
            } else {
                return ("you win!");
            }
        }
    }
}

var playagain = "Y";

while (playagain == "Y") {
    var userChoice = prompt("Choose rock, paper, or scissors");
    var computerChoice = Math.random();

    if (computerChoice < 0.34) {
        computerChoice = "rock";
    } else if (computerChoice <= 0.67) {
        computerChoice = "paper";
    } else {
        computerChoice = "scissors";
    }

    document.write(compare(userChoice, computerChoice));
    document.write("<br>");
    playagain = prompt("Do you want to play again, Y or N");
}

我有:

  • 将除playagain声明之外的所有内容移至循环
  • 删除了choice1choice2个本地变量,因为它们是多余的
  • 在循环结束时删除了提示,因为我们在开头有一个
  • 删除了开头的空userChoice声明
  • 根据@LiYinKing的建议,将该功能移出循环

这是working JSFiddle demo

答案 1 :(得分:0)

在while循环内部,您需要包含用户提示和新计算机生成的随机数。问题是你的循环一遍又一遍地计算相同的随机数和用户输入。

答案 2 :(得分:0)

很难准确解释什么是错误的,因为代码很难遵循。

  • 我不知道在代码执行到脚本底部之后,您希望如何设置计算机选择的代码。
  • 在循环中声明命名函数没有意义。在顶部声明它们。然后循环体中没有任何东西。腥...

你需要的是一个运行游戏的函数和一个调用该函数的while循环。

我正在打电话,这是未经测试的

function runGame() {
  var userChoice = prompt("Choose rock, paper, or scissors");
  var computerChoice= Math.random();

  if(computerChoice < 0.34){
      computerChoice = "rock";
  } else if(computerChoice<=0.67){
     computerChoice = "paper";
  } else {
     computerChoice = "scissors";
  }

  if(userChoice==computerChoice){
     return "It's a tie!";
  }
  if(userChoice=="rock"){
    if(computerChoice=="scissors"){
      return "You win!";
    } else{
      return "computer wins!";
    }
  }
  if(userChoice=="paper"){
    if(computerChoice=="rock"){
        return "you win!";         
    } else{
        return "computer wins!";
    }
  }    
  if(userChoice=="scissors"){
    if(computerChoice=="rock"){
        return "computer wins!" ;           
    } else{
        return "you win!" ;
    }
  }
}

alert(runGame());
while ( prompt("Do you want to play again, Y or N") == "Y") {
  alert(runGame());
}

答案 3 :(得分:0)

稍后凭借经验和知识,您可以编写如下脚本:

function compare(choice1, choice2) {
    if (choice1 == choice2) {
        return ("It's a tie!");
    }
    var lose = {
        "rock": "scissors",
        "paper": "rock",
        "scissors": "paper"
    };
    return (lose[choice1] == choice2) ? "You win!" : "computer wins!";
}

(function() {
    var CHOICE = ["rock", "paper", "scissors"];
    var userChoice = "";
    var userChoice, computerChoice, playagain, i = 0;

    do {
        document.write("Ground " + i + " :<br>");
        computerChoice = CHOICE[Math.floor(Math.random() * 3)];

        do {
            userChoice = (""+prompt("Choose rock, paper, or scissors")).toLowerCase();
        } while (CHOICE.indexOf(userChoice) === -1);

        document.write(userChoice + " vs " + computerChoice + "<br>");
        document.write(compare(userChoice, computerChoice) + "<br>");
        playagain = (""+prompt("Do you want to play again, Y or N")).toUpperCase();

        i++;
    } while (["Y", "YES"].indexOf(playagain) !== -1);
});

说明:

我添加toLowerCase()功能来替换&#34; Rock&#34; to&#34; rock&#34;,我添加toUpperCase()来替换&#34; y&#34;到&#34; Y&#34;。

在这种情况下do { /* script */ } while (bool)更合适。

使用:

do {
    userChoice = (""+prompt("Choose rock, paper, or scissors")).toLowerCase();
} while (CHOICE.indexOf(userChoice) === -1);

您等待有效答案,仅限三个字摇滚纸张剪刀。当userChoice等于&#34; rock&#34; indexOf返回0;等于&#34;纸张#34; indexOf返回1 ...

此语法:

return (lose[choice1] == choice2) ? "You win!" : "computer wins!";

是:

if (lose[choice1] == choice2)
    return "You win!";
else
    return "computer wins!"

JavaScript ternary operator example with functions