0

所以,我正在使用 Javascript 制作摇滚、剪纸、剪刀游戏,但我在开始时遇到了一些麻烦。我需要有一个文本框和提交按钮,然后用户输入的“石头”、“纸”、“剪刀”将与计算机的随机选择进行对抗。我将如何让计算机获取输入到文本字段中的内容并根据计算机选择运行它?我是新手,需要朝着正确的方向轻推,因为我不知道如何开始这个问题。

谢谢

编辑:所以,一个朋友给我发了一些代码,我添加了一些代码,看起来它可以工作(至少对我来说),但我不确定将变量“player”设置为等于什么以便等于文本框信息。

var player = 


var choices = ["rock","paper","scissors"];
var computer = choices[Math.floor(Math.random()*3)];

var win = "Your "+player+" beats "+computer+". You win.";
var lose = "Your "+player+" loses to "+computer+". You lose.";
var draw = "A draw: "+player+" on "+computer+".";

if(player === "rock"){
    if(computer === "scissors"){
        result = win;
        alert="win";
    }
    else if(computer === "paper"){
        result = lose;
        alert="lose";
    }
    else if(computer === "rock"){
        result = draw;
        alert="draw";
    }
}
else if(player === "paper"){
    if(computer === "rock"){
        result = win;
        alert="win";
    }
    else if(computer === "scissors"){
      result = lose;
        alert="lose";
    }
    else if(computer === "paper"){
        result = draw;
        alert="draw";
    }
}
else if(player === "scissors"){
    if(computer === "paper"){
        result = win;
        alert="win";
    }
    else if(computer === "rock"){
        result = lose;
        alert="lose";
    }
    else if(computer === "scissors"){
        result = draw;
        alert="draw";
    }
}
</script>
</head>
<body>
<form>
<input type="text" id="rockTextInput" size="100" placeholder="Rock, Paper, or Scissors" >
<input type="button" id="Button" value="Play Hand">
</form>

</body>
</html>
4

2 回答 2

0

计算机的选择可以这样生成:

...
var plays = ["rock", "paper", "scissors"];
var rand = Math.round(Math.random() * 2);

alert("Computer played: " + plays[rand]);

//Check to see the winner
...

希望对您有所帮助。

于 2013-04-05T21:28:45.743 回答
0

由于可能性有限,一种方法是使用查找表。您必须使用更多的 JavaScript 将其链接到您的 HTML。为此,您可能希望使用document.getElementByIda<input>并输出到其他一些Element

/*
Usage:
play(player_choice)
    player_choice String, 'rock', 'paper' or 'scissors'
returns Object with properties
    player String, player's choice
    ai String, ai's choice
    result Integer, 0 - lose, 1 - draw, 2 - win
    resultText String, description of result
*/
var play = (function () {
    var options = ['rock', 'paper', 'scissors'],
        result_table = {
            'rock': {
                'rock': 1,
                'paper': 0,
                'scissors': 2
            },
            'paper': {
                'rock': 2,
                'paper': 1,
                'scissors': 0
            },
            'scissors': {
                'rock': 0,
                'paper': 2,
                'scissors': 1
            }
        },
        result_text = ['AI wins', 'Draw', 'Player wins'];
    return function (player_choice) {
            var ai_choice;
            player_choice = player_choice.toLowerCase();
            if (!result_table.hasOwnProperty(player_choice)) {
                throw '[' + player_choice + '] not a valid choice.';
            }
            ai_choice = options[Math.floor(Math.random() * options.length)];
            return {
                    'player': player_choice,
                    'ai': ai_choice,
                    'result': result_table[player_choice][ai_choice],
                    'resultText': result_text[result_table[player_choice][ai_choice]]
                };
        };
}());
于 2013-04-05T21:52:26.537 回答