度々質問失礼します.
じゃんけんのプログラムを作成していて,出た手によって勝ち負けの判定を出力したいです.
ユーザの各手を0,1,2で表し,PCの手はarrの0,1,2で表して計算により勝ち負けを判定できると思ったのですが,今のところエラーや勝ち負けの結果も何も出ません.
どこがおかしいのでしょうか.
javascript
1<!DOCTYPE html> 2<html> 3<head> 4 <meta charset="utf-8" /> 5 <title>じゃんけん</title> 6</head> 7<body> 8 <p> 9 <input type="button" value="start" onclick="toggle()" /> 10 PCの手:<span id="pchand"></span> 11 </p> 12 <p> 13 <input type="button" value="グー" onclick="serect_hand(0)"/> 14 <input type="button" value="チョキ" onclick="serect_hand(1)"/> 15 <input type="button" value="パー" onclick="serect_hand(2)"/> 16 あなたの手:<span id="userhand"></span> 17 </p> 18 <p> 19 <input type="button" value="結果" onclick="result()"/> 20 結果:<span id="kekka"></span> 21 </p> 22 <p> 23 <input type="button" id="reset" value="リセット" onclick="reset()"/> 24 </p> 25 26 <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> 27 <script> 28 //PC側:ランダムにグー,チョキ,パー出力 29 var view=document.getElementById("pchand"); 30 var pc_hand; 31 var running=false; 32 var intervalID; 33 var usernohand; 34 35 function toggle(){ 36 intervalID=setInterval(pc_hand,250); //pc_hand関数を250ミリ秒ごとに繰り返す 37 running=true; 38 }; 39 function pc_hand(){ 40 var arr=["グー","チョキ","パー"]; 41 var pcnohand=Math.floor(Math.random()*arr.length); 42 console.log(arr[pcnohand]); 43 view.textContent=arr[pcnohand]; 44 //console.log(arr[0]); //グー 45 // var pc0=arr.lastIndexOf('グー'); 46 // var pc1=arr.lastIndexOf('チョキ'); 47 // var pc2=arr.lastIndexOf('パー'); 48 // console.log(pc0); //0 49 // console.log(pc1); //1 50 // console.log(pc2); //2 51 }; 52 53 //ユーザ側:ボタンを押したらPCの手が止まる,選んだ手を出力 54 function serect_hand(usernohand){ 55 if(usernohand==0){ 56 document.getElementById("userhand").innerHTML="グー"; 57 clearInterval(intervalID); 58 running=false; 59 }else if (usernohand==1) { 60 document.getElementById("userhand").innerHTML="チョキ"; 61 clearInterval(intervalID); 62 running=false; 63 }else if (usernohand==2) { 64 document.getElementById("userhand").innerHTML="パー"; 65 clearInterval(intervalID); 66 running=false; 67 } 68 console.log("ユーザ:"+usernohand); //0,1,2 69 }; 70 71 //結果:ユーザの勝敗 72 function result(){ 73 if(pc_hand(arr[0])-usernohand==0){ 74 document.getElementById("kekka").innerHTML="あいこ"; 75 }else if (pc_hand(arr[1])-usernohand==1) { 76 document.getElementById("kekka").innerHTML="あなたの勝ち"; 77 }else if (pc_hand(arr[2])-usernohand==2) { 78 document.getElementById("kekka").innerHTML="あなたの負け"; 79 } 80 }; 81 82 //リセットボタンで初期化 83 function reset(){ 84 document.getElementById("pchand").innerHTML=""; 85 document.getElementById("userhand").innerHTML=""; 86 document.getElementById("kekka").innerHTML=""; 87 }; 88 </script> 89</body> 90</html> 91
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/10/29 09:52