首页 文章

在javascript中遇到Rock Paper Scissors Lizard Spock Final的麻烦

提问于
浏览
0

所以我是一名正在进行决赛的学生,我一直在从我们已经完成的其他项目中抽取例子 . 我可以得到一切工作,但结果是一个简单的纸岩剪刀蜥蜴spock游戏,但我不知道为什么结果不起作用

<html>
<head>
<script type ="text/javascript">

var gameResults     // Game Results
var playerChoice    // Players choice
var BR = "
"; // Line break var ES = ""; // Empty space var PA = "<p />"; // full paragraph break var NL = "\n"; // New Line function winResults(string) { gameResults = wcType; } function setChoice(pcType) { playerChoice = pcType; } function displayResults() { var name = document.RockPaperSpockForm.name.value; var computerChoice = Math.random(); if (computerChoice < 0.2) { computerChoice = "Rock"; } else if (computerChoice <= 0.4) { computerChoice = "Paper"; } else if (computerChoice <= 0.6) { computerChoice = "Scissors"; } else if (computerChoice <= 0.8) { computerChoice = "Lizard"; } else { computerChoice = "Spock"; } var compare = function(playerChoice, computerChoice) { if (playerChoice === computerChoice) { winResults(Tie); } else if (playerChoice === "Rock") { if (computerChoice === "Scissors") { winResults(Win); } else if (computerChoice === "Paper") { winResults(Lose); } else if (computerChoice === "Lizard") { winResults(Win); } else { winResults(Lose); } } else if (playerChoice === "Paper") { if (computerChoice === "Scissors") { winResults(Lose); } else if (computerChoice === "Rock") { winResults(Win); } else if (computerChoice === "Lizard") { winResults(Lose); } else { winResults(Win); } } else if (playerChoice === "Scissors") { if (computerChoice === "Paper") { winResults(Win); } else if (computerChoice === "Rock") { winResults(Lose); } else if (computerChoice === "Lizard") { winResults(Win); } else { winResults(Lose); } } else if (playerChoice === "Lizard") { if (computerChoice === "Scissors") { winResults(Lose); } else if (computerChoice === "Rock") { winResults(Lose); } else if (computerChoice === "Paper") { winResults(Win); } else { winResults(Win); } } else if (playerChoice === "Spock") { if (computerChoice === "Scissors") { winResults(Win); } else if (computerChoice === "Rock") { winResults(Win); } else if (computerChoice === "Lizard") { winResults(Lose); } else { winResults(Lose); } } } compare(playerChoice, computerChoice); alert("Hello! " + name + " you have chosen " + playerChoice + " and the computer has chosen " + computerChoice + "!" + NL + "You " + gameResults + "!"); } </script> </head> <body bgcolor="Azure"> <h3>Rock Paper Scissors Lizard Spock!</h3> <form name="RockPaperSpockForm" action=""> <strong>Enter your name:</strong>
<input type="text" name="name" value="Name" size="40"><p /> <strong>Select Paper, Rock, Scissors, Lizard, or Spock:</strong>
<input type="radio" name="choice" value="Paper" onclick="setChoice(this.value)" /><img src="PaperThumb.JPG"><p /> <input type="radio" name="choice" value="Rock" onclick="setChoice(this.value)" /><img src="RockThumb.JPG"><p /> <input type="radio" name="choice" value="Scissors" onclick="setChoice(this.value)" /><img src="ScissorsThumb.JPG"><p /> <input type="radio" name="choice" value="Lizard" onclick="setChoice(this.value)" /><img src="LizardThumb.JPG"><p /> <input type="radio" name="choice" value="Spock" onclick="setChoice(this.value)" /><img src="SpockThumb.JPG"><p /> <input type="button" name="displaybutton" value="Go" onclick="displayResults()" /><p /> <textarea name="messageBox" readonly="true" value="" rows="8" cols="50"></textarea>
</form> </body> </html>

我想要的是结果会将函数内部的变量设置为Tie,Win,Lose然后我可以将它附加到警报但它不能正常显示为未定义 . 任何帮助将不胜感激我被卡住了 .

1 回答

  • 1

    如果您更改此行代码:

    function winResults(string)

    阅读:

    function winResults(wcType)

    您还需要修复对 winResults 的调用,以便在每种情况下传递的参数都是字符串文字 - 现在所有这些调用都会被写入,就像它们传递名为 WINLOSETIE 的变量一样 . 例如,您目前拥有的位置:

    winResults(TIE)

    你应该改为:

    winResults("TIE")

相关问题