여기까지 í•˜ê³ ë��내기로 했습니다.
숫ìž�야구란? ¶
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
}
}










