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
}
}