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

时间:2015-12-09 23:50:00

标签: javascript if-statement output

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

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

var gameResults     // Game Results
var playerChoice    // Players choice
var BR = "<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><br />
<input type="text" name="name" value="Name" size="40"><p />

<strong>Select Paper, Rock, Scissors, Lizard, or Spock:</strong><br />
<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><br />

</form>
</body>
</html>

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

1 个答案:

答案 0 :(得分:1)

如果您更改此行代码:

function winResults(string)

阅读:

function winResults(wcType)

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

TIE

你应该改为:

winResults(TIE)