From 21eaf25f4935075123ba98d7d52fa50e01fe3d6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=B0=95=EC=B0=AC=EC=98=81?= Date: Sat, 21 Oct 2023 23:00:12 +0900 Subject: [PATCH 1/6] functional requirements --- docs/README.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 docs/README.md diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 000000000..c9ae7fa92 --- /dev/null +++ b/docs/README.md @@ -0,0 +1,31 @@ +## 구현할 기능 정리 +0. 게임 시작 시 출력 문구 + - [숫자 야구 게임을 시작합니다.] 문구 출력 + +1. 랜덤으로 숫자를 생성하는 기능 + - `@woowacourse/mission-utils`의 `Random.pickNumberInRange()`를 활용 + +2. 사용자가 숫자 세자리를 입력하는 기능 + - `@woowacourse/mission-utils`의 `Console.readLineAsync`, `Console.print`를 활용 + - [숫자를 입력해 주세요:] 문구 출력 + - 예외처리는 `throw`문을 사용, 앞에 [ERROR]를 포함하여 경고메시지를 출력하고, 애플리케이션을 종료 + * 예외처리 경우 + (1) 숫자 이외에 다른 문자를 입력한 경우 + (2) 3개의 숫자를 입력하지 않은 경우 + (3) 1~9까지의 정수가 아닌 다른 숫자를 입력한 경우 + (4) 위의 경우가 모두 아닌 경우 + +3. 생성한 값과 입력한 값을 비교하여 힌트를 출력해주는 기능 + - 스트라이크: 같은 수가 같은 자리에 있는 경우 + - 볼: 다른 자리에 있는 경우 + - 낫싱: 같은 수가 전혀 없는 경우 + - 출력 방식: 입력한 숫자에 대한 결과를 [n볼 n스트라이크] 방식으로 출력 + +4. 게임 종료 시 + - 3개의 숫자를 모두 맞힐 경우 [3개의 숫자를 모두 맞히셨습니다! 게임 종료] 출력 + - 이후 [재시작(1)/종료(2)] 중 하나의 수를 입력받아서 프로그램 재시작/종료여부 결정 + +## 코드 컨벤션(이름 구현 규칙) + - 소스의 변수명, 클래스명 등에는 영문만 사용 + - 클래스, 메서드 등의 이름에는 특수 문자를 사용x + - 상수명은 SNAKE_CASE로 작성(대문자 + _ + 대문자) From 07d32565f476a973a3eac54c74e96877c29806be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=B0=95=EC=B0=AC=EC=98=81?= Date: Sat, 21 Oct 2023 23:04:52 +0900 Subject: [PATCH 2/6] functional requirements --- docs/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/README.md b/docs/README.md index c9ae7fa92..78806ebc5 100644 --- a/docs/README.md +++ b/docs/README.md @@ -13,6 +13,7 @@ (1) 숫자 이외에 다른 문자를 입력한 경우 (2) 3개의 숫자를 입력하지 않은 경우 (3) 1~9까지의 정수가 아닌 다른 숫자를 입력한 경우 + (4) 동일한 숫자를 입력한 경우: set().length != 3 (4) 위의 경우가 모두 아닌 경우 3. 생성한 값과 입력한 값을 비교하여 힌트를 출력해주는 기능 From fe8a1e88ef79d59e238444a7809b928752482a77 Mon Sep 17 00:00:00 2001 From: nyeonge Date: Sat, 21 Oct 2023 23:07:53 +0900 Subject: [PATCH 3/6] functional requirements --- docs/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index 78806ebc5..82a9e3587 100644 --- a/docs/README.md +++ b/docs/README.md @@ -9,7 +9,7 @@ - `@woowacourse/mission-utils`의 `Console.readLineAsync`, `Console.print`를 활용 - [숫자를 입력해 주세요:] 문구 출력 - 예외처리는 `throw`문을 사용, 앞에 [ERROR]를 포함하여 경고메시지를 출력하고, 애플리케이션을 종료 - * 예외처리 경우 + * 예외처리하는 경우 (1) 숫자 이외에 다른 문자를 입력한 경우 (2) 3개의 숫자를 입력하지 않은 경우 (3) 1~9까지의 정수가 아닌 다른 숫자를 입력한 경우 From 5074b9e6410e7cf8f846648de9f4cc485de147a3 Mon Sep 17 00:00:00 2001 From: nyeonge Date: Sat, 21 Oct 2023 23:09:41 +0900 Subject: [PATCH 4/6] functional requirements --- docs/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index 82a9e3587..78806ebc5 100644 --- a/docs/README.md +++ b/docs/README.md @@ -9,7 +9,7 @@ - `@woowacourse/mission-utils`의 `Console.readLineAsync`, `Console.print`를 활용 - [숫자를 입력해 주세요:] 문구 출력 - 예외처리는 `throw`문을 사용, 앞에 [ERROR]를 포함하여 경고메시지를 출력하고, 애플리케이션을 종료 - * 예외처리하는 경우 + * 예외처리 경우 (1) 숫자 이외에 다른 문자를 입력한 경우 (2) 3개의 숫자를 입력하지 않은 경우 (3) 1~9까지의 정수가 아닌 다른 숫자를 입력한 경우 From e407d679f86416b004810670edec2c9fd57cc1c8 Mon Sep 17 00:00:00 2001 From: nyeonge Date: Wed, 25 Oct 2023 00:52:25 +0900 Subject: [PATCH 5/6] add functions except error function --- src/App.js | 33 ++++++++++++++++++++-- src/PlayGame.js | 69 ++++++++++++++++++++++++++++++++++++++++++++++ src/comparison.js | 33 ++++++++++++++++++++++ src/retrunError.js | 24 ++++++++++++++++ 4 files changed, 157 insertions(+), 2 deletions(-) create mode 100644 src/PlayGame.js create mode 100644 src/comparison.js create mode 100644 src/retrunError.js diff --git a/src/App.js b/src/App.js index c38b30d5b..cece757b9 100644 --- a/src/App.js +++ b/src/App.js @@ -1,5 +1,34 @@ +import PlayGame from "./PlayGame.js" +import { MissionUtils } from "@woowacourse/mission-utils"; + class App { - async play() {} + constructor() { + this.playgame = new PlayGame(); + this.play(); + this.handleUserInput(); + } + play() { + MissionUtils.Console.print('숫자 야구 게임을 시작합니다.'); + this.playgame.setRandomNumber(); + } + handleUserInput() { + const randomNumber = this.playgame.getRandomNumber(); + this.playgame.handleInput().then((result) => { + this.playgame.compareNumbers(randomNumber, result).then((strike) => { + if (strike !== 3) { + this.handleUserInput() + } else { + this.askRetry() + } + }) + }) + } + askRetry() { + MissionUtils.Console.print('3개의 숫자를 모두 맞히셨습니다! 게임 종료'); + } } -export default App; +const app = new App(); +// app.play(); + +export default App; \ No newline at end of file diff --git a/src/PlayGame.js b/src/PlayGame.js new file mode 100644 index 000000000..638dc042f --- /dev/null +++ b/src/PlayGame.js @@ -0,0 +1,69 @@ +import { MissionUtils } from "@woowacourse/mission-utils"; + +class PlayGame { + randomNumber; + strikeCount = 0; + ballCount = 0; + + getStrikeCount() { + return this.strikeCount; + } + + getRandomNumber() { + return this.randomNumber; + } + + setRandomNumber() { + const COMPUTER = [] + while (COMPUTER.length < 3) { + const RANDOM_NUM = MissionUtils.Random.pickNumberInRange(1, 9); + if (!COMPUTER.includes(RANDOM_NUM)) { + COMPUTER.push(RANDOM_NUM) + } + } + this.randomNumber = COMPUTER; + } + // 사용자 인풋에 대해 예외처리를 해줘야함 + async handleInput() { + this.strikeCount = 0; + this.ballCount = 0; + try { + const INPUT = await MissionUtils.Console.readLineAsync('숫자를 입력해 주세요:'); + const USER_INPUT = INPUT.split('').map(Number); + // console.log('USER_INPUT', USER_INPUT); + return USER_INPUT; + // this.compareNumbers(); + // ReturnError(USER_INPUT); + } catch (ERROR) { + MissionUtils.Console.print(ERROR); + } + } + + async compareNumbers(random, input) { + for (let i = 0; i < random.length; i++) { + if (random.includes(input[i])) { + if (random[i] == input[i]) { + this.strikeCount++; + } else { + this.ballCount++; + } + } + } + this.printResult() + return this.strikeCount; + } + + printResult () { + if (this.strikeCount === 0 && this.ballCount === 0) { + MissionUtils.Console.print('낫싱'); + } else if (this.ballCount === 0) { + MissionUtils.Console.print(this.strikeCount + '스트라이크'); + } else if (this.strikeCount === 0) { + MissionUtils.Console.print(this.ballCount + '볼'); + } else { + MissionUtils.Console.print(this.ballCount + '볼' + ' ' + this.strikeCount + '스트라이크'); + } + } +} + +export default PlayGame; \ No newline at end of file diff --git a/src/comparison.js b/src/comparison.js new file mode 100644 index 000000000..9837bdb5d --- /dev/null +++ b/src/comparison.js @@ -0,0 +1,33 @@ +function Comparison(com, user) { + let length = com.length; + let strike = 0; + let ball = 0; + for(let i=0; i Date: Wed, 25 Oct 2023 23:48:24 +0900 Subject: [PATCH 6/6] Fix: basic function for comparing and generate randomNumber --- src/App.js | 25 ++++++++++++++++++++----- src/PlayGame.js | 44 ++++++++++++++++++++++++++++++-------------- src/comparison.js | 33 --------------------------------- src/retrunError.js | 23 +++++++---------------- 4 files changed, 57 insertions(+), 68 deletions(-) delete mode 100644 src/comparison.js diff --git a/src/App.js b/src/App.js index cece757b9..ccc9646ea 100644 --- a/src/App.js +++ b/src/App.js @@ -5,30 +5,45 @@ class App { constructor() { this.playgame = new PlayGame(); this.play(); + this.setRandomNumber(); this.handleUserInput(); + } play() { MissionUtils.Console.print('숫자 야구 게임을 시작합니다.'); + } + + setRandomNumber() { this.playgame.setRandomNumber(); } - handleUserInput() { + + async handleUserInput() { const randomNumber = this.playgame.getRandomNumber(); this.playgame.handleInput().then((result) => { this.playgame.compareNumbers(randomNumber, result).then((strike) => { if (strike !== 3) { this.handleUserInput() } else { - this.askRetry() + MissionUtils.Console.print('3개의 숫자를 모두 맞히셨습니다! 게임 종료'); + this.askRestart() } + }) }) } - askRetry() { - MissionUtils.Console.print('3개의 숫자를 모두 맞히셨습니다! 게임 종료'); + + askRestart() { + this.playgame.restartInput().then((response) => { + if (['1', '2'].includes(response)) { + if (response === '1') { + this.setRandomNumber(); + this.handleUserInput(); + } + } + }) } } const app = new App(); -// app.play(); export default App; \ No newline at end of file diff --git a/src/PlayGame.js b/src/PlayGame.js index 638dc042f..9de91574e 100644 --- a/src/PlayGame.js +++ b/src/PlayGame.js @@ -1,9 +1,16 @@ import { MissionUtils } from "@woowacourse/mission-utils"; +import ReturnError from "./RetrunError.js"; class PlayGame { + constructor() { + this.returnError = new ReturnError(); + } + randomNumber; strikeCount = 0; ballCount = 0; + restartUserInput; + getStrikeCount() { return this.strikeCount; @@ -14,28 +21,26 @@ class PlayGame { } setRandomNumber() { - const COMPUTER = [] - while (COMPUTER.length < 3) { - const RANDOM_NUM = MissionUtils.Random.pickNumberInRange(1, 9); - if (!COMPUTER.includes(RANDOM_NUM)) { - COMPUTER.push(RANDOM_NUM) + const computer = [] + while (computer.length < 3) { + const randomNum = MissionUtils.Random.pickNumberInRange(1, 9); + if (!computer.includes(randomNum)) { + computer.push(randomNum) } } - this.randomNumber = COMPUTER; + this.randomNumber = computer; } - // 사용자 인풋에 대해 예외처리를 해줘야함 + async handleInput() { this.strikeCount = 0; this.ballCount = 0; try { - const INPUT = await MissionUtils.Console.readLineAsync('숫자를 입력해 주세요:'); - const USER_INPUT = INPUT.split('').map(Number); - // console.log('USER_INPUT', USER_INPUT); - return USER_INPUT; - // this.compareNumbers(); - // ReturnError(USER_INPUT); + const input = await MissionUtils.Console.readLineAsync('숫자를 입력해 주세요:'); + const userInput = input.split('').map(Number); + this.returnError.sayError(userInput); + return userInput; } catch (ERROR) { - MissionUtils.Console.print(ERROR); + throw new Error(ERROR) } } @@ -64,6 +69,17 @@ class PlayGame { MissionUtils.Console.print(this.ballCount + '볼' + ' ' + this.strikeCount + '스트라이크'); } } + + + async restartInput() { + try { + const restartUserInput = await MissionUtils.Console.readLineAsync('게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.\n'); + return restartUserInput; + } catch (ERROR) { + throw new Error('[ERROR] 1 또는 2를 입력해야 합니다.'); + } + } + } export default PlayGame; \ No newline at end of file diff --git a/src/comparison.js b/src/comparison.js deleted file mode 100644 index 9837bdb5d..000000000 --- a/src/comparison.js +++ /dev/null @@ -1,33 +0,0 @@ -function Comparison(com, user) { - let length = com.length; - let strike = 0; - let ball = 0; - for(let i=0; i 9 ){ throw new Error('[ERROR] 1부터 9사이의 숫자를 입력해야 합니다.') - } */ - else if (new Set(userInput).size != 3) { + } else if (new Set(userInput).size != 3) { throw new Error('[ERROR] 서로 다른 숫자 세개를 입력해야 합니다.') } - - } - stop(){ - //애플리케이션 종료 ?????? } -} \ No newline at end of file +} +export default ReturnError; \ No newline at end of file