연습-랜덤 숫자 게임
package dev_java.khexample;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class RandomGameView implements ActionListener {
RandomGameLogic rgLogic = new RandomGameLogic(this);
// 선언부
JFrame jf = new JFrame();
JLabel jl = new JLabel("1~9 사이의 숫자를 입력해주세요.");
// 중앙에 붙임
JTextArea jta_display = new JTextArea(10, 20);
JScrollPane jsp_display = new JScrollPane(jta_display, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
// 남쪽에 붙임
JTextField jtf_input = new JTextField(5);
// 생성자
public RandomGameView() {
initDisplay();
}
// 화면처리부
public void initDisplay() {
// 이벤트 리스너 연결
jtf_input.addActionListener(this);
jta_display.setEditable(false);
// JFrame에 추가
jf.add("Center", jsp_display);
jf.add("South", jtf_input);
jf.add("North", jl);
// 라벨 중앙 정렬
jl.setHorizontalAlignment(JLabel.CENTER);
// 창닫기 버튼 클릭시 종료처리
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jtf_input.requestFocus();
jf.setSize(300, 400);
jf.setVisible(true);
}
// 메인메소드
public static void main(String[] args) {
new RandomGameView();
}
@Override
public void actionPerformed(ActionEvent e) {
Object obj = e.getSource();
if (obj == jtf_input) {
rgLogic.gameMethod();
}
}
}
package dev_java.khexample;
import javax.swing.JOptionPane;
public class RandomGameLogic {
RandomGameView rgView = null;
int random_value = (int) (Math.random() * 10); // 난수값의 범위를 1~9 으로 지정;
int count = 0; // 시도 횟수 카운트
public RandomGameLogic(RandomGameView rgView) {
this.rgView = rgView;
}
// 새로운 난수값 설정
void newGame() {
random_value = (int) (Math.random() * 10);
System.out.println("답: " + random_value);
count = 0;
}
void gameMethod() {
System.out.println("답: " + random_value);
String input = rgView.jtf_input.getText();
rgView.jta_display.append(input + "\n");
String[] buttons = { "새 게임", "종료" };
count++;
// 정답일 경우
if (random_value == Integer.parseInt(input)) {
int result = JOptionPane.showOptionDialog(rgView.jf, "정답입니다!", "숫자 게임 결과", JOptionPane.YES_NO_OPTION,
JOptionPane.INFORMATION_MESSAGE, null, buttons, "종료");
if (result == JOptionPane.CLOSED_OPTION) {
System.exit(0);
} else if (result == JOptionPane.YES_OPTION) {
rgView.jta_display.setText("");
newGame();
} else {
System.exit(0);
}
} else if (random_value > Integer.parseInt(input)) {
rgView.jta_display.append("더 높은 값입니다." + "\n남은 시도 횟수: " + (3 - count) + "\n");
} else if (random_value < Integer.parseInt(input)) {
rgView.jta_display.append("더 낮은 값입니다." + "\n남은 시도 횟수: " + (3 - count) + "\n");
}
// 시도 횟수 초과
if (count == 3) {
int result = JOptionPane.showOptionDialog(rgView.jf, "시도횟수를 모두 소진했습니다!", "시도 횟수 초과", JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE, null, buttons, "종료");
if (result == JOptionPane.CLOSED_OPTION) {
System.exit(0);
} else if (result == JOptionPane.YES_OPTION) {
rgView.jta_display.setText("");
newGame();
} else {
System.exit(0);
}
}
rgView.jtf_input.setText("");
System.out.println("횟수: " + (3 - count));
}
}
'국비학원 > 숙제' 카테고리의 다른 글
Day18_단순연산 계산기 (0) | 2022.12.27 |
---|---|
Day14_자바 연습문제 (0) | 2022.12.27 |
Day12_성적 관리 테이블 (0) | 2022.12.27 |
Day11_ 주소록, 자바 연습문제 (0) | 2022.12.27 |
Day10_총점, 평균, 석차 구하기 (0) | 2022.12.27 |
댓글