增加了一个判断程序就乱了。。。
HTML code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>stone scissors cloth</title>
<script type="text/javascript" src="js/stone scissors cloth.js"></script>
<link rel="stylesheet" type="text/css" href="css/stone scissors cloth.css" />
</head>
<body>
<select id="choose">
<option value="">请选择</option>
<option value="0">石头</option>
<option value="1">剪刀</option>
<option value="2">布</option>
</select>
<img id="choosePreview" src=""></img>
<input type="button" value="确定" id="choosed" />
<input type="button" value="再来一局" id="chooseAgain" />
<img id="youChoosed" src=""></img>
<img id="cpuChoosed" src=""></img>
<h1 id="result"></h1>
</body>
</html>
JScript code
/**
* @author Administrator
*/
var myArr=new Array("img/stone.png","img/scissors.png","img/cloth.png")
window.onload=function()
{
var choose=document.getElementById("choose");
choose.selectedIndex=0;
choose.onclick=choosePreview;
document.getElementById("chooseAgain").onclick=chooseAgain;
}
//玩家选择预览
function choosePreview()
{
var choosePreview=document.getElementById("choosePreview");
choosePreview.src=myArr[this.value];
document.getElementById("choosed").onclick=choosed;
}
//确定按钮以及游戏结果
function choosed()
{
document.getElementById("youChoosed").src=document.getElementById("choosePreview").src;
do
{
document.getElementById("cpuChoosed").src=myArr[Math.floor(Math.random()*3)];
}
while(document.getElementById("youChoosed").src==document.getElementById("cpuChoosed").src)
if(document.getElementById("youChoosed").src=myArr[0])
{
if(document.getElementById("cpuChoosed").src=myArr[1])
{
document.getElementById("result").innerHTML="You win!";
}
else
{
document.getElementById("result").innerHTML="You lost!";
}
}
else if(document.getElementById("youChoosed").src=myArr[1])
{
if(document.getElementById("cpuChoosed").src=myArr[0])
{
document.getElementById("result").innerHTML="You lost!";
}
else
{
document.getElementById("result").innerHTML="You win!";
}
}
else if(document.getElementById("youChoosed").src=myArr[2])
{
if(document.getElementById("cpuChoosed").src=myArr[0])
{
document.getElementById("result").innerHTML="You win!";
}
else
{
document.getElementById("result").innerHTML="You lost!";
}
}
}
//清空画面 再来一局
function chooseAgain()
{
document.getElementById("choose").selectedIndex=0;
document.getElementById("choosePreview").src="";
document.getElementById("youChoosed").src="";
document.getElementById("cpuChoosed").src="";
}