From 61c2c3b99cdbb42d163e602023d4e8468413bab5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=82=A8=EC=83=81=ED=98=84?= Date: Tue, 7 May 2024 12:09:37 +0900 Subject: [PATCH] fix --- src/main/java/Main.java | 8 ++ src/main/java/com/baseball/BaseballGame.java | 41 ++++++++++ src/main/java/com/baseball/Computer.java | 10 +++ src/main/java/com/baseball/GameManager.java | 24 ++++++ src/main/java/com/baseball/Result.java | 29 ++++++++ src/main/java/com/baseball/ResumeOrNot.java | 43 +++++++++++ src/main/java/com/domain/Baseball.java | 38 ++++++++++ src/main/java/com/domain/BaseballFactory.java | 42 +++++++++++ src/main/java/com/domain/Baseballs.java | 74 +++++++++++++++++++ src/main/java/com/ui/Input.java | 20 +++++ src/main/java/com/ui/Output.java | 36 +++++++++ 11 files changed, 365 insertions(+) create mode 100644 src/main/java/Main.java create mode 100644 src/main/java/com/baseball/BaseballGame.java create mode 100644 src/main/java/com/baseball/Computer.java create mode 100644 src/main/java/com/baseball/GameManager.java create mode 100644 src/main/java/com/baseball/Result.java create mode 100644 src/main/java/com/baseball/ResumeOrNot.java create mode 100644 src/main/java/com/domain/Baseball.java create mode 100644 src/main/java/com/domain/BaseballFactory.java create mode 100644 src/main/java/com/domain/Baseballs.java create mode 100644 src/main/java/com/ui/Input.java create mode 100644 src/main/java/com/ui/Output.java diff --git a/src/main/java/Main.java b/src/main/java/Main.java new file mode 100644 index 00000000..545ca9f2 --- /dev/null +++ b/src/main/java/Main.java @@ -0,0 +1,8 @@ +import com.baseball.GameManager; + +public class Main { + public static void main(String[] args) { + GameManager gameManager = new GameManager(); + gameManager.run(); + } +} \ No newline at end of file diff --git a/src/main/java/com/baseball/BaseballGame.java b/src/main/java/com/baseball/BaseballGame.java new file mode 100644 index 00000000..0263e8b1 --- /dev/null +++ b/src/main/java/com/baseball/BaseballGame.java @@ -0,0 +1,41 @@ +package com.baseball; + +import com.domain.BaseballFactory; +import com.domain.Baseballs; +import com.ui.Input; +import com.ui.Output; + +public class BaseballGame { + private Computer computer = new Computer(); + private Baseballs userBaseballs; + private Baseballs computerBaseballs; + + public void playBaseball(){ + computerBaseballs = computer.generateTandomBaseballs(); + + do{ + playBaseballOnce(); + } while(!isCorrectAnswer()); + Output.printCorrectAnswer(); + } + + private void playBaseballOnce(){ + userBaseballs = receiveUserBaseballs(); + Result result = new Result(computerBaseballs, userBaseballs); + + Output.printBaseballGameResult(result.getResult()); + } + + private Baseballs receiveUserBaseballs() { + try { + return BaseballFactory.createBaseballs(Input.receiveNumberInput()); + } catch (IllegalArgumentException e) { + Output.printExceptionMessage(e.getMessage()); + return receiveUserBaseballs(); + } + } + + private boolean isCorrectAnswer(){ + return userBaseballs.equals(computerBaseballs); + } +} diff --git a/src/main/java/com/baseball/Computer.java b/src/main/java/com/baseball/Computer.java new file mode 100644 index 00000000..9b1b1631 --- /dev/null +++ b/src/main/java/com/baseball/Computer.java @@ -0,0 +1,10 @@ +package com.baseball; + +import com.domain.BaseballFactory; +import com.domain.Baseballs; + +public class Computer { + public Baseballs generateTandomBaseballs(){ + return BaseballFactory.createRandomBaseballs(); + } +} diff --git a/src/main/java/com/baseball/GameManager.java b/src/main/java/com/baseball/GameManager.java new file mode 100644 index 00000000..07f9a919 --- /dev/null +++ b/src/main/java/com/baseball/GameManager.java @@ -0,0 +1,24 @@ +package com.baseball; + +import com.ui.Input; +import com.ui.Output; + +public class GameManager { + public void run(){ + do { + BaseballGame baseballGame = new BaseballGame(); + baseballGame.playBaseball(); + } while (isResumeOpinion()); + + } + + private boolean isResumeOpinion(){ + try { + ResumeOrNot opinion = new ResumeOrNot(Input.receiveResumeInput()); + return opinion.isResume(); + } catch (IllegalArgumentException e){ + Output.printExceptionMessage(e.getMessage()); + return isResumeOpinion(); + } + } +} diff --git a/src/main/java/com/baseball/Result.java b/src/main/java/com/baseball/Result.java new file mode 100644 index 00000000..14b63125 --- /dev/null +++ b/src/main/java/com/baseball/Result.java @@ -0,0 +1,29 @@ +package com.baseball; + +import com.domain.Baseballs; +import com.ui.Output; + +public class Result { + private static final int Count_Zero = 0; + private int strikeNumber; + private int ballNumber; + + public Result(Baseballs computerBaseballs, Baseballs userBaseballs){ + strikeNumber = computerBaseballs.countStrike(userBaseballs); + ballNumber = computerBaseballs.countBall(userBaseballs); + } + + public String getResult(){ + StringBuilder sb = new StringBuilder(); + if (strikeNumber == Count_Zero && ballNumber == Count_Zero){ + sb.append(Output.RESULT_NOTHING_MESSAGE); + } + if (strikeNumber != Count_Zero){ + sb.append(strikeNumber).append(Output.RESULT_STRIKE_MESSAGE); + } + if (ballNumber != Count_Zero){ + sb.append(ballNumber).append(Output.RESULT_BALL_MESSAGE); + } + return sb.toString(); + } +} diff --git a/src/main/java/com/baseball/ResumeOrNot.java b/src/main/java/com/baseball/ResumeOrNot.java new file mode 100644 index 00000000..b8b9e006 --- /dev/null +++ b/src/main/java/com/baseball/ResumeOrNot.java @@ -0,0 +1,43 @@ +package com.baseball; + +import com.ui.Output; + +import java.util.Objects; + +public class ResumeOrNot { + private static final int Valid_Operation_Input_Size = 1; + private static final int Resume_Reply = 1; + private static final int Stop_Reply = 2; + private static final char Zero_char = '0'; + + private final int opinion; + + public ResumeOrNot(String userReply) { + isValidLength(userReply); + + this.opinion = userReply.charAt(0) - Zero_char; + isValidOpinionRange(opinion); + } + + private void isNullValue(String userReply) { + if (Objects.isNull(userReply)) { + throw new IllegalArgumentException(Output.NULL_EXCEPTION_MESSAGE); + } + } + + private void isValidLength(String userReply) { + if (userReply.length() != Valid_Operation_Input_Size) { + throw new IllegalArgumentException(Output.RESUME_INPUT_SIZE_EXCEPTION_MESSAGE); + } + } + + private void isValidOpinionRange(int opinion) { + if ((opinion != Resume_Reply) && (opinion != Stop_Reply)) { + throw new IllegalArgumentException(Output.RESUME_INPUT_FORMAT_EXCEPTION_MESSAGE); + } + } + + public boolean isResume() { + return (opinion == Resume_Reply); + } +} diff --git a/src/main/java/com/domain/Baseball.java b/src/main/java/com/domain/Baseball.java new file mode 100644 index 00000000..980f519b --- /dev/null +++ b/src/main/java/com/domain/Baseball.java @@ -0,0 +1,38 @@ +package com.domain; + +import com.ui.Output; + +import java.util.Objects; + +public class Baseball { + private static final int Min_Number = 1; + private static final int Max_Number = 9; + + private final int baseballNumber; + + public Baseball(int baseballNumber){ + isValidBaseballNumber(baseballNumber); + + this.baseballNumber = baseballNumber; + } + + private void isValidBaseballNumber(int baseballNumber){ + if (baseballNumber < Min_Number || baseballNumber > Max_Number){ + throw new IllegalArgumentException(Output.INPUT_NUMBER_FORMAT_EXCEPTION_MESSAGE); + } + } + + @Override + public boolean equals(Object o){ + if (this ==o) return true; + if (o == null || getClass() != o.getClass()) return false; + Baseball baseball = (Baseball) o; + return baseballNumber == baseball.baseballNumber; + } + + @Override + public int hashCode(){ + return Objects.hash(baseballNumber); + } + +} diff --git a/src/main/java/com/domain/BaseballFactory.java b/src/main/java/com/domain/BaseballFactory.java new file mode 100644 index 00000000..ca8d8664 --- /dev/null +++ b/src/main/java/com/domain/BaseballFactory.java @@ -0,0 +1,42 @@ +package com.domain; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +public class BaseballFactory { + private static final char Zero_Char = '0'; + private static final int Valid_Baseball_Size = 3; + private static final int Random_Number_Origin = 1; + private static final int Random_Number_Bound = 10; + + private static Baseball createBaseball(int number){ + return new Baseball(number); + } + + public static Baseballs createBaseballs(String baseballNumbers){ + List baseballs = new ArrayList<>(); + + for (int i = 0; i baseballs = new ArrayList<>(); + Random random = new Random(); + + while (baseballs.size() < Valid_Baseball_Size) { + int randomNumber = random.nextInt(Random_Number_Bound - Random_Number_Origin) + Random_Number_Origin; + Baseball baseball = createBaseball(randomNumber); + if (!baseballs.contains(baseball)) { + baseballs.add(baseball); + } + } + + return new Baseballs(baseballs); + } + +} diff --git a/src/main/java/com/domain/Baseballs.java b/src/main/java/com/domain/Baseballs.java new file mode 100644 index 00000000..3cb46f6e --- /dev/null +++ b/src/main/java/com/domain/Baseballs.java @@ -0,0 +1,74 @@ +package com.domain; + +import com.ui.Output; + +import java.util.List; +import java.util.Objects; +import java.util.stream.IntStream; + +public class Baseballs { + private static final int Valid_Baseball_Size = 3; + private static final int Zero_Start_Index = 0; + private static final int Exist_Number = 1; + + private final List baseballs; + + public Baseballs(List baseballs){ + isValidSize(baseballs); + isDuplicate(baseballs); + + this.baseballs = baseballs; + } + + private void isValidSize(List baseball){ + if (baseball.size() != Valid_Baseball_Size) { + throw new IllegalArgumentException(Output.BASEBALLS_SIZE_EXCEPTION_MESSAGE); + } + } + + private void isDuplicate(List baseballs){ + if (baseballs.stream().distinct().count() != Valid_Baseball_Size){ + throw new IllegalArgumentException(Output.BASEBALLS_SIZE_EXCEPTION_MESSAGE); + } + } + + public int countStrike(Baseballs cmpBaseballs) { + int strikeCount = 0; + + for (int i = 0; i < Valid_Baseball_Size; i++) { + if (baseballs.get(i).equals(cmpBaseballs.baseballs.get(i))) { + strikeCount++; + } + } + return strikeCount; + } + + public int countBall(Baseballs cmpBaseballs) { + int ballCount = 0; + + for (int i = 0; i < Valid_Baseball_Size; i++) { + if (!baseballs.get(i).equals(cmpBaseballs.baseballs.get(i)) && + cmpBaseballs.baseballs.contains(this.baseballs.get(i))) { + ballCount++; + } + } + return ballCount; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Baseballs baseballs = (Baseballs) o; + return Valid_Baseball_Size == + IntStream.range(Zero_Start_Index, Valid_Baseball_Size) + .filter(i -> this.baseballs.get(i).equals(baseballs.baseballs.get(i))) + .count(); + } + + @Override + public int hashCode() { + return Objects.hash(baseballs); + } + +} diff --git a/src/main/java/com/ui/Input.java b/src/main/java/com/ui/Input.java new file mode 100644 index 00000000..23587a0d --- /dev/null +++ b/src/main/java/com/ui/Input.java @@ -0,0 +1,20 @@ +package com.ui; + +import java.util.Scanner; + +public class Input { + private static Scanner scanner; + + public static String receiveNumberInput(){ + scanner = new Scanner(System.in); + Output.printRequestNumberInput(); + return scanner.nextLine(); + } + + public static String receiveResumeInput(){ + scanner = new Scanner(System.in); + Output.printRequestResumeInput(); + return scanner.nextLine(); + } + +} diff --git a/src/main/java/com/ui/Output.java b/src/main/java/com/ui/Output.java new file mode 100644 index 00000000..493bb6d5 --- /dev/null +++ b/src/main/java/com/ui/Output.java @@ -0,0 +1,36 @@ +package com.ui; + +public class Output { + private static final String REQUIRE_NUMBER_INPUT_MESSAGE = "숫자를 입력해 주세요 : "; + private static final String REQUIRE_RESTART_INPUT_MESSAGE = "게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."; + private static final String CORRECT_ANSWER_AND_EXIT_MESSAGE = "3개의 숫자를 모두 맞히셨습니다! 게임 종료."; + public static final String BASEBALLS_SIZE_EXCEPTION_MESSAGE = "3자리 수를 입력해야 합니다."; + public static final String BASEBALLS_DUPLICATE_EXCEPTION_MESSAGE = "중복된 숫자가 존재합니다."; + public static final String INPUT_NUMBER_FORMAT_EXCEPTION_MESSAGE = "각 자리의 수는 1부터 9까지의 숫자만 가능합니다."; + public static final String NULL_EXCEPTION_MESSAGE = "입력이 NULL이 될 수 없습니다."; + public static final String RESUME_INPUT_SIZE_EXCEPTION_MESSAGE = "입력한 문자의 길이가 1을 넘을 수 없습니다."; + public static final String RESUME_INPUT_FORMAT_EXCEPTION_MESSAGE = "입력은 1 또는 2만 가능합니다."; + public static final String RESULT_NOTHING_MESSAGE = "낫싱"; + public static final String RESULT_STRIKE_MESSAGE = " 스트라이크 "; + public static final String RESULT_BALL_MESSAGE = " 볼"; + + public static void printRequestNumberInput() { + System.out.print(REQUIRE_NUMBER_INPUT_MESSAGE); + } + + public static void printRequestResumeInput() { + System.out.println(REQUIRE_RESTART_INPUT_MESSAGE); + } + + public static void printBaseballGameResult(String result) { + System.out.println(result); + } + + public static void printCorrectAnswer() { + System.out.println(CORRECT_ANSWER_AND_EXIT_MESSAGE); + } + + public static void printExceptionMessage(String exceptionMessage) { + System.out.println(exceptionMessage); + } +}