2017-08-16 66 views
0

我正在創建一個岩石紙剪刀遊戲,我使用的按鈕,用戶可以選擇要麼選擇岩石紙或剪刀,但我不知道如何比較用戶的答案和計算機的答案的結果找出誰贏了。如何在比較兩個不同的值後進行更改?

number = Math.floor(Math.random()*6)+1; 

function results(){ 
    switch(number){ 
     case 6: case 5: var a = document.getElementById("computer").innerHTML="Rock"; 
     break; 
     case 4: case 3: var b = document.getElementById("computer").innerHTML="Paper"; 
     break; 
     case 2: case 1: var c = document.getElementById("computer").innerHTML="Scissors"; 
     break; 
    } 
} 

function paper(){ 
    switch(true){ 
     case (number <= 6): var paper = document.getElementById("user").innerHTML="Paper"; 
     break; 
    } 
} 

function rock(){ 
    switch(true){ 
     case (number <= 6): var rock = document.getElementById("user").innerHTML="Rock"; 
     break; 
    } 
} 

function scissors(){ 
    switch(true){ 
     case (number <= 6): var rock = document.getElementById("user").innerHTML="Scissors"; 
     break; 
    } 
} 
+1

'開關(真){ 情況下(數<= 6):'< - 具有'if'的解決方案將更具可讀性和更短。 – zerkms

+0

[Rock,Paper,Scissors in JavaScript]可能的重複(https://stackoverflow.com/questions/17976883/rock-paper-scissors-in-javascript) – mscheker

+0

爲什麼你使用交換機作爲條件語句?而且,所有的函數都會創建局部變量,然後不做任何事情。 – Carcigenicate

回答

0

你只需要一個功能,這將觸發時,用戶點擊任何一個選項,然後在同樣的功能,你產生一種選擇計算機,然後根據他們找出贏家。檢查下面的例子:

HTML:

<div id="c"></div> 
<div id="u"></div> 
<div id="r"></div> 
// send parameter to function, 1 for Rock, 2 for Paper and 3 for Scissors 
<button onclick="startg(1)"> 
    Rock 
</button> 
<button onclick="startg(2)"> 
    Paper 
</button> 
<button onclick="startg(3)"> 
    Scissors 
</button> 

的Java腳本:

function startg(userChoice){ 

document.getElementById("u").innerHTML=""; 
document.getElementById("c").innerHTML=""; 
document.getElementById("r").innerHTML=""; 

var comNumber = Math.floor(Math.random()*3)+1; 

switch (userChoice){ 
    case 1 : { 
     document.getElementById("u").innerHTML="You Choosed : Rock"; 
     break; 
     } 
    case 2 : { 
     document.getElementById("u").innerHTML="You Choosed : Paper"; 
     break; 
     } 
    case 3 : { 
     document.getElementById("u").innerHTML="You Choosed : Scissors"; 
     break; 
    } 
} 

switch (comNumber){ 
    case 1 : { 
     document.getElementById("c").innerHTML="Compouter Choosed :Rock"; 
     break; 
    } 
    case 2 : { 
     document.getElementById("c").innerHTML="Compouter Choosed :Paper"; 
     break; 
    } 
    case 3 : { 
     document.getElementById("c").innerHTML="Compouter Choosed :Scissors"; 
     break; 
    } 
} 


    //Results : 

    if (userChoice == 1 && comNumber==2) 
     document.getElementById("r").innerHTML="Result : Computer Win!"; 
    else if (userChoice == 2 && comNumber==1) 
     document.getElementById("r").innerHTML="Result : You Win!"; 
    else if (userChoice == 1 && comNumber==3) 
     document.getElementById("r").innerHTML="Result : You Win!"; 
    else if (userChoice == 3 && comNumber==1) 
     document.getElementById("r").innerHTML="Result : Computer Win!"; 
    else if (userChoice == 2 && comNumber==3) 
     document.getElementById("r").innerHTML="Result : Computer Win!"; 
    else if (userChoice == 3 && comNumber==2) 
     document.getElementById("r").innerHTML="Result : You Win!"; 
    else if (userChoice == comNumber) 
     document.getElementById("r").innerHTML="Result : Draw"; 
} 

這裏你可以看到一個工作示例:https://jsfiddle.net/emilvr/wpkj6rw6/4/

相關問題