2013-04-05 79 views
0

因此,我正在使用Javascript制作一个摇滚,纸张,剪刀游戏,并且我在开始时遇到了一些麻烦。我需要有一个文本框和提交按钮,然后用户输入“摇滚”,“纸”,“剪刀”将针对计算机的随机选择进行播放。我如何让计算机将输入到文本字段中的内容与计算机选项进行对比?我是新手,需要向正确的方向推动,因为我不知道如何开始这个问题。在Javascript中使用文本框信息作为变量?

感谢

编辑: 因此,一个朋友给我发了一些代码,我加入到它的一些,它看起来像它会工作(至少对我来说),但我不知道如何设置变量“玩家”等于等于文本框信息。

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> 
+0

你甚至谷歌什么? – btevfik 2013-04-05 21:27:58

回答

0

电脑的选择可以这样产生:

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

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

//Check to see the winner 
... 

希望帮助给你一个开始。

+1

var plays = {“rock”,“paper”,“scissors”};'给出'SyntaxError:意外的标记,',也许你是想写一个_Array_文字呢? ..例如'['rock','paper','scissors']'(方括号,不是大括号) – 2013-04-05 21:29:55

+0

@PaulS。谢谢。我最近做了太多''C'',并且有点混乱。 – 2013-04-05 21:32:14

0

由于可能性有限,一种方法是使用查找表。您必须使用更多的JavaScript将其链接到您的HTML。为此,您可能需要将document.getElementById<input>value一起使用,并输出到其他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]] 
       }; 
     }; 
}()); 
相关问题