html+js 实现石头剪刀布 与电脑进行比赛

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>石头剪刀布</title>
</head>
<body>
<center>
<script>
    var l=new Array();
    l[0]="";
    l[1]="剪刀";
    l[2]="石头";
    l[3]="布";
    function run()
    {
        var t1=Math.round(Math.random()%3)+1;
        var s="";
        var t2;
        var str="";
        for(var i=1;i<=3;i++)
        {
            s="p"+i;
            if(document.getElementById(s).checked==true)
            {
                t2=i;break;
            }
        }   
        if(t1==t2)
        {
            str="机器人:"+l[t1]+";您的选择:"+l[t2]+";平局";
        }
        else
        {
            if(t1==1&&t2==2) str="机器人:"+l[t1]+";您的选择:"+l[t2]+";你赢了";
            if(t1==1&&t2==3) str="机器人:"+l[t1]+";您的选择:"+l[t2]+";机器人赢了";
            if(t1==2&&t2==1) str="机器人:"+l[t1]+";您的选择:"+l[t2]+";机器人赢了";
            if(t1==2&&t2==3) str="机器人:"+l[t1]+";您的选择:"+l[t2]+";你赢了";
            if(t1==3&&t2==1) str="机器人:"+l[t1]+";您的选择:"+l[t2]+";你赢了";
            if(t1==3&&t2==2) str="机器人:"+l[t1]+";您的选择:"+l[t2]+";机器人赢了";
         }
        var pp=document.createElement("p");
        var pp_add=document.createTextNode(str);
        pp.appendChild(pp_add);
        var p_position=document.getElementById("pp1");
        p_position.appendChild(pp);

    }
</script>
<h2 align="center">石头剪刀布人机大战</h2>
<br>
您的选择:
<input type="radio" id="p1" name="war">剪刀
<input type="radio" id="p2" name="war">石头
<input type="radio" id="p3" name="war">布
<button onClick="run()">确定</button>
<br>
<br>
<p id="pp1">
</p>
</center>
</body>
</html>

0

Leave a Reply

Your email address will not be published.