From 85415f87d7f595c0b00bb137b3cd8f5501c904a1 Mon Sep 17 00:00:00 2001 From: Da-Youn Date: Wed, 25 Oct 2023 23:05:06 +0900 Subject: [PATCH 1/9] =?UTF-8?q?docs=20=20:=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84=20=EB=AA=A9=EB=A1=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 docs/README.md diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 000000000..b096cdf6f --- /dev/null +++ b/docs/README.md @@ -0,0 +1,16 @@ +## ✍️ 구현 기능 목록 + +- @woowacourse/mission-utils API 사용하기 (MissionUtils) + +- 정답 숫자 만들기 + - [ ] 1~9까지 중 랜덤으로 3개의 수 선택하기 (정답 아닐 시 반복) +- 숫자 맞추기 + - [ ] 숫자 입력 받기 (3자리) + - [ ] 같은 자리에 있으면 - 스트라이크 카운트 - "(카운트)스트라이크"출력 + - [ ] 다른 자리에 있으면 - 볼 카운트 - "(카운트)볼" 출력 + - [ ] 하나도 없는 경우 - "낫싱" + - [ ] 3개의 숫자를 모두 맞힐 경우 - "3스트라이크" 3개의 숫자를 모두 맞히셨습니다! 게임 종료 +- 게임 재시작/종료 + - [ ] 입력 받기 - "게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요." + - [ ] 1 입력 - 재시작 + - [ ] 2 입력 - 종료 From fd4853a23fd30752318c56526816205ffdde5113 Mon Sep 17 00:00:00 2001 From: Da-Youn Date: Wed, 25 Oct 2023 23:13:09 +0900 Subject: [PATCH 2/9] =?UTF-8?q?docs=20=20:=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84=20=EB=AA=A9=EB=A1=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/README.md b/docs/README.md index b096cdf6f..721338ec5 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,7 +1,11 @@ ## ✍️ 구현 기능 목록 +### 사용 라이브러리 + - @woowacourse/mission-utils API 사용하기 (MissionUtils) +### 기능 + - 정답 숫자 만들기 - [ ] 1~9까지 중 랜덤으로 3개의 수 선택하기 (정답 아닐 시 반복) - 숫자 맞추기 From 48afaf6fd2e3b445dc71138a4023f2e2b0991eba Mon Sep 17 00:00:00 2001 From: Da-Youn Date: Wed, 25 Oct 2023 23:34:08 +0900 Subject: [PATCH 3/9] =?UTF-8?q?feat=20:=20=EA=B2=8C=EC=9E=84=20=EC=8B=9C?= =?UTF-8?q?=EC=9E=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/App.js b/src/App.js index c38b30d5b..84ff354e5 100644 --- a/src/App.js +++ b/src/App.js @@ -1,5 +1,16 @@ +import { Console, Random } from '@woowacourse/mission-utils'; + class App { - async play() {} + constructor() { + this.answer = null; + } + + async play() { + Console.print('숫자 야구 게임을 시작합니다.'); + this.answer = this.generateAnswer(); + await this.runGame(); + } + } export default App; From 71b4abd99c32b904c7d96fa30fd03c78c738f079 Mon Sep 17 00:00:00 2001 From: Da-Youn Date: Wed, 25 Oct 2023 23:34:35 +0900 Subject: [PATCH 4/9] =?UTF-8?q?feat=20:=20=EC=A0=95=EB=8B=B5=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/App.js b/src/App.js index 84ff354e5..044bc441b 100644 --- a/src/App.js +++ b/src/App.js @@ -11,6 +11,17 @@ class App { await this.runGame(); } + generateAnswer() { + let answer = []; + while (answer.length < 3) { + const number = String(Random.pickNumberInRange(1, 9)); + if (!answer.includes(number)) { + answer.push(number); + } + } + return answer.join(''); + } + } export default App; From 258ebd1efa1352fb2a97514ca6edb55eaa2dabc0 Mon Sep 17 00:00:00 2001 From: Da-Youn Date: Wed, 25 Oct 2023 23:34:57 +0900 Subject: [PATCH 5/9] =?UTF-8?q?feat=20:=20=EA=B2=8C=EC=9E=84=20=EC=8B=A4?= =?UTF-8?q?=ED=96=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/App.js b/src/App.js index 044bc441b..c4834d83c 100644 --- a/src/App.js +++ b/src/App.js @@ -22,6 +22,25 @@ class App { return answer.join(''); } + async runGame() { + let gameContinue = true; + while (gameContinue) { + const playerInput = await Console.readLineAsync('숫자를 입력해주세요 : '); + this.validateInput(playerInput); + + const { strike, ball } = this.compareWithAnswer(playerInput); + + if (strike === 3) { + Console.print( + `3스트라이크 +3개의 숫자를 모두 맞히셨습니다! 게임 종료` + ); + gameContinue = await this.askForRestart(); + } else { + Console.print(this.buildResultMessage({ strike, ball })); + } + } + } } export default App; From 659850efc7f9761ab29fcca27786df94326aec3e Mon Sep 17 00:00:00 2001 From: Da-Youn Date: Wed, 25 Oct 2023 23:35:17 +0900 Subject: [PATCH 6/9] =?UTF-8?q?feat=20:=20=EC=9E=85=EB=A0=A5=20=EC=9C=A0?= =?UTF-8?q?=ED=9A=A8=EC=84=B1=20=EA=B2=80=EC=82=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/App.js b/src/App.js index c4834d83c..42c59f1d6 100644 --- a/src/App.js +++ b/src/App.js @@ -41,6 +41,19 @@ class App { } } } + + validateInput(input) { + if ( + input.length !== 3 || + new Set(input).size !== 3 || + !/^[1-9]+$/.test(input) || + input.includes(' ') + ) { + throw new Error( + `[ERROR] 서로 다른 수로 이루어진 세 자리 숫자를 입력해야 합니다. (0은 제외)` + ); + } + } } export default App; From 15ac42d44d2486dfb53d0c1987cf11e27d004e23 Mon Sep 17 00:00:00 2001 From: Da-Youn Date: Wed, 25 Oct 2023 23:36:26 +0900 Subject: [PATCH 7/9] =?UTF-8?q?feat=20:=20=EC=A0=95=EB=8B=B5=20=EB=B9=84?= =?UTF-8?q?=EA=B5=90=20=EB=B0=8F=20=EA=B2=B0=EA=B3=BC=20=EB=A9=94=EC=8B=9C?= =?UTF-8?q?=EC=A7=80=20=EB=B0=98=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/App.js b/src/App.js index 42c59f1d6..3c7d00e9a 100644 --- a/src/App.js +++ b/src/App.js @@ -54,6 +54,32 @@ class App { ); } } + + compareWithAnswer(input) { + let strike = 0; + let ball = 0; + + for (let i = 0; i < 3; i++) { + if (input[i] === this.answer[i]) { + strike++; + } else if (this.answer.includes(input[i])) { + ball++; + } + } + + return { strike, ball }; + } + + buildResultMessage({ strike, ball }) { + const resultArr = []; + if (ball > 0) { + resultArr.push(`${ball}볼`); + } + if (strike > 0) { + resultArr.push(`${strike}스트라이크`); + } + return resultArr.length === 0 ? '낫싱' : resultArr.join(' '); + } } export default App; From e43274a17d0ce407483d65c958dd19b0e18d09dd Mon Sep 17 00:00:00 2001 From: Da-Youn Date: Wed, 25 Oct 2023 23:37:36 +0900 Subject: [PATCH 8/9] =?UTF-8?q?feat=20:=20=EA=B2=8C=EC=9E=84=20=EC=9E=AC?= =?UTF-8?q?=EC=8B=9C=EC=9E=91=20=EB=98=90=EB=8A=94=20=EC=A2=85=EB=A3=8C=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 --- src/App.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/App.js b/src/App.js index 3c7d00e9a..ca70bfa67 100644 --- a/src/App.js +++ b/src/App.js @@ -11,6 +11,11 @@ class App { await this.runGame(); } + async restart() { + this.answer = this.generateAnswer(); + await this.runGame(); + } + generateAnswer() { let answer = []; while (answer.length < 3) { @@ -80,6 +85,29 @@ class App { } return resultArr.length === 0 ? '낫싱' : resultArr.join(' '); } + + async askForRestart() { + let input; + while (true) { + input = await Console.readLineAsync( + '게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요. ' + ); + + if (input === '1' || input === '2') { + break; + } + } + + if (input === '1') { + this.answer = this.generateAnswer(); + return true; + } + + if (input === '2') { + Console.print('게임을 종료합니다.'); + return false; + } + } } export default App; From 8ac21a4fbbac7611611cde0675b148bf87085bda Mon Sep 17 00:00:00 2001 From: Da-Youn Date: Wed, 25 Oct 2023 23:54:37 +0900 Subject: [PATCH 9/9] =?UTF-8?q?docs=20:=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20=EB=AA=A9=EB=A1=9D=20=EC=B2=B4=ED=81=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/docs/README.md b/docs/README.md index 721338ec5..d247782a4 100644 --- a/docs/README.md +++ b/docs/README.md @@ -7,14 +7,14 @@ ### 기능 - 정답 숫자 만들기 - - [ ] 1~9까지 중 랜덤으로 3개의 수 선택하기 (정답 아닐 시 반복) + - [x] 1~9까지 중 랜덤으로 3개의 수 선택하기 (정답 아닐 시 반복) - 숫자 맞추기 - - [ ] 숫자 입력 받기 (3자리) - - [ ] 같은 자리에 있으면 - 스트라이크 카운트 - "(카운트)스트라이크"출력 - - [ ] 다른 자리에 있으면 - 볼 카운트 - "(카운트)볼" 출력 - - [ ] 하나도 없는 경우 - "낫싱" - - [ ] 3개의 숫자를 모두 맞힐 경우 - "3스트라이크" 3개의 숫자를 모두 맞히셨습니다! 게임 종료 + - [x] 숫자 입력 받기 (3자리) + - [x] 같은 자리에 있으면 - 스트라이크 카운트 - "(카운트)스트라이크"출력 + - [x] 다른 자리에 있으면 - 볼 카운트 - "(카운트)볼" 출력 + - [x] 하나도 없는 경우 - "낫싱" + - [x] 3개의 숫자를 모두 맞힐 경우 - "3스트라이크" 3개의 숫자를 모두 맞히셨습니다! 게임 종료 - 게임 재시작/종료 - - [ ] 입력 받기 - "게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요." - - [ ] 1 입력 - 재시작 - - [ ] 2 입력 - 종료 + - [x] 입력 받기 - "게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요." + - [x] 1 입력 - 재시작 + - [x] 2 입력 - 종료