所以,我正在使用 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>