//网页游戏:猜拳
<!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=GB18030">
<title>Insert title here</title>
<style type="text/css">
input{
width:150px;
}
</style>
<script type="text/javascript">
window.onload = function(){
//获取4个按钮和两个输入框
var stone = document.getElementById("stone");
var cut = document.getElementById("cut");
var cloth = document.getElementById("cloth");
var mine = document.getElementById("mine");
var guess = document.getElementById("guess");
var computer = document.getElementById("computer");
//当点击按钮时,把按钮的value赋值给mine的value
function click(){
mine.value = this.value;
}
stone.onclick = click;
cut.onclick = click;
cloth.onclick = click;
//生成一个随机数, 1代表石头, 2 代表剪刀 3布
guess.onclick = function(){
var num = Math.floor(Math.random()* 3 + 1);
if(num == 1)
computer.value = "石头";
else if(num == 2)
computer.value = "剪刀";
else if(num == 3)
computer.value = "布";
var mineValue = mine.value;
var computerValue = computer.value;
if(mineValue == computerValue)
alert("平局");
else if(mineValue == "石头")
alert(computerValue == "剪刀" ? "赢了" : "输了");
else if(mineValue == "剪刀")
alert(computerValue == "布" ? "赢了" : "输了");
else if(mineValue == "布")
alert(computerValue == "石头" ? "赢了" : "输了");
mine.value ="";
computer.value ="";
}
}
</script>
</head>
<body>
<input type="button" value="石头" id="stone">
<input type="button" value="剪刀" id="cut">
<input type="button" value="布" id="cloth"> <br><br>
您出的拳是:<input id="mine"><br><br>
<input type="button" value="猜猜猜" id="guess"><br><br>
电脑出的拳是:<input id="computer">
</body>
</html>
javascript 网页游戏:猜拳
最新推荐文章于 2022-03-13 23:15:32 发布