import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class HighLowGame extends JFrame {
public static final int FIXED_COUNT = 6;
private JTextArea taOutput;
private JTextField tfInput;
private JTextField tfStatus;
private int num;
private int myNum;
private int gameCount;
private int ceiling = 100;
private int floor = 1;
public HighLowGame() {
init();
setDisplay();
comNumber();
addListeners();
showFrame();
}
public void init() {
taOutput = new JTextArea();
taOutput.setEditable(false);
tfInput = new JTextField(30);
tfInput.setText("여기에 당신의 숫자를 입력하세요!!!");
tfInput.setFocusable(true);
tfInput.selectAll();
tfStatus = new JTextField(30);
tfStatus.setEditable(false);
tfStatus.setBackground(null);
taSetText();
}
public void taSetText() {
String space = "♠♠♠♠♠♠ ";
taOutput.setText("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n\n");
taOutput.append(" HighLow Game\n\n");
taOutput.append("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n\n");
taOutput.append(space + "하이로우 게임에 온 것을 환영합니다!!!\n\n");
taOutput.append(space + "6번의 기회 안에 숨겨진 숫자를 맞추면 당신이 승리합니다.\n\n");
taOutput.append(space + "숨겨진 숫자는 1에서 100 사이의 숫자입니다.\n\n");
taOutput.append(space + "건투를 빕니다!!! 게임을 시작합니다.\n\n");
tfStatus.setText("Status :: 현재 남은 기회 > " + FIXED_COUNT + "회");
}
public void setDisplay() {
add(new JScrollPane(taOutput, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER), BorderLayout.CENTER);
add(tfInput, BorderLayout.NORTH);
add(tfStatus, BorderLayout.SOUTH);
}
public void addListeners() {
tfInput.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
try {
inputMynumber();
playGame();
} catch (NumberFormatException ne) {
taOutput.setText(taOutput.getText() + "♠ 숫자를 입력하세요!\n\n");
tfInput.setText("");
}
}
});
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
int choice = JOptionPane.showConfirmDialog(
HighLowGame.this,
"종료합니다.",
"HighLowGame 종료",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE
);
if(choice == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
});
}
public void comNumber() {
Random r = new Random();
num = r.nextInt(100) + 1;
System.out.println("생성된수: " + num);
gameCount = 1;
}
public void inputMynumber() {
myNum = Integer.parseInt(tfInput.getText());
tfInput.setText("");
}
public void playGame() {
String tf = "Status :: 현재 남은 기회 > ";
if(myNum < 0 || myNum > 100) {
taOutput.setText(taOutput.getText() + "♠ 다시 입력해주세요. (범위초과 및 미만)\n\n");
} else {
if(myNum == num) {
taOutput.setText(taOutput.getText() + "♠ 결과: 축하합니다. 당신의 승리입니다!!!!\n\n");
taOutput.append("♠ 숨겨진 숫자는 " + num + "이었습니다~!!\n\n");
tfStatus.setText("축하합니다. 당신의 승리입니다.");
gameCount = 8;
} else {
if(myNum < num) {
floor = myNum;
tfStatus.setText(tf + (6 - gameCount) + "회");
gameCount++;
} else {
ceiling = myNum;
tfStatus.setText(tf + (6 - gameCount) + "회");
gameCount++;
}
taOutput.setText(taOutput.getText() +
"♠ 틀렸습니다.\n\n♠ hint>> 숨겨진 숫자는 " + floor +
"보다 크고 " + ceiling + "보다 작습니다.\n\n"
);
}
if(gameCount == 7) {
taOutput.setText(taOutput.getText() + "♠ 결과: 당신은 졌습니다. 게임종료!!\n\n숨겨진 숫자는 " +
num + "이었습니다 ~!!\n\n"
);
tfStatus.setText("Status :: 당신이 졌습니다!!!");
gameCount = 8;
}
if(gameCount == 8) {
taOutput.append("♠ restart!! 맞출 숫자를 바로 입력하세요 ~ !\n\n");
comNumber();
ceiling = 100;
floor = 1;
}
}
}
public void showFrame() {
setTitle("this is HighLow Game!!!");
setSize(450, 600);
setLocation(100, 0);
setResizable(false);
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new HighLowGame();
}
}