javascript 掷骰子2011/10/09
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>掷骰子</title>
</head>
<body>
<div style="background-color:yellow">
<form>
<label id="cap"></label><br/>
<label id="money"></label><br/>
选点:<input type="button" value="》》》大《《《" name="large" onclick="setLarge()">
<input type="button" value="》》》小《《《" name="little" onclick="setLittle()"><br/>
下注:<input type="text" name="zhu" size="21"><br/>
<input type="button" value=" 开盘 "onclick="writeResult()"/>
</form>
</div>
<hr/>
<div id="reslt"></div>
<script>
var yourNum;
var resultNum;
var allMoney=500;
writeInfo("掷骰子");
document.getElementById("money").innerHTML="你的现金为:"+allMoney;
//选择大
function setLarge()
{
yourNum=1;
}
//选择小
function setLittle()
{
yourNum=0;
}
//输出标题
function writeInfo(str)
{
str=str.fontsize(10);
str=str.fontcolor("red");
document.getElementById("cap").innerHTML=str;
}
//随机生成点可能的值为1、2、3、4、5、6
function getNumber()
{
var checkNum=Math.random()*6;
checkNum=Math.floor(checkNum)+1;
resultNum=checkNum;
}
//判断是否赢了
function isWinner()
{
var checker;
if(resultNum>=4)
checker=1;
else
checker=0;
if(checker==yourNum)
return true;
else
return false;
}
//输出结果
function writeResult()
{
var str="你选择的是";
var pay=eval(document.forms[0].zhu.value);
if(!(pay>0)){
alert("请下注!!");
document.forms[0].zhu.focus();
return;
}
getNumber();
if(yourNum==1)
str+="大";
else
str+="小";
str+=",结果为"+resultNum;
if(isWinner())
{
str+=",你赢了!";
str+=pay;
str=str.fontcolor("blue");
writeInfo("运气不错");
}
else
{
str+=",你输了!";
pay*=-1;
str+=pay;
str=str.fontcolor("red");
writeInfo("加油,刚把内!");
}
allMoney+=pay;
document.getElementById("reslt").innerHTML+=str.bold()+"<br>";
document.getElementById("money").innerHTML=("你的现金为:"+allMoney).fontcolor("red");
document.forms[0].zhu.value="";
}
</script>
</body>
</html>
结果演示: