import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JComboBox; import javax.swing.DefaultComboBoxModel; import java.awt.Font; import java.awt.Color; import javax.swing.JLabel; import java.awt.Component; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.Random; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.swing.JTextField; public class GuessNumber extends JFrame { private static final long serialVersionUID = 1L; private JPanel contentPane; private static final Random random = new Random(); private String randomNumber; private int times; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { GuessNumber frame = new GuessNumber(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } private JComboBox comboBox; private JLabel label; private JButton btnStart; private JTextField textField; private JButton btnSwitchLanguage; private JLabel additionalLabel; private JButton btnEnter; private JLabel giAgiBJLabel; /** * Create the frame. */ public GuessNumber() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(400, 100, 450, 300); setResizable(false); setTitle("Guess Number"); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setLayout(null); setContentPane(contentPane); comboBox = new JComboBox(); comboBox.setForeground(Color.BLUE); comboBox.setFont(new Font("微軟正黑體", Font.BOLD, 16)); comboBox.setModel(new DefaultComboBoxModel(new String[] {"English", "\u4E2D\u6587"})); comboBox.setBounds(150, 125, 150, 35); contentPane.add(comboBox); label = new JLabel(); setLabel(); contentPane.add(label); btnStart = new JButton("Start"); btnStart.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { start(); } }); btnStart.setForeground(Color.RED); btnStart.setFont(new Font("Century", Font.BOLD, 14)); btnStart.setBounds(180, 190, 90, 30); contentPane.add(btnStart); textField = new JTextField(); textField.setFont(new Font("Tahoma", Font.BOLD, 18)); textField.setBounds(170, 120, 120, 35); textField.setVisible(false); textField.addKeyListener(enterListener); contentPane.add(textField); btnSwitchLanguage = new JButton("Switch Language"); btnSwitchLanguage.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { switchLanguage(); } }); btnSwitchLanguage.setFont(new Font("Sylfaen", Font.PLAIN, 10)); btnSwitchLanguage.setBounds(328, 239, 110, 23); btnSwitchLanguage.setVisible(false); contentPane.add(btnSwitchLanguage); additionalLabel = new JLabel("without duplicate digits"); additionalLabel.setFont(new Font("Kartika", Font.BOLD, 16)); additionalLabel.setForeground(new Color(153, 102, 0)); additionalLabel.setAlignmentX(Component.CENTER_ALIGNMENT); additionalLabel.setBounds(130, 65, 300, 35); additionalLabel.setVisible(false); contentPane.add(additionalLabel); btnEnter = new JButton("Enter"); btnEnter.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { enter(); } }); btnEnter.addKeyListener(enterListener); btnEnter.setBackground(new Color(173, 255, 47)); btnEnter.setFont(new Font("新細明體", Font.BOLD, 12)); btnEnter.setForeground(Color.BLUE); btnEnter.setBounds(310, 125, 69, 23); btnEnter.setVisible(false); contentPane.add(btnEnter); giAgiBJLabel = new JLabel(""); giAgiBJLabel.setForeground(new Color(0, 0, 255)); giAgiBJLabel.setAlignmentX(Component.CENTER_ALIGNMENT); giAgiBJLabel.setFont(new Font("Comic Sans MS", Font.PLAIN, 28)); giAgiBJLabel.setBounds(190, 170, 130, 30); contentPane.add(giAgiBJLabel); } public KeyListener enterListener = new KeyListener() { @Override public void keyTyped(KeyEvent arg0) {} @Override public void keyReleased(KeyEvent arg0) {} @Override public void keyPressed(KeyEvent arg0) { int key = arg0.getKeyCode(); if (key == KeyEvent.VK_ENTER){ enter(); } } }; private void enter(){ times++; boolean hasNoInputError = checkInputError(); if (hasNoInputError){ if (textField.getText().equals(randomNumber)){ if (comboBox.getSelectedIndex() == 1) JOptionPane.showMessageDialog(null, "恭喜你答對了。你一共猜了"+times+"次"); else JOptionPane.showMessageDialog(null, "Congratulations! Your answer is correct. You have guessed " + times + " times."); switchLanguage(); } else { setGiAGiB(); } } } private void switchLanguage(){ btnEnter.setVisible(false); comboBox.setVisible(true); btnStart.setVisible(true); textField.setText(""); textField.setVisible(false); setLabel(); additionalLabel.setVisible(false); btnSwitchLanguage.setVisible(false); giAgiBJLabel.setVisible(false); } private boolean checkInputError(){ String inputText = textField.getText().trim(); if (inputText.length()!=4){ if (comboBox.getSelectedIndex() == 1) JOptionPane.showMessageDialog(null, "請輸入四位數正整數"); else JOptionPane.showMessageDialog(null, "Please enter a 4-digit integer"); return false; } Pattern pattern = Pattern.compile("\\d\\d\\d\\d"); Matcher matcher = pattern.matcher(inputText); if (!matcher.find()){ if (comboBox.getSelectedIndex() == 1) JOptionPane.showMessageDialog(null, "請輸入有效的正整數"); else JOptionPane.showMessageDialog(null, "Please enter a valid integer"); return false; } for (int i = 0; i <= 2; i ++){ for (int j = i+1; j <= 3 ; j ++ ){ if (inputText.charAt(i) == inputText.charAt(j)){ if (comboBox.getSelectedIndex() == 1) JOptionPane.showMessageDialog(null, "輸入有誤。四個數字都不能重複"); else JOptionPane.showMessageDialog(null, "Input error. There should be no duplicate digits"); return false; } } } textField.setText(inputText); return true; } private void setLabel(){ label.setText("Select a language"); label.setForeground(new Color(153, 102, 0)); label.setAlignmentX(Component.CENTER_ALIGNMENT); label.setFont(new Font("Kartika", Font.BOLD, 16)); label.setBounds(150, 60, 150, 35); } private void start(){ setRandomNumber(); btnEnter.setVisible(true); comboBox.setVisible(false); btnStart.setVisible(false); textField.setVisible(true); btnSwitchLanguage.setVisible(true); setTexts(); times = 0; } private void setRandomNumber(){ boolean[] used = new boolean[10]; int d1 = random.nextInt(10); used[d1] = true; int d2 = random.nextInt(9); if (used[d2]) d2++; used[d2] = true; int d3 = random.nextInt(8); if (used[d3]) d3++; if (used[d3]) d3++; used[d3] = true; int d4 = random.nextInt(7); if (used[d4]) d4++; if (used[d4]) d4++; if (used[d4]) d4++; randomNumber = "" + d4 + d3 + d2 + d1; System.out.println(randomNumber); } private void setGiAGiB(){ int a = 0; for (int i = 0 ; i < 4 ; i ++){ if (textField.getText().charAt(i) == randomNumber.charAt(i)) a++; } int b = 0; for (int i = 0 ; i < 4 ; i ++){ if (randomNumber.contains(textField.getText().charAt(i) + "")){ if (textField.getText().charAt(i) != randomNumber.charAt(i)) b++; } } giAgiBJLabel.setText(a + "A" + b + "B"); giAgiBJLabel.setVisible(true); } private void setTexts(){ if (comboBox.getSelectedIndex() == 1){ setTitle("猜數字"); label.setBounds(80, 40, 300, 35); label.setFont(new Font("微軟正黑體", Font.BOLD, 16)); label.setText("請輸入一個數字不重複的四位數正整數"); btnSwitchLanguage.setBounds(328, 239, 110, 23); btnSwitchLanguage.setFont(new Font("微軟正黑體", Font.PLAIN, 10)); btnSwitchLanguage.setText("切換語言"); btnEnter.setText("輸入"); } else { setTitle("Guess Number"); label.setBounds(75, 40, 300, 35); label.setText("Please enter a 4-digit positive integer"); btnSwitchLanguage.setText("Switch Language"); btnSwitchLanguage.setFont(new Font("Sylfaen", Font.PLAIN, 10)); btnSwitchLanguage.setBounds(328, 239, 110, 23); additionalLabel.setVisible(true); btnEnter.setText("Enter"); } } }
This blog contains posts regarding my practice in programming. The posts are about questions in Yahoo Knowledge or some program design ideas or implementation of algorithms.
Saturday, March 2, 2013
A Java source code for Guess Number game (猜數字遊戲)
The Java code below is my implementation of the "guess number game"(猜數字遊戲). It is a fully runnable Java application. A feature of the program is that it supports both Chinese and English languages. You can also copy and configure it on your own.
Labels:
Java
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment