2013-04-05 78 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]] 
       }; 
     }; 
}()); 
相關問題