本次做業屬於結對編程項目,由我和薛鵬飛用了2天時間共同完成。java
開始作這個結對編程前,一開始我提議和薛鵬飛用C語言寫,可是後來有看了老師對結對編程的細節要求,選擇了最後剛最新學習的Java語言,C語言實現可視化界面十分困難,放棄了C語言。因爲Java有不少優勢,對於可視化界面編程很方便,正好薛鵬飛在eclipse裏裝了一個可視化編程工具——windowbulider,這樣實現一個較爲友好的的遊戲界面就十分容易了。正則表達式
有了便捷的編譯環境必須考慮項目的需求分析,個人設計參考了其同窗輸入輸出,這樣對玩遊戲出現很大的可預測下,薛鵬飛說不該該要在遊戲輸入數據時出現可預測性,將每個輸入的數據用加密輸入,用*顯示輸出,這樣保證了數據遊戲樂趣,這樣的遊戲在內心上就刺激了遊戲玩家,吸引更多的人去玩。由於咱們感受這個遊戲的樂趣其實在於玩家會由上一組結果來預測其餘人將輸入的數並決定本次要輸入多大的數(親自試過這遊戲,最後你們輸入的數愈來愈小......),是一個心理博弈類的遊戲,因此咱們決定設計一個不限人數和次數的黃金點遊戲。編程
咱們設計遊戲界面,做爲兩個普通的理工科學生,咱們儘可能精簡遊戲的界面,經過定義了一個JPasswordField做爲玩家的加密輸入,定義了兩個按鈕JButton分別添加了單擊事件用做輸入本次結果和總成績,定義了一個JTextArea用來顯示結果。數組
設計完界面後咱們進行具體編碼操做,首先是顯示本次結果部分:定義一個字符串用正則表達式獲取玩家輸入的數並顯示結果,定義一個double類型的數組存輸入的數(方便取絕對值),通過計算找出勝利者和失敗者,算出他們得分和扣分,算出既沒有得分又沒扣分的人員得分均顯示0分。app
最後是顯示總分的部分:以前打算在這裏一次計算出總成績。例如:m我的在玩,1號玩家贏了一次輸了2次那麼得分爲m-2-2分。可是以前的單擊事件(顯示本次結果)獲得的結果沒有進行保存,而要達到輸出目的必需要把每次結果存放到一個容器裏而後在輸出,因此咱們要在顯示本次結果的事件裏在寫一些代碼用來存放本次數據。首先咱們想到數組但馬上就被我否決了,由於數組長度是固定的,而遊戲人數咱們不但願是固定的,就好像一幫人玩遊戲,忽然有我的走了或者有其餘人想加入了,那這遊戲就不能玩了?因此數組不知足要求,和數組功能相似的是arraylist,這個能夠改變長度,而且把索引位置看成玩家就能夠輸出該玩家的結果。但薛鵬飛感受既然是一我的對應了一個分數,那麼這種映射關係仍是用HashMap來存放比較好,這樣一個玩家(key)對應着一個得分(value)。因此咱們採用了HashMap,每輪都對裏面的內容進行修改。而後在顯示總分的事件裏只須要打印裏面的內容便可。eclipse
經過這次結對編程,加深了對合做的認識,在如今社會就是一個合做的時代,並非一個適合特立獨行的時代。要認識本身的劣勢和長處,客觀的認識本身,結對編程鍛鍊的是彼此統一的合做,必須一塊兒努力。在此次結對編程中,特別感謝薛鵬飛同窗在此次合做時的全力以赴,在一些我出現錯誤分析時馬上予以糾正,同時薛鵬飛有很強的創新思想,思考問題很全面,在自學Java學習方面很刻苦,用了很短的時間就學會了一門語言,學習能力很強,但願之後可以與他多多合做。ide
學海無涯,必須共同努力,多與任何人去參與項目,鍛鍊本身的團隊意識,加油,走向「源始時代」。工具
源代碼以下:學習
import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.border.EmptyBorder; import javax.swing.GroupLayout; import javax.swing.GroupLayout.Alignment; import javax.swing.JLabel; import javax.swing.LayoutStyle.ComponentPlacement; import javax.swing.JTextField; import javax.swing.JButton; import javax.swing.JTextArea; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.HashMap; public class dfgsd extends JFrame { private JPanel contentPane; private JTextField textField; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { dfgsd frame = new dfgsd(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public dfgsd() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); HashMap<Integer,Double> map=new HashMap<Integer,Double>(); textField = new JPasswordField(); textField.setColumns(10); JTextArea textArea = new JTextArea(); textArea.setWrapStyleWord(true); JLabel lblNewLabel = new JLabel("\u8BF7\u8F93\u5165"); JButton btnNewButton = new JButton("\u67E5\u770B\u672C\u6B21\u7ED3\u679C"); btnNewButton.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { int sum = 0; int length = 0; String result = textField.getText(); String[] a = result.split("\\D"); for(int i = 0;i < a.length;i++) { textArea.append(((i+1)+"輸入的數爲" + a[i])); textArea.append("\n"); sum =sum + Integer.parseInt(a[i]); length ++; } double b = sum/(length)*0.618; textArea.append("結果爲"+b); textArea.append("\n"); double[] type = new double[a.length]; int max = 1,min = 1; for(int i = 0;i < a.length;i++) { type[i] = Double.parseDouble(a[i]); } double Min = type[0]; double Max = 0f; for(int i = 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("獲勝者是:"+min+"輸入的數爲"+type[min-1]+"加"+a.length+"分"); textArea.append("\n"); textArea.append("失敗者是:"+max+"輸入的數爲"+type[max-1]+"減2分"); textArea.append("\n"); boolean contains = map.containsKey(max); if(contains) { map.put(max, (double) (map.get(max) - 2)); }else { map.put(max, (double) - 2); } boolean contains2 = map.containsKey(min); if(contains2) { map.put(min, (double) (a.length)+map.get(min)); }else { map.put(min, (double) a.length); } } }); JButton btnNewButton_1 = new JButton("\u67E5\u770B\u6700\u7EC8\u5F97\u5206"); btnNewButton_1.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { for (Integer Key : map.keySet()) { textArea.append( Key + ":\t" + map.get(Key)); textArea.append("\n"); } textArea.append("其餘人均爲0分,請繼續努力"); } }); GroupLayout gl_contentPane = new GroupLayout(contentPane); gl_contentPane.setHorizontalGroup( gl_contentPane.createParallelGroup(Alignment.LEADING) .addGroup(gl_contentPane.createSequentialGroup() .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING) .addGroup(gl_contentPane.createSequentialGroup() .addComponent(lblNewLabel) .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.RELATED) .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE) .addComponent(btnNewButton) .addComponent(btnNewButton_1)) .addGap(2) .addComponent(textArea, GroupLayout.DEFAULT_SIZE, 206, Short.MAX_VALUE)) ); contentPane.setLayout(gl_contentPane); } }
測試運行結果截圖:測試
結對編程學習時的照片: