標籤:猜數字 遊戲 java 圖形介面
遊戲規則:
通常由兩個人玩,一方出數字,一方猜。出數位人要想好一個沒有重複數位4位元,不能讓猜的人知道。猜的人就可以開始猜。每猜一個數字,出數者就要根據這個數字給出幾A幾B,其中A前面的數字表示數字正確位置也正確的數的個數,而B前的數字表示數字正確而位置不對的數的個數。 如正確答案為 5234,而猜的人猜 5346,則是 1A2B,其中有一個5的位置對了,記為1A,而3和4這兩個數字對了,而位置沒對,因此記為 2B,合起來就是 1A2B。
遊戲截屏:
Run.java:
package xjj.java.GuessNumber2;public class Run {public static void main(String[] args) {JGuessGame g=new JGuessGame();g.str=GuessNumb.getNumber();//得到隨機的四位元}}
GuessNumb.java:
package xjj.java.GuessNumber2;public class GuessNumb {public static String getNumber(){//隨機產生四位元 char[] ch=new char[4];for(int i=0;i<ch.length;i++){ch[i]=(char) ((int)(Math.random()*10)+'0');}//System.out.println(ch);String str=new String(ch);System.out.println(str);return str;}}
JGuessGame.java:
package xjj.java.GuessNumber2;import javax.swing.*;import java.awt.Button;import java.awt.Color;import java.awt.Dialog;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.Font;import java.awt.Frame;import java.awt.GridLayout;import java.awt.JobAttributes;import java.awt.Label;import java.awt.TextArea;import java.awt.TextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseListener;public class JGuessGame extends JFrame implements ActionListener{String string="\tGuess\tResult";int count=1;String str;JTextField tfd;JTextArea tar;JButton btn;public JGuessGame(){super("Guess Game !");//用JFrame類的構造方法設定標題this.setDefaultCloseOperation(EXIT_ON_CLOSE);//設定叉關閉功能this.setResizable(false);//控制架構能否改變大小Dimension dim=this.getToolkit().getScreenSize();//擷取螢幕解析度this.setBounds(dim.width/3, dim.height/5, dim.width/3, 2*dim.height/3);//設定架構大小與位置this.setBackground(Color.lightGray);//設定架構背景顏色this.getContentPane().setBackground(Color.lightGray);this.getContentPane().setLayout(new FlowLayout());//設定布局類型JPanel p=new JPanel();//添加面板p.setBackground(Color.lightGray);p.add(new JLabel("Input : "));btn=new JButton("確定");//設定按鈕tfd=new JTextField(20);//設定編輯框p.add(tfd);//向面板添加按鈕和編輯框p.add(btn);this.getContentPane().add(p);//向架構添加面板tar=new JTextArea(20,20);//添加文本域tar.setBackground(Color.lightGray);this.getContentPane().add(tar);tar.setEditable(false);//設定文本域為不可編輯btn.addActionListener(this);//監聽按鈕addMyMenu();//添加菜單this.setVisible(true);//顯示架構}private void addMyMenu() {// TODOJMenuBar menuBar =new JMenuBar();//建立功能表列this.setJMenuBar(menuBar);//添加功能表列String menuStrs[]={"Game","Help"};JMenu[] menu =new JMenu[menuStrs.length];//建立菜單for(int i=0;i<menuStrs.length;i++){menu[i]=new JMenu(menuStrs[i]);menuBar.add(menu[i]);}JMenuItem menuItemView = new JMenuItem("玩法");//建立功能表項目JMenuItem menuItemExit = new JMenuItem("退出");JMenuItem menuItemNew = new JMenuItem("新遊戲");JMenuItem menuItemPase = new JMenuItem("暫停");//JMenuItem menuItemBook = new JMenuItem("熱門排行榜");menu[0].add(menuItemNew) ;menu[0].add(menuItemPase) ;//menu[0].add(menuItemBook) ;menu[0].addSeparator();menu[1].add(menuItemView);menuItemView.setActionCommand("View");menuItemPase.setActionCommand("Pase");menuItemNew.setActionCommand("New");menuItemExit.setActionCommand("Exit");menu[0].add(menuItemExit) ;menuItemView.addActionListener(this);//對功能表項目進行監聽menuItemPase.addActionListener(this);menuItemNew.addActionListener(this);menuItemExit.addActionListener(this);}public String getTextField(){return tfd.getText();}public void actionPerformed(ActionEvent e) {if(e.getSource()==btn){try {//監聽輸入 裡是否儲存不是數位字元int x = Integer.parseInt(tfd.getText());} catch (NumberFormatException e1) {JOptionPane.showMessageDialog(this, "請輸入一個四位元 ! ! !");tfd.setText("");return ;}if(tfd.getText().length()!=4){//監聽輸入的是否為四為數的數JOptionPane.showMessageDialog(this, "請輸入一個四位元 ! ! !");tfd.setText("");return ;}String strresult=Result.getResult(tfd.getText(), str);//得到結果string=string+"\n"+count+"\t"+tfd.getText()+"\t"+strresult;//將結果處理,並輸出到文本域tar.setText(string);tfd.setText("");if(strresult.charAt(0)=='4'&&strresult.charAt(2)=='4'){//猜對,遊戲結束System.out.println("congratulation");JOptionPane.showMessageDialog(this, "congratulation ! 小JJ萬歲 !");tfd.setEditable(false);}if(count==20){//步數耗盡,遊戲結束JOptionPane.showMessageDialog(this, "Game Over ! You Fail !");tfd.setEditable(false);//不能對文字框繼續編輯}count++;}if(e.getSource() instanceof JMenuItem &&e.getActionCommand().equalsIgnoreCase("exit")){System.exit(0);//對按下菜單中的退出項做出應答}if(e.getSource() instanceof JMenuItem &&e.getActionCommand().equalsIgnoreCase("new")){string="\tGuess\tResult";//對按下菜單中的新遊戲項做出應答tfd.setEditable(true);tar.setText("");tfd.setText("");count=1;this.str=GuessNumb.getNumber();}if(e.getSource() instanceof JMenuItem &&e.getActionCommand().equalsIgnoreCase("pase")){JOptionPane.showMessageDialog(this, "點擊‘確定’繼續遊戲 !!!");}if(e.getSource() instanceof JMenuItem &&e.getActionCommand().equalsIgnoreCase("view")){JOptionPane.showMessageDialog(this, "1、輸入一個四位元\n2、根據顯示的幾A幾B進行下一次輸入(A前面數字表示位置正確的數的個數,而B前面的數字表示數字正確而位置不對的數的個數)\n3、直到顯示4A4B時,遊戲結束。\n4、20次內沒得到正確結果,遊戲也結束,你輸了!");}}}
Result.java:
package xjj.java.GuessNumber2;public class Result {public static String getResult(String str1,String str2) {//將猜的與原答案進行比較,得到提示int a=0,b=0;for(int i=0;i<str1.length();i++){//位置相同且數相同的 數的個數if(str1.charAt(i)==str2.charAt(i)){b++;}}for(int i=0;i<str1.length();i++){for(int j=0;j<str2.length();j++){//數相同的數的個數if(str1.charAt(i)==str2.charAt(j)){a++;break;}}}System.out.println(a+" "+b);return a+"A"+b+"B";//返回結果}}
初次用java做圖形介面,還有很多不足的地方,容我慢慢改進哈!
猜數字遊戲(java)