jsp实现剪子石头布小游戏

2023-12-05 0 270

本文实例为大家分享了jsp实现剪子石头布游戏的具体代码,供大家参考,具体内容如下

老师前两天除了一道小游戏的题目要大家做做,其实不太难,用了接近两个小时才做出来,先看一下题目。

问题描述:实现两个页面,第一个页面要求用图片或radio或select,第二个页面显示输赢的结果并把所有的结果保存输出。剪子石头布小游戏,跟常理一样,不必多说。

实现过程:使用form表单进行跳转处理,难点在图片传值这部分和数据统计部分,以下是代码:

游戏界面代码:

<html>
<head>
<base href=\”<%=basePath%>\” >

<title>欢迎来到剪刀石头布游戏大厅</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=\”styles.css\” rel=\”external nofollow\” rel=\”external nofollow\” >
–>
</head>

<body>
<!– 获得当前web project的名字 –>
<%String pa = request.getContextPath(); %>
<!– form表单页面跳转 –>
<form action=\”result.jsp\” method=\”post\”>
<input type=\”radio\” name=\”option\” value=\”jiandao\”><img alt=\”剪刀\” src=\”<%=pa%>/images/jiandao.jpg\”>
<input type=\”radio\” name=\”option\” value=\”shitou\”><img alt=石头\” src=\”<%=pa%>/images/shitou.jpg\”>
<input type=\”radio\”name=\”option\” value=\”bu\”><img alt=\”布\” src=\”<%=pa%>/images/bu.jpg\”>
<input type=\”submit\”value=\”确定\”/>
</form>
</body>
</html>

游戏界面:

jsp实现剪子石头布小游戏

游戏结果页面代码:

<html>
<head>
<base href=\”<%=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=\”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=\”<%=pathnew %>/images/<%=reslong %>\”>
<span style=\”bold\” color=\”red\”>vs</span>
电脑出的是:<img alt=\”\” src=\”<%=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>

游戏结果:

jsp实现剪子石头布小游戏

这个绝对没作弊,因为我输了5局哪!老师还提过下次要写个类似老虎机的游戏,估计在这个程序上做作弊就可以了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持悠久资源网。

您可能感兴趣的文章:

  • jsp+servlet实现猜数字游戏
  • JSP实现百万富翁猜数字游戏
  • 基于jsp的井字游戏实例
  • jsp网页实现贪吃蛇小游戏

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

悠久资源 JSP编程 jsp实现剪子石头布小游戏 https://www.u-9.cn/biancheng/jsp/95980.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务