2015-06-20 66 views
0

這是我的第一款遊戲和Rock Paper Scissors遊戲的代碼,其中CodeAcademy進行了一些調整。我一直困住了兩個小時,試圖弄清楚爲什麼它會一直持續下去。總是一個TIE! Rock Paper Scissors遊戲

這是我的HTML

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8" /> 
    <title>Rock Paper Scissors</title> 
</head> 

<body> 
<form> 
Rock 
<input type="checkbox" id="rock"> 
Paper 
<input type="checkbox" id="paper"> 
Scissors 
<input type="checkbox" id="scissors"> 
<button onclick="startGame()">Start<button> 
</form> 

<script src="userInt.js"></script> 
</body> 

</html> 

JavaScript代碼:

function userChoice(){ 
var rock = document.getElementById("rock").checked; 
var scissors = document.getElementById("scissors").checked; 
var paper = document.getElementById("paper").checked; 

} 
function computerChoice(){ 
var computerChoice = Math.random(); 
if (computerChoice < 0.34) { 
computerChoice = "rock"; 

} else if(computerChoice <= 0.67) { 
computerChoice = "paper"; 

} else { 
computerChoice = "scissors"; 
} 

} 
function compare(userChoice, computerChoice){ 
if (userChoice === computerChoice){ 
    return "The result is a tie!"; 

} 
if (userChoice ==="rock"){ 

    if(computerChoice ==="scissors"){ 
     return "rock wins"; 
    }else{ 
     return "paper wins"; 
    } 
} 
    if(userChoice ==="paper"){ 

     if (computerChoice ==="scissors"){ 
      return "paper wins" 
     }else{ 
      return "scissors wins" 
     } 
    } 

    if(userChoice ==="scissors"){ 

     if (computerChoice ==="rock"){ 
      return "rock wins" 
     }else{ 
      return "scissors wins" 
     } 
    } 
} 

function startGame(){ 
var getUserChoice = userChoice(); 
var getComputerChoice = computerChoice(); 
var endGame = compare(getUserChoice, getComputerChoice); 
alert(endGame) 
} 

感謝您的支持!

回答

1

你在你的代碼2個問題:

  1. userChoice返回任何(undefined
  2. computerChoice返回任何(undefined

function userChoice() { 
    var rock = document.getElementById("rock").checked; 
    var scissors = document.getElementById("scissors").checked; 
    var paper = document.getElementById("paper").checked; 

    if (rock) { 
     return 'rock'; 
    } 

    if (scissors) { 
     return 'scissors'; 
    } 

    if (paper) { 
     return 'paper'; 
    } 

} 

function computerChoice() { 
    var computerChoice = Math.random(); 
    if (computerChoice < 0.34) { 
     computerChoice = "rock"; 

    } else if (computerChoice <= 0.67) { 
     computerChoice = "paper"; 

    } else { 
     computerChoice = "scissors"; 
    } 

    return computerChoice; 

} 

function compare(userChoice, computerChoice) { 
    if (userChoice === computerChoice) { 
     return "The result is a tie!"; 

    } 
    if (userChoice === "rock") { 

     if (computerChoice === "scissors") { 
      return "rock wins"; 
     } else { 
      return "paper wins"; 
     } 
    } 
    if (userChoice === "paper") { 

     if (computerChoice === "scissors") { 
      return "paper wins" 
     } else { 
      return "scissors wins" 
     } 
    } 

    if (userChoice === "scissors") { 

     if (computerChoice === "rock") { 
      return "rock wins" 
     } else { 
      return "scissors wins" 
     } 
    } 
} 

function startGame() { 
    var getUserChoice = userChoice(); 
    console.log(getUserChoice); 
    var getComputerChoice = computerChoice(); 
    console.log(getComputerChoice); 
    var endGame = compare(getUserChoice, getComputerChoice); 
    alert(endGame); 
} 

Working demo on JSFiddle.

1

您的userChoice()和computerChoice()不返回任何內容。

function userChoice() { 
    var choice; 
    ['rock', 'scissors', 'paper'].some(function (e) { 
     if (document.getElementById(e).checked) { 
      choice = e; 
      return true; 
     } 
    }); 
    return choice; 
} 

function computerChoice() { 
    var computerChoice = Math.random(); 
    if (computerChoice < 0.34) { 
     computerChoice = "rock"; 
    } else if (computerChoice <= 0.67) { 
     computerChoice = "paper"; 
    } else { 
     computerChoice = "scissors"; 
    } 
    return computerChoice; 
} 
相關問題