'JAVA/JSP/Source'에 해당되는 글 36건
- 2008.07.14 야구게임
import java.awt.*;
import java.awt.event.*;
import java.util.*;
class BaseBall extends Frame implements ActionListener
{
int count, strike, ball;
int[] com = new int[3];
int[] my = new int[3];
char[] temp = new char[3];
Button newg = new Button("New Game");
Button answer = new Button("Answer");
Button clear = new Button("Clear");
Button exit = new Button("Exit");
TextArea ta = new TextArea();
TextField insert = new TextField(10);
Panel rpanel = new Panel();
Panel spanel = new Panel();
Random r = new Random(); // Component 생성
BaseBall()
{
setLayout(new BorderLayout());
rpanel.setLayout(new GridLayout(4,1,7,7));
add("Center",ta);
rpanel.add(newg);
rpanel.add(answer);
rpanel.add(clear);
rpanel.add(exit);
spanel.add(new Label("Insert Number :"));
spanel.add(insert);
add("East",rpanel);
add("South",spanel);
this.random();
setBounds(200,200,500,500);
setVisible(true);
newg.addActionListener(this);
answer.addActionListener(this);
clear.addActionListener(this);
exit.addActionListener(this);
insert.addActionListener(this); // Listener 붙이기
addWindowListener(new WindowAdapter()
{
public void windowColsing(WindowEvent e)
{
System.exit(0);
}
});
insert.requestFocus();
}
///// 난수 구하는 함수 /////
void random()
{
com[0] = r.nextInt(9)+1;
do{
com[1] = r.nextInt(9)+1;
}
while(com[0]==com[1]);
do{
com[2] = r.nextInt(9)+1;
}while(com[1]==com[2]||com[2]==com[0]);
}
///// 난수와 입력받은 수를 비교 하여 strike 와 ball값 구하기 /////
void compare()
{
for(int k=0; k<com.length; k++)
{
if(com[k]==my[k])
{
strike++;
}
}
for(int i=0; i<com.length; i++)
{
for(int j=0; j<my.length; j++)
{
if(com[i]==my[j])
ball++;
}
}
ball-=strike;
}
///// 화면에 출력 /////
void display()
{
ta.append(count+" : "+insert.getText()+" "+strike+" Strike\t"+ball+" Ball\n");
if(strike == 3)
{
if(count>=3 && count<=5)
ta.append("<<<<< Very Good !! >>>>>");
else if(count>=5 && count<=8)
ta.append("<<<<< Good !! >>>>");
else if(count>=8 && count<=10)
ta.append("<<<<< Not Bad XXX >>>>");
else if(count>10)
ta.append("<<<<< Bad >>>>>");
}
}
///// Main /////
public static void main(String[] args)
{
new BaseBall();
}
///// Event 구현 /////
public void actionPerformed(ActionEvent e)
{
Object ob = e.getSource();
insert.requestFocus();
if(ob == newg) // newGame 버튼이 눌렸을때
{
random();
ta.setText(null);
}
else if(ob == answer) // answer버튼이 눌렸을때
{
ta.setText("Alswer is :");
for(int i = 0; i<com.length; i++)
{
Integer in = new Integer(com[i]);
ta.append(in.toString());
// ta.append(""+com[i]); <--- 위 두 문장을 이 한문장으로 대치할 수 있다.
}
}
else if(ob == clear) // clear 버튼이 눌렸을때
{
ta.setText(null);
insert.setText(null);
}
else if(ob == exit) // exit 버튼이 눌렸을때
{
System.exit(0);
}
else if(ob == insert) // TextField에 문자가 입력될때
{
///// 입력된 문자가 3자 가 아닐때 다시 입력 /////
if((((TextField)ob).getText()).length()!=3)
{
ta.append("Try again!! You must Insert 3 Numbers!\n");
insert.setText(null);
return;
}
///// 입력받은 문자를 숫자로 변환 /////
int tem;
try
{
tem = Integer.parseInt(insert.getText());
}catch(NumberFormatException ex)
{
ta.append("<<<<< insert Number Format >>>>>\n");
insert.setText(null);
return;
} // 입력받은 문자가 숫자가 아닐때 나타나는 예외 처리
my[0] = tem/100;
my[1] = (tem-(my[0]*100))/10;
my[2] = tem%10;
ball=0; strike=0;
this.compare();
count++;
this.display();
insert.setText(null);
}
}
}
'JAVA/JSP > Source' 카테고리의 다른 글
Thread (0) | 2008.07.14 |
---|---|
Graphics(clipRect()) (0) | 2008.07.14 |
아날로그 시계(AWT) (0) | 2008.07.14 |
JCheckBox/ JRadioButton /JComboBox/ JTable (0) | 2008.07.14 |
개미 수열 (0) | 2008.07.14 |