From 2814fa7dcf9ff47450df6747beab72238ad03db7 Mon Sep 17 00:00:00 2001 From: delee3064 Date: Thu, 19 Oct 2023 18:21:33 +0900 Subject: [PATCH 1/9] docs: add feature list README.md file --- docs/README.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 docs/README.md diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 000000000..9a1e36ea7 --- /dev/null +++ b/docs/README.md @@ -0,0 +1,2 @@ +## 구현할 기능 목록 +- \ No newline at end of file From 3df670cca594a7639462e958c427c233c5915559 Mon Sep 17 00:00:00 2001 From: delee3064 Date: Mon, 23 Oct 2023 15:15:04 +0900 Subject: [PATCH 2/9] =?UTF-8?q?docs=20:=20=EA=B5=AC=ED=98=84=ED=95=A0=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EB=AA=A9=EB=A1=9D=20=EB=82=B4=EC=9A=A9=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index 9a1e36ea7..543716d74 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,2 +1,47 @@ ## 구현할 기능 목록 -- \ No newline at end of file +#### 1. 컴퓨터가 랜덤으로 1에서 9까지 서로 다른 임의의 수 3개를 선택한다. +- @woowacourse/mission-utils의 Random 및 Console API를 사용하여 구현해야 한다. +- Random 값 추출은 Random.pickNumberInRange()를 활용한다. + +#### 2. 게임이 시작되어 '숫자 야구 게임을 시작합니다.' 를 출력한다. + +#### 3. 게임 플레이어로부터 서로 다른 3자리의 수를 입력받는다. +- 입력을 받을 때 '숫자를 입력해주세요 : '를 출력한다. +- 사용자의 값을 입력 받고 출력하기 위해서는 Console.readLineAsync, Console.print를 활용한다. + +#### 4. 컴퓨터는 사용자의 입력과 자신이 선택한 숫자를 비교 후 결과 값을 다음의 규칙에 따라 출력한다. + +[규칙] +1) 같은 수가 자리에 있으면 **스트라이크** +2) 같은 수가 다른 자리에 있으면 **볼** +3) 같은 수가 전혀 없으면 **낫싱** + +[출력] +1) 스트라이크, 볼의 경우가 하나도 없을 때 : **낫싱** + +2) 스트라이크는 없으나 볼의 경우가 존재할 때 : **x볼** +- x는 (1~3)의 범위 내에 존재한다. + +3) 볼은 없으나 스트라이크의 경우만 존재할 때 : **y스트라이크** +- y는 (1~3)의 범위 내에 존재한다. +- 3스트라이크가 된다면 답을 맞춘 것이다. + +4) 스트라이크와 볼의 경우가 모두 존재할 때 : **x볼 y스트라이크** +- x는 (1~2)의 범위 내에 존재하며 y는 1만 가능하다. +- 1볼 2스트라이크는 존재할 수 없는 경우의 수이다. + +#### 5. 정답을 맞출때까지 3번~4번의 과정을 반복한다. +- 스트라이크가 3이 되면 컴퓨터가 랜덤으로 선택한 숫자를 맞췄으므로 게임을 종료한다. +- 정답을 맞추면 '3개의 숫자를 모두 맞히셨습니다! 게임 종료', '게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.' 를 출력한다. + +#### 6. 사용자로부터 1 또는 2의 입력을 받는다. +- 1을 입력받으면 1번의 과정으로 돌아가 게임을 새롭게 시작한다. +- 2를 입력받으면 게임을 완전히 종료한다. 프로그램 종료 시 process.exit()를 호출하지 않는다. + +#### 7. 예외처리 +- 사용자가 잘못된 값을 입력한 경우 throw문을 사용해 예외를 발생시킨후 애플리케이션은 종료되어야 한다. + - 3번의 입력에서 사용자가 서로 다른 3자리의 수를 입력하지 않은 경우 + - 6번의 입력에서 사용자가 1 또는 2가 아닌 다른 것을 입력한 경우 + + +#### 8. 프로그램 구현이 완료되면 ApplicationTest의 모든 테스트가 성공해야 한다. \ No newline at end of file From 98435cdd0dfe2f38c05ed1fdd4ad802d2577e727 Mon Sep 17 00:00:00 2001 From: delee3064 Date: Wed, 25 Oct 2023 17:40:50 +0900 Subject: [PATCH 3/9] =?UTF-8?q?feat=20:=20=EB=9E=9C=EB=8D=A4=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EC=84=9C=EB=A1=9C=20=EB=8B=A4=EB=A5=B8=203?= =?UTF-8?q?=EC=9E=90=EB=A6=AC=20=EC=88=98=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/App.js b/src/App.js index c38b30d5b..d7be6bc0b 100644 --- a/src/App.js +++ b/src/App.js @@ -1,5 +1,21 @@ +import { MissionUtils } from "@woowacourse/mission-utils"; + class App { - async play() {} + async play() { + MissionUtils.Console.print("숫자 야구 게임을 시작합니다."); + const num = this.setNum() + } + + setNum() { + const computer = []; + while (computer.length < 3) { + const number = MissionUtils.Random.pickNumberInRange(1, 9); + if (!computer.includes(number)) { + computer.push(number); + } + } + return computer; + } } export default App; From f93de096b6f2ed9cff8d1c612b89668592f5124a Mon Sep 17 00:00:00 2001 From: delee3064 Date: Wed, 25 Oct 2023 19:48:12 +0900 Subject: [PATCH 4/9] =?UTF-8?q?feat=20:=20=EC=82=AC=EC=9A=A9=EC=9E=90?= =?UTF-8?q?=EC=9D=98=20=EC=88=AB=EC=9E=90=20=EC=9E=85=EB=A0=A5=EA=B3=BC=20?= =?UTF-8?q?=EC=98=88=EC=99=B8=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, 26 insertions(+), 2 deletions(-) diff --git a/src/App.js b/src/App.js index d7be6bc0b..f9fb70a81 100644 --- a/src/App.js +++ b/src/App.js @@ -3,9 +3,30 @@ import { MissionUtils } from "@woowacourse/mission-utils"; class App { async play() { MissionUtils.Console.print("숫자 야구 게임을 시작합니다."); - const num = this.setNum() + const computerNum = this.setNum(); + const userNum = await MissionUtils.Console.readLineAsync("숫자를 입력해주세요 : "); + this.vaildation(userNum) + } - + + vaildation(userNum) { + + if(userNum.length === 0) { + throw new Error("[ERROR] 숫자가 입력되지 않았습니다.") + } + + if (userNum.length !== 3) { + throw new Error("[ERROR] 3자리의 수를 입력해주세요.") + } + + if(userNum.length !== new Set(Array.from(String(userNum))).size) { + throw new Error("[ERROR] 중복된 숫자가 입력되었습니다.") + } + + return true + + } + setNum() { const computer = []; while (computer.length < 3) { @@ -18,4 +39,7 @@ class App { } } +const app = new App(); +app.play(); + export default App; From 855283d6fccf8c4f1a9eeaebf0df83dea38f74f5 Mon Sep 17 00:00:00 2001 From: delee3064 Date: Wed, 25 Oct 2023 20:47:43 +0900 Subject: [PATCH 5/9] =?UTF-8?q?feat=20:=20=EB=9E=9C=EB=8D=A4=EA=B0=92?= =?UTF-8?q?=EA=B3=BC=20=EC=9E=85=EB=A0=A5=EA=B0=92=EC=9D=84=20=EB=B9=84?= =?UTF-8?q?=EA=B5=90=ED=95=98=EC=97=AC=20=EA=B2=B0=EA=B3=BC=20=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/App.js b/src/App.js index f9fb70a81..3005f5f57 100644 --- a/src/App.js +++ b/src/App.js @@ -1,14 +1,58 @@ import { MissionUtils } from "@woowacourse/mission-utils"; class App { + async play() { MissionUtils.Console.print("숫자 야구 게임을 시작합니다."); const computerNum = this.setNum(); const userNum = await MissionUtils.Console.readLineAsync("숫자를 입력해주세요 : "); this.vaildation(userNum) + this.result(computerNum, userNum) + + } + + // 랜덤값과 입력값을 비교 + result(computerNum, userNum) { + const checkNum = Array.from(userNum, Number) + let strike = 0 + let ball = 0 + + for (let i = 0; i < 3; i++) { + if (computerNum.includes(Number(checkNum[i]))) { + if (computerNum[i] === checkNum[i]) { + strike += 1; + } + else{ + ball += 1; + } + } + } + this.printresult(ball, strike) + } + + //비교값 출력 + printresult(ball, strike) { + if (ball === 0 && strike === 0) { + MissionUtils.Console.print("낫싱") + } + else if(ball > 0) { + if (strike > 0) { + MissionUtils.Console.print(`${ball}볼 ${strike}스트라이크`) + } + else { + MissionUtils.Console.print(`${ball}볼`) + } + } + + else if (strike > 0) { + MissionUtils.Console.print(`${strike}스트라이크`) + } + } + + //예외처리 vaildation(userNum) { if(userNum.length === 0) { @@ -27,6 +71,8 @@ class App { } + + //랜덤으로 3자리의 수 선택 setNum() { const computer = []; while (computer.length < 3) { From fbad09f092a8bad04fa40af1b2d12533106741a1 Mon Sep 17 00:00:00 2001 From: delee3064 Date: Wed, 25 Oct 2023 21:01:06 +0900 Subject: [PATCH 6/9] =?UTF-8?q?feat=20:=203=EC=8A=A4=ED=8A=B8=EB=9D=BC?= =?UTF-8?q?=EC=9D=B4=ED=81=AC=EA=B0=80=20=EB=90=98=EB=A9=B4=20=EA=B2=8C?= =?UTF-8?q?=EC=9E=84=20=EC=A2=85=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/App.js b/src/App.js index 3005f5f57..a2af053bf 100644 --- a/src/App.js +++ b/src/App.js @@ -1,13 +1,17 @@ import { MissionUtils } from "@woowacourse/mission-utils"; class App { - + continue = false async play() { + this.continue = true MissionUtils.Console.print("숫자 야구 게임을 시작합니다."); const computerNum = this.setNum(); - const userNum = await MissionUtils.Console.readLineAsync("숫자를 입력해주세요 : "); - this.vaildation(userNum) - this.result(computerNum, userNum) + MissionUtils.Console.print(computerNum); + while(this.continue) { + const userNum = await MissionUtils.Console.readLineAsync("숫자를 입력해주세요 : "); + this.vaildation(userNum) + this.result(computerNum, userNum) + } } @@ -47,6 +51,9 @@ class App { else if (strike > 0) { MissionUtils.Console.print(`${strike}스트라이크`) + if (strike === 3) { + this.continue = false + } } } From 1b615097240b9882dcac4141cad8523bf52fc4e0 Mon Sep 17 00:00:00 2001 From: delee3064 Date: Wed, 25 Oct 2023 22:22:24 +0900 Subject: [PATCH 7/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=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/App.js b/src/App.js index a2af053bf..5f03699b8 100644 --- a/src/App.js +++ b/src/App.js @@ -10,9 +10,23 @@ class App { while(this.continue) { const userNum = await MissionUtils.Console.readLineAsync("숫자를 입력해주세요 : "); this.vaildation(userNum) - this.result(computerNum, userNum) + let [ball, strike] = this.result(computerNum, userNum) + this.printresult(ball, strike) + if (strike === 3) { + const userReturn = await MissionUtils.Console.readLineAsync("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); + this.restart(userReturn) + } } - + } + + restart(userReturn) { + if (userReturn !== "1" && userReturn !== "2") { + throw new Error("1과 2중에 하나만 입력해주세요.") + } + if (userReturn === "2") { + this.continue = false; + } + } // 랜덤값과 입력값을 비교 @@ -31,7 +45,7 @@ class App { } } } - this.printresult(ball, strike) + return [ball, strike] } //비교값 출력 @@ -52,7 +66,7 @@ class App { else if (strike > 0) { MissionUtils.Console.print(`${strike}스트라이크`) if (strike === 3) { - this.continue = false + MissionUtils.Console.print("3개의 숫자를 모두 맞히셨습니다! 게임 종료") } } @@ -92,7 +106,5 @@ class App { } } -const app = new App(); -app.play(); export default App; From fbd24ebfb36ca5510033fa1b81c6ca3996561591 Mon Sep 17 00:00:00 2001 From: delee3064 Date: Wed, 25 Oct 2023 23:42:09 +0900 Subject: [PATCH 8/9] =?UTF-8?q?fix=20:=20test=EC=9A=A9=20=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/App.js b/src/App.js index 5f03699b8..738407f67 100644 --- a/src/App.js +++ b/src/App.js @@ -6,7 +6,6 @@ class App { this.continue = true MissionUtils.Console.print("숫자 야구 게임을 시작합니다."); const computerNum = this.setNum(); - MissionUtils.Console.print(computerNum); while(this.continue) { const userNum = await MissionUtils.Console.readLineAsync("숫자를 입력해주세요 : "); this.vaildation(userNum) @@ -106,5 +105,7 @@ class App { } } +const app = new App(); +app.play(); export default App; From 03b255091054ba35eb61ad4263fcf222aaf6f94b Mon Sep 17 00:00:00 2001 From: delee3064 Date: Thu, 26 Oct 2023 00:45:51 +0900 Subject: [PATCH 9/9] =?UTF-8?q?fix=20:=20=EB=9E=9C=EB=8D=A4=EA=B0=92=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD=EB=90=98=EC=A7=80=20=EC=95=8A=EB=8D=98=20?= =?UTF-8?q?=EC=98=A4=EB=A5=98=20=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/App.js b/src/App.js index 738407f67..e1cae8a7e 100644 --- a/src/App.js +++ b/src/App.js @@ -1,15 +1,17 @@ import { MissionUtils } from "@woowacourse/mission-utils"; class App { - continue = false - async play() { + constructor() { this.continue = true + } + + async play() { MissionUtils.Console.print("숫자 야구 게임을 시작합니다."); - const computerNum = this.setNum(); + this.computerNum = this.setNum(); while(this.continue) { const userNum = await MissionUtils.Console.readLineAsync("숫자를 입력해주세요 : "); this.vaildation(userNum) - let [ball, strike] = this.result(computerNum, userNum) + let [ball, strike] = this.result(this.computerNum, userNum) this.printresult(ball, strike) if (strike === 3) { const userReturn = await MissionUtils.Console.readLineAsync("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); @@ -22,6 +24,11 @@ class App { if (userReturn !== "1" && userReturn !== "2") { throw new Error("1과 2중에 하나만 입력해주세요.") } + + if (userReturn === "1") { + this.computerNum = this.setNum(); + } + if (userReturn === "2") { this.continue = false; }