2017-05-09 2 views
0

저는 프롬프트와 숫자를 통해 javascript로 RockPaperScissors 게임을 만들 수 있기 때문에 솔직히 궁금합니다.하지만 이제는 라디오 버튼으로 기능을 사용하는 방법을 알고 싶었습니다. 여기에 내가 지금까지 가지고 있지만 논리로 어디서 시작해야할지 모르겠습니다.라디오 버튼과 기능으로 만든이 JavaScript RockPaper 가위를 어떻게 마무리 할 수 ​​있습니까? (숙제가 아님)

자바 스크립트

function RPSGame() 
{ 
comp = Math.floor(Math.random()*3+1); 
if (RPS.radiobutton[0].checked == true) && (comp === RPS.radiobutton[0]) 
    document.RPS.result.value = ("A Tie!"); 


} 

가 난 그냥 자바 스크립트 프롬프트에 가지고있는 것처럼 내가 IF ELSE 중첩 루프를 사용할 것입니다 가정,하지만 난 기능에 너무 능숙하지 않다.

HTML

<form name = "RPS"> 
<p>Rock Paper Scissors <br> 
<input type = "radio" name="radiobutton" value ="1"/>Rock <br> 
<input type = "radio" name="radiobutton" value ="2"/>Paper <br> 
<input type = "radio" name="radiobutton" value ="3"/>Scissors <br> 

<br> 
<input type = "button" name="Play" value="Play" onclick="RPSGame(RPS)"/><br> <br> 
<input type="button" onclick="clearDoc();" value="Clear" /> 
<input type = "text" name ="result" size = "8"> 
</p> 
</form> 

는 다시이 나는이 튜토리얼 - 억양 질문의 아마 더 이해, 숙제를하지 않습니다. 어떤 도움을 주셔서 감사합니다.

답변

0

나는 그것을 이해했다고 생각합니다.

자바 스크립트

function RPSGame() 
{ 
comp = Math.floor(Math.random()*3+1); 


if ((comp == 1) && (RPS.radiobutton[2].checked)) 
{ document.RPS.result.value = ("You LOSE!"); } 
    else if ((comp == 1) && (RPS.radiobutton[1].checked)) 
    { document.RPS.result.value = ("You won!"); } 
    else if ((comp == 1) && (RPS.radiobutton[0].checked)) 
    {document.RPS.result.value = ("Tie!"); } 

    else if ((comp == 2) && (RPS.radiobutton[0].checked)) 
    { document.RPS.result.value = ("You LOSE!"); } 
    else if ((comp == 2) && (RPS.radiobutton[1].checked)) 
    { document.RPS.result.value = ("TIE!"); } 
    else if ((comp == 2) && (RPS.radiobutton[2].checked)) 
    { document.RPS.result.value = ("You won!"); } 

    else if ((comp == 3) && (RPS.radiobutton[0].checked)) 
     { document.RPS.result.value = ("You won!"); } 
     else if ((comp == 3) && (RPS.radiobutton[1].checked)) 
     { document.RPS.result.value = ("You LOSE!"); } 
     else if ((comp == 3) && (RPS.radiobutton[2].checked)) 
      { document.RPS.result.value = ("TIE!"); } 
      else 
      { document.RPS.result.value = ("Nothing!"); } 
} 

HTML

<form name = "RPS"> 
<p>Rock Paper Scissors <br> 
<input type = "radio" name="radiobutton" value ="1"/>Rock <br> 
<input type = "radio" name="radiobutton" value ="2"/>Paper <br> 
<input type = "radio" name="radiobutton" value ="3"/>Scissors <br> 

<br> 
<input type = "button" name="Play" value="Play" onclick="RPSGame(RPS)"/><br> <br> 
<input type="button" onclick="clearDoc(RPSGame);" value="Clear" /> 
<input type = "text" name ="result" size = "8"> 
</p> 
</form>