From 1def0aa2d3f42026aabdd40d2a90d790bec74d0b Mon Sep 17 00:00:00 2001 From: yeonjy Date: Wed, 17 May 2023 01:09:08 +0900 Subject: [PATCH 01/29] =?UTF-8?q?feat:=20Controller=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../baseball/controller/InputController.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/main/java/baseball/controller/InputController.java diff --git a/src/main/java/baseball/controller/InputController.java b/src/main/java/baseball/controller/InputController.java new file mode 100644 index 0000000000..810282f0a1 --- /dev/null +++ b/src/main/java/baseball/controller/InputController.java @@ -0,0 +1,29 @@ +package baseball.controller; + +import baseball.repository.AnswerRepository; +import baseball.service.BaseballService; +import camp.nextstep.edu.missionutils.Console; +import camp.nextstep.edu.missionutils.Randoms; + +import java.util.ArrayList; +import java.util.List; + +public class InputController { + private final AnswerRepository answerRepository = new AnswerRepository(); + private final BaseballService baseballService = new BaseballService(); + + public void requireAnswer() { + answerRepository.makeAnswer(); + } + + public void recieveUserAnswer() { + String userAns = Console.readLine(); + answerRepository.setUerAnswer(userAns); + } + public String checkCorrect() { + return baseballService.makeReply(answerRepository.getAnswer(), answerRepository.getUserAnswer()); + } + + + +} From c7bd80629aafc5c02d1f78e3063588065a1eae4a Mon Sep 17 00:00:00 2001 From: yeonjy Date: Wed, 17 May 2023 01:09:24 +0900 Subject: [PATCH 02/29] =?UTF-8?q?feat:=20Repository=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../baseball/repository/AnswerRepository.java | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/main/java/baseball/repository/AnswerRepository.java diff --git a/src/main/java/baseball/repository/AnswerRepository.java b/src/main/java/baseball/repository/AnswerRepository.java new file mode 100644 index 0000000000..c7389b1bda --- /dev/null +++ b/src/main/java/baseball/repository/AnswerRepository.java @@ -0,0 +1,43 @@ +package baseball.repository; + +import camp.nextstep.edu.missionutils.Randoms; + +import java.util.ArrayList; +import java.util.List; + +public class AnswerRepository { + + private List answer; + private List userAnswer; + + public List getAnswer() { + return answer; + } + + public List getUserAnswer() { + return userAnswer; + } + + public List makeAnswer() { + List answer = new ArrayList<>(); + while(answer.size() < 3) { + int randomNumber = Randoms.pickNumberInRange(1, 9); + if(!answer.contains(randomNumber)) { + answer.add(randomNumber); + } + } + this.answer = answer; + return answer; + } + + public List setUerAnswer(String answer) { +// List ints = Arrays.asList(Stream.of(answer.split("")).mapToInt(Integer::parseInt).toArray()); + List userAnswer = new ArrayList(); + String[] splitAnswer = answer.split(""); + for(String tmp : splitAnswer) { + userAnswer.add(Integer.parseInt(tmp)); + } + this.userAnswer = userAnswer; + return userAnswer; + } +} \ No newline at end of file From b3126f6aa6ce8c91e47d71651da94d8b1e9f265c Mon Sep 17 00:00:00 2001 From: yeonjy Date: Wed, 17 May 2023 01:09:36 +0900 Subject: [PATCH 03/29] =?UTF-8?q?feat:=20Service=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../baseball/service/BaseballService.java | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/main/java/baseball/service/BaseballService.java diff --git a/src/main/java/baseball/service/BaseballService.java b/src/main/java/baseball/service/BaseballService.java new file mode 100644 index 0000000000..e66c020d52 --- /dev/null +++ b/src/main/java/baseball/service/BaseballService.java @@ -0,0 +1,42 @@ +package baseball.service; +import java.util.List; + + +public class BaseballService { + + private static final String NOTHING = "낫싱"; + private static final String STRIKE = "스트라이크"; + private static final String BALL = "볼"; + + public String makeReply(List answer, List userAnswer) { + return makeStringReply(countStrikes(answer, userAnswer), countBalls(answer, userAnswer)); + } + + private int countStrikes(List answer, List userAnswer) { + int cnt = 0; + for(int i = 0; i < answer.size(); i++) { + if(answer.get(i) == userAnswer.get(i)) { + cnt++; + } + } + return cnt; + } + + private int countBalls(List answer, List userAnswer) { + int cnt = 0; + for(int userAns : userAnswer) { + if(answer.contains(userAns)) { + cnt++; + } + } + return cnt; + } + + private String makeStringReply(int strikeCnt, int ballCnt) { + if(ballCnt == 0) { + return NOTHING; + } + ballCnt -= strikeCnt; + return strikeCnt + STRIKE + ballCnt + BALL; + } +} From f2d07fe3d12a9d76c27e77fa5bacd3dda5576640 Mon Sep 17 00:00:00 2001 From: yeonjy Date: Wed, 17 May 2023 11:50:52 +0900 Subject: [PATCH 04/29] =?UTF-8?q?feat:=20Application=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/baseball/Application.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index dd95a34214..851dfdccfa 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -1,7 +1,11 @@ package baseball; +import baseball.controller.InputController; + public class Application { public static void main(String[] args) { - // TODO: 프로그램 구현 + InputController inputController = new InputController(); + System.out.println("숫자 야구 게임을 시작합니다."); + inputController.requireAnswer(); } } From ef864f50199e12cb1f54e0c3aabb7e7a5a101ac3 Mon Sep 17 00:00:00 2001 From: yeonjy Date: Wed, 17 May 2023 11:57:08 +0900 Subject: [PATCH 05/29] =?UTF-8?q?feat:=20=EC=9E=90=EB=8F=99=20=EB=B0=98?= =?UTF-8?q?=EB=B3=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../baseball/controller/InputController.java | 47 ++++++++++++++----- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/src/main/java/baseball/controller/InputController.java b/src/main/java/baseball/controller/InputController.java index 810282f0a1..8495c1355e 100644 --- a/src/main/java/baseball/controller/InputController.java +++ b/src/main/java/baseball/controller/InputController.java @@ -3,27 +3,52 @@ import baseball.repository.AnswerRepository; import baseball.service.BaseballService; import camp.nextstep.edu.missionutils.Console; -import camp.nextstep.edu.missionutils.Randoms; -import java.util.ArrayList; import java.util.List; +import java.util.Map; public class InputController { - private final AnswerRepository answerRepository = new AnswerRepository(); - private final BaseballService baseballService = new BaseballService(); + + private static final String NOTHING = "낫싱"; + + + private BaseballService baseballService = new BaseballService(); + private AnswerRepository answerRepository = new AnswerRepository(); + + private static final String STRIKE = "스트라이크"; + + public void continueGame() { + System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요"); + int continueGame = Integer.parseInt(Console.readLine()); + if (continueGame == 1) { + requireAnswer(); + } + if (continueGame == 2) { + return; + } + } public void requireAnswer() { - answerRepository.makeAnswer(); + baseballService.makeAnswer(); + recieveUserAnswer(); } public void recieveUserAnswer() { - String userAns = Console.readLine(); - answerRepository.setUerAnswer(userAns); - } - public String checkCorrect() { - return baseballService.makeReply(answerRepository.getAnswer(), answerRepository.getUserAnswer()); + System.out.print("숫자를 입력해주세요 : "); + String userAnswer = Console.readLine(); + baseballService.makeUserAnswer(userAnswer); + checkCorrect(); } - + public void checkCorrect() { + Map result = baseballService.makeReply(answerRepository.getAnswer(), answerRepository.getUserAnswer()); + System.out.println(baseballService.makeStringReply(result)); + if (result.containsKey(STRIKE) && result.get(STRIKE).equals(3)) { + System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); + continueGame(); + return; + } + recieveUserAnswer(); + } } From 9ec51d856eb1060935f973e2336fbb916bd8a138 Mon Sep 17 00:00:00 2001 From: yeonjy Date: Wed, 17 May 2023 12:00:01 +0900 Subject: [PATCH 06/29] =?UTF-8?q?style:=20=EC=BD=94=EB=93=9C=20=ED=8F=AC?= =?UTF-8?q?=EB=A7=B7=ED=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/baseball/repository/AnswerRepository.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/baseball/repository/AnswerRepository.java b/src/main/java/baseball/repository/AnswerRepository.java index c7389b1bda..36c52e77ff 100644 --- a/src/main/java/baseball/repository/AnswerRepository.java +++ b/src/main/java/baseball/repository/AnswerRepository.java @@ -7,8 +7,8 @@ public class AnswerRepository { - private List answer; - private List userAnswer; + private static List answer; + private static List userAnswer; public List getAnswer() { return answer; @@ -20,13 +20,14 @@ public List getUserAnswer() { public List makeAnswer() { List answer = new ArrayList<>(); - while(answer.size() < 3) { + while (answer.size() < 3) { int randomNumber = Randoms.pickNumberInRange(1, 9); - if(!answer.contains(randomNumber)) { + if (!answer.contains(randomNumber)) { answer.add(randomNumber); } } this.answer = answer; + System.out.println("answer = " + answer); return answer; } @@ -34,7 +35,7 @@ public List setUerAnswer(String answer) { // List ints = Arrays.asList(Stream.of(answer.split("")).mapToInt(Integer::parseInt).toArray()); List userAnswer = new ArrayList(); String[] splitAnswer = answer.split(""); - for(String tmp : splitAnswer) { + for (String tmp : splitAnswer) { userAnswer.add(Integer.parseInt(tmp)); } this.userAnswer = userAnswer; From 8bbf19b067b5edce7cf06f4ee8c035579b8b7116 Mon Sep 17 00:00:00 2001 From: yeonjy Date: Wed, 17 May 2023 12:02:20 +0900 Subject: [PATCH 07/29] =?UTF-8?q?refactor:=20String=20->=20Map=20=ED=83=80?= =?UTF-8?q?=EC=9E=85=EC=9C=BC=EB=A1=9C=20=EB=B0=98=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../baseball/service/BaseballService.java | 69 ++++++++++++++++--- 1 file changed, 60 insertions(+), 9 deletions(-) diff --git a/src/main/java/baseball/service/BaseballService.java b/src/main/java/baseball/service/BaseballService.java index e66c020d52..631c24cc84 100644 --- a/src/main/java/baseball/service/BaseballService.java +++ b/src/main/java/baseball/service/BaseballService.java @@ -1,21 +1,58 @@ package baseball.service; + +import baseball.repository.AnswerRepository; + import java.util.List; +import java.util.Map; public class BaseballService { + private final AnswerRepository answerRepository = new AnswerRepository(); + private static final String NOTHING = "낫싱"; private static final String STRIKE = "스트라이크"; private static final String BALL = "볼"; - public String makeReply(List answer, List userAnswer) { - return makeStringReply(countStrikes(answer, userAnswer), countBalls(answer, userAnswer)); + + public List makeUserAnswer(String userAnswer) { + isCorrectUserAnswer(userAnswer); + return answerRepository.setUerAnswer(userAnswer); + } + + private void isCorrectUserAnswer(String userAnswer) { + lengthOfUserAnswer(userAnswer); + isInteger(userAnswer); + + } + + private void lengthOfUserAnswer(String userAnswer) { + if (userAnswer.length() != 3) { + throw new IllegalArgumentException("숫자는 3개만 입력해주세요."); + } + } + + private void isInteger(String userAnswer) { + try { + Integer.parseInt(userAnswer); + } catch (NumberFormatException e) { + throw new IllegalArgumentException(); + } + } + + public List makeAnswer() { + return answerRepository.makeAnswer(); + } + + public Map makeReply(List answer, List userAnswer) { + return makeMapReply(countStrikes(answer, userAnswer), countBalls(answer, userAnswer)); } private int countStrikes(List answer, List userAnswer) { int cnt = 0; - for(int i = 0; i < answer.size(); i++) { - if(answer.get(i) == userAnswer.get(i)) { + for (int i = 0; i < answer.size(); i++) { + + if (answer.get(i).equals(userAnswer.get(i))) { cnt++; } } @@ -24,19 +61,33 @@ private int countStrikes(List answer, List userAnswer) { private int countBalls(List answer, List userAnswer) { int cnt = 0; - for(int userAns : userAnswer) { - if(answer.contains(userAns)) { + for (int userAns : userAnswer) { + if (answer.contains(userAns)) { cnt++; } } return cnt; } - private String makeStringReply(int strikeCnt, int ballCnt) { - if(ballCnt == 0) { + public String makeStringReply(Map mapReply) { + StringBuilder sb = new StringBuilder(); + if (mapReply.containsKey(NOTHING)) { return NOTHING; } + if (mapReply.get(BALL) != 0) { + sb.append(mapReply.get(BALL).toString()).append(BALL).append(" "); + } + if (mapReply.get(STRIKE) != 0) { + sb.append(mapReply.get(STRIKE).toString()).append(STRIKE); + } + return sb.toString(); + } + + private Map makeMapReply(int strikeCnt, int ballCnt) { + if (ballCnt == 0) { + return Map.of(NOTHING, 0); + } ballCnt -= strikeCnt; - return strikeCnt + STRIKE + ballCnt + BALL; + return Map.of(BALL, ballCnt, STRIKE, strikeCnt); } } From ae1c4e7e7575c84afb346f5f95dd4f96d8cee1da Mon Sep 17 00:00:00 2001 From: yeonjy Date: Wed, 17 May 2023 12:06:52 +0900 Subject: [PATCH 08/29] =?UTF-8?q?chore:=20=EB=8B=B5=20=ED=99=95=EC=9D=B8?= =?UTF-8?q?=EC=9A=A9=20System.out=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/baseball/repository/AnswerRepository.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/baseball/repository/AnswerRepository.java b/src/main/java/baseball/repository/AnswerRepository.java index 36c52e77ff..3af19b73f2 100644 --- a/src/main/java/baseball/repository/AnswerRepository.java +++ b/src/main/java/baseball/repository/AnswerRepository.java @@ -27,7 +27,6 @@ public List makeAnswer() { } } this.answer = answer; - System.out.println("answer = " + answer); return answer; } From 00f92bcc83d5cb68db4e541094da2c3dc9674f8f Mon Sep 17 00:00:00 2001 From: yeonjy Date: Wed, 17 May 2023 12:07:19 +0900 Subject: [PATCH 09/29] =?UTF-8?q?test:=20repository=20test=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/AnswerRepositoryTest.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/test/java/baseball/repository/AnswerRepositoryTest.java diff --git a/src/test/java/baseball/repository/AnswerRepositoryTest.java b/src/test/java/baseball/repository/AnswerRepositoryTest.java new file mode 100644 index 0000000000..653b9f6354 --- /dev/null +++ b/src/test/java/baseball/repository/AnswerRepositoryTest.java @@ -0,0 +1,38 @@ +package baseball.repository; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + + +class AnswerRepositoryTest { + + private static AnswerRepository repository; + + @BeforeEach + void setUp() { + repository = new AnswerRepository(); + } + + @Test + void correctAnswer() { + List answer = repository.makeAnswer(); + for (int ans : answer) { + assertThat(ans > 0 && ans < 10); + } + } + + @Test + void stringToListUserAnswer() { + String userTry = "926"; + List answer = repository.setUerAnswer(userTry); + List expected = Arrays.asList(9, 2, 6); + assertThat(answer.equals(expected)); + } + + +} \ No newline at end of file From c3a563894be4cff1984a0f03214ddf7d7d6f75a5 Mon Sep 17 00:00:00 2001 From: yeonjy Date: Wed, 17 May 2023 12:07:41 +0900 Subject: [PATCH 10/29] =?UTF-8?q?test:=20service=20test=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../baseball/service/BaseballServiceTest.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/test/java/baseball/service/BaseballServiceTest.java diff --git a/src/test/java/baseball/service/BaseballServiceTest.java b/src/test/java/baseball/service/BaseballServiceTest.java new file mode 100644 index 0000000000..596d3e23b0 --- /dev/null +++ b/src/test/java/baseball/service/BaseballServiceTest.java @@ -0,0 +1,35 @@ +package baseball.service; + +import baseball.repository.AnswerRepository; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; + +class BaseballServiceTest { + + static AnswerRepository repository; + static BaseballService service; + + + @BeforeEach + void setUp() { + service = new BaseballService(); + } + + @Test + void testStringReply() { + List answer = Arrays.asList(7, 1, 3); + List userTryList = Arrays.asList(1, 2, 3); + String expected = "1볼 1스트라이크"; + Map result = service.makeReply(answer, userTryList); + assertThat(result.equals(Map.of("볼", 1, "스트라이크", 1))); + + } + + +} \ No newline at end of file From 3491ba8f8110ced41b0f5747d7ca38656bec16d4 Mon Sep 17 00:00:00 2001 From: yeonjy Date: Wed, 24 May 2023 02:37:48 +0900 Subject: [PATCH 11/29] =?UTF-8?q?refactor:=20=EC=83=88=20=EA=B0=9D?= =?UTF-8?q?=EC=B2=B4=20=EC=83=9D=EC=84=B1=20=ED=9B=84=20=EB=B0=98=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../baseball/repository/AnswerRepository.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/main/java/baseball/repository/AnswerRepository.java b/src/main/java/baseball/repository/AnswerRepository.java index 3af19b73f2..33480300dd 100644 --- a/src/main/java/baseball/repository/AnswerRepository.java +++ b/src/main/java/baseball/repository/AnswerRepository.java @@ -3,19 +3,23 @@ import camp.nextstep.edu.missionutils.Randoms; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; public class AnswerRepository { - private static List answer; - private static List userAnswer; + private List answer; + private List userAnswer; public List getAnswer() { - return answer; + return new ArrayList<>(answer); } public List getUserAnswer() { - return userAnswer; + return new ArrayList<>(userAnswer); } public List makeAnswer() { @@ -31,13 +35,11 @@ public List makeAnswer() { } public List setUerAnswer(String answer) { -// List ints = Arrays.asList(Stream.of(answer.split("")).mapToInt(Integer::parseInt).toArray()); - List userAnswer = new ArrayList(); +// List ints = Arrays.asList(Stream.of(answer.split("")).mapToInt(Integer::parseInt).collect(Collections.())); String[] splitAnswer = answer.split(""); for (String tmp : splitAnswer) { userAnswer.add(Integer.parseInt(tmp)); } - this.userAnswer = userAnswer; return userAnswer; } } \ No newline at end of file From 19d82a59eb1554785fdc8fbae7ade3c7c68bc265 Mon Sep 17 00:00:00 2001 From: yeonjy Date: Wed, 24 May 2023 03:19:19 +0900 Subject: [PATCH 12/29] refactor: play result string -> enum --- src/main/java/baseball/PlayResult.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/main/java/baseball/PlayResult.java diff --git a/src/main/java/baseball/PlayResult.java b/src/main/java/baseball/PlayResult.java new file mode 100644 index 0000000000..7b2ff816ff --- /dev/null +++ b/src/main/java/baseball/PlayResult.java @@ -0,0 +1,18 @@ +package baseball; + +public enum PlayResult { + + STRIKE("스트라이크"), + BALL("볼"), + OUT("아웃"), + NOTHING("낫싱"); + + final private String playResult; + private PlayResult(String playResult) { + this.playResult = playResult; + } + public String getPlayResult() { + return this.playResult; + } + +} From da6ada383cb2e7160d53af9baba6b2367df65ec4 Mon Sep 17 00:00:00 2001 From: yeonjy Date: Wed, 24 May 2023 03:26:44 +0900 Subject: [PATCH 13/29] =?UTF-8?q?refactor:=20=EB=B3=80=EC=88=98=20?= =?UTF-8?q?=EC=9D=B4=EB=A6=84=20=EC=A4=91=EB=B3=B5=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/baseball/repository/AnswerRepository.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/baseball/repository/AnswerRepository.java b/src/main/java/baseball/repository/AnswerRepository.java index 33480300dd..e9dd3a8d17 100644 --- a/src/main/java/baseball/repository/AnswerRepository.java +++ b/src/main/java/baseball/repository/AnswerRepository.java @@ -22,16 +22,16 @@ public List getUserAnswer() { return new ArrayList<>(userAnswer); } - public List makeAnswer() { - List answer = new ArrayList<>(); - while (answer.size() < 3) { + public List createAnswer() { + List createdAnswer = new ArrayList<>(); + while (createdAnswer.size() < 3) { int randomNumber = Randoms.pickNumberInRange(1, 9); - if (!answer.contains(randomNumber)) { - answer.add(randomNumber); + if (!createdAnswer.contains(randomNumber)) { + createdAnswer.add(randomNumber); } } - this.answer = answer; - return answer; + this.answer = createdAnswer; + return new ArrayList<>(answer); } public List setUerAnswer(String answer) { From 2974b612652ac95ce9e4f4f777542e46cd35bcd8 Mon Sep 17 00:00:00 2001 From: yeonjy Date: Wed, 24 May 2023 03:27:42 +0900 Subject: [PATCH 14/29] =?UTF-8?q?refactor:=20play=20result=20enum=20?= =?UTF-8?q?=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../baseball/service/BaseballService.java | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/main/java/baseball/service/BaseballService.java b/src/main/java/baseball/service/BaseballService.java index 631c24cc84..a0c002e433 100644 --- a/src/main/java/baseball/service/BaseballService.java +++ b/src/main/java/baseball/service/BaseballService.java @@ -1,19 +1,18 @@ package baseball.service; +import baseball.PlayResult; import baseball.repository.AnswerRepository; import java.util.List; import java.util.Map; +import static baseball.PlayResult.*; + public class BaseballService { private final AnswerRepository answerRepository = new AnswerRepository(); - private static final String NOTHING = "낫싱"; - private static final String STRIKE = "스트라이크"; - private static final String BALL = "볼"; - public List makeUserAnswer(String userAnswer) { isCorrectUserAnswer(userAnswer); @@ -41,17 +40,16 @@ private void isInteger(String userAnswer) { } public List makeAnswer() { - return answerRepository.makeAnswer(); + return answerRepository.createAnswer(); } - public Map makeReply(List answer, List userAnswer) { - return makeMapReply(countStrikes(answer, userAnswer), countBalls(answer, userAnswer)); + public Map makeReply() { + return makeMapReply(countStrikes(answerRepository.getAnswer(), answerRepository.getUserAnswer()), countBalls(answerRepository.getAnswer(), answerRepository.getUserAnswer())); } private int countStrikes(List answer, List userAnswer) { int cnt = 0; for (int i = 0; i < answer.size(); i++) { - if (answer.get(i).equals(userAnswer.get(i))) { cnt++; } @@ -69,10 +67,10 @@ private int countBalls(List answer, List userAnswer) { return cnt; } - public String makeStringReply(Map mapReply) { + public String makeStringReply(Map mapReply) { StringBuilder sb = new StringBuilder(); if (mapReply.containsKey(NOTHING)) { - return NOTHING; + return NOTHING.toString(); } if (mapReply.get(BALL) != 0) { sb.append(mapReply.get(BALL).toString()).append(BALL).append(" "); @@ -83,7 +81,7 @@ public String makeStringReply(Map mapReply) { return sb.toString(); } - private Map makeMapReply(int strikeCnt, int ballCnt) { + private Map makeMapReply(int strikeCnt, int ballCnt) { if (ballCnt == 0) { return Map.of(NOTHING, 0); } From b9a4bd787a15a447b0e1e3ac56c646b5c19eba66 Mon Sep 17 00:00:00 2001 From: yeonjy Date: Wed, 24 May 2023 07:10:53 +0900 Subject: [PATCH 15/29] =?UTF-8?q?feat:=20controller=20=EC=98=88=EC=99=B8?= =?UTF-8?q?=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/baseball/exception/ControllerException.java | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/main/java/baseball/exception/ControllerException.java diff --git a/src/main/java/baseball/exception/ControllerException.java b/src/main/java/baseball/exception/ControllerException.java new file mode 100644 index 0000000000..3be5b6bd4d --- /dev/null +++ b/src/main/java/baseball/exception/ControllerException.java @@ -0,0 +1,10 @@ +package baseball.exception; + +public class ControllerException { + + public void rightResponse(int response) { + if(response != 1 && response != 2) { + throw new IllegalArgumentException("Continue game signal is not supported"); + } + } +} From 4b5799664739e116c14ecebadb2e5fef432c3ded Mon Sep 17 00:00:00 2001 From: yeonjy Date: Wed, 24 May 2023 07:11:20 +0900 Subject: [PATCH 16/29] =?UTF-8?q?feat:=20service=20=EC=98=88=EC=99=B8=20?= =?UTF-8?q?=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../baseball/exception/ServiceException.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/main/java/baseball/exception/ServiceException.java diff --git a/src/main/java/baseball/exception/ServiceException.java b/src/main/java/baseball/exception/ServiceException.java new file mode 100644 index 0000000000..7765081cf2 --- /dev/null +++ b/src/main/java/baseball/exception/ServiceException.java @@ -0,0 +1,24 @@ +package baseball.exception; + +public class ServiceException { + + public void correctUserAnswer(String userAnswer) { + lengthOfUserAnswer(userAnswer); + integerValue(userAnswer); + } + + private void lengthOfUserAnswer(String userAnswer) { + if (userAnswer.length() != 3) { + throw new IllegalArgumentException("숫자는 3개만 입력해주세요."); + } + } + + private void integerValue(String userAnswer) { + try { + Integer.parseInt(userAnswer); + } catch (NumberFormatException e) { + throw new IllegalArgumentException(); + } + } + +} From 31a683c794fd80f2845ed13b3f539dc12e1a6e56 Mon Sep 17 00:00:00 2001 From: yeonjy Date: Wed, 24 May 2023 07:12:03 +0900 Subject: [PATCH 17/29] =?UTF-8?q?refactor:=20controller=20=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/GameProcessController.java | 47 +++++++++++++++++++ .../baseball/controller/InputController.java | 46 +++--------------- 2 files changed, 54 insertions(+), 39 deletions(-) create mode 100644 src/main/java/baseball/controller/GameProcessController.java diff --git a/src/main/java/baseball/controller/GameProcessController.java b/src/main/java/baseball/controller/GameProcessController.java new file mode 100644 index 0000000000..1338091fc3 --- /dev/null +++ b/src/main/java/baseball/controller/GameProcessController.java @@ -0,0 +1,47 @@ +package baseball.controller; + +import baseball.PlayResult; +import baseball.service.BaseballService; +import baseball.service.StringReplyService; + +import java.util.Map; + +import static baseball.PlayResult.STRIKE; + +public class GameProcessController { + private final InputController inputController; + private final BaseballService baseballService = new BaseballService(); + private final StringReplyService stringReplyService = new StringReplyService(); + + public GameProcessController(InputController inputController) { + this.inputController = inputController; + } + + public void continueGame() { + System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요"); + if (inputController.isContinueGame()) { + makeGameAnswer(); + } + } + + public void makeGameAnswer() { + baseballService.makeAnswer(); + requireUserAnswer(); + } + + public void requireUserAnswer() { + inputController.recieveUserAnswer(baseballService); + checkCorrect(); + } + + public void checkCorrect() { + Map result = baseballService.makeReply(); + System.out.println(stringReplyService.makeStringReply(result)); + if (result.containsKey(STRIKE) && result.get(STRIKE).equals(3)) { + System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); + continueGame(); + return; + } + requireUserAnswer(); + } +} diff --git a/src/main/java/baseball/controller/InputController.java b/src/main/java/baseball/controller/InputController.java index 8495c1355e..8d06fac759 100644 --- a/src/main/java/baseball/controller/InputController.java +++ b/src/main/java/baseball/controller/InputController.java @@ -1,54 +1,22 @@ package baseball.controller; -import baseball.repository.AnswerRepository; +import baseball.exception.ControllerException; import baseball.service.BaseballService; import camp.nextstep.edu.missionutils.Console; -import java.util.List; -import java.util.Map; - public class InputController { - private static final String NOTHING = "낫싱"; - - - private BaseballService baseballService = new BaseballService(); - private AnswerRepository answerRepository = new AnswerRepository(); + final ControllerException controllerException = new ControllerException(); - private static final String STRIKE = "스트라이크"; - - public void continueGame() { - System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요"); - int continueGame = Integer.parseInt(Console.readLine()); - if (continueGame == 1) { - requireAnswer(); - } - if (continueGame == 2) { - return; - } - } - - public void requireAnswer() { - baseballService.makeAnswer(); - recieveUserAnswer(); - } - - public void recieveUserAnswer() { + public void recieveUserAnswer(BaseballService baseballService) { System.out.print("숫자를 입력해주세요 : "); String userAnswer = Console.readLine(); baseballService.makeUserAnswer(userAnswer); - checkCorrect(); } - - public void checkCorrect() { - Map result = baseballService.makeReply(answerRepository.getAnswer(), answerRepository.getUserAnswer()); - System.out.println(baseballService.makeStringReply(result)); - if (result.containsKey(STRIKE) && result.get(STRIKE).equals(3)) { - System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); - continueGame(); - return; - } - recieveUserAnswer(); + public boolean isContinueGame() { + int continueGame = Integer.parseInt(Console.readLine()); + controllerException.rightResponse(continueGame); + return continueGame == 1; } } From ee25364b6af1711c52adbd0615ae8dca95557bfd Mon Sep 17 00:00:00 2001 From: yeonjy Date: Wed, 24 May 2023 07:15:23 +0900 Subject: [PATCH 18/29] =?UTF-8?q?refactor:=20userAnswer=20=EC=B4=88?= =?UTF-8?q?=EA=B8=B0=ED=99=94=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../baseball/repository/AnswerRepository.java | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/main/java/baseball/repository/AnswerRepository.java b/src/main/java/baseball/repository/AnswerRepository.java index e9dd3a8d17..66c99502e6 100644 --- a/src/main/java/baseball/repository/AnswerRepository.java +++ b/src/main/java/baseball/repository/AnswerRepository.java @@ -1,13 +1,9 @@ package baseball.repository; import camp.nextstep.edu.missionutils.Randoms; - import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; import java.util.List; -import java.util.stream.Collectors; -import java.util.stream.Stream; + public class AnswerRepository { @@ -25,21 +21,29 @@ public List getUserAnswer() { public List createAnswer() { List createdAnswer = new ArrayList<>(); while (createdAnswer.size() < 3) { - int randomNumber = Randoms.pickNumberInRange(1, 9); - if (!createdAnswer.contains(randomNumber)) { - createdAnswer.add(randomNumber); - } + createdAnswer = createComputerAnswer(createdAnswer); } this.answer = createdAnswer; return new ArrayList<>(answer); } + private List createComputerAnswer(List createdAnswer) { + int randomNumber = Randoms.pickNumberInRange(1, 9); + if (!createdAnswer.contains(randomNumber)) { + createdAnswer.add(randomNumber); + } + return createAnswer(); + } + + + public List setUerAnswer(String answer) { -// List ints = Arrays.asList(Stream.of(answer.split("")).mapToInt(Integer::parseInt).collect(Collections.())); String[] splitAnswer = answer.split(""); + ArrayList userAnswerTmp = new ArrayList<>(); for (String tmp : splitAnswer) { - userAnswer.add(Integer.parseInt(tmp)); + userAnswerTmp.add(Integer.parseInt(tmp)); } - return userAnswer; + userAnswer = userAnswerTmp; + return new ArrayList<>(userAnswer); } } \ No newline at end of file From 564da8cdbe3a7eeba87fe135c0082355977d2b7b Mon Sep 17 00:00:00 2001 From: yeonjy Date: Wed, 24 May 2023 07:16:19 +0900 Subject: [PATCH 19/29] =?UTF-8?q?refactor:=20service=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../baseball/service/BaseballService.java | 38 ++----------------- .../baseball/service/StringReplyService.java | 35 +++++++++++++++++ 2 files changed, 38 insertions(+), 35 deletions(-) create mode 100644 src/main/java/baseball/service/StringReplyService.java diff --git a/src/main/java/baseball/service/BaseballService.java b/src/main/java/baseball/service/BaseballService.java index a0c002e433..ed8d0955c8 100644 --- a/src/main/java/baseball/service/BaseballService.java +++ b/src/main/java/baseball/service/BaseballService.java @@ -1,6 +1,7 @@ package baseball.service; import baseball.PlayResult; +import baseball.exception.ServiceException; import baseball.repository.AnswerRepository; import java.util.List; @@ -11,34 +12,15 @@ public class BaseballService { + private final ServiceException serviceException = new ServiceException(); private final AnswerRepository answerRepository = new AnswerRepository(); public List makeUserAnswer(String userAnswer) { - isCorrectUserAnswer(userAnswer); + serviceException.correctUserAnswer(userAnswer); return answerRepository.setUerAnswer(userAnswer); } - private void isCorrectUserAnswer(String userAnswer) { - lengthOfUserAnswer(userAnswer); - isInteger(userAnswer); - - } - - private void lengthOfUserAnswer(String userAnswer) { - if (userAnswer.length() != 3) { - throw new IllegalArgumentException("숫자는 3개만 입력해주세요."); - } - } - - private void isInteger(String userAnswer) { - try { - Integer.parseInt(userAnswer); - } catch (NumberFormatException e) { - throw new IllegalArgumentException(); - } - } - public List makeAnswer() { return answerRepository.createAnswer(); } @@ -67,20 +49,6 @@ private int countBalls(List answer, List userAnswer) { return cnt; } - public String makeStringReply(Map mapReply) { - StringBuilder sb = new StringBuilder(); - if (mapReply.containsKey(NOTHING)) { - return NOTHING.toString(); - } - if (mapReply.get(BALL) != 0) { - sb.append(mapReply.get(BALL).toString()).append(BALL).append(" "); - } - if (mapReply.get(STRIKE) != 0) { - sb.append(mapReply.get(STRIKE).toString()).append(STRIKE); - } - return sb.toString(); - } - private Map makeMapReply(int strikeCnt, int ballCnt) { if (ballCnt == 0) { return Map.of(NOTHING, 0); diff --git a/src/main/java/baseball/service/StringReplyService.java b/src/main/java/baseball/service/StringReplyService.java new file mode 100644 index 0000000000..82cc738fbc --- /dev/null +++ b/src/main/java/baseball/service/StringReplyService.java @@ -0,0 +1,35 @@ +package baseball.service; + +import baseball.PlayResult; + +import java.util.Map; + +import static baseball.PlayResult.*; +import static baseball.PlayResult.STRIKE; + +public class StringReplyService { + + public String makeStringReply(Map mapReply) { + StringBuilder sb = new StringBuilder(); + if (mapReply.containsKey(NOTHING)) { + return NOTHING.toString(); + } + sb.append(makeBallString(mapReply)); + sb.append(makeStrikeString(mapReply)); + return sb.toString(); + } + + private String makeBallString(Map mapReply) { + if (mapReply.get(BALL) != 0) { + return mapReply.get(BALL).toString() + BALL + " "; + } + return ""; + } + private String makeStrikeString(Map mapReply) { + if (mapReply.get(STRIKE) != 0) { + return mapReply.get(STRIKE).toString() + STRIKE; + } + return ""; + } + +} From af1b850d338bc1604d37645340b8ddfe0c244d1f Mon Sep 17 00:00:00 2001 From: yeonjy Date: Wed, 24 May 2023 07:18:13 +0900 Subject: [PATCH 20/29] =?UTF-8?q?refactor:=20=EB=B6=84=EB=A6=AC=20controll?= =?UTF-8?q?er=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/baseball/Application.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index 851dfdccfa..5db4aabe80 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -1,11 +1,12 @@ package baseball; +import baseball.controller.GameProcessController; import baseball.controller.InputController; public class Application { public static void main(String[] args) { - InputController inputController = new InputController(); + GameProcessController gameProcessController = new GameProcessController(new InputController()); System.out.println("숫자 야구 게임을 시작합니다."); - inputController.requireAnswer(); + gameProcessController.makeGameAnswer(); } } From b658848353eae23aae3c9d22a20c79df74f6b6fb Mon Sep 17 00:00:00 2001 From: yeonjy <81320703+yeonjy@users.noreply.github.com> Date: Wed, 24 May 2023 13:06:02 +0900 Subject: [PATCH 21/29] Update README.md --- README.md | 170 +++++------------------------------------------------- 1 file changed, 15 insertions(+), 155 deletions(-) diff --git a/README.md b/README.md index b50d652cca..5194496a16 100644 --- a/README.md +++ b/README.md @@ -1,155 +1,15 @@ -# 미션 - 숫자 야구 - -## 🔍 진행 방식 - -- 미션은 **기능 요구 사항, 프로그래밍 요구 사항, 과제 진행 요구 사항** 세 가지로 구성되어 있다. -- 세 개의 요구 사항을 만족하기 위해 노력한다. 특히 기능을 구현하기 전에 기능 목록을 만들고, 기능 단위로 커밋 하는 방식으로 진행한다. -- 기능 요구 사항에 기재되지 않은 내용은 스스로 판단하여 구현한다. - -## 📮 미션 제출 방법 - -- 미션 구현을 완료한 후 GitHub을 통해 제출해야 한다. - - GitHub을 활용한 제출 방법은 [프리코스 과제 제출](https://github.com/woowacourse/woowacourse-docs/tree/master/precourse) 문서를 참고해 - 제출한다. -- GitHub에 미션을 제출한 후 [우아한테크코스 지원](https://apply.techcourse.co.kr) 사이트에 접속하여 프리코스 과제를 제출한다. - - 자세한 방법은 [제출 가이드](https://github.com/woowacourse/woowacourse-docs/tree/master/precourse#제출-가이드) 참고 - - **Pull Request만 보내고 지원 플랫폼에서 과제를 제출하지 않으면 최종 제출하지 않은 것으로 처리되니 주의한다.** - -## 🚨 과제 제출 전 체크 리스트 - 0점 방지 - -- 기능 구현을 모두 정상적으로 했더라도 **요구 사항에 명시된 출력값 형식을 지키지 않을 경우 0점으로 처리**한다. -- 기능 구현을 완료한 뒤 아래 가이드에 따라 테스트를 실행했을 때 모든 테스트가 성공하는지 확인한다. -- **테스트가 실패할 경우 0점으로 처리**되므로, 반드시 확인 후 제출한다. - -### 테스트 실행 가이드 - -- 터미널에서 `java -version`을 실행하여 Java 버전이 11인지 확인한다. 또는 Eclipse 또는 IntelliJ IDEA와 같은 IDE에서 Java 11로 실행되는지 확인한다. -- 터미널에서 Mac 또는 Linux 사용자의 경우 `./gradlew clean test` 명령을 실행하고, - Windows 사용자의 경우 `gradlew.bat clean test` 명령을 실행할 때 모든 테스트가 아래와 같이 통과하는지 확인한다. - -``` -BUILD SUCCESSFUL in 0s -``` - ---- - -## 🚀 기능 요구 사항 - -기본적으로 1부터 9까지 서로 다른 수로 이루어진 3자리의 수를 맞추는 게임이다. - -- 같은 수가 같은 자리에 있으면 스트라이크, 다른 자리에 있으면 볼, 같은 수가 전혀 없으면 낫싱이란 힌트를 얻고, 그 힌트를 이용해서 먼저 상대방(컴퓨터)의 수를 맞추면 승리한다. - - 예) 상대방(컴퓨터)의 수가 425일 때 - - 123을 제시한 경우 : 1스트라이크 - - 456을 제시한 경우 : 1볼 1스트라이크 - - 789를 제시한 경우 : 낫싱 -- 위 숫자 야구 게임에서 상대방의 역할을 컴퓨터가 한다. 컴퓨터는 1에서 9까지 서로 다른 임의의 수 3개를 선택한다. 게임 플레이어는 컴퓨터가 생각하고 있는 서로 다른 3개의 숫자를 입력하고, 컴퓨터는 입력한 숫자에 대한 - 결과를 출력한다. -- 이 같은 과정을 반복해 컴퓨터가 선택한 3개의 숫자를 모두 맞히면 게임이 종료된다. -- 게임을 종료한 후 게임을 다시 시작하거나 완전히 종료할 수 있다. -- 사용자가 잘못된 값을 입력할 경우 `IllegalArgumentException`을 발생시킨 후 애플리케이션은 종료되어야 한다. - -### 입출력 요구 사항 - -#### 입력 - -- 서로 다른 3자리의 수 -- 게임이 끝난 경우 재시작/종료를 구분하는 1과 2 중 하나의 수 - -#### 출력 - -- 입력한 수에 대한 결과를 볼, 스트라이크 개수로 표시 - -``` -1볼 1스트라이크 -``` - -- 하나도 없는 경우 - -``` -낫싱 -``` - -- 3개의 숫자를 모두 맞힐 경우 - -``` -3스트라이크 -3개의 숫자를 모두 맞히셨습니다! 게임 종료 -``` - -- 게임 시작 문구 출력 - -``` -숫자 야구 게임을 시작합니다. -``` - -#### 실행 결과 예시 - -``` -숫자 야구 게임을 시작합니다. -숫자를 입력해주세요 : 123 -1볼 1스트라이크 -숫자를 입력해주세요 : 145 -1볼 -숫자를 입력해주세요 : 671 -2볼 -숫자를 입력해주세요 : 216 -1스트라이크 -숫자를 입력해주세요 : 713 -3스트라이크 -3개의 숫자를 모두 맞히셨습니다! 게임 종료 -게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요. -1 -숫자를 입력해주세요 : 123 -1볼 -... -``` - ---- - -## 🎯 프로그래밍 요구 사항 - -- JDK 11 버전에서 실행 가능해야 한다. **JDK 11에서 정상적으로 동작하지 않을 경우 0점 처리한다.** -- 프로그램 실행의 시작점은 `Application`의 `main()`이다. -- `build.gradle` 파일을 변경할 수 없고, 외부 라이브러리를 사용하지 않는다. -- [Java 코드 컨벤션](https://github.com/woowacourse/woowacourse-docs/tree/master/styleguide/java) 가이드를 준수하며 프로그래밍한다. -- 프로그램 종료 시 `System.exit()`를 호출하지 않는다. -- 프로그램 구현이 완료되면 `ApplicationTest`의 모든 테스트가 성공해야 한다. **테스트가 실패할 경우 0점 처리한다.** -- 프로그래밍 요구 사항에서 달리 명시하지 않는 한 파일, 패키지 이름을 수정하거나 이동하지 않는다. - -### 추가된 요구 사항 - -- indent(인덴트, 들여쓰기) depth를 3이 넘지 않도록 구현한다. 2까지만 허용한다. - - 예를 들어 while문 안에 if문이 있으면 들여쓰기는 2이다. - - 힌트: indent(인덴트, 들여쓰기) depth를 줄이는 좋은 방법은 함수(또는 메서드)를 분리하면 된다. -- 3항 연산자를 쓰지 않는다. -- 함수(또는 메서드)가 한 가지 일만 하도록 최대한 작게 만들어라. -- JUnit 5와 AssertJ를 이용하여 본인이 정리한 기능 목록이 정상 동작함을 테스트 코드로 확인한다. - - 테스트 도구 사용법이 익숙하지 않다면 `test/java/study`를 참고하여 학습한 후 테스트를 구현한다. - -### 라이브러리 - -- `camp.nextstep.edu.missionutils`에서 제공하는 `Randoms` 및 `Console` API를 사용하여 구현해야 한다. - - Random 값 추출은 `camp.nextstep.edu.missionutils.Randoms`의 `pickNumberInRange()`를 활용한다. - - 사용자가 입력하는 값은 `camp.nextstep.edu.missionutils.Console`의 `readLine()`을 활용한다. - -#### 사용 예시 - -```java -List computer = new ArrayList<>(); -while (computer.size() < 3) { - int randomNumber = Randoms.pickNumberInRange(1, 9); - if (!computer.contains(randomNumber)) { - computer.add(randomNumber); - } -} -``` - ---- - -## ✏️ 과제 진행 요구 사항 - -- 미션은 [java-baseball](https://github.com/woowacourse-precourse/java-baseball) 저장소를 Fork & Clone해 시작한다. -- **기능을 구현하기 전 `docs/README.md`에 구현할 기능 목록을 정리**해 추가한다. -- **Git의 커밋 단위는 앞 단계에서 `docs/README.md`에 정리한 기능 목록 단위**로 추가한다. - - [커밋 메시지 컨벤션](https://gist.github.com/stephenparish/9941e89d80e2bc58a153) 가이드를 참고해 커밋 메시지를 작성한다. -- 과제 진행 및 제출 방법은 [프리코스 과제 제출](https://github.com/woowacourse/woowacourse-docs/tree/master/precourse) 문서를 참고한다. +- 기능 목록 + - 정답 생성 기능 + - 정답 저장 기능 + - 사용자 정답 input 기능 + - 사용자 정답 판단 기능 + - STRIKE 수 카운트 기능 + - BALL 수 카운트 기능 + - NOTHING 수 카운트 기능 + - 사용자 정답 STRIKE, BALL, NOTHING 문자열 추출 기능 + - 정답 시, 게임 계속 진행 여부 사용자에게 input 받는 기능 + +- 예외 처리 기능 + - 사용자 입력 값 숫자 여부 판단 기능 + - 사용자 정답 숫자의 개수 3개 판단 기능 + - 게임 계속 진행 여부 input 값 1과 2만 있는지 판단 기능 From daa90801d12a427f32f3811b48d2185c53788531 Mon Sep 17 00:00:00 2001 From: yeonjy Date: Wed, 24 May 2023 13:47:33 +0900 Subject: [PATCH 22/29] fix: createAnswer() -> createdAnswer --- src/main/java/baseball/repository/AnswerRepository.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/baseball/repository/AnswerRepository.java b/src/main/java/baseball/repository/AnswerRepository.java index 66c99502e6..64c01ed13a 100644 --- a/src/main/java/baseball/repository/AnswerRepository.java +++ b/src/main/java/baseball/repository/AnswerRepository.java @@ -32,7 +32,7 @@ private List createComputerAnswer(List createdAnswer) { if (!createdAnswer.contains(randomNumber)) { createdAnswer.add(randomNumber); } - return createAnswer(); + return createdAnswer; } From 6b142fc0cd66fdd37b26dd693f2e6e9ebc2b5547 Mon Sep 17 00:00:00 2001 From: yeonjy Date: Wed, 24 May 2023 13:59:07 +0900 Subject: [PATCH 23/29] =?UTF-8?q?refactor:=20enum=20=ED=95=9C=EA=B8=80?= =?UTF-8?q?=EB=A1=9C=20=EB=B3=80=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/baseball/PlayResult.java | 2 +- src/main/java/baseball/service/StringReplyService.java | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/baseball/PlayResult.java b/src/main/java/baseball/PlayResult.java index 7b2ff816ff..ea5f21b5a8 100644 --- a/src/main/java/baseball/PlayResult.java +++ b/src/main/java/baseball/PlayResult.java @@ -12,7 +12,7 @@ private PlayResult(String playResult) { this.playResult = playResult; } public String getPlayResult() { - return this.playResult; + return playResult; } } diff --git a/src/main/java/baseball/service/StringReplyService.java b/src/main/java/baseball/service/StringReplyService.java index 82cc738fbc..c3069feea2 100644 --- a/src/main/java/baseball/service/StringReplyService.java +++ b/src/main/java/baseball/service/StringReplyService.java @@ -12,7 +12,7 @@ public class StringReplyService { public String makeStringReply(Map mapReply) { StringBuilder sb = new StringBuilder(); if (mapReply.containsKey(NOTHING)) { - return NOTHING.toString(); + return NOTHING.getPlayResult(); } sb.append(makeBallString(mapReply)); sb.append(makeStrikeString(mapReply)); @@ -21,13 +21,13 @@ public String makeStringReply(Map mapReply) { private String makeBallString(Map mapReply) { if (mapReply.get(BALL) != 0) { - return mapReply.get(BALL).toString() + BALL + " "; + return mapReply.get(BALL).toString() + BALL.getPlayResult() + " "; } return ""; } private String makeStrikeString(Map mapReply) { if (mapReply.get(STRIKE) != 0) { - return mapReply.get(STRIKE).toString() + STRIKE; + return mapReply.get(STRIKE).toString() + STRIKE.getPlayResult(); } return ""; } From 1f004eab095ffcf3f33b0df6477c36c24600ce59 Mon Sep 17 00:00:00 2001 From: yeonjy Date: Wed, 31 May 2023 10:55:15 +0900 Subject: [PATCH 24/29] =?UTF-8?q?refactor:=20PlayResult=20static=20import?= =?UTF-8?q?=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/GameProcessController.java | 4 ++-- .../java/baseball/service/BaseballService.java | 6 +++--- .../java/baseball/service/StringReplyService.java | 15 ++++++--------- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/src/main/java/baseball/controller/GameProcessController.java b/src/main/java/baseball/controller/GameProcessController.java index 1338091fc3..c166cbac57 100644 --- a/src/main/java/baseball/controller/GameProcessController.java +++ b/src/main/java/baseball/controller/GameProcessController.java @@ -6,7 +6,7 @@ import java.util.Map; -import static baseball.PlayResult.STRIKE; + public class GameProcessController { private final InputController inputController; @@ -37,7 +37,7 @@ public void requireUserAnswer() { public void checkCorrect() { Map result = baseballService.makeReply(); System.out.println(stringReplyService.makeStringReply(result)); - if (result.containsKey(STRIKE) && result.get(STRIKE).equals(3)) { + if (result.containsKey(PlayResult.STRIKE) && result.get(PlayResult.STRIKE).equals(3)) { System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); continueGame(); return; diff --git a/src/main/java/baseball/service/BaseballService.java b/src/main/java/baseball/service/BaseballService.java index ed8d0955c8..85836fa739 100644 --- a/src/main/java/baseball/service/BaseballService.java +++ b/src/main/java/baseball/service/BaseballService.java @@ -1,7 +1,7 @@ package baseball.service; import baseball.PlayResult; -import baseball.exception.ServiceException; +import baseball.exception.InputValidator; import baseball.repository.AnswerRepository; import java.util.List; @@ -12,12 +12,12 @@ public class BaseballService { - private final ServiceException serviceException = new ServiceException(); + private final InputValidator inputValidator = new InputValidator(); private final AnswerRepository answerRepository = new AnswerRepository(); public List makeUserAnswer(String userAnswer) { - serviceException.correctUserAnswer(userAnswer); + inputValidator.correctUserAnswer(userAnswer); return answerRepository.setUerAnswer(userAnswer); } diff --git a/src/main/java/baseball/service/StringReplyService.java b/src/main/java/baseball/service/StringReplyService.java index c3069feea2..b1ab41a276 100644 --- a/src/main/java/baseball/service/StringReplyService.java +++ b/src/main/java/baseball/service/StringReplyService.java @@ -4,15 +4,12 @@ import java.util.Map; -import static baseball.PlayResult.*; -import static baseball.PlayResult.STRIKE; - public class StringReplyService { public String makeStringReply(Map mapReply) { StringBuilder sb = new StringBuilder(); - if (mapReply.containsKey(NOTHING)) { - return NOTHING.getPlayResult(); + if (mapReply.containsKey(PlayResult.NOTHING)) { + return PlayResult.NOTHING.getPlayResult(); } sb.append(makeBallString(mapReply)); sb.append(makeStrikeString(mapReply)); @@ -20,14 +17,14 @@ public String makeStringReply(Map mapReply) { } private String makeBallString(Map mapReply) { - if (mapReply.get(BALL) != 0) { - return mapReply.get(BALL).toString() + BALL.getPlayResult() + " "; + if (mapReply.get(PlayResult.BALL) != 0) { + return mapReply.get(PlayResult.BALL).toString() + PlayResult.BALL.getPlayResult() + " "; } return ""; } private String makeStrikeString(Map mapReply) { - if (mapReply.get(STRIKE) != 0) { - return mapReply.get(STRIKE).toString() + STRIKE.getPlayResult(); + if (mapReply.get(PlayResult.STRIKE) != 0) { + return mapReply.get(PlayResult.STRIKE).toString() + PlayResult.STRIKE.getPlayResult(); } return ""; } From dbfe04fc20e25cd3c075b67de9ec5f5aea1bc797 Mon Sep 17 00:00:00 2001 From: yeonjy Date: Wed, 31 May 2023 10:56:04 +0900 Subject: [PATCH 25/29] refactor: ServiceException -> InputValidator --- .../exception/{ServiceException.java => InputValidator.java} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/main/java/baseball/exception/{ServiceException.java => InputValidator.java} (94%) diff --git a/src/main/java/baseball/exception/ServiceException.java b/src/main/java/baseball/exception/InputValidator.java similarity index 94% rename from src/main/java/baseball/exception/ServiceException.java rename to src/main/java/baseball/exception/InputValidator.java index 7765081cf2..98feaa0d3d 100644 --- a/src/main/java/baseball/exception/ServiceException.java +++ b/src/main/java/baseball/exception/InputValidator.java @@ -1,6 +1,6 @@ package baseball.exception; -public class ServiceException { +public class InputValidator { public void correctUserAnswer(String userAnswer) { lengthOfUserAnswer(userAnswer); From 7df3007f7ce33c4f3d74c8e9df7c1a90eb9c41ac Mon Sep 17 00:00:00 2001 From: yeonjy Date: Wed, 31 May 2023 11:32:50 +0900 Subject: [PATCH 26/29] =?UTF-8?q?refactor:=20print=20controller=20?= =?UTF-8?q?=EC=B6=94=EC=B6=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/baseball/Application.java | 3 ++- .../controller/GameProcessController.java | 16 +++++++++------- .../baseball/controller/PrintController.java | 17 +++++++++++++++++ 3 files changed, 28 insertions(+), 8 deletions(-) create mode 100644 src/main/java/baseball/controller/PrintController.java diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index 5db4aabe80..1d07e11b29 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -2,10 +2,11 @@ import baseball.controller.GameProcessController; import baseball.controller.InputController; +import baseball.controller.PrintController; public class Application { public static void main(String[] args) { - GameProcessController gameProcessController = new GameProcessController(new InputController()); + GameProcessController gameProcessController = new GameProcessController(new InputController(), new PrintController()); System.out.println("숫자 야구 게임을 시작합니다."); gameProcessController.makeGameAnswer(); } diff --git a/src/main/java/baseball/controller/GameProcessController.java b/src/main/java/baseball/controller/GameProcessController.java index c166cbac57..ba408e9fad 100644 --- a/src/main/java/baseball/controller/GameProcessController.java +++ b/src/main/java/baseball/controller/GameProcessController.java @@ -10,15 +10,17 @@ public class GameProcessController { private final InputController inputController; + private final PrintController printController; private final BaseballService baseballService = new BaseballService(); private final StringReplyService stringReplyService = new StringReplyService(); - public GameProcessController(InputController inputController) { + public GameProcessController(InputController inputController, PrintController printController) { this.inputController = inputController; + this.printController = printController; } - public void continueGame() { - System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요"); + private void continueGame() { + printController.askGameRestart(); if (inputController.isContinueGame()) { makeGameAnswer(); } @@ -29,16 +31,16 @@ public void makeGameAnswer() { requireUserAnswer(); } - public void requireUserAnswer() { + private void requireUserAnswer() { inputController.recieveUserAnswer(baseballService); checkCorrect(); } - public void checkCorrect() { + private void checkCorrect() { Map result = baseballService.makeReply(); - System.out.println(stringReplyService.makeStringReply(result)); + printController.printResult(stringReplyService.makeStringReply(result)); if (result.containsKey(PlayResult.STRIKE) && result.get(PlayResult.STRIKE).equals(3)) { - System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); + printController.successGame(); continueGame(); return; } diff --git a/src/main/java/baseball/controller/PrintController.java b/src/main/java/baseball/controller/PrintController.java new file mode 100644 index 0000000000..5dc5c38569 --- /dev/null +++ b/src/main/java/baseball/controller/PrintController.java @@ -0,0 +1,17 @@ +package baseball.controller; + +public class PrintController { + private static final String ASK_RESTART_GAME = "게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요"; + + public void askGameRestart() { + System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요"); + } + + public void successGame() { + System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); + } + + public void printResult(String reply) { + System.out.println(reply); + } +} From 1ee7de7727924afd40607b2afa045a7f3ad258a9 Mon Sep 17 00:00:00 2001 From: yeonjy Date: Wed, 31 May 2023 11:39:32 +0900 Subject: [PATCH 27/29] =?UTF-8?q?refactor:=20regex=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/baseball/exception/InputValidator.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/main/java/baseball/exception/InputValidator.java b/src/main/java/baseball/exception/InputValidator.java index 98feaa0d3d..77ca635f4f 100644 --- a/src/main/java/baseball/exception/InputValidator.java +++ b/src/main/java/baseball/exception/InputValidator.java @@ -14,11 +14,6 @@ private void lengthOfUserAnswer(String userAnswer) { } private void integerValue(String userAnswer) { - try { - Integer.parseInt(userAnswer); - } catch (NumberFormatException e) { - throw new IllegalArgumentException(); - } + userAnswer.matches("[0-9]+"); } - } From a08d94338d785bfceb02cee42667baf828112634 Mon Sep 17 00:00:00 2001 From: yeonjy Date: Wed, 31 May 2023 12:06:22 +0900 Subject: [PATCH 28/29] =?UTF-8?q?refactor:=20=EB=B3=80=EC=88=98=EB=AA=85?= =?UTF-8?q?=20=EC=A0=95=ED=99=95=ED=95=98=EA=B2=8C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/baseball/service/BaseballService.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/baseball/service/BaseballService.java b/src/main/java/baseball/service/BaseballService.java index 85836fa739..3c0bfcff93 100644 --- a/src/main/java/baseball/service/BaseballService.java +++ b/src/main/java/baseball/service/BaseballService.java @@ -7,11 +7,11 @@ import java.util.List; import java.util.Map; -import static baseball.PlayResult.*; public class BaseballService { + private final InputValidator inputValidator = new InputValidator(); private final AnswerRepository answerRepository = new AnswerRepository(); @@ -49,11 +49,11 @@ private int countBalls(List answer, List userAnswer) { return cnt; } - private Map makeMapReply(int strikeCnt, int ballCnt) { - if (ballCnt == 0) { - return Map.of(NOTHING, 0); + private Map makeMapReply(int strikeCnt, int ballTotalCnt) { + if (ballTotalCnt == 0) { + return Map.of(PlayResult.NOTHING, 0); } - ballCnt -= strikeCnt; - return Map.of(BALL, ballCnt, STRIKE, strikeCnt); + ballTotalCnt -= strikeCnt; + return Map.of(PlayResult.BALL, ballTotalCnt, PlayResult.STRIKE, strikeCnt); } } From 644febf0d0fb38fe8b5ae5f55dd24fa3fedbde3a Mon Sep 17 00:00:00 2001 From: yeonjy Date: Wed, 31 May 2023 12:07:06 +0900 Subject: [PATCH 29/29] =?UTF-8?q?refactor:=20Hard=20Coding=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/baseball/service/StringReplyService.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/baseball/service/StringReplyService.java b/src/main/java/baseball/service/StringReplyService.java index b1ab41a276..0727ac0938 100644 --- a/src/main/java/baseball/service/StringReplyService.java +++ b/src/main/java/baseball/service/StringReplyService.java @@ -6,6 +6,9 @@ public class StringReplyService { + private static final int ZERO = 0; + + public String makeStringReply(Map mapReply) { StringBuilder sb = new StringBuilder(); if (mapReply.containsKey(PlayResult.NOTHING)) { @@ -17,13 +20,13 @@ public String makeStringReply(Map mapReply) { } private String makeBallString(Map mapReply) { - if (mapReply.get(PlayResult.BALL) != 0) { + if (mapReply.get(PlayResult.BALL) != ZERO) { return mapReply.get(PlayResult.BALL).toString() + PlayResult.BALL.getPlayResult() + " "; } return ""; } private String makeStrikeString(Map mapReply) { - if (mapReply.get(PlayResult.STRIKE) != 0) { + if (mapReply.get(PlayResult.STRIKE) != ZERO) { return mapReply.get(PlayResult.STRIKE).toString() + PlayResult.STRIKE.getPlayResult(); } return "";