본문 바로가기

프로젝트 & TIL/자바 토이 프로젝트

자바 토이 프로젝트 2 - 구구단 게임

전체 코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        System.out.println("구구단 게임을 시작합니다.");

        System.out.println("당신의 이름 또는 닉네임을 입력해주세요.");
        String username = br.readLine();

        System.out.println("규칙 1 : 0점에서 시작하여 정답을 맞추면 +10점, 틀리면 -5점이 됩니다.");
        System.out.println("규칙 2 : 총 점수가 1000점 이상 또는 -500점 이하가 되면 게임은 자동으로 종료됩니다.");
        System.out.println("규칙 3 : 0을 입력하면 게임이 종료됩니다.");
        System.out.println("%s 님, 구구단 게임을 시작하려면 숫자 1을 입력해주세요.".formatted(username));

        String startGame = br.readLine();

        if(startGame.equals("1")) {
            System.out.println("============== 게임 시작 ==============");

            int score = 0;

            while(true) {
                if(score >= 1000 || score <= -500) break;

                int a = (int) (Math.random() * 7 + 2);
                int b = (int) (Math.random() * 8 + 1);
                System.out.println("\n%d X %d = ?".formatted(a, b));

                int answer = -1;
                try {
                    answer = Integer.parseInt(br.readLine());
                } catch(NumberFormatException e) {
                    System.out.println("정확한 숫자를 입력해주세요. (게임 종료 : 0)");
                    continue;
                }

                if(answer == 0) break;
                else if(answer == a * b){
                    score += 10;
                    System.out.println("정답입니다!!! (현재 점수 : %d)".formatted(score));
                } else {
                    score -= 5;
                    System.out.println("틀렸습니다. (현재 점수 : %d)".formatted(score));
                }
            }
            System.out.println("============== 최종 결과 ==============");
            System.out.println("%s 님의 점수는 %d 점 입니다.".formatted(username, score));
            System.out.println("======================================");
        } else {}

        System.out.println("구구단 게임이 종료되었습니다.");

        br.close();
    }
}

 


진행 흐름

1. 게임 이용자의 이름(닉네임)을 입력 받는다.

  

        System.out.println("구구단 게임을 시작합니다.");

        System.out.println("당신의 이름 또는 닉네임을 입력해주세요.");
        String username = br.readLine();

  

2. 규칙을 알려준 뒤 "1"을 입력 받으면 게임이 시작된다. "1" 이외의 텍스트가 입력되면 게임은 종료된다.

  

        System.out.println("규칙 1 : 0점에서 시작하여 정답을 맞추면 +10점, 틀리면 -5점이 됩니다.");
        System.out.println("규칙 2 : 총 점수가 1000점 이상 또는 -500점 이하가 되면 게임은 자동으로 종료됩니다.");
        System.out.println("규칙 3 : 0을 입력하면 게임이 종료됩니다.");
        System.out.println("%s 님, 구구단 게임을 시작하려면 숫자 1을 입력해주세요.".formatted(username));

        String startGame = br.readLine();

        if(startGame.equals("1")) {
            
            ... // 게임 실행
            
        } else {}
        
        System.out.println("구구단 게임이 종료되었습니다.");

        br.close();
    }
}

  

3-1. 구구단 게임이 진행된다.

  

            System.out.println("============== 게임 시작 ==============");

            int score = 0;

            while(true) {
                if(score >= 1000 || score <= -500) break;

                int a = (int) (Math.random() * 7 + 2);
                int b = (int) (Math.random() * 8 + 1);
                System.out.println("\n%d X %d = ?".formatted(a, b));

                int answer = -1;
                try {
                    answer = Integer.parseInt(br.readLine());
                } catch(NumberFormatException e) {
                    System.out.println("정확한 숫자를 입력해주세요. (게임 종료 : 0)");
                    continue;
                }

                if(answer == 0) break;
                else if(answer == a * b){
                    score += 10;
                    System.out.println("정답입니다!!! (현재 점수 : %d)".formatted(score));
                } else {
                    score -= 5;
                    System.out.println("틀렸습니다. (현재 점수 : %d)".formatted(score));
                }
            }

  

3-2. 입력 받은 값이 숫자가 아니면 안내와 함께 다시 입력 받는다.

  

                try {
                    answer = Integer.parseInt(br.readLine());
                } catch(NumberFormatException e) {
                    System.out.println("정확한 숫자를 입력해주세요. (게임 종료 : 0)");
                    continue;
                }

  

3-3. 점수가 1000점 이상이 되거나 -500점 이하가 되면 게임은 자동 종료된다.

  

            while(true) {
                if(score >= 1000 || score <= -500) break;
                
                ...
                
            }

  

4. 0을 입력하면 게임이 종료되고, 최종 점수를 출력한다.

  

            while(true) {
                
                ...

                if(answer == 0) break;
                
                ...
                
            }
            System.out.println("============== 최종 결과 ==============");
            System.out.println("%s 님의 점수는 %d 점 입니다.".formatted(username, score));
            System.out.println("======================================");

  


실행 화면

  


  

* 게임 시작 전 1이 아닌 다른 텍스트를 입력하면 게임이 바로 종료된다.

  

  


 

* 게임 실행 시 입력 받은 값이 숫자가 아니면(문자, 기호, 공백 등) 안내와 함께 다시 입력 받는다.

  


가위바위보 게임과 마찬가지로 워낙 단순한 진행 방식이라 구현도 쉽게 했다.

사용자가 임의의 목표 점수를 입력하고, 해당 점수 이상이 되면 게임이 종료되는 방식으로 구현해도 재밌을 것 같다.