기까 고 기 .
구? ¶
1~9까 겹 골 기
¶
간 | |||
기 | 1/13 4 | 7 | |
기 - 겹 | . | . | |
. | . | ||
결과 | . | . | |
기 | . | . | |
기 - 고, 게 | . | . | |
기 | . | . | |
기 | . | . | . |
기 | . | . | |
기 | . | . |
- | . | . | . |
3까 기 | . | . | . |
. | . | . |
기 ¶
~cpp public class BBGameFrame extends Frame implements WindowListener{ static BBGameFrame f; public BBGameFrame(String aStr){ super(aStr); addWindowListener(this); } public static void main(String[] args) { f = new BBGameFrame(" 구 게 !"); f.setSize(400, 300); Panel up = new Panel(); List result = new List(); up.add(result); f.add(up, "North"); Panel lp = new Panel(); TextField input = new TextField(); lp.add(input, "West"); Button ok = new Button(""); lp.add(ok, "Center"); Button submit = new Button("기"); lp.add(submit); f.add(lp, "South"); f.pack(); f.show(); } public void windowClosing(WindowEvent e) { dispose(); // . System.exit(0); // Program . } public void windowOpened(WindowEvent e) { } public void windowIconified(WindowEvent e) { } public void windowDeiconified(WindowEvent e) { } public void windowClosed(WindowEvent e) { } public void windowActivated(WindowEvent e) { } public void windowDeactivated(WindowEvent e) { } }
¶
BBGame.java
~cpp import java.util.Random; /* * Created on 2004. 1. 17. * * To change the template for this generated file go to * Window - Preferences - Java - Code Generation - Code and Comments */ /** * @author 125 * * To change the template for this generated type comment go to * Window - Preferences - Java - Code Generation - Code and Comments */ public class BBGame { public static String correct_answer; public static void startGame(){ correct_answer = ""; Random rmd = new Random(); int temp[] = new int[3]; while ( temp[0] == temp [1] | temp[1] == temp [2] | temp[2] == temp [0]) for ( int i = 0 ; i < 3 ; i++) temp[i] = rmd.nextInt(9); for ( int i = 0 ; i < 3 ; i++ ) correct_answer += temp[i]; } public static String compare(String aStr){ if ( correct_answer.compareTo(aStr) == 0) return ""; int strike = 0, ball = 0; for ( int i = 0 ; i < 3 ; i++) if ( correct_answer.charAt(i) == aStr.charAt(i)) strike++; else for ( int j = 0 ; j < 3 ; j++) if ( correct_answer.charAt(i) == aStr.charAt(j)) ball++; if ( strike == ball & ball == 0) return "-_-"; return strike + " , " + ball + ""; } public static void main(String[] args) { startGame(); BBGameFrame frame = new BBGameFrame(); frame.show(); frame.giveFocus(); } }
BBGameFrame.java
~cpp import java.awt.Container; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JFrame; /* * Created on 2004. 1. 17. * * To change the template for this generated file go to * Window - Preferences - Java - Code Generation - Code and Comments */ /** * @author 125 * * To change the template for this generated type comment go to * Window - Preferences - Java - Code Generation - Code and Comments */ public class BBGameFrame extends JFrame { public BBGameFrame(){ this.setTitle(" 구 게 "); this.setSize(200, 200); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); /*Container upper = getContentPane(); upper.add(new UpperPanel(), "North"); Container lower = getContentPane(); lower.add(new LowerPanel(), "South");*/ Container cp = getContentPane(); cp.add(new UpperPanel(), "Center"); cp.add(new LowerPanel(), "South"); } public void giveFocus(){ LowerPanel.input.requestFocus(); } public static void main(String[] args) { } }
UpperPanel.java
~cpp import java.awt.BorderLayout; import java.awt.List; import java.awt.Panel; /* * Created on 2004. 1. 17. * * To change the template for this generated file go to * Window - Preferences - Java - Code Generation - Code and Comments */ /** * @author 125 * * To change the template for this generated type comment go to * Window - Preferences - Java - Code Generation - Code and Comments */ public class UpperPanel extends Panel { private static int tail = 0; private static List result; public UpperPanel(){ result = new List(); result.setSize(400,200); this.setLayout(new BorderLayout()); this.add(result, "Center"); } public static void addResult(String aStr){ result.add(aStr); result.select(tail); tail++; } public static void main(String []args) { } public static void restart() { result.clear(); } }
LowerPanel.java
~cpp import java.awt.Button; import java.awt.Panel; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; /* * Created on 2004. 1. 17. * * To change the template for this generated file go to * Window - Preferences - Java - Code Generation - Code and Comments */ /** * @author 125 * * To change the template for this generated type comment go to * Window - Preferences - Java - Code Generation - Code and Comments */ public class LowerPanel extends Panel implements ActionListener, KeyListener{ public static TextField input; private static Button ok; private static Button submit; public LowerPanel(){ input = new TextField(3); this.add(input, "West"); input.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) {processOK();} }); ok = new Button(""); this.add(ok, "Center"); ok.addActionListener(this); ok.addKeyListener(this); submit = new Button("기"); this.add(submit, "East"); submit.addActionListener(this); input.requestFocus(); } public void actionPerformed(ActionEvent e){ processEvent(e.getSource()); } private static boolean isCorrect(String aStr) { if ( aStr.length() != 3) return false; return true; } public static void main(String[] args) { } public void keyPressed(KeyEvent e) { processEvent(e.getSource()); } public static void processEvent(Object aSource){ if ( aSource == ok) processOK(); else if ( aSource == submit) processSubmit(); input.selectAll(); input.requestFocus(); } private static void processSubmit() { UpperPanel.restart(); UpperPanel.addResult(" 게 : " + BBGame.correct_answer); UpperPanel.addResult("-_-"); BBGame.startGame(); } public static void processOK(){ String newResult = null; if (!isCorrect(input.getText())) return; newResult = input.getText() + " -> " + BBGame.compare(input.getText()); UpperPanel.addResult(newResult); } /* (non-Javadoc) * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent) */ public void keyReleased(KeyEvent arg0) { // TODO Auto-generated method stub } /* (non-Javadoc) * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent) */ public void keyTyped(KeyEvent arg0) { // TODO Auto-generated method stub } }