Pairing programming--The Golden dot game

Source: Internet
Author: User
Tags addgroup

This assignment belongs to the pair programming project, by me and Shirpen spent 2 days together to complete.

Start doing this pair programming, I first proposed and Shirpen in C language, but later saw the teacher to the details of the pair programming requirements, chose the last new Learning Java language, C language implementation of the visual interface is very difficult to abandon the C language. Because Java has many advantages, it is convenient to program the visual interface, just Shirpen a visual programming tool--windowbulider in Eclipse, so it is easy to implement a more friendly game interface.

With a convenient compilation environment must consider the requirements of the project analysis, my design reference to their classmate input and output, so that the game appears very big predictable, Shirpen said should not be in the game input data when the predictability, will each input data with encrypted input, with * display output, so as to ensure the data game fun, This kind of game in the heart stimulates the game player, attracts more people to play. Because we feel the fun of the game is that the player will be the result of the previous set to predict the number of other people will input and determine the number of secondary input (personally tried the game, and finally the number of people entered more and more small ...) ), is a game of psychological game, so we decided to design an unlimited number and number of golden point games.

We design the game interface, as the two ordinary science and engineering students, we try to streamline the game interface, by defining a JPasswordField as the player's encryption input, defined two buttons JButton respectively added click event for the input of this result and total, Defines a jtextarea used to display the results.

After designing the interface we do the specific coding operation, first of all is to show the results section: Define a string with regular expression to get the number of player input and display the result, define a double type of array of the number of input (easy to take absolute value), calculated to find winners and losers, calculate their score and deduction, Figure out that both the score and no points of the personnel score are displayed 0 points.

The last is the part that shows the total score: We're going to calculate the overall grade here once. For example: M personal playing, player number 1th won a lost 2 times so scored m-2-2 points. However, the result of the previous click event (which shows this result) is not saved, and to achieve the output it is necessary to store each result in a container and then output, so we are going to write some code to store this data in the event of displaying this result. First we think of the array but immediately I vetoed, because the array length is fixed, and the number of games we do not want to be fixed, like a bunch of people playing games, suddenly there is a person to go or someone else want to join, then this game can not play? So arrays do not meet the requirements, and arrays are similar to ArrayList, which can change the length, and the index position as the player can output the player's results. But Shirpen feel that since it is a person corresponding to a score, then this mapping relationship with HashMap to store better, so that a player (key) corresponds to a score (value). So we used the HashMap, each round of the contents of the change. Then in the event of displaying the total score, only the contents of the content can be printed.

Through this pair programming, deepened the understanding of cooperation, in the present society is a cooperative era, not a suitable for the era of Maverick. To know their own weaknesses and strengths, objective understanding of their own, pair programming exercise is a unified cooperation, must work together. In this pair programming, special thanks to Shirpen classmate in this cooperation of dedication, in some of my error analysis immediately corrected, at the same time Shirpen has a strong innovative ideas, thinking about the problem is comprehensive, in self-study Java learning very hard, with a very short time to learn a language, learning ability is very strong, Hope to be able to cooperate with him more in the future.

The boundless, must work together, more with any person to participate in the project, exercise their own team consciousness, refueling, towards the "source of the beginning of the era."

The source code is as follows:

ImportJava.awt.EventQueue;ImportJavax.swing.JFrame;ImportJavax.swing.JPanel;ImportJavax.swing.JPasswordField;ImportJavax.swing.border.EmptyBorder;Importjavax.swing.GroupLayout;Importjavax.swing.GroupLayout.Alignment;ImportJavax.swing.JLabel;Importjavax.swing.LayoutStyle.ComponentPlacement;ImportJavax.swing.JTextField;ImportJavax.swing.JButton;ImportJavax.swing.JTextArea;ImportJava.awt.event.MouseAdapter;Importjava.awt.event.MouseEvent;ImportJava.util.HashMap; Public classDfgsdextendsJFrame {PrivateJPanel ContentPane; PrivateJTextField TextField;  Public Static voidMain (string[] args) {Eventqueue.invokelater (NewRunnable () { Public voidrun () {Try{DFGSD frame=NewDfgsd (); Frame.setvisible (true); } Catch(Exception e) {e.printstacktrace ();    }            }        }); }    /*** Create the frame. */     PublicDfgsd () {setdefaultcloseoperation (jframe.exit_on_close); SetBounds (100, 100, 450, 300); ContentPane=NewJPanel (); Contentpane.setborder (NewEmptyborder (5, 5, 5, 5));                Setcontentpane (ContentPane); HashMap<Integer,Double> map=NewHashmap<integer,double>(); TextField=NewJPasswordField (); Textfield.setcolumns (10); JTextArea TextArea=NewJTextArea (); Textarea.setwrapstyleword (true); JLabel Lblnewlabel=NewJLabel ("\u8bf7\u8f93\u5165"); JButton Btnnewbutton=NewJButton ("\u67e5\u770b\u672c\u6b21\u7ed3\u679c"); Btnnewbutton.addmouselistener (NewMouseadapter () {@Override Public voidmouseclicked (MouseEvent e) {intsum = 0; intLength = 0; String result=Textfield.gettext (); String[] a= Result.split ("\\d");  for(inti = 0;i < a.length;i++) {Textarea.append (((I+1) + "number of inputs" +a[i])); Textarea.append ("\ n"); Sum=sum +Integer.parseint (A[i]); Length++; }                Doubleb = sum/(length) *0.618; Textarea.append ("Result is" +b); Textarea.append ("\ n"); Double[] Type =New Double[A.length]; intmax = 1,min = 1;  for(inti = 0;i < a.length;i++) {Type[i]=double.parsedouble (A[i]); }                DoubleMin = type[0]; DoubleMax =0f;  for(inti = 0;i < a.length;i++) {                    if(Max < Java.lang.Math.abs (type[i]-b) {Max= Java.lang.Math.abs (type[i]-b); Max= i + 1; }                    Else                        if(Min > Java.lang.Math.abs (type[i]-b)) {Min= Java.lang.Math.abs (type[i]-b); Min= i + 1; }} textarea.append ("The winner is:" +min+ "entered the number of" +type[min-1]+ "plus" +a.length+ "points"); Textarea.append ("\ n"); Textarea.append ("Loser is:" +max+ "entered the number of" +type[max-1]+ "Minus 2 points"); Textarea.append ("\ n"); Booleancontains =Map.containskey (max); if(contains) {Map.put (max, (Double) (Map.get (max)-2)); }Else{map.put (max, (Double)-2); }                BooleanContains2 =map.containskey (min); if(contains2) {map.put (min, (Double) (A.length) +map.get (min)); }Else{map.put (min, (Double) a.length);                }                }        }); JButton btnnewbutton_1=NewJButton ("\u67e5\u770b\u6700\u7ec8\u5f97\u5206"); Btnnewbutton_1.addmouselistener (NewMouseadapter () {@Override Public voidmouseclicked (MouseEvent e) { for(Integer Key:map.keySet ()) {Textarea.append (Key+ ": \ T" +Map.get (Key)); Textarea.append ("\ n"); } textarea.append ("Everyone else is 0 points, please keep trying");                }        }); Grouplayout Gl_contentpane=Newgrouplayout (ContentPane); Gl_contentpane.sethorizontalgroup (Gl_contentpane.createparallelgroup (alignment.leading). AddGro Up (Gl_contentpane.createsequentialgroup (). AddGroup (Gl_contentpane.createparallelgroup (Alignment.LEADIN G). AddGroup (Gl_contentpane.createsequentialgroup (). AddComponent (lblnew Label). Addpreferredgap (componentplacement.unrelated). AddComponent ( TextField, Grouplayout.preferred_size,301, Grouplayout.preferred_size)) . AddGroup (Gl_contentpane.createsequentialgroup (). Addgap (60). AddComponent (Btnnewbutton). Addgap (37). AddComponent (btnnewbutton_1)). AddComponent (TextArea, Grouplayout . Preferred_size,382, Grouplayout.preferred_size)) . Addcontainergap (42, Short.max_value))        ); Gl_contentpane.setverticalgroup (Gl_contentpane.createparallelgroup (alignment.leading). AddGroup (Gl_contentpane.createsequentialgroup (). AddGroup (Gl_contentpane.createparallelgroup (Alignment.BASELINE ). AddComponent (Lblnewlabel). AddComponent (TextField, Grouplayout.preferred_ SIZE, Grouplayout.default_size, Grouplayout.preferred_size)). Addpreferredgap (componentplacement.relate D). AddGroup (Gl_contentpane.createparallelgroup (alignment.baseline). Addcomponen T (Btnnewbutton). AddComponent (btnnewbutton_1)). Addgap (2). AddComponent (TextArea, Grouplayout.default_size,206, Short.max_value))        );    Contentpane.setlayout (Gl_contentpane); }}

Test Run Results:

Pair programming when learning photos:

Pairing programming--The Golden dot game

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.