U E D R , A S I H C RSS

The Java Man/숫자야구

여기까지 하고 끝내기로 했습니다.

숫자야구란?

1~9까지 겹치지 않는 숫자를 골라서 사용자가 맞추기

할 일

세부사항 시간 장소 진행상황
틀 만들기 1/13일 4시 7피 끝냄
사용자 입력 받기 - 겹치지 않는 숫자 . . 끝냄
확인 버튼 누를 때 동작 . . 끝냄
확인 버튼 누를 때 리스트에서 결과 표시 . . 끝냄
리스트에 스크롤바 달기 . . 자동으로 생김
포기 버튼 누를 때 동작 - 답 알려주고, 새 게임 시작 . . 끝냄
크기 조절 . . 끝냄
입력 제대로 하지 않을때 처리하기 . . .
스크롤 같이 내려가기 . . 끝냄
시작할 때 포커스를 텍스트필드에 맞추기 . . 끝냄
+a
하이스코어 - 물어본 횟수가 적은 순으로 . . .
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 
                 
        } 
 
} 

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:28:12
Processing time 0.0169 sec