0
我上的RPS遊戲的工作,和我有一點麻煩得到它在基層工作之前,我樣式。這是我的HTML:RPS遊戲應用程序無法正常工作
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="rpsfinal1.css">
<title>Shattered Template</title>
</head>
<body>
<div class="corners">
<h1 class="title">Rock Paper Scissors!</h1>
<p>Welcome to a Rock Paper Scissors web app!1
</p>
<script type="text/javascript" src="rps.js"></script>
<a href='linkhref.html' id='mylink'>Play Again</a>
<input type="button" value="Rock" onClick="rockChoice()">
<input type="button" value="Paper" onclick="paperChoice()">
<input type="button" value="Scissors" onClick="scissorsChoice()">
<p id="output"></p>
</div>
</body>
</html>
這裏是我的JS:
var compare = function(choice1,choice2) {
if (choice1 === "rock") {
if (choice2 === "rock") {
console.log("It is a tie! The computer chose rock!");
alert ("It is a tie!");
}
else if (choice2 === "paper") {
console.log ("Sorry, you loose. :(");
alert ("Sorry, you lose. :(The computer chose paper!");
}
else {
console.log ("You win!");
alert ("You win! The computer chose scissors!");
}
}
else if (choice1 === "paper") {
if (choice2 === "rock") {
console.log ("You win!");
alert ("You win! The computer chose rock!");
}
else if (choice2 === "paper") {
console.log ("It is a tie!");
alert ("It is a tie! The computer chose paper!");
}
else {
console.log("Sorry, you lose. :(");
alert ("Sorry, you lose. :(The computer chose scissors!");
}
}
else {
if (choice2 === "rock") {
console.log ("You win!");
alert ("You win! The computer chose rock!");
}
else if (choice2 === "paper") {
console.log ("Sorry, you lose. :(");
alert ("Sorry, you lose. :(The computer chose paper!");
}
else {
console.log ("It is a tie!");
alert ("It is a tie! The computer chose scissors!");
}
}
};
var rockChoice = function() {
var userChoice = "rock";
var computerChoice = Math.random();
console.log(computerChoice);
if (computerChoice >= 0 && computerChoice <= 0.33) {
computerChoice = "rock";
console.log(computerChoice);
}
else if (computerChoice >= 0.34 && computerChoice <= 0.66) {
computerChoice = "paper";
console.log(computerChoice);
}
else {
computerChoice = "scissors";
console.log(computerChoice);
}
compare(userChoice,computerChoice);
}
var paperChoice = function() {
var userChoice = "paper";
var computerChoice = Math.random();
console.log(computerChoice);
if (computerChoice >= 0 && computerChoice <= 0.33) {
computerChoice = "rock";
console.log(computerChoice);
}
else if (computerChoice >= 0.34 && computerChoice <= 0.66) {
computerChoice = "paper";
console.log(computerChoice);
}
else {
computerChoice = "scissors";
console.log(computerChoice);
}
compare(userChoice,computerChoice);
}
var scissorsChoice = function() {
var userChoice = "scissors";
var computerChoice = Math.random();
console.log(computerChoice);
if (computerChoice >= 0 && computerChoice <= 0.33) {
computerChoice = "rock";
console.log(computerChoice);
}
else if (computerChoice >= 0.34 && computerChoice <= 0.66) {
computerChoice = "paper";
console.log(computerChoice);
}
else {
computerChoice = "scissors";
console.log(computerChoice);
}
compare(userChoice,computerChoice);
}
我認爲這些代碼是不工作的原因是,我可能無法正確調用函數。請告訴我。 謝謝!
您在控制檯中遇到什麼特定錯誤,或者它不起作用? – Andy
通過將計算機猜測代碼本身放入一個函數中,可以使這更高效。還要注意,絕對沒有理由使用'var foo = function',因爲您可以使用'function foo'。 – h2ooooooo
這是事情,我沒有得到任何錯誤,我沒有得到任何回報。真奇怪... –