Honestly... the previous
example is hard to call a game...
A game where you have to type is a bit... So I made it in a button format.
My approach was...
1. Create buttons for the user to use.
2. First, create global variables for the computer's random possibilities.
3. Create separate functions that run each time the user clicks, to execute the game.
|
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ko" lang="ko"> |
|
|
|
|
|
<head> |
|
|
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" /> |
|
|
|
|
|
<title> Math Object 2</title> |
|
|
|
|
|
|
|
|
<script type="text/javascript"> |
|
|
//<!CDATA[ |
|
|
|
|
pc=(Math.random()*3)+1; // a number between 0~3 |
|
|
pc=Math.floor(pc); |
|
|
|
|
|
|
|
|
function fight1() |
|
|
{ |
|
|
if(pc==1){ |
|
|
document.write("pc = Scissors — it's a tie.") ; |
|
|
} |
|
|
else if(pc==2){ |
|
|
document.write("pc = Rock — you lost.") ; |
|
|
} |
|
|
else if(pc==3){ |
|
|
document.write("pc = Paper — you won.") ; |
|
|
} |
|
|
else{ |
|
|
document.write("pc =...") ; |
|
|
} |
|
|
} |
|
|
|
|
|
// |
|
|
|
|
|
function fight2() |
|
|
{ |
|
|
if(pc==1){ |
|
|
document.write("pc = Scissors — you won.") ; |
|
|
} |
|
|
else if(pc==2){ |
|
|
document.write("pc = Rock — it's a tie.") ; |
|
|
} |
|
|
else if(pc==3){ |
|
|
document.write("pc = Paper — you lost.") ; |
|
|
} |
|
|
else{ |
|
|
document.write("pc =...") ; |
|
|
} |
|
|
} |
|
|
|
|
|
// |
|
|
|
|
|
function fight3() |
|
|
{ |
|
|
if(pc==1){ |
|
|
document.write("pc = Scissors — you lost.") ; |
|
|
} |
|
|
else if(pc==2){ |
|
|
document.write("pc = Rock — you won.") ; |
|
|
} |
|
|
else if(pc==3){ |
|
|
document.write("pc = Paper — it's a tie.") ; |
|
|
} |
|
|
else{ |
|
|
document.write("pc =...") ; |
|
|
} |
|
|
} |
|
|
|
|
|
//]]1]]]]> |
|
|
</script> |
|
|
|
|
|
</head> |
|
|
|
|
|
|
|
|
<body> |
|
|
<p><input type="button" value="Scissors" onclick="fight1()" /></p> |
|
|
<p><input type="button" value="Rock" onclick="fight2()" /></p> |
|
|
<p><input type="button" value="Paper" onclick="fight3()" /></p> |
|
|
</body> |
|
|
|
|
|
</html> |
|
|
|
