结对项目黄金点游戏

黄金点游戏,共10名玩家,每位玩家输入一个0-100之间的有理数,不包括0和100,系统会算出这 10个点的平均值然后乘上0.168以此作为黄金点,提交的数字最接近黄金点的玩家得到1分,离黄金点最远的玩家-2分,其他玩家得0分。10轮游戏后分数最高的玩家获胜。课上老师带领我们进行了实践,我们发现,随着游戏的进行,黄巾点的值逐渐变小,且变化范围越来越小,通过编程,我们要实现这一功能。
结对项目:

1、根据两人的擅长,选择合适的语言进行项目的编写。最后我们决定使用java完成后台,配合jsp、js为游戏添加界面。

2、寻找源代码,在别人代码的基础上进行修改。原文博主:black_old_jack(同班)  原文链接:http://www.cnblogs.com/liupeixuan/p/7586444.html

3、读代码,决定修改内容,制定计划。因我们在原文代码上无法做出更优的修改,我们决定在原代码的基础上添加前端页面,使游戏能够在网页上运行。

原文代码:

  1 package GoldPoint;
  2  
  3 import java.util.ArrayList;
  4 import java.util.HashMap;
  5 import java.util.Iterator;
  6 import java.util.List;
  7 import java.util.Map;
  8 import java.util.Scanner;
  9  
 10 public class GoldPoint {
 11 public static void main(String[] args){
 12     GoldPoint gd=new GoldPoint();
 13     gd.goldPoint();
 14 }
 15 public void goldPoint(){
 16     HashMap<String,Double> inputMap=new HashMap<String,Double>();//存入输入分数
 17     HashMap<String,Double> scoreMap=new HashMap<String,Double>();//存入分数
 18     String name="";
 19     Double inputScore;
 20     int peopleNum;//参加的人数
 21     int time;//进行轮数
 22     Double sum=0.0;
 23     Double aver=0.0;
 24     Scanner scan=new Scanner(System.in); //参数对象是系统进来的流
 25     System.out.println("输入参加的人数:");
 26     peopleNum=scan.nextInt();
 27     System.out.println("输入需要进行几轮:");
 28     time=scan.nextInt();
 29     for(int i=0;i<peopleNum;i++){
 30         System.out.println("请输入第"+(i+1)+"个参加者的姓名:");
 31         name=scan.next();
 32         System.out.println("请输入第一轮的分数:");
 33         inputScore=scan.nextDouble();
 34          inputMap.put(name, inputScore);
 35          scoreMap.put(name,(double) 0);//初始化scoreMap
 36          sum+=inputScore;
 37     }
 38     aver=sum/peopleNum*0.618;
 39     System.out.println("aver="+aver);
 40     this.findWinner(inputMap, scoreMap, aver);
 41     this.show(scoreMap);
 42     System.out.println("第一轮结束");
 43     for(int i=0;i<time-1;i++){
 44             sum=0.0;
 45             System.out.println("请输入第"+(i+2)+"轮的分数:");
 46             Iterator iter = inputMap.entrySet().iterator();
 47             while (iter.hasNext()) {
 48                 Map.Entry entry0 = (Map.Entry) iter.next();
 49                 String key = (String) entry0.getKey();
 50                 System.out.println(key+"输入第"+(i+2)+"轮分数:");
 51                 Double score =scan.nextDouble();
 52                 inputMap.put(key, score);//替换掉以前的分数
 53                 sum+=score;
 54         }
 55             aver=sum/peopleNum*0.618;
 56             System.out.println("aver="+aver);
 57             this.findWinner(inputMap, scoreMap, aver);
 58             this.show(scoreMap);
 59         System.out.println("第"+(i+2)+"轮结束");
 60     }     System.out.println("游戏结束");
 61 }
 62 //找出每次分数最接近黄金点的 和最远的 最接近的加一分 最远的减一分 其余加零分(可能有相同的)
 63 public void findWinner(HashMap<String,Double> inputMap,HashMap<String,Double> scoreMap,Double aver){   
 64     Double temp;
 65     Double temp0;
 66     List<String> latest=new ArrayList<String>();
 67     List<String> farthest=new ArrayList<String>();
 68      
 69     Iterator iter = inputMap.entrySet().iterator();
 70     Map.Entry entry = (Map.Entry) iter.next();
 71     Double input = (Double) entry.getValue();
 72     String key0 = (String) entry.getKey();
 73     latest.add(key0);
 74     farthest.add(key0);
 75     //iter.hasNext();
 76     temp0=temp=Math.abs(aver-input);
 77     //遍历map
 78     while (iter.hasNext()) {   
 79         entry = (Map.Entry) iter.next();
 80         String key = (String) entry.getKey();
 81         input = (Double) entry.getValue();
 82         Double temp1=Math.abs(aver-input);
 83         if(temp>temp1){//寻找最近
 84             temp=temp1;
 85              latest.clear();
 86              latest.add(key);
 87         }else if(temp==temp1){
 88             latest.add(key);
 89         }
 90         if(temp0<temp1){//寻找最远
 91             temp0=temp1;
 92             farthest.clear();
 93             farthest.add(key);}
 94         else if(temp0==temp1){
 95             farthest.add(key);
 96         }
 97     }
 98     //实现加分
 99     iter = scoreMap.entrySet().iterator();
100     while (iter.hasNext()) {
101         Map.Entry entry0 = (Map.Entry) iter.next();
102         String key = (String) entry0.getKey();
103         Double score =(Double) entry0.getValue();
104         if(this.containList(key, latest)){
105             score=score+1;
106             scoreMap.put(key, score);
107             }
108         if(this.containList(key, farthest)){
109             score=score-1;
110             scoreMap.put(key, score);
111         }
112         }
113 }
114 public boolean containList(String str,List<String> list){
115     for(int i=0;i<list.size();i++){
116         if(str.equals(list.get(i))){
117             return true;
118         }
119     }
120     return false;
121 }
122 public void show(HashMap<String,Double> scoreMap){
123     System.out.println("得分情况:");
124     Iterator iter = scoreMap.entrySet().iterator();
125     while (iter.hasNext()) {
126         Map.Entry entry0 = (Map.Entry) iter.next();
127         String key = (String) entry0.getKey();
128         Double score =(Double) entry0.getValue();
129         System.out.println(key+":"+score);
130     }
131 }
132  
133 }

部分前端代码:

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title>黄金点游戏</title>
 8 <script type="text/javascript">
 9     function gamestart() {
10         window.open('MyJSP/inputname.jsp', '_blank')
11     }
12 </script>
13 </head>
14 <body style="background-color: dimgrey;">
15     <h1 align="center" style="color: white; padding-top: 280px;">黄金点游戏</h1>
16     <p
17         style="color: white; padding-left: 600px; padding-right: 620px; font-size: 18px; line-height: 30px; height: 120px; overflow: hidden;">
18         共10名玩家,每位玩家输入一个0-100之间的有理数,不包括0和100,系统会算出这
19         10个点的平均值然后乘上0.168以此作为黄金点,提交的数字最接近黄金点的玩家得到1分,离黄金点最远的玩家-2分,其他玩家得0分。5轮游戏后分数最高的玩家获胜。
20     </p>
21     <div style="padding-left: 920px;">
22         <button style="font-size: 18px;" class="gamestart"
23             onclick="gamestart()">开始</button>
24     </div>
25 </body>
26 </html>

首页

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!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>
<script src='WebContent/js/inputname.js', '_blank'>
    function name1() {
        name1 = prompt("请输入玩家1的姓名");
    }
</script>
</head>
<body style="background-color: dimgray;">
    <div id="firstline" style="padding-left: 500px; padding-top: 200px; height: 150px;">
        <input style="font-size: 25px" name="user1" type="button"onClick="name1()" value="玩家1"> 
        <input style="font-size: 25px; margin-left: 300px;" name="user2" type="button" onClick="name2()" value="玩家2"> 
        <input style="font-size: 25px; margin-left: 300px;" name="user3" type="button" onClick="name3()" value="玩家3" />
    </div>

    <div id="secondline" style="padding-left: 350px; height: 75px;">
        <input style="font-size: 25px" name="user4" type="button" onClick="name4()" value="玩家4" /> 
        <input style="font-size: 25px; margin-left: 980px;" name="user5" type="button" onClick="name5()" value="玩家5" />
    </div>
    <div id="nameend" style="padding-left: 865px; height: 75px;">
        <input style="font-size: 25px;" type="button" name="nameend" id="nameend" value="开始游戏" onclick="firstround()" />
    </div>
    <div id="thirdline" style="padding-left: 350px; height: 150px;">
        <input style="font-size: 25px" type="button" onClick="name6()" value="玩家6" /> 
        <input style="font-size: 25px; margin-left: 980px;" name="user7" type="button" onClick="name7()" value="玩家7" />

    </div>
    <div id="lastline" style="padding-left: 500px; height: 150px;">
        <input style="font-size: 25px" name="user8" type="button" onClick="name8()" value="玩家8" /> 
        <input style="font-size: 25px; margin-left: 300px;" name="user9" type="button" onClick="name9()" value="玩家9" /> 
        <input style="font-size: 25px; margin-left: 300px;" name="user10" type="button" onClick="name10()" value="玩家10" />
    </div>

</body>
</html>

目前状况,后台代码能够直接在控制台运行,我们对代码进行了一点优化,但没有本质的修改。页面构建完成,能够实现各界面之间的跳转,但是后台和界面并没有成功进行连接,我们会在后续的工作中完成这整个游戏。

原文地址:https://www.cnblogs.com/reverseAC/p/7643493.html