文曲星上的猜数字游戏程序

来源:互联网 发布:seo网络优化是什么 编辑:程序博客网 时间:2024/04/28 10:23

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class GuessNumber
    extends JFrame {
  JTextField numberDisplay;
  JTextArea reasultDisplay;
  JButton button1;
  JButton button2;
  JButton button3;
  JButton button4;
  JButton button5;
  JButton button6;
  JButton button7;
  JButton button8;
  JButton button9;
  JButton button0;
  JButton btnOk;
  JButton btnHelp;
  JButton btnCancel;
  int[] gessArray;
  int[] rndNumber;
  String sstr;
  String reasultStr;
  int aNum, bNum;
  int n;

  public GuessNumber() {
    createComponents();
    layoutComponents();
    setTitle("猜数字");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pack();
    this.setVisible(true);
    createRandom();
    registerEventHander();

  }

  private void createComponents() {
    numberDisplay = new JTextField(4);
    reasultDisplay = new JTextArea();
    button1 = new JButton("1");
    button2 = new JButton("2");
    button3 = new JButton("3");
    button4 = new JButton("4");
    button5 = new JButton("5");
    button6 = new JButton("6");
    button7 = new JButton("7");
    button8 = new JButton("8");
    button9 = new JButton("9");
    button0 = new JButton("0");
    btnOk = new JButton("确定");
    btnHelp = new JButton("?");
    btnCancel = new JButton("清除");
    reasultStr = new String("");
    n = 0;
  }

  private void layoutComponents() {
    Container contentPane = this.getContentPane();
    contentPane.setLayout(new GridLayout(6, 1));
    //
    JScrollPane scrollPane = new JScrollPane(reasultDisplay);
    //
    JPanel ButtonPanel = new JPanel();
    ButtonPanel.add(button1);
    ButtonPanel.add(button2);
    ButtonPanel.add(button3);
    //
    JPanel ButtonPane2 = new JPanel();
    ButtonPane2.add(button4);
    ButtonPane2.add(button5);
    ButtonPane2.add(button6);
    //
    JPanel ButtonPane3 = new JPanel();
    ButtonPane3.add(button7);
    ButtonPane3.add(button8);
    ButtonPane3.add(button9);
    //
    JPanel BtnDis = new JPanel();
    BtnDis.add(button0);
    BtnDis.add(btnHelp);
    BtnDis.add(numberDisplay);
    //
    JPanel ButtonPane4 = new JPanel();
    ButtonPane4.add(btnOk);
    ButtonPane4.add(btnCancel);
    //
    contentPane.add(scrollPane);
    contentPane.add(ButtonPanel);
    contentPane.add(ButtonPane2);
    contentPane.add(ButtonPane3);
    contentPane.add(BtnDis);
    contentPane.add(ButtonPane4);

  }

  private class btnNumberActionEventHander
      implements ActionListener {
    boolean ClearDisplay;
    int i = 0;

    public void actionPerformed(ActionEvent e) {

      JButton btn = (JButton) e.getSource();
      if (ClearDisplay) {
        numberDisplay.setText("");
        ClearDisplay = false;
      }
      numberDisplay.setText(numberDisplay.getText() + btn.getText());

      String cc = numberDisplay.getText();
      i = cc.length();

      if (i > 4) {
        ClearDisplay = true;
        JOptionPane.showMessageDialog(null, "只能输入四个数字",
                                      "消息", JOptionPane.PLAIN_MESSAGE);

        numberDisplay.setText("");
      }

    }

  }

  private class btnCancelActionEventHander
      implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      numberDisplay.setText("");
    }
  }


  private class btnOkActionEventHander
      implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      aNum = 0;
      bNum = 0;

      String str = new String(numberDisplay.getText());
      int userGess = Integer.parseInt(str);
      gessArray = new int[4];

      n++;

      for (int i = 3; i >= 0; i--) {
        gessArray[i] = userGess % 10;
        userGess /= 10;
      }

      for (int i = 0; i <= 3; i++) {

        for (int j = 0; j <= 3; j++) {

          if (i == j) {
            if (gessArray[i] == rndNumber[j]) {
              aNum++;
            }
          }
          else {
            if (gessArray[i] == rndNumber[j]) {
              bNum++;
            }
          }

        }

      }
       //供测试用
      /*   String bb=str+"/n"+sstr+"/n"+aNum+"/n"+bNum;
           JOptionPane.showMessageDialog(null, bb,
                                           "消息", JOptionPane.PLAIN_MESSAGE);*/
      String aa;
      if (aNum == 4) {
        aa = "#" + n + "    " + str + "     " + "A:" + aNum + "     " + "B:" +
            bNum + "/n"
            + "You are right!" + "/n";
      }
      else {
        aa = "#" + n + "    " + str + "     " + "A:" + aNum + "     " + "B:" +
            bNum + "/n";

      }
      reasultStr += aa;
      reasultDisplay.setText(reasultStr);
      numberDisplay.setText("");

    }
  }

  private class btnHelpActionEventHander
      implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      String helpStr = "文曲星上的猜数字游戏 MagicGuess" +
          "/n" + "A:数字相同且位置相同 B:数字相同但位置不同" +
          "/n" + " e-mail:codedear@gmail.com" + "/n" +
          "/n" + "/u00A92007 codear";
      JOptionPane.showMessageDialog(null, helpStr,
                                    "帮助", JOptionPane.PLAIN_MESSAGE);
    }

  }


  private void registerEventHander() {
      btnNumberActionEventHander number = new btnNumberActionEventHander();
      button1.addActionListener(number);
      button2.addActionListener(number);
      button3.addActionListener(number);
      button4.addActionListener(number);
      button5.addActionListener(number);
      button6.addActionListener(number);
      button7.addActionListener(number);
      button8.addActionListener(number);
      button9.addActionListener(number);
      button0.addActionListener(number);

      btnCancelActionEventHander cancel = new btnCancelActionEventHander();

      btnCancel.addActionListener(cancel);

      btnOkActionEventHander ok = new btnOkActionEventHander();
      btnOk.addActionListener(ok);

      btnHelpActionEventHander help = new btnHelpActionEventHander();
      btnHelp.addActionListener(help);

    }


  private void createRandom() {
    rndNumber = new int[4];
    Set s = new HashSet();
    Random random = new Random();

    while (s.size() < 4) {
      Integer i = new Integer(random.nextInt(10));
      String x = i.toString();

      if (!s.contains(x)) {
        s.add(x);
      }

     // System.out.println(s.toString());

    }
    Iterator t = s.iterator();
    int ii = 0;
    while (t.hasNext() && ii <= 3) {

      String x = (String) (t.next());
      rndNumber[ii] = Integer.parseInt(x);
      ii++;
    }

    sstr = s.toString();

  }

  public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    GuessNumber guessNumber = new GuessNumber();
    guessNumber.setVisible(true);

  }

原创粉丝点击