2차 개인 프로젝트 - 업다운 게임

jk·2024년 1월 18일
0

kdt 풀스택

목록 보기
25/127
//2차 개인 프로젝트 - 업다운 게임

import java.util.*;
import java.util.concurrent.ThreadLocalRandom;

class Print {
    private static StringBuilder print = new StringBuilder();
    
    static void up() {
        Print.print.append("Up ===> ");
        System.out.print(Print.print);
        Print.print.setLength(0);
    }
    static void down() {
        Print.print.append("Down ===> ");
        System.out.print(Print.print);
        Print.print.setLength(0);
    }
    static void match() {
        Print.print.append("일치\n");
        System.out.print(Print.print);
        Print.print.setLength(0);
    }
    static void intro() {
        Print.print.append("게임시작 1\n게임종료 2\n>>\n");
        System.out.print(Print.print);
        Print.print.setLength(0);
    }
    static void typeAgain() {
        Print.print.append("1이나 2를 입력해주세요: ");
        System.out.print(Print.print);
        Print.print.setLength(0);
    }
    static void typeNumber() {
        Print.print.append("숫자를 입력해주세요: ");
        System.out.print(Print.print);
        Print.print.setLength(0);
    }
    static void countRemained(int count) {
        Print.print.append(count);
        Print.print.append("번 남아 있습니다.");
        System.out.println(Print.print);
        Print.print.setLength(0);
    }
    static void lose(int randomNum) {
        Print.print.append("당신은 졌습니다. 답은 ");
        Print.print.append(randomNum);
        Print.print.append(" 입니다.");
        System.out.println(Print.print);
        Print.print.setLength(0);
    }
    static void exit() {
        Print.print.append("게임을 종료합니다.");
        System.out.println(Print.print);
        Print.print.setLength(0);
    }
    static void wrongInt() {
        Print.print.append("올바르지 않은 값입니다.");
        System.out.println(Print.print);
        Print.print.setLength(0);
    }
}
class InGame {
    private static final int FIRST_NUM = 1;
    private static final int LAST_NUM = 99;
    private static final int COUNT_MAX = 10;
    
    private static Scanner scanner;
    
    private static int InputNum() {
        int num;
        
        while (true) {
            scanner = new Scanner(System.in);
            try {
                num = scanner.nextInt();
                if (num >= FIRST_NUM && num <= LAST_NUM) {
                    break;
                } else {
                    Print.wrongInt();
                    continue;
                }
            } catch(Exception e) {
                Print.wrongInt();
                continue;
            }
        };
        return num;
    }
    InGame() {
        running:
        while (true) {
            Print.intro();
            
            int randomNum = ThreadLocalRandom.current().nextInt(FIRST_NUM, LAST_NUM + 1);
            int inputNum;
            
            getInputNum:
            while(true) {
                inputNum = InputNum();
                switch (inputNum) {
                    case 1: {
                        break getInputNum;
                    }
                    case 2: {
                        break running;
                    }
                    default: {
                        Print.typeAgain();
                    }
                };
            };
            
            int count = COUNT_MAX;
            
            while(count > 0) {
                count--;
                
                Print.typeNumber();
                
                inputNum = InputNum();
                if (randomNum > inputNum) {
                    Print.up();
                } else if (randomNum < inputNum) {
                    Print.down();
                } else {
                    Print.match();
                    continue running;
                };
                Print.countRemained(count);
            };
            Print.lose(randomNum);
        };
        Print.exit();
    }
}
class GameUpDown {
    public static void main(String[] args) {
        InGame ingame = new InGame();
    }
}

profile
Brave but clumsy

0개의 댓글