2012-02-07 2 views
0

私がやっていることは、ページ内でシンプルな紙、岩、はさみのゲームをシミュレートすることです。ラジオボタンでr/p/sを選択し、プレーヤー2は同じことをします。javacript関数で正しい値を返す問題

このコードには複数の問題がありますが、ロック/ペーパー/ハサミを解決する機能を実行しようとするたびに、私は本当に奇妙なものが返ってきます。

else if (player1Choice("Rock") && player2Choice("Scissors")) { 
     $("resultOutput").value = "Player 1s Rock beats Player 2s Scissors"; 
    } 

いつも私はこの結果を私の結果として得ています。私は関数を見てきたので、私はおそらく、現時点で明らかな解決策についてはあまり気にしていません。

// create our $() shortcut function for easily retrieving elements by id 
var $ = function(id) { 
    return document.getElementById(id); 
} 

//function executed on page load 
window.onload = function() { 

    //clear any previous values 
    document.forms[0].reset(); 

    //store value from player1Weapon radio choice with the player1Choice function 
    $("player1Submit").onclick = player1Choice; 

    //store value from player1Weapon radio choice with the player2Choice function 
    $("player2Submit").onclick = player2Choice; 

    //assign the fight button to run the fightCrunch function to determine the winner 
    $("fight").onclick = fightCrunch; 
} 

var player1Choice = function(x){ 
    var a = "Paper"; 
    var b = "Rock"; 
    var c = "Scissors"; 
    //the html has several radio buttons with id's of "player1Paper", etc. the $ function is to get the id name, and then I'm doing if, else ifs to see which on is checked. 
    if ($("player1Paper").checked) { 
     return a; 
} 
else if ($("player1Rock").checked) { 
    return b; 
} 
else if ($("player1Scissors").checked) { 
    return c; 
} 
else { 
    alert("Player 1, you need to pick a crude instrument of violence first"); 
} 
}; 

var player2Choice = function(y){ 
    var d = "Paper"; 
    var e = "Rock"; 
    var f = "Scissors"; 
    //var d = $("player2Paper").value; 
    //var e = $("player2Rock").value; 
    //var f = $("player2Scissors").value; 
    if ($("player2Paper").checked) { 
     return d; 
} 
else if ($("player2Rock").checked) { 
    return e; 
} 
else if ($("player2Scissors").checked) { 
    return f; 
} 
else { 
    alert("Player 2, you need to pick a crude instrument of violence first"); 
} 
}; 

var fightCrunch = function(){ 
     //The next few lines are commented out because the if gets hung up on this part, this always comes back as true no matter what I do with the previous functions. 
    //if (player1Choice || player2Choice == undefined) { 
     //alert("Both players need to select and submit a weapon of choice before a winner is determined"); 
    //} 
    if (player1Choice && player2Choice == "Rock") { 
     $("resultOutput").value = "Both players chose Rock, which results in a stalemate"; 
    } 
    else if (player1Choice && player2Choice == "Paper") { 
     $("resultOutput").value = "Both players chose Paper, which results in a stalemate"; 
    } 
    else if (player1Choice && player2Choice == "Scissors") { 
     $("resultOutput").value = "Both players chose Scissors, which results in a stalemate"; 
    } 
    else if (player1Choice("Rock") && player2Choice("Scissors")) { 
     $("resultOutput").value = "Player 1s Rock beats Player 2s Scissors"; 
    } 
    else if (player1Choice("Rock") && player2Choice("Paper")) { 
     $("resultOutput").value = "Player 2s Paper beats Player 1s Rock"; 
    } 
    else if (player1Choice("Paper") && player2Choice("Rock")) { 
     $("resultOutput").value = "Player 1s Paper beats Player 2s Rock"; 
    } 
    else if (player1Choice("Paper") && player2Choice("Scissors")) { 
     $("resultOutput").value = "Player 2s Scissors beats Player 1s Paper"; 
    } 
    else if (player2Choice("Paper").value && player1Choice("Scissors")) { 
     $("resultOutput").value = "Player 1s Scissors beats Player 2s Paper"; 
    } 
    else if (player2Choice("Rock").value && player1Choice("Scissors")) { 
     $("resultOutput").value = "Player 2s Rock beats Player 1s Scissors"; 
    } 
    else { 
     alert("something is wrong here"); 
    } 
} 
+0

これを[jsfiddle.net](http:// jsfiddle。ネット)私はそれをあなたのために働かせます。 –

+0

初めてjsfiddleを使用してチェックアウトしました。 – Benny

+0

http://jsfiddle.net/bennythedroid/ELFXc/ 。あなたのご意見を事前におねがいします! – Benny

答えて

1

コードにはかなりの問題があります。しかし、あなたが求めている問題は、あなたが比較を行っているところから生じる。

あなたはこれを行うことはできません

if ((player1Choice == true) && (player2Choice == "Rock")) 

は代わりに、あなたはそれをこのように書きたい(それはまだあるため、他の多くのエラーの動作しません。何を意味するのか

if (player1Choice && player2Choice == "Rock") 

が本質的です):

if (player1Choice == player2Choice) { 
    $("resultOutput").value = "Both players chose " + player1Choice + ", which results in a stalemate"; 
} 

比較操作が少ないだけでなく、多くの行のcああ!

".value"を誤って追加した最後の2つの比較で、追加のタイプミスがあることに注意してください。

さらに、関数player1Choiceplayer2Choiceは変数ではないことに注意してください。クリックイベントのイベントハンドラに指定しました。返される値は、fightcrunch関数によって受信されず、受信されません。

私は本当にこのプログラムを作ることであなたの楽しみを台無しにしたくはありませんが、あなたがあきらめたら、ここで修正された機能コードを見ることができます(それを見たくない場合はタブが右側にあります) :

                   <form> 
                        Player 1 
                        <select id="player1"> 
                         <option value="0" selected>Paper</option> 
                         <option value="1">Rock</option> 
                         <option value="2">Scissors</option> 
                        </select> 

                        Player 2 
                        <select id="player2"> 
                         <option value="0" selected>Paper</option> 
                         <option value="1">Rock</option> 
                         <option value="2">Scissors</option> 
                        </select> 

                        <input type="submit" id="fight" value="Fight"> 
                       </form> 

                       <div id="resultOutput"></div> 


                       <script> 
                        // create our $() shortcut function for easily retrieving elements by id 
                        var $ = function(id) { 
                         return document.getElementById(id); 
                        } 

                        //function executed on page load 
                        window.onload = function() { 

                         //clear any previous values 
                         document.forms[0].reset(); 

                         $("fight").onclick = fightCrunch; 
                        } 

                        var fightCrunch = function(){ 

                         var choices = ["Paper", "Rock", "Scissors"]; 
                         var player1 = $("player1").value; 
                         var player2 = $("player2").value; 
                         var result = ""; 
                         var diff = player1 - player2; 

                         if (!diff) 
                          result = "Both players chose " + choices[player1] + ", which results in a stalemate"; 
                         else if (diff == -1 || diff == 2) 
                          result = "Player 1's " + choices[player1] + " beats Player 2's " + choices[player2]; 
                         else 
                          result = "Player 2's " + choices[player2] + " beats Player 1's " + choices[player1]; 

                         $("resultOutput").innerHTML = result; 
                         return false; 
                        } 

                       </script> 

幸運!

編集:問題の全体の束がここにありますリターンとグローバル変数

var player1Choice, player2Choice; 

window.onload = function() { 

    //clear any previous values 
    document.forms[0].reset(); 

    //store value from player1Weapon radio choice with the getPlayer1Choice function 
    $("player1Submit").onclick = function() { 
     player1Choice = getPlayer1Choice(); 
    }; 

    //store value from player1Weapon radio choice with the getPlayer2Choice function 
    $("player2Submit").onclick = function() { 
     player2Choice = getPlayer2Choice(); 
    }; 

    //assign the fight button to run the fightCrunch function to determine the winner 
    $("fight").onclick = fightCrunch; 
} 

function getPlayer1Choice() { 
    //... 
    // return ...; 
} 

function getPlayer2Choice() { 
    //... 
    // return ...; 
} 
+0

私のクリックイベントハンドラでは、選択したラジオボタンの値を格納する関数を作成しようとしていました。 onclickで何かを実行させてからボタンやラベルの値を返し、fightCrunch関数でアクセスできるようにする最も簡単な方法は何ですか? 私がやっていることの大部分は、関数とreturn文で練習することです。私はそれが畳み込まれていることを知っています(そして、はい、あなたのコードは非常にうまく動作します、ありがとう!)、ここで余分ないくつかの割り当て要件を満たすことです。あなたのご意見をもう一度お聞かせください! – Benny

+0

私は参照してください。その場合、グローバル変数が必要になり、どこにそれらを格納します。もう一度編集します。 – caleb

+0

ねえ、このものは私にトンを助けた。本当にありがとう! – Benny

1

を使用します。最大のものはあると思われる:

  • あなたはplayer1Choiceは、文字列値(例えばplayer1Choice == "Rock")であると仮定すると、それは機能(player1Choice("Paper"))だと仮定するとの間で交互にされています。関数であると仮定しても、関数が実際に文字列の値("Paper")を返すように見えるとき、ブール値の真偽値(if (player1Choice("Paper")))を返すと仮定します。したがって、毎回player1Choiceまたはplayer1Choice("Paper")のいずれかをチェックすると、それは明らかに動作しないであろうtrueと評価されます。
+0

彼はjQueryを使用していません。彼は$をdocument.getElementByIdのエイリアスとして定義しました。 – caleb

+0

ああ、そうです。私はあまりにも急に読んでいます。 – nrabinowitz

関連する問題