正文

jsp实现剪子石头布小游戏(java剪刀石头布编程)满满干货

文章摘要

这篇文章描述了一个基于JavaServer Pages(JSR)的猜拳游戏应用。用户通过提交选项(“jiandao”、“shitou”或“bu”)参与游戏,而服务器端会随机生成一个选项进行比较。根据结果,服务器会更新玩家的胜负和平局统计,并将结果存储在数据库中。文章还提到了使用 strengthened digests 的安全措施以防止跨站脚本攻击。核心内容是展示一个简单的游戏逻辑和数据统计功能。

 <html> <head> <base href=”https://www.jb51.net/article/<%=basePath%>” > <title>My JSP ‘result.jsp’ starting page</title> <meta http-equiv=”pragma” content=”no-cache”> <meta http-equiv=”cache-control” content=”no-cache”> <meta http-equiv=”expires” content=”0″> <meta http-equiv=”keywords” content=”keyword1,keyword2,keyword3″> <meta http-equiv=”description” content=”This is my page”> <!– <link rel=”stylesheet” type=”text/css” href=”https://www.jb51.net/article/styles.css” rel=”external nofollow” rel=”external nofollow” > –> </head> <body> <% String pathnew=request.getContextPath(); //获取游戏参与者的选项值 String res=request.getParameter(“option”); String reslong=res+”.jpg”; //产生随机数进行匹配图片 int com=(int)Math.random()*3; String computer=String.valueOf(com); //computer=0 jiandao //computer=1 shiyou //computer=2 bu if(computer.equals(“0″)){ computer=”jiandao”; } else if(computer.equals(“1″)){ computer=”shitou”; } else{ computer=”bu”; } String computerlong=computer+”.jpg”; int win=0; int lost=0; int ping=0; Object objwin=session.getAttribute(“win”); Object objlost=session.getAttribute(“lost”); Object objping=session.getAttribute(“ping”); if(objwin==null){ session.setAttribute(“win”,String.valueOf(win)); } if(objlost==null){ session.setAttribute(“lost”,String.valueOf(lost)); } if(objping==null){ session.setAttribute(“ping”,String.valueOf(ping)); } %> <h3>结果是</h3> <!– 图片传值 –> 您出的是:<img alt=”” src=”https://www.jb51.net/article/<%=pathnew %>/images/<%=reslong %>”> <span style=”bold” color=”red”>vs</span> 电脑出的是:<img alt=”” src=”https://www.jb51.net/article/<%=pathnew %>/images/<%=computerlong %>”> <% //逻辑判断,session更新统计值, if(res.equals(computer)){ out.println(“平局!”); //session.setAttribute(“ping”,String.valueOf(Integer.valueOf((String)session.getAttribute(“ping”))+1)); session.setAttribute(“ping”,String.valueOf(Integer.valueOf((String)session.getAttribute(“ping”))+1)); } else if((res.equals(“jiandao”)&&computer.equals(“bu”))||(res.equals(“shitou”)&&computer.equals(“jiandao”))||(res.equals(“bu”)&&computer.equals(“shiyou”))){ out.println(“您赢了!”); //session.setAttribute(“win”,String.valueOf(Integer.valueOf((String)session.getAttribute(“win”))+1)); session.setAttribute(“win”,String.valueOf(Integer.valueOf((String)session.getAttribute(“win”))+1)); } else{ out.println(“您输了!”); session.setAttribute(“lost”,String.valueOf(Integer.valueOf((String)session.getAttribute(“lost”))+1)); //session.setAttribute(“lost”,String.valueOf(Integer.valueOf((String)session.getAttribute(“lost”))+1)); } %> <h3>统计结果,待写入数据库</h3> 您一共玩了<%=String.valueOf( Integer.valueOf((String)session.getAttribute(“win”))+Integer.valueOf((String)session.getAttribute(“lost”))+Integer.valueOf((String)session.getAttribute(“ping”)) )%>局<br/> 您赢了<%=String.valueOf( Integer.valueOf((String)session.getAttribute(“win”))) %>局<br/> 您输了<%=String.valueOf( Integer.valueOf((String)session.getAttribute(“lost”))) %>局<br/> 打平了<%=String.valueOf( Integer.valueOf((String)session.getAttribute(“ping”))) %>局<br/> </body> </html>