-
Notifications
You must be signed in to change notification settings - Fork 2
2주차 미션 다시 구현 #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: oliviarla
Are you sure you want to change the base?
2주차 미션 다시 구현 #11
Changes from 24 commits
2c8a53d
72369e7
9ee8c96
44464e3
bf277a4
2cfa9fb
15c6b05
01dd974
8024763
eb92b3d
a9185d4
5f8046b
b294dca
19461c4
be73fe2
8829032
967f871
a1bb02d
f13c04b
01e34cb
5b260ec
6c2a62c
65d6ef8
bbf4efa
f7a0fa7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| # 숫자야구게임 | ||
| ### 룰 | ||
| 1부터 9까지 서로 다른 수로 이루어진 3자리의 수를 맞추는 게임 | ||
|
|
||
| 같은 수가 같은 자리에 있으면 `스트라이크` | ||
|
|
||
| 같은 수가 다른 자리에 있으면 `볼` | ||
|
|
||
| 같은 수가 전혀 없으면 `포볼 또는 낫싱` | ||
|
|
||
| <b>여러번 반복해 정보를 얻으며 상대방(컴퓨터)의 수를 맞추면 승리</b> | ||
|
|
||
| e.g. 상대방(컴퓨터)의 수가 425일 때 123을 제시한 경우 : `1스트라이크`, 456을 제시한 경우 : `1볼 1스트라이크`, 789를 제시한 경우 : `낫싱` | ||
|
|
||
| ### 작동 방식 | ||
|
|
||
| 1) 컴퓨터는 1에서 9까지 서로 다른 임의의 수 3개를 선택 | ||
| 2) 사용자는 컴퓨터가 생각하고 있는 3개의 숫자를 입력하고, 컴퓨터는 입력한 숫자에 대한 결과 출력 | ||
| 3) 같은 과정을 반복해 컴퓨터가 선택한 3개의 숫자를 모두 맞히면 게임 종료 | ||
| 4) 게임을 종료한 후 게임을 다시 시작하거나 완전히 종료할 수 있다 | ||
|
|
||
| ### 기능 목록 | ||
| <b>엔티티</b> | ||
| - [x] Ball: 세자리 숫자 저장하는 엔티티 클래스 | ||
| - [x] BallStatus: 유저 Ball과 랜덤 Ball 비교한 결과를 저장하는 엔티티 클래스 | ||
| - [x] Action: 동작들을 담는 enum 클래스 | ||
|
|
||
| <b>서비스</b> | ||
| - [x] BaseballService | ||
| - isBall: 볼 여부 판별 | ||
| - isStrike: 스트라이크 여부 판별 | ||
| - compare: RandomNum, UserNum 비교해 BallStatus 반환 | ||
| - [x] RandomNumGenerator: 임의의 서로다른 3자리 수 생성 | ||
|
|
||
| <b>뷰</b> | ||
| - [x] InputView: 사용자로부터 입력 받는 뷰 담당하는 클래스 | ||
| - [x] OutputView: 출력 하는 뷰 담당하는 클래스 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package baseball; | ||
|
|
||
| import baseball.domain.Baseball; | ||
| import baseball.domain.BaseballStatus; | ||
| import baseball.service.BaseballService; | ||
| import baseball.service.RandomNumGenerator; | ||
| import baseball.view.InputView; | ||
| import baseball.view.OutputView; | ||
|
|
||
| public class application { | ||
| public static void main(String[] args) throws Exception { | ||
| InputView inputView = new InputView(); | ||
| OutputView outputView = new OutputView(); | ||
| RandomNumGenerator randomNumGenerator = new RandomNumGenerator(); | ||
| BaseballService baseballService = new BaseballService(); | ||
|
|
||
| while(true){ | ||
| Baseball randomBaseball = new Baseball(randomNumGenerator.makeNum()); | ||
| BaseballStatus baseballStatus = new BaseballStatus(); | ||
|
|
||
| while(!outputView.exitGame(baseballStatus)){ | ||
| Baseball userBaseball = inputView.inputBall(); | ||
| baseballStatus = baseballService.compare(userBaseball, randomBaseball); | ||
| outputView.printBaseballStatus(baseballStatus); | ||
| } | ||
| if(!inputView.resumeGame()){ | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package baseball.domain; | ||
|
|
||
| public enum Action { | ||
| 볼, 스트라이크, 낫싱 | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package baseball.domain; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class Baseball { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. InputView에서 Balseball의 리스르 크기 검사를 Baseball 구현체에서 진행하는건 어떨까요? |
||
| List<Integer> baseballs; | ||
|
||
|
|
||
| public Baseball(List<Integer> balls) { | ||
| this.baseballs = balls; | ||
| } | ||
|
|
||
| public List<Integer> getBaseballs() { | ||
| return baseballs; | ||
| } | ||
| } | ||
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package baseball.domain; | ||
|
|
||
| public class BaseballStatus { | ||
| int ball; | ||
| int strike; | ||
|
||
|
|
||
| public int getBall() { | ||
| return ball; | ||
| } | ||
|
|
||
| public int getStrike() { | ||
| return strike; | ||
| } | ||
|
|
||
| public void setBall(int ball) { | ||
| this.ball = ball; | ||
| } | ||
|
|
||
| public void setStrike(int strike) { | ||
| this.strike = strike; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "BaseballStatus{" + | ||
| "ball=" + ball + | ||
| ", strike=" + strike + | ||
| '}'; | ||
| } | ||
|
|
||
| public boolean nothing() { | ||
| return strike == 0 && ball == 0; | ||
| } | ||
|
|
||
| public boolean existsBall() { | ||
| return ball > 0; | ||
| } | ||
|
|
||
| public boolean existsStrike() { | ||
| return strike > 0; | ||
| } | ||
|
|
||
| public boolean exitGame() { | ||
| return getStrike() == 3; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,34 @@ | ||||||||
| package baseball.service; | ||||||||
|
|
||||||||
| import baseball.domain.Baseball; | ||||||||
| import baseball.domain.BaseballStatus; | ||||||||
|
|
||||||||
| public class BaseballService { | ||||||||
| public boolean isStrike(int userNum, int randomNum) { | ||||||||
| return userNum == randomNum; | ||||||||
| } | ||||||||
|
|
||||||||
| public boolean isBall(int userNum, int userIdx, Baseball randomBall) { | ||||||||
| if (!isStrike(userNum, randomBall.getBaseballs().get(userIdx))) { | ||||||||
|
||||||||
| return randomBall.getBaseballs().contains(userNum); | ||||||||
| } | ||||||||
| return false; | ||||||||
| } | ||||||||
|
|
||||||||
| public BaseballStatus compare(Baseball userBall, Baseball randomBall) { | ||||||||
| int strike = 0, ball = 0; | ||||||||
|
||||||||
| int strike = 0, ball = 0; | |
| int strike = 0; | |
| int ball = 0; |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,16 @@ | ||||||
| package baseball.service; | ||||||
|
|
||||||
| import java.util.*; | ||||||
|
|
||||||
| public class RandomNumGenerator { | ||||||
| public List<Integer> makeNum(){ | ||||||
| Random random = new Random(); | ||||||
| Set<Integer> set = new HashSet<>(); | ||||||
|
|
||||||
| while(set.size()<3){ | ||||||
|
||||||
| int num = random.nextInt(9)+1; | ||||||
|
||||||
| int num = random.nextInt(9)+1; | |
| int num = random.nextInt(9)+1; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package baseball.view; | ||
|
|
||
| import baseball.domain.Baseball; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Scanner; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class InputView { | ||
| public Baseball inputBall() throws Exception { | ||
| Scanner scanner = new Scanner(System.in); | ||
| System.out.print("숫자를 입력해 주세요 : "); | ||
| String input = scanner.next(); | ||
| List<Integer> list = Arrays.stream(input.split("")).mapToInt(Integer::parseInt).boxed().collect(Collectors.toList()); | ||
|
||
| if(list.size()!=3){ | ||
| throw new Exception("3자리 숫자여야 합니다."); | ||
| } | ||
|
|
||
| return new Baseball(list); | ||
| } | ||
|
|
||
| public boolean resumeGame() { | ||
| Scanner scanner = new Scanner(System.in); | ||
| System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); | ||
| int input = scanner.nextInt(); | ||
| return input == 1; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package baseball.view; | ||
|
|
||
| import baseball.domain.Action; | ||
| import baseball.domain.BaseballStatus; | ||
|
|
||
| public class OutputView { | ||
| public String outputBaseballStatus(BaseballStatus baseballStatus) throws Exception { | ||
| String result = ""; | ||
| if (!baseballStatus.existsBall() && !baseballStatus.existsStrike() && !baseballStatus.nothing()) | ||
|
||
| throw new Exception("결과를 반환할 수 없습니다"); | ||
| if (baseballStatus.existsBall()) { | ||
| result += baseballStatus.getBall() + Action.볼.toString() + " "; | ||
| } | ||
| if (baseballStatus.existsStrike()) { | ||
| result += baseballStatus.getStrike() + Action.스트라이크.toString(); | ||
| } | ||
| if (baseballStatus.nothing()) { | ||
| return Action.낫싱.toString(); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| public void printBaseballStatus(BaseballStatus baseballStatus) throws Exception { | ||
| System.out.println(outputBaseballStatus(baseballStatus)); | ||
| } | ||
|
|
||
| public boolean exitGame(BaseballStatus ballStatus) { | ||
| if (ballStatus.exitGame()) { | ||
| System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package baseball.service; | ||
|
|
||
| import baseball.domain.Baseball; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.CsvSource; | ||
|
|
||
| import java.util.Arrays; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| public class BaseballServiceTest { | ||
| BaseballService baseballService; | ||
|
|
||
| @BeforeEach | ||
| void setUp(){ | ||
| baseballService= new BaseballService(); | ||
| } | ||
|
|
||
| @Test | ||
| void isStrikeTest(){ | ||
| Baseball userBall = new Baseball(Arrays.asList(1,2,3)); | ||
| Baseball randomBall = new Baseball(Arrays.asList(1,2,3)); | ||
| for(int i=0;i<3;i++){ | ||
| assertThat(baseballService.isStrike(userBall.getBaseballs().get(i), randomBall.getBaseballs().get(i))).isTrue(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void isBallTest(){ | ||
| Baseball userBall = new Baseball(Arrays.asList(1,3,2)); | ||
| Baseball randomBall = new Baseball(Arrays.asList(2,1,3)); | ||
| for(int i=0;i<3;i++){ | ||
| assertThat(baseballService.isBall(userBall.getBaseballs().get(i), i, randomBall)).isTrue(); | ||
| } | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @CsvSource({"1,2,6,1,9,2,1,1", "3,7,5,7,3,9,2,0", "1,2,3,4,5,6,0,0", "3,4,8,3,4,2,0,2"}) | ||
| void compareTest(int u1,int u2,int u3,int r1,int r2,int r3, int e1, int e2){ | ||
| Baseball userBall = new Baseball(Arrays.asList(u1, u2, u3)); | ||
| Baseball randomBall = new Baseball(Arrays.asList(r1, r2, r3)); | ||
|
|
||
| assertThat(new int[]{baseballService.compare(userBall, randomBall).getBall(), baseballService.compare(userBall, randomBall).getStrike()}).isEqualTo(new int[]{e1, e2}); | ||
| //assertThat().isEqualTo(ballStatus.getStrike()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package baseball.service; | ||
|
|
||
| import baseball.domain.Baseball; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| public class RandomNumTest { | ||
| RandomNumGenerator randomNumGenerator; | ||
|
|
||
| @BeforeEach | ||
| public void setUp() { | ||
| randomNumGenerator = new RandomNumGenerator(); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("3개의 서로다른 숫자가 생성되는지 확인하는 테스트입니다.") | ||
| public void makeNumTest() { | ||
| List<Integer> list = randomNumGenerator.makeNum(); | ||
|
|
||
| Baseball randomNum = new Baseball(list); | ||
| assertThat(randomNum.getBaseballs().size()).isEqualTo(3); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("1부터 9 사이의 값인지 확인하는 테스트입니다.") | ||
| public void rangeTest() { | ||
| List<Integer> list = randomNumGenerator.makeNum(); | ||
|
|
||
| assertThat(list.stream().filter(integer -> integer >= 1 && integer <= 9).count()).isEqualTo(3); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package baseball.view; | ||
|
|
||
| import baseball.domain.Baseball; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.io.*; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| public class InputViewTest { | ||
| InputView inputView; | ||
| Baseball baseball; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| inputView = new InputView(); | ||
| String input = "349"; | ||
| OutputStream out = new ByteArrayOutputStream(); | ||
| System.setOut(new PrintStream(out)); | ||
| InputStream in = new ByteArrayInputStream(input.getBytes()); | ||
| System.setIn(in); | ||
| } | ||
|
|
||
| @Test | ||
| void countTest() throws Exception { | ||
| baseball = inputView.inputBall(); | ||
| assertThat(baseball.getBaseballs().size()).isEqualTo(3); | ||
| } | ||
|
|
||
| @Test | ||
| void rangeTest() throws Exception { | ||
| baseball = inputView.inputBall(); | ||
| assertThat(baseball.getBaseballs().stream().filter(b -> b < 10 && b > 0).count()).isEqualTo(3); | ||
| } | ||
|
|
||
| @Test | ||
| void resumeTest() { | ||
| String data = "1"; | ||
| InputStream in = new ByteArrayInputStream(data.getBytes()); | ||
| System.setIn(in); | ||
| assertThat(inputView.resumeGame()).isTrue(); | ||
| } | ||
| } |

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이런 방법은 어떨까요?☺️