• Topic Introduction
The Golden Dot game is a digital mini-game whose game rules are:
n students (n usually greater than ten), each writing a rational number between 0~100 ( excluding 0 or three ), to the referee, the referee calculates the average of all the numbers, and then times the 0.618(so-called golden partition constant) , the G value is obtained. The number of the submitted nearest to G(absolute value) of the students get n points, the farthest from the G students get-2 points, the other students get 0 points. after a few days of playing, we found some interesting phenomena, such as the golden dots moving down gradually.
Now please according to the rules of the game, make up a can play with a number of small game program, the requirements are as follows:
1, this job belongs to the pair programming project, must be completed by two people, and will be sent to the blog, respectively, the operation of the source code submitted to the codeing system;
2, if possible to use the C/s or b/n as much as possible, that is, using the server to receive and process all the players submitted by the number, and the results back to the players, players can be submitted by the client number;
3, if the use of single-machine approach, it is necessary to provide users with convenient input interface;
4, the game can run at least 10 rounds at a time, and can keep the results of the rounds.
• Requirements analysis and code implementation
This project requires that data be obtained from n users, the difference is obtained by the G-point operation, and the difference between the maximum and the minimum is given, and the game is ended and the result is output after the preset number of rounds. The idea is clear and broadly divided into the following steps:
1. Get Data from N users
Since this program is implemented in standalone form, all values except real players are randomly generated by the system, and the method used is math.random () in the Java class Library, which randomly generates a double-precision decimal between 0.0 and 1.0, with the following code:
/**@author*/protecteddouble[] rand () { Double New Double [One]; for (int i=0;i<10;i++) { Number[i]=math.random () *100; } number[] = 0; return Number ;}
2. Get the G value
The G-value is the key data for the operation of this program, and the algorithm provides all users with the average of the numbers and 0.618 of the flight, the code is as follows:
/*** Get g value *@authorEric*/ protected DoubleGet_average (Double[] number) { Doublesum = 0; Doubleaverage; for(inti=0;i<11;i++) {sum+=Number[i]; } Average= SUM/11; Average= Average * 0.618; returnaverage; }
3. Get the nearest value and farthest from the G point in the user data
That is, to obtain the winner data to save the subscript and the loser data to save the subscript, the first call to get the G-point method to get the value of G, and then subtract all the data from the absolute values to get the difference array, and finally the difference in the array of each value and the G value of the comparison, to save the difference between the largest and
/*** Judging and returning two extremes *@return */ Private int[] Judge (Double[] num) { DoubleAve =get_average (num); int[] End =New int[2]; DoubleMax; Doublemin; Double[] Ju =New Double[11]; for(inti = 0;i<11;i++) {Ju[i]= Math.Abs (Num[i]-Ave); } Max=ju[0]; Min=ju[0]; end[0]=0;//Save distance nearend[1]=0;//Save Distance far for(inti=1;i<11;i++) { if(max<Ju[i]) {end[1]=i; } if(min>Ju[i]) {end[0] =i; } } returnend; }
4. Duplicate data processing
After winning the winners and losers, we found that if the winner is more than one, the loser is more than one, two positions have more than one person occupy, then the above code can not meet the requirements, so to do data duplication, get the maximum and minimum, back to the original distance data comparison, The data for the maximum and minimum values is subscript, the code is as follows:
/*** Digital Repeat processing, to obtain the close and distance of the same person's subscript *@authorEric*/ protected int[] Gethighscore (Double[] number) { int[] End; int[] Highscore =New int[10]; End=judge (number); intJ =0; for(inti=0;i<number.length;i++) { if(Number[i] = = Number[end[0]]) {highscore[++J] =i; }} highscore[0] = j;//valid quantity is saved in the first array returnHighscore; }
/*** Digital Repeat processing, to obtain the distance from the same person's subscript *@authorEric*/ protected int[] Getlowscore (Double[] number) { int[] End; int[] Lowscore =New int[10]; End=judge (number); intJ =0; for(inti=0;i<number.length;i++) { if(Number[i] = = number[end[1]]) {lowscore[++J] =i; }} lowscore[0] = j;//valid quantity is saved in the first array returnLowscore; }
5. Interface Design
The interface Editor used for this project is SWT, which does not go through the controls, but the properties of some controls are interesting, as follows:
SWT. Center more for text settings, text centered, visible in text,label, Clabel
Swt. Wrap text Box properties, line Wrap
Swt. BORDER Control Display Border
Swt. Read_Only Text Read-only
Filllayout: Layout type, full space
Swt. Horizontal Landscape layout
Swt. VERTICAL Portrait layout
The window is implemented as follows:
6.Java pop-up window:
Joptionpane.showmessagedialog (NULL, "message")//Prompt message box
Joptionpane.showinputdialog ("message"); Input box
Error Handling in input box
It turns out that if the input window to obtain the data is empty, the system will error, in order to ensure smooth operation of the system, error handling is very important. In this way, we use two different input boxes to collect the data until the qualified information is recycled, and the code is as follows.
Maxxx = Joptionpane.showinputdialog ("Please enter the number of innings you wish to make"); while (Maxxx.equals ("")) { = Joptionpane.showinputdialog ("You have not entered the number of rounds you want to make, please re-enter"); = Integer.parseint (maxxx);
• Run the show
1. Game Rules Tips
2. User Operation Guide Tips
3. Enter a preset game for the number of rounds
4. Game Initial Interface
5. Run the Game interface
6. Enter an error
7. End of game results show
Golden Dot Game