본문 바로가기
교내|외 활동/멋사 13기

멋사 13기 지원 | 2단계 과제_baseball

by 0/0 2025. 2. 13.

목차

1. baseball 과제 코드(GitHub)

2. 구현

3. 소감

 

1. baseball 과제 코드(GitHub)

 

https://github.com/lye5615/13th-backend-baseball

 

2. 구현

 

이번 과제는 객체지향을 크게 고려하지 않고 Application 클래스 내에서 모두 구현했다.

(단지 매직넘버없이 구현 하기 위해 소프트 코딩에 대해 고려하면서 노력해보았다.)

 

main()

 

->게임의 흐름을 담당하는 start()

-맞춰야 할 숫자 생성

-사용자 숫자 입력

-입력된 숫자 검증

 

->cleanNumbers()

-게임을 다시 시작할 때 스트라이크인 경우와 아닌 경우를 나누어

맞춰야 할 숫자 혹은 사용자 입력 숫자 ArrayList를 clear()

 

->compare() *가장 고려할 것이 많았던 메소드이다. 또 들여쓰기(indent)를 3이상으로 작성해서 후에 리팩터링이 필요할 것 같다.

-먼저 숫자 비교를 해서 스트라이크와 볼의 개수를 센다.

-개수가 스트라이크 혹은 볼의 조건을 충족하는 지에 따라 스트라이크 / 스트라이크와 볼 / 볼 / 낫싱을 구분했다.

		while (!flag) {
            for (int i = 0; i < number; i++) {
                if (computerNumbersArray.get(i).equals(userNumbersArray.get(i))) {
                    strikeCount++;
                } else if (computerNumbersArray.contains(userNumbersArray.get(i))) {
                    ballCount++;
                }
            }
            if (strikeCount == number) {
                System.out.printf("%d%s\n", strikeCount, "스트라이크");
                System.out.printf("%d%s\n", number, "개의 숫자를 모두 맞히셨습니다! 게임 종료");
                //게임 재시작 선택
                System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.");
                selection = Integer.parseInt(Console.readLine());
                if (selection == 1) {
                    cleanNumbers(1);
                    start();
                    flag = true;
                } else if (selection == 2) {
                    flag = true;
                } else {
                    System.out.println("1 혹은 2를 입력해주세요.");
                    throw new IllegalArgumentException();
                }
            } else {
                if (ballCount == 0 && strikeCount == 0) {
                    System.out.println("낫싱");
                    cleanNumbers(2);
                    break; //TODO: flag=true; 로 바꿔야 하나?
                } else if (ballCount == 0) {
                    System.out.printf("%d%s\n", strikeCount, "스트라이크");
                    cleanNumbers(2);
                    break;
                } else if (strikeCount == 0) {
                    System.out.printf("%d%s\n", ballCount, "볼");
                    cleanNumbers(2);
                    break;
                } else {
                    System.out.printf("%d%s %d%s\n", ballCount, "볼", strikeCount, "스트라이크");
                    cleanNumbers(2);
                    break;
                }
            }
        }

 

 

3. Issues

 

처음에는 어렵지 않을까 걱정을 많이 했는데, 구현하다 보니 걱정은 거의 사라지고  재밌게 할 수 있었다.

 

이번 과제는 따로 코드 리뷰가 없었는데, 스스로 객체지향을 고려해보면서 리팩터링이 필요할 것 같다.