From 1def0aa2d3f42026aabdd40d2a90d790bec74d0b Mon Sep 17 00:00:00 2001 From: yeonjy Date: Wed, 17 May 2023 01:09:08 +0900 Subject: [PATCH 01/10] =?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/10] =?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/10] =?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/10] =?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/10] =?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/10] =?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/10] =?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/10] =?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/10] =?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/10] =?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