From d831b0b464a02df52df38f4c9b18d62f3f688d1e Mon Sep 17 00:00:00 2001 From: Boeun Date: Sun, 22 Oct 2023 16:41:21 +0900 Subject: [PATCH 01/29] =?UTF-8?q?docs:=20readme=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 docs/README.md diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 000000000..dc99a8d43 --- /dev/null +++ b/docs/README.md @@ -0,0 +1,5 @@ +- 랜덤 숫자 세자리 지정하기 +- 사용자 숫자 입력받기 +- 잘못된 값 입력 시 에러 발생하기 +- 랜덤 숫자와 사용자 입력 비교하기 +- 게임 종료 시 재시작/종료 입력받기 From 0f94e34f5c1a0ed3d54d3dc5060b7077a3dc3deb Mon Sep 17 00:00:00 2001 From: Boeun Date: Sun, 22 Oct 2023 19:12:51 +0900 Subject: [PATCH 02/29] =?UTF-8?q?feat:=20=EB=9E=9C=EB=8D=A4=20=EC=88=AB?= =?UTF-8?q?=EC=9E=90=20=EC=84=B8=EC=9E=90=EB=A6=AC=20=EC=A7=80=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- src/App.js | 11 ++++++++++- src/Computer.js | 14 ++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 src/Computer.js diff --git a/docs/README.md b/docs/README.md index dc99a8d43..182c73255 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,4 +1,4 @@ -- 랜덤 숫자 세자리 지정하기 +- ✅랜덤 숫자 세자리 지정하기 - 사용자 숫자 입력받기 - 잘못된 값 입력 시 에러 발생하기 - 랜덤 숫자와 사용자 입력 비교하기 diff --git a/src/App.js b/src/App.js index c38b30d5b..85bebec74 100644 --- a/src/App.js +++ b/src/App.js @@ -1,5 +1,14 @@ +import Computer from "./Computer.js"; + class App { - async play() {} + async play() { + const RAND_NUM = selectRandNum(); + } +} + +function selectRandNum() { + Computer.pickRandomNumbers(); + return Computer.numbers; } export default App; diff --git a/src/Computer.js b/src/Computer.js new file mode 100644 index 000000000..325ab94be --- /dev/null +++ b/src/Computer.js @@ -0,0 +1,14 @@ +import { MissionUtils } from "@woowacourse/mission-utils"; +export default class Computer { + static numbers = []; + static pickRandomNumbers() { + while (numbers.length < 3) { + const number = MissionUtils.Random.pickNumberInRange(1, 9); + if (!numbers.includes(number)) { + numbers.push(number); + } + } + } + static returnMessage(expect_numbers) {} + static clearNumbers() {} +} From f17380f888dc5d261e6adf36369d3e7ef4bb9fb5 Mon Sep 17 00:00:00 2001 From: Boeun Date: Sun, 22 Oct 2023 19:56:06 +0900 Subject: [PATCH 03/29] =?UTF-8?q?feat:=20=EC=82=AC=EC=9A=A9=EC=9E=90=20?= =?UTF-8?q?=EC=9E=85=EB=A0=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- src/User.js | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 src/User.js diff --git a/docs/README.md b/docs/README.md index 182c73255..b64e6101d 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,5 +1,5 @@ - ✅랜덤 숫자 세자리 지정하기 -- 사용자 숫자 입력받기 +- ✅사용자 입력받기 - 잘못된 값 입력 시 에러 발생하기 - 랜덤 숫자와 사용자 입력 비교하기 - 게임 종료 시 재시작/종료 입력받기 diff --git a/src/User.js b/src/User.js new file mode 100644 index 000000000..d883c8636 --- /dev/null +++ b/src/User.js @@ -0,0 +1,6 @@ +import { MissionUtils } from "@woowacourse/mission-utils"; +export default class User { + returnUserInput() { + return MissionUtils.Console.readLineAsync(null); + } +} From 12f237ba4598a1e18ab2cb9a41a5aadfe35d4ee0 Mon Sep 17 00:00:00 2001 From: Boeun Date: Sun, 22 Oct 2023 20:02:56 +0900 Subject: [PATCH 04/29] =?UTF-8?q?feat:=20=EC=BB=B4=ED=93=A8=ED=84=B0=20?= =?UTF-8?q?=EC=9D=91=EB=8B=B5=20=EC=B6=9C=EB=A0=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 1 + src/App.js | 3 +++ src/Computer.js | 24 +++++++++++++++++++++++- src/User.js | 3 +++ 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index b64e6101d..7b433a8d2 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,5 +1,6 @@ - ✅랜덤 숫자 세자리 지정하기 - ✅사용자 입력받기 +- ✅컴퓨터 응답 출력하기 - 잘못된 값 입력 시 에러 발생하기 - 랜덤 숫자와 사용자 입력 비교하기 - 게임 종료 시 재시작/종료 입력받기 diff --git a/src/App.js b/src/App.js index 85bebec74..4c0c34ca8 100644 --- a/src/App.js +++ b/src/App.js @@ -1,8 +1,11 @@ import Computer from "./Computer.js"; +import User from "./User.js"; class App { async play() { + const USER = new User(); const RAND_NUM = selectRandNum(); + Computer.playGame(USER); } } diff --git a/src/Computer.js b/src/Computer.js index 325ab94be..c1623a712 100644 --- a/src/Computer.js +++ b/src/Computer.js @@ -9,6 +9,28 @@ export default class Computer { } } } - static returnMessage(expect_numbers) {} + static playGame(USER) { + while (true) { + MissionUtils.Console.print("숫자 야구 게임을 시작합니다."); + while (true) { + const INPUT = USER.returnUserQuery("숫자를 입력해주세요 : "); + const RESULT = returnMessage(INPUT); + MissionUtils.Console.print(RESULT.result); + if (RESULT.success) { + MissionUtils.Console.print( + "3개의 숫자를 모두 맞히셨습니다! 게임 종료" + ); + break; + } + } + MissionUtils.Console.print( + "게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요." + ); + if (USER.returnUserInput() === 2) break; + } + } + static returnMessage(expect_numbers) { + return { result: "", success: false }; + } static clearNumbers() {} } diff --git a/src/User.js b/src/User.js index d883c8636..18bd7c8f5 100644 --- a/src/User.js +++ b/src/User.js @@ -1,5 +1,8 @@ import { MissionUtils } from "@woowacourse/mission-utils"; export default class User { + returnUserQuery(query) { + return MissionUtils.Console.readLineAsync(query); + } returnUserInput() { return MissionUtils.Console.readLineAsync(null); } From 29736f2f119f1468978cafe1f171c8c059785c39 Mon Sep 17 00:00:00 2001 From: Boeun Date: Wed, 25 Oct 2023 20:17:41 +0900 Subject: [PATCH 05/29] =?UTF-8?q?feat:=20=EB=B3=BC=20=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=EB=9D=BC=EC=9D=B4=ED=81=AC=20=EA=B3=84=EC=82=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 15 +++++++-------- src/Computer.js | 48 +++++++++++++++++++++++++++++++++++++----------- 2 files changed, 44 insertions(+), 19 deletions(-) diff --git a/src/App.js b/src/App.js index 4c0c34ca8..209a52165 100644 --- a/src/App.js +++ b/src/App.js @@ -2,16 +2,15 @@ import Computer from "./Computer.js"; import User from "./User.js"; class App { + constructor() { + this.COMPUTER = new Computer(); + this.USER = new User(); + } async play() { - const USER = new User(); - const RAND_NUM = selectRandNum(); - Computer.playGame(USER); + this.COMPUTER.pickRandomNumbers(); + this.COMPUTER.playGame(this.USER); } } - -function selectRandNum() { - Computer.pickRandomNumbers(); - return Computer.numbers; -} +new App().play(); export default App; diff --git a/src/Computer.js b/src/Computer.js index c1623a712..80c9941cd 100644 --- a/src/Computer.js +++ b/src/Computer.js @@ -1,20 +1,25 @@ import { MissionUtils } from "@woowacourse/mission-utils"; export default class Computer { - static numbers = []; - static pickRandomNumbers() { - while (numbers.length < 3) { + constructor() { + this.numbers = []; + } + pickRandomNumbers() { + while (this.numbers.length < 3) { const number = MissionUtils.Random.pickNumberInRange(1, 9); - if (!numbers.includes(number)) { - numbers.push(number); + if (!this.numbers.includes(number)) { + this.numbers.push(number); } } + console.log({ number: this.numbers }); } - static playGame(USER) { + async playGame(USER) { + MissionUtils.Console.print("숫자 야구 게임을 시작합니다."); while (true) { - MissionUtils.Console.print("숫자 야구 게임을 시작합니다."); while (true) { - const INPUT = USER.returnUserQuery("숫자를 입력해주세요 : "); - const RESULT = returnMessage(INPUT); + const INPUT = await USER.returnUserQuery( + "숫자를 입력해주세요 : " + ); + const RESULT = this.returnMessage(INPUT); MissionUtils.Console.print(RESULT.result); if (RESULT.success) { MissionUtils.Console.print( @@ -29,8 +34,29 @@ export default class Computer { if (USER.returnUserInput() === 2) break; } } - static returnMessage(expect_numbers) { + returnMessage(expect_numbers) { + const BALL_STRIKE = this.calculateBallStrike(expect_numbers); + const RESULT = this.getResultString(BALL_STRIKE); + if (BALL_STRIKE.strike === 3) return { result: RESULT, success: true }; return { result: "", success: false }; } - static clearNumbers() {} + calculateBallStrike(expect_numbers) { + let ball = 0; + let strike = 0; + [...expect_numbers] + .map((n) => +n) + .forEach((n, idx) => { + if (this.numbers[idx] === n) strike++; + else if (this.numbers.includes(n)) ball++; + }); + return { ball, strike }; + } + getResultString({ ball, strike }) { + if (ball === 0 && strike === 0) return "낫싱"; + else if (ball === 0) return `${strike}스트라이크`; + else if (strike === 0) return `${ball}볼`; + else return `${ball}볼 ${strike}스트라이크`; + } + + clearNumbers() {} } From d7353c3fed472e5cb52648a4be645ad0921e42dc Mon Sep 17 00:00:00 2001 From: Boeun Date: Wed, 25 Oct 2023 20:22:54 +0900 Subject: [PATCH 06/29] =?UTF-8?q?feat:=20=EC=8A=A4=ED=8A=B8=EB=9D=BC?= =?UTF-8?q?=EC=9D=B4=ED=81=AC=20=EB=B3=BC=20=EB=A9=94=EC=84=B8=EC=A7=80=20?= =?UTF-8?q?=EC=B6=9C=EB=A0=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Computer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Computer.js b/src/Computer.js index 80c9941cd..48f46a574 100644 --- a/src/Computer.js +++ b/src/Computer.js @@ -38,7 +38,7 @@ export default class Computer { const BALL_STRIKE = this.calculateBallStrike(expect_numbers); const RESULT = this.getResultString(BALL_STRIKE); if (BALL_STRIKE.strike === 3) return { result: RESULT, success: true }; - return { result: "", success: false }; + return { result: RESULT, success: false }; } calculateBallStrike(expect_numbers) { let ball = 0; From 2890ef2c47cd5347e6fc124e8401ca038adae1f9 Mon Sep 17 00:00:00 2001 From: Boeun Date: Wed, 25 Oct 2023 20:28:46 +0900 Subject: [PATCH 07/29] =?UTF-8?q?feat:=20=EA=B2=8C=EC=9E=84=20=EC=9E=AC?= =?UTF-8?q?=EC=8B=9C=EC=9E=91=20=EC=BF=BC=EB=A6=AC=20=EB=A9=94=EC=84=B8?= =?UTF-8?q?=EC=A7=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Computer.js | 7 +++---- src/User.js | 3 --- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/src/Computer.js b/src/Computer.js index 48f46a574..248f01091 100644 --- a/src/Computer.js +++ b/src/Computer.js @@ -10,7 +10,6 @@ export default class Computer { this.numbers.push(number); } } - console.log({ number: this.numbers }); } async playGame(USER) { MissionUtils.Console.print("숫자 야구 게임을 시작합니다."); @@ -28,10 +27,10 @@ export default class Computer { break; } } - MissionUtils.Console.print( - "게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요." + const INPUT = await USER.returnUserQuery( + "게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.\n" ); - if (USER.returnUserInput() === 2) break; + if (INPUT === 2) break; } } returnMessage(expect_numbers) { diff --git a/src/User.js b/src/User.js index 18bd7c8f5..cef540718 100644 --- a/src/User.js +++ b/src/User.js @@ -3,7 +3,4 @@ export default class User { returnUserQuery(query) { return MissionUtils.Console.readLineAsync(query); } - returnUserInput() { - return MissionUtils.Console.readLineAsync(null); - } } From 0fc67db85b0e96bec548daca6ef38679b17de103 Mon Sep 17 00:00:00 2001 From: Boeun Date: Wed, 25 Oct 2023 20:33:40 +0900 Subject: [PATCH 08/29] =?UTF-8?q?feat:=20=EA=B2=8C=EC=9E=84=20=EC=9E=AC?= =?UTF-8?q?=EC=8B=9C=EC=9E=91=20=EC=8B=9C=20numbers=20=EC=B4=88=EA=B8=B0?= =?UTF-8?q?=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 1 - src/Computer.js | 5 ++++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/App.js b/src/App.js index 209a52165..6babb6402 100644 --- a/src/App.js +++ b/src/App.js @@ -7,7 +7,6 @@ class App { this.USER = new User(); } async play() { - this.COMPUTER.pickRandomNumbers(); this.COMPUTER.playGame(this.USER); } } diff --git a/src/Computer.js b/src/Computer.js index 248f01091..36db2c61a 100644 --- a/src/Computer.js +++ b/src/Computer.js @@ -4,16 +4,19 @@ export default class Computer { this.numbers = []; } pickRandomNumbers() { + this.numbers = []; while (this.numbers.length < 3) { const number = MissionUtils.Random.pickNumberInRange(1, 9); if (!this.numbers.includes(number)) { this.numbers.push(number); } } + console.log({ numbers: this.numbers }); } async playGame(USER) { MissionUtils.Console.print("숫자 야구 게임을 시작합니다."); while (true) { + this.pickRandomNumbers(); while (true) { const INPUT = await USER.returnUserQuery( "숫자를 입력해주세요 : " @@ -30,7 +33,7 @@ export default class Computer { const INPUT = await USER.returnUserQuery( "게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.\n" ); - if (INPUT === 2) break; + if (INPUT === "2") break; } } returnMessage(expect_numbers) { From e6d07f56ea42534b353560769f2a6156e6a5e900 Mon Sep 17 00:00:00 2001 From: Boeun Date: Wed, 25 Oct 2023 20:54:25 +0900 Subject: [PATCH 09/29] =?UTF-8?q?feat:=20=EB=B0=B0=EC=97=B4=20=EC=B4=88?= =?UTF-8?q?=EA=B8=B0=ED=99=94=20=ED=95=A8=EC=88=98=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Computer.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Computer.js b/src/Computer.js index 36db2c61a..e4109eaf8 100644 --- a/src/Computer.js +++ b/src/Computer.js @@ -4,7 +4,7 @@ export default class Computer { this.numbers = []; } pickRandomNumbers() { - this.numbers = []; + this.clearNumbers(); while (this.numbers.length < 3) { const number = MissionUtils.Random.pickNumberInRange(1, 9); if (!this.numbers.includes(number)) { @@ -60,5 +60,7 @@ export default class Computer { else return `${ball}볼 ${strike}스트라이크`; } - clearNumbers() {} + clearNumbers() { + this.numbers = []; + } } From aab592a6aafe322e6a325b4a2ac1aa19d0e95e91 Mon Sep 17 00:00:00 2001 From: Boeun Date: Wed, 25 Oct 2023 21:08:47 +0900 Subject: [PATCH 10/29] =?UTF-8?q?feat:=20=EC=82=AC=EC=9A=A9=EC=9E=90=20?= =?UTF-8?q?=EC=9E=85=EB=A0=A5=20=EC=98=88=EC=99=B8=20=EB=B0=9C=EC=83=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/User.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/User.js b/src/User.js index cef540718..6ed37ca92 100644 --- a/src/User.js +++ b/src/User.js @@ -1,6 +1,14 @@ import { MissionUtils } from "@woowacourse/mission-utils"; export default class User { - returnUserQuery(query) { - return MissionUtils.Console.readLineAsync(query); + async returnUserQuery(query) { + const INPUT = await MissionUtils.Console.readLineAsync(query); + if (!this.checkValidation(INPUT)) + throw new Error("[ERROR] 숫자가 잘못된 형식입니다."); + return INPUT; + } + checkValidation(str) { + if (str.length > 3) return false; + else if (isNaN(str)) return false; + else return true; } } From f898cb5e4294c89f0a6fd7d33d3b92efc9be7636 Mon Sep 17 00:00:00 2001 From: Boeun Date: Wed, 25 Oct 2023 21:25:09 +0900 Subject: [PATCH 11/29] =?UTF-8?q?feat:=20=EC=BF=BC=EB=A6=AC=20=EB=B6=84?= =?UTF-8?q?=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 1 - src/Computer.js | 8 +++++--- src/User.js | 10 ++++++---- src/query_status.js | 4 ++++ 4 files changed, 15 insertions(+), 8 deletions(-) create mode 100644 src/query_status.js diff --git a/src/App.js b/src/App.js index 6babb6402..5957386c3 100644 --- a/src/App.js +++ b/src/App.js @@ -10,6 +10,5 @@ class App { this.COMPUTER.playGame(this.USER); } } -new App().play(); export default App; diff --git a/src/Computer.js b/src/Computer.js index e4109eaf8..e9a51b77c 100644 --- a/src/Computer.js +++ b/src/Computer.js @@ -1,4 +1,5 @@ import { MissionUtils } from "@woowacourse/mission-utils"; +import { QUERY_STATUS } from "./query_status.js"; export default class Computer { constructor() { this.numbers = []; @@ -11,7 +12,6 @@ export default class Computer { this.numbers.push(number); } } - console.log({ numbers: this.numbers }); } async playGame(USER) { MissionUtils.Console.print("숫자 야구 게임을 시작합니다."); @@ -19,7 +19,8 @@ export default class Computer { this.pickRandomNumbers(); while (true) { const INPUT = await USER.returnUserQuery( - "숫자를 입력해주세요 : " + "숫자를 입력해주세요 : ", + QUERY_STATUS.PLAYING ); const RESULT = this.returnMessage(INPUT); MissionUtils.Console.print(RESULT.result); @@ -31,7 +32,8 @@ export default class Computer { } } const INPUT = await USER.returnUserQuery( - "게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.\n" + "게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.\n", + QUERY_STATUS.REPLAY ); if (INPUT === "2") break; } diff --git a/src/User.js b/src/User.js index 6ed37ca92..28ed5c3a1 100644 --- a/src/User.js +++ b/src/User.js @@ -1,13 +1,15 @@ import { MissionUtils } from "@woowacourse/mission-utils"; +import { QUERY_STATUS } from "./query_status.js"; export default class User { - async returnUserQuery(query) { + async returnUserQuery(query, status) { const INPUT = await MissionUtils.Console.readLineAsync(query); - if (!this.checkValidation(INPUT)) + if (!this.checkValidation(INPUT, status)) throw new Error("[ERROR] 숫자가 잘못된 형식입니다."); return INPUT; } - checkValidation(str) { - if (str.length > 3) return false; + checkValidation(str, status) { + if (status === QUERY_STATUS.PLAYING && str.length > 3) return false; + else if (status === QUERY_STATUS.REPLAY && str.length > 1) return false; else if (isNaN(str)) return false; else return true; } diff --git a/src/query_status.js b/src/query_status.js new file mode 100644 index 000000000..fb1e3d0e1 --- /dev/null +++ b/src/query_status.js @@ -0,0 +1,4 @@ +export const QUERY_STATUS = { + PLAYING: "playing", + REPLAY: "replay", +}; From cd10f7d9e569f89b9b0143885753307c1cda0ad0 Mon Sep 17 00:00:00 2001 From: Boeun Date: Wed, 25 Oct 2023 21:30:57 +0900 Subject: [PATCH 12/29] =?UTF-8?q?style:=20=EB=B3=80=EC=88=98=20naming=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 6 +++--- src/App.js | 6 +++--- src/Computer.js | 8 ++++---- src/User.js | 6 +++--- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/docs/README.md b/docs/README.md index 7b433a8d2..6c727f39d 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,6 +1,6 @@ - ✅랜덤 숫자 세자리 지정하기 - ✅사용자 입력받기 - ✅컴퓨터 응답 출력하기 -- 잘못된 값 입력 시 에러 발생하기 -- 랜덤 숫자와 사용자 입력 비교하기 -- 게임 종료 시 재시작/종료 입력받기 +- ✅잘못된 값 입력 시 에러 발생하기 +- ✅랜덤 숫자와 사용자 입력 비교하기 +- ✅게임 종료 시 재시작/종료 입력받기 diff --git a/src/App.js b/src/App.js index 5957386c3..5034d72a0 100644 --- a/src/App.js +++ b/src/App.js @@ -3,11 +3,11 @@ import User from "./User.js"; class App { constructor() { - this.COMPUTER = new Computer(); - this.USER = new User(); + this.computer = new Computer(); + this.user = new User(); } async play() { - this.COMPUTER.playGame(this.USER); + this.computer.playGame(this.user); } } diff --git a/src/Computer.js b/src/Computer.js index e9a51b77c..af427224b 100644 --- a/src/Computer.js +++ b/src/Computer.js @@ -39,10 +39,10 @@ export default class Computer { } } returnMessage(expect_numbers) { - const BALL_STRIKE = this.calculateBallStrike(expect_numbers); - const RESULT = this.getResultString(BALL_STRIKE); - if (BALL_STRIKE.strike === 3) return { result: RESULT, success: true }; - return { result: RESULT, success: false }; + const ballStrike = this.calculateBallStrike(expect_numbers); + const result = this.getResultString(ballStrike); + if (ballStrike.strike === 3) return { result: result, success: true }; + return { result: result, success: false }; } calculateBallStrike(expect_numbers) { let ball = 0; diff --git a/src/User.js b/src/User.js index 28ed5c3a1..2d1f049e1 100644 --- a/src/User.js +++ b/src/User.js @@ -2,10 +2,10 @@ import { MissionUtils } from "@woowacourse/mission-utils"; import { QUERY_STATUS } from "./query_status.js"; export default class User { async returnUserQuery(query, status) { - const INPUT = await MissionUtils.Console.readLineAsync(query); - if (!this.checkValidation(INPUT, status)) + const input = await MissionUtils.Console.readLineAsync(query); + if (!this.checkValidation(input, status)) throw new Error("[ERROR] 숫자가 잘못된 형식입니다."); - return INPUT; + return input; } checkValidation(str, status) { if (status === QUERY_STATUS.PLAYING && str.length > 3) return false; From ce33efde2fa708abe63ab0ecf251cf02bcda1995 Mon Sep 17 00:00:00 2001 From: Boeun Date: Wed, 25 Oct 2023 21:52:06 +0900 Subject: [PATCH 13/29] =?UTF-8?q?fix:=20App.play()=20=ED=95=A8=EC=88=98=20?= =?UTF-8?q?await=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 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/App.js b/src/App.js index 5034d72a0..cfc167216 100644 --- a/src/App.js +++ b/src/App.js @@ -7,7 +7,7 @@ class App { this.user = new User(); } async play() { - this.computer.playGame(this.user); + await this.computer.playGame(this.user); } } From c9ba2beab6a16d401c54ef32e428103020c55444 Mon Sep 17 00:00:00 2001 From: Boeun Date: Wed, 25 Oct 2023 22:14:38 +0900 Subject: [PATCH 14/29] =?UTF-8?q?style:=20=EB=B3=80=EC=88=98=20naming=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Computer.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Computer.js b/src/Computer.js index af427224b..7959bcc6a 100644 --- a/src/Computer.js +++ b/src/Computer.js @@ -13,41 +13,41 @@ export default class Computer { } } } - async playGame(USER) { + async playGame(user) { MissionUtils.Console.print("숫자 야구 게임을 시작합니다."); while (true) { this.pickRandomNumbers(); while (true) { - const INPUT = await USER.returnUserQuery( + const input = await user.returnUserQuery( "숫자를 입력해주세요 : ", QUERY_STATUS.PLAYING ); - const RESULT = this.returnMessage(INPUT); - MissionUtils.Console.print(RESULT.result); - if (RESULT.success) { + const result = this.returnMessage(input); + MissionUtils.Console.print(result.result); + if (result.success) { MissionUtils.Console.print( "3개의 숫자를 모두 맞히셨습니다! 게임 종료" ); break; } } - const INPUT = await USER.returnUserQuery( + const input = await user.returnUserQuery( "게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.\n", QUERY_STATUS.REPLAY ); - if (INPUT === "2") break; + if (input === "2") break; } } - returnMessage(expect_numbers) { - const ballStrike = this.calculateBallStrike(expect_numbers); + returnMessage(expectNumbers) { + const ballStrike = this.calculateBallStrike(expectNumbers); const result = this.getResultString(ballStrike); if (ballStrike.strike === 3) return { result: result, success: true }; return { result: result, success: false }; } - calculateBallStrike(expect_numbers) { + calculateBallStrike(expectNumbers) { let ball = 0; let strike = 0; - [...expect_numbers] + [...expectNumbers] .map((n) => +n) .forEach((n, idx) => { if (this.numbers[idx] === n) strike++; From d4aac0f75eefd99a03e567522416f6fa77e09e90 Mon Sep 17 00:00:00 2001 From: Boeun Date: Wed, 25 Oct 2023 22:21:16 +0900 Subject: [PATCH 15/29] =?UTF-8?q?style:=20QUIT=20=EC=83=81=EC=88=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Computer.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Computer.js b/src/Computer.js index 7959bcc6a..508e31315 100644 --- a/src/Computer.js +++ b/src/Computer.js @@ -1,6 +1,7 @@ import { MissionUtils } from "@woowacourse/mission-utils"; import { QUERY_STATUS } from "./query_status.js"; export default class Computer { + static QUIT = "2"; constructor() { this.numbers = []; } @@ -35,7 +36,7 @@ export default class Computer { "게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.\n", QUERY_STATUS.REPLAY ); - if (input === "2") break; + if (input === Computer.QUIT) break; } } returnMessage(expectNumbers) { From ff32a2e64f9e21f1a4ed5e39088c1785b78e1125 Mon Sep 17 00:00:00 2001 From: Boeun Date: Wed, 25 Oct 2023 22:22:12 +0900 Subject: [PATCH 16/29] =?UTF-8?q?style:=20STRIKE=20=EC=83=81=EC=88=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Computer.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Computer.js b/src/Computer.js index 508e31315..f6aa359cf 100644 --- a/src/Computer.js +++ b/src/Computer.js @@ -2,6 +2,7 @@ import { MissionUtils } from "@woowacourse/mission-utils"; import { QUERY_STATUS } from "./query_status.js"; export default class Computer { static QUIT = "2"; + static STRIKE = 3; constructor() { this.numbers = []; } @@ -42,7 +43,8 @@ export default class Computer { returnMessage(expectNumbers) { const ballStrike = this.calculateBallStrike(expectNumbers); const result = this.getResultString(ballStrike); - if (ballStrike.strike === 3) return { result: result, success: true }; + if (ballStrike.strike === Computer.STRIKE) + return { result: result, success: true }; return { result: result, success: false }; } calculateBallStrike(expectNumbers) { From cd951be6e7a39a82c51c39a1fe2c63800f524a7c Mon Sep 17 00:00:00 2001 From: Boeun Date: Wed, 25 Oct 2023 22:23:30 +0900 Subject: [PATCH 17/29] =?UTF-8?q?style:=20returnMessage=20=EB=B0=98?= =?UTF-8?q?=ED=99=98=EA=B0=92=20=ED=86=B5=EC=9D=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Computer.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Computer.js b/src/Computer.js index f6aa359cf..6335618f2 100644 --- a/src/Computer.js +++ b/src/Computer.js @@ -43,9 +43,10 @@ export default class Computer { returnMessage(expectNumbers) { const ballStrike = this.calculateBallStrike(expectNumbers); const result = this.getResultString(ballStrike); - if (ballStrike.strike === Computer.STRIKE) - return { result: result, success: true }; - return { result: result, success: false }; + return { + result: result, + success: ballStrike.strike === Computer.STRIKE, + }; } calculateBallStrike(expectNumbers) { let ball = 0; From bd94bdba8ee66082d807cb036509b6d4c6c93df7 Mon Sep 17 00:00:00 2001 From: Boeun Date: Wed, 25 Oct 2023 22:34:39 +0900 Subject: [PATCH 18/29] =?UTF-8?q?feat:=20=EC=88=AB=EC=9E=90=20=EC=9E=85?= =?UTF-8?q?=EB=A0=A5,=20=EC=9E=AC=EC=8B=9C=EC=9E=91=20=EC=9E=85=EB=A0=A5?= =?UTF-8?q?=20=ED=95=A8=EC=88=98=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Computer.js | 12 +++--------- src/User.js | 31 ++++++++++++++++++++++--------- src/query_status.js | 4 ---- 3 files changed, 25 insertions(+), 22 deletions(-) delete mode 100644 src/query_status.js diff --git a/src/Computer.js b/src/Computer.js index 6335618f2..b9b246cec 100644 --- a/src/Computer.js +++ b/src/Computer.js @@ -1,5 +1,5 @@ import { MissionUtils } from "@woowacourse/mission-utils"; -import { QUERY_STATUS } from "./query_status.js"; + export default class Computer { static QUIT = "2"; static STRIKE = 3; @@ -20,10 +20,7 @@ export default class Computer { while (true) { this.pickRandomNumbers(); while (true) { - const input = await user.returnUserQuery( - "숫자를 입력해주세요 : ", - QUERY_STATUS.PLAYING - ); + const input = await user.returnUserNumber(); const result = this.returnMessage(input); MissionUtils.Console.print(result.result); if (result.success) { @@ -33,10 +30,7 @@ export default class Computer { break; } } - const input = await user.returnUserQuery( - "게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.\n", - QUERY_STATUS.REPLAY - ); + const input = await user.returnUserReplay(); if (input === Computer.QUIT) break; } } diff --git a/src/User.js b/src/User.js index 2d1f049e1..5f2129cad 100644 --- a/src/User.js +++ b/src/User.js @@ -1,16 +1,29 @@ import { MissionUtils } from "@woowacourse/mission-utils"; -import { QUERY_STATUS } from "./query_status.js"; export default class User { - async returnUserQuery(query, status) { - const input = await MissionUtils.Console.readLineAsync(query); - if (!this.checkValidation(input, status)) + async returnUserNumber() { + const input = await MissionUtils.Console.readLineAsync( + "숫자를 입력해주세요 : " + ); + + if (input.length !== 3) throw new Error("[ERROR] 숫자가 잘못된 형식입니다."); + if (input.includes("0")) + throw new Error("[ERROR] 숫자가 잘못된 형식입니다."); + if (isNaN(input)) throw new Error("[ERROR] 숫자가 잘못된 형식입니다."); + return input; } - checkValidation(str, status) { - if (status === QUERY_STATUS.PLAYING && str.length > 3) return false; - else if (status === QUERY_STATUS.REPLAY && str.length > 1) return false; - else if (isNaN(str)) return false; - else return true; + + async returnUserReplay() { + const input = await MissionUtils.Console.readLineAsync( + "게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.\n" + ); + if (input.length !== 1) + throw new Error("[ERROR] 숫자가 잘못된 형식입니다."); + if (input !== "1" && input !== "2") + throw new Error("[ERROR] 숫자가 잘못된 형식입니다."); + if (isNaN(input)) throw new Error("[ERROR] 숫자가 잘못된 형식입니다."); + + return input; } } diff --git a/src/query_status.js b/src/query_status.js deleted file mode 100644 index fb1e3d0e1..000000000 --- a/src/query_status.js +++ /dev/null @@ -1,4 +0,0 @@ -export const QUERY_STATUS = { - PLAYING: "playing", - REPLAY: "replay", -}; From 9e171e76dd3818797bd8262c33ddad52fb39e741 Mon Sep 17 00:00:00 2001 From: Boeun Date: Wed, 25 Oct 2023 22:50:09 +0900 Subject: [PATCH 19/29] =?UTF-8?q?feat:=20calculateBallStrike=20reduce?= =?UTF-8?q?=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Computer.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/Computer.js b/src/Computer.js index b9b246cec..e27280776 100644 --- a/src/Computer.js +++ b/src/Computer.js @@ -43,15 +43,16 @@ export default class Computer { }; } calculateBallStrike(expectNumbers) { - let ball = 0; - let strike = 0; - [...expectNumbers] + return [...expectNumbers] .map((n) => +n) - .forEach((n, idx) => { - if (this.numbers[idx] === n) strike++; - else if (this.numbers.includes(n)) ball++; - }); - return { ball, strike }; + .reduce( + (res, cur, idx) => { + if (this.numbers[idx] === cur) res.strike++; + else if (this.numbers.includes(cur)) res.ball++; + return res; + }, + { ball: 0, strike: 0 } + ); } getResultString({ ball, strike }) { if (ball === 0 && strike === 0) return "낫싱"; From 41e3cc8467d562b59a081653f97f0c32f2ae971a Mon Sep 17 00:00:00 2001 From: Boeun Date: Wed, 25 Oct 2023 22:55:04 +0900 Subject: [PATCH 20/29] style: property value shorthand convention --- src/Computer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Computer.js b/src/Computer.js index e27280776..01951f4b3 100644 --- a/src/Computer.js +++ b/src/Computer.js @@ -38,7 +38,7 @@ export default class Computer { const ballStrike = this.calculateBallStrike(expectNumbers); const result = this.getResultString(ballStrike); return { - result: result, + result, success: ballStrike.strike === Computer.STRIKE, }; } From f39bd22db5b17e28f638b4f6c43df5d583105f80 Mon Sep 17 00:00:00 2001 From: Boeun Date: Wed, 25 Oct 2023 23:06:34 +0900 Subject: [PATCH 21/29] =?UTF-8?q?style:=20=EB=AC=B8=EC=9E=90=EC=97=B4=20?= =?UTF-8?q?=EC=9E=91=EC=9D=80=EB=94=B0=EC=98=B4=ED=91=9C=20=EC=82=AC?= =?UTF-8?q?=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 4 ++-- src/Computer.js | 10 +++++----- src/User.js | 22 +++++++++++----------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/App.js b/src/App.js index cfc167216..608adfe71 100644 --- a/src/App.js +++ b/src/App.js @@ -1,5 +1,5 @@ -import Computer from "./Computer.js"; -import User from "./User.js"; +import Computer from './Computer.js'; +import User from './User.js'; class App { constructor() { diff --git a/src/Computer.js b/src/Computer.js index 01951f4b3..ea4e5c942 100644 --- a/src/Computer.js +++ b/src/Computer.js @@ -1,7 +1,7 @@ -import { MissionUtils } from "@woowacourse/mission-utils"; +import { MissionUtils } from '@woowacourse/mission-utils'; export default class Computer { - static QUIT = "2"; + static QUIT = '2'; static STRIKE = 3; constructor() { this.numbers = []; @@ -16,7 +16,7 @@ export default class Computer { } } async playGame(user) { - MissionUtils.Console.print("숫자 야구 게임을 시작합니다."); + MissionUtils.Console.print('숫자 야구 게임을 시작합니다.'); while (true) { this.pickRandomNumbers(); while (true) { @@ -25,7 +25,7 @@ export default class Computer { MissionUtils.Console.print(result.result); if (result.success) { MissionUtils.Console.print( - "3개의 숫자를 모두 맞히셨습니다! 게임 종료" + '3개의 숫자를 모두 맞히셨습니다! 게임 종료' ); break; } @@ -55,7 +55,7 @@ export default class Computer { ); } getResultString({ ball, strike }) { - if (ball === 0 && strike === 0) return "낫싱"; + if (ball === 0 && strike === 0) return '낫싱'; else if (ball === 0) return `${strike}스트라이크`; else if (strike === 0) return `${ball}볼`; else return `${ball}볼 ${strike}스트라이크`; diff --git a/src/User.js b/src/User.js index 5f2129cad..47e8dd607 100644 --- a/src/User.js +++ b/src/User.js @@ -1,28 +1,28 @@ -import { MissionUtils } from "@woowacourse/mission-utils"; +import { MissionUtils } from '@woowacourse/mission-utils'; export default class User { async returnUserNumber() { const input = await MissionUtils.Console.readLineAsync( - "숫자를 입력해주세요 : " + '숫자를 입력해주세요 : ' ); if (input.length !== 3) - throw new Error("[ERROR] 숫자가 잘못된 형식입니다."); - if (input.includes("0")) - throw new Error("[ERROR] 숫자가 잘못된 형식입니다."); - if (isNaN(input)) throw new Error("[ERROR] 숫자가 잘못된 형식입니다."); + throw new Error('[ERROR] 숫자가 잘못된 형식입니다.'); + if (input.includes('0')) + throw new Error('[ERROR] 숫자가 잘못된 형식입니다.'); + if (isNaN(input)) throw new Error('[ERROR] 숫자가 잘못된 형식입니다.'); return input; } async returnUserReplay() { const input = await MissionUtils.Console.readLineAsync( - "게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.\n" + '게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.\n' ); if (input.length !== 1) - throw new Error("[ERROR] 숫자가 잘못된 형식입니다."); - if (input !== "1" && input !== "2") - throw new Error("[ERROR] 숫자가 잘못된 형식입니다."); - if (isNaN(input)) throw new Error("[ERROR] 숫자가 잘못된 형식입니다."); + throw new Error('[ERROR] 숫자가 잘못된 형식입니다.'); + if (input !== '1' && input !== '2') + throw new Error('[ERROR] 숫자가 잘못된 형식입니다.'); + if (isNaN(input)) throw new Error('[ERROR] 숫자가 잘못된 형식입니다.'); return input; } From 6ab159feeab540b4b069afcc65ff1e62a0e15d56 Mon Sep 17 00:00:00 2001 From: Boeun Date: Wed, 25 Oct 2023 23:07:35 +0900 Subject: [PATCH 22/29] =?UTF-8?q?style:=20reduce=20=EA=B0=9D=EC=B2=B4=20?= =?UTF-8?q?=EB=B3=B5=EC=82=AC=20=ED=9B=84=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Computer.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Computer.js b/src/Computer.js index ea4e5c942..7e81f738d 100644 --- a/src/Computer.js +++ b/src/Computer.js @@ -47,9 +47,10 @@ export default class Computer { .map((n) => +n) .reduce( (res, cur, idx) => { - if (this.numbers[idx] === cur) res.strike++; - else if (this.numbers.includes(cur)) res.ball++; - return res; + const newRes = { ...res }; + if (this.numbers[idx] === cur) newRes.strike++; + else if (this.numbers.includes(cur)) newRes.ball++; + return newRes; }, { ball: 0, strike: 0 } ); From d0f3cbdab39f51c7aed40de86bfcdcd99dc8f16b Mon Sep 17 00:00:00 2001 From: Boeun Date: Wed, 25 Oct 2023 23:20:15 +0900 Subject: [PATCH 23/29] =?UTF-8?q?style:=20++=EB=A5=BC=20+=3D1=EB=A1=9C=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Computer.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Computer.js b/src/Computer.js index 7e81f738d..f014c90aa 100644 --- a/src/Computer.js +++ b/src/Computer.js @@ -48,8 +48,8 @@ export default class Computer { .reduce( (res, cur, idx) => { const newRes = { ...res }; - if (this.numbers[idx] === cur) newRes.strike++; - else if (this.numbers.includes(cur)) newRes.ball++; + if (this.numbers[idx] === cur) newRes.strike += 1; + else if (this.numbers.includes(cur)) newRes.ball += 1; return newRes; }, { ball: 0, strike: 0 } From 20bb592b24e0a2054a59c9e35eb77b71ac1fa484 Mon Sep 17 00:00:00 2001 From: Boeun Date: Wed, 25 Oct 2023 23:31:12 +0900 Subject: [PATCH 24/29] =?UTF-8?q?style:=20if=EB=AC=B8=20=EC=A4=91=EA=B4=84?= =?UTF-8?q?=ED=98=B8=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/User.js | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/User.js b/src/User.js index 47e8dd607..d2a4b790b 100644 --- a/src/User.js +++ b/src/User.js @@ -5,11 +5,15 @@ export default class User { '숫자를 입력해주세요 : ' ); - if (input.length !== 3) + if (input.length !== 3) { throw new Error('[ERROR] 숫자가 잘못된 형식입니다.'); - if (input.includes('0')) + } + if (input.includes('0')) { throw new Error('[ERROR] 숫자가 잘못된 형식입니다.'); - if (isNaN(input)) throw new Error('[ERROR] 숫자가 잘못된 형식입니다.'); + } + if (isNaN(input)) { + throw new Error('[ERROR] 숫자가 잘못된 형식입니다.'); + } return input; } @@ -18,11 +22,15 @@ export default class User { const input = await MissionUtils.Console.readLineAsync( '게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.\n' ); - if (input.length !== 1) + if (input.length !== 1) { + throw new Error('[ERROR] 숫자가 잘못된 형식입니다.'); + } + if (input !== '1' && input !== '2') { throw new Error('[ERROR] 숫자가 잘못된 형식입니다.'); - if (input !== '1' && input !== '2') + } + if (isNaN(input)) { throw new Error('[ERROR] 숫자가 잘못된 형식입니다.'); - if (isNaN(input)) throw new Error('[ERROR] 숫자가 잘못된 형식입니다.'); + } return input; } From c0d686b874d3be43adc11c940384f405cd72ce9f Mon Sep 17 00:00:00 2001 From: Boeun Date: Wed, 25 Oct 2023 23:37:29 +0900 Subject: [PATCH 25/29] =?UTF-8?q?style:=20=EC=A1=B0=EA=B1=B4=EB=AC=B8=20?= =?UTF-8?q?=EA=B2=B0=ED=95=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/User.js | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/src/User.js b/src/User.js index d2a4b790b..d2389f983 100644 --- a/src/User.js +++ b/src/User.js @@ -5,13 +5,7 @@ export default class User { '숫자를 입력해주세요 : ' ); - if (input.length !== 3) { - throw new Error('[ERROR] 숫자가 잘못된 형식입니다.'); - } - if (input.includes('0')) { - throw new Error('[ERROR] 숫자가 잘못된 형식입니다.'); - } - if (isNaN(input)) { + if (input.length !== 3 || input.includes('0') || Number.isNaN(input)) { throw new Error('[ERROR] 숫자가 잘못된 형식입니다.'); } @@ -22,13 +16,11 @@ export default class User { const input = await MissionUtils.Console.readLineAsync( '게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.\n' ); - if (input.length !== 1) { - throw new Error('[ERROR] 숫자가 잘못된 형식입니다.'); - } - if (input !== '1' && input !== '2') { - throw new Error('[ERROR] 숫자가 잘못된 형식입니다.'); - } - if (isNaN(input)) { + if ( + input.length !== 1 || + (input !== '1' && input !== '2') || + Number.isNaN(input) + ) { throw new Error('[ERROR] 숫자가 잘못된 형식입니다.'); } From f15170f3d3abd55392934c1a639b7ae07c1e7301 Mon Sep 17 00:00:00 2001 From: Boeun Date: Wed, 25 Oct 2023 23:39:09 +0900 Subject: [PATCH 26/29] =?UTF-8?q?style:=20=EB=B3=80=EC=88=98=EB=AA=85,=20?= =?UTF-8?q?=ED=95=A8=EC=88=98=EB=AA=85=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Computer.js | 14 +++++++------- src/User.js | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Computer.js b/src/Computer.js index f014c90aa..27698b38c 100644 --- a/src/Computer.js +++ b/src/Computer.js @@ -20,8 +20,8 @@ export default class Computer { while (true) { this.pickRandomNumbers(); while (true) { - const input = await user.returnUserNumber(); - const result = this.returnMessage(input); + const input = await user.getUserNumber(); + const result = this.getMessage(input); MissionUtils.Console.print(result.result); if (result.success) { MissionUtils.Console.print( @@ -30,20 +30,20 @@ export default class Computer { break; } } - const input = await user.returnUserReplay(); + const input = await user.getUserReplay(); if (input === Computer.QUIT) break; } } - returnMessage(expectNumbers) { - const ballStrike = this.calculateBallStrike(expectNumbers); + getMessage(expectedNumbers) { + const ballStrike = this.calculateBallStrike(expectedNumbers); const result = this.getResultString(ballStrike); return { result, success: ballStrike.strike === Computer.STRIKE, }; } - calculateBallStrike(expectNumbers) { - return [...expectNumbers] + calculateBallStrike(expectedNumbers) { + return [...expectedNumbers] .map((n) => +n) .reduce( (res, cur, idx) => { diff --git a/src/User.js b/src/User.js index d2389f983..3a36cf394 100644 --- a/src/User.js +++ b/src/User.js @@ -1,6 +1,6 @@ import { MissionUtils } from '@woowacourse/mission-utils'; export default class User { - async returnUserNumber() { + async getUserNumber() { const input = await MissionUtils.Console.readLineAsync( '숫자를 입력해주세요 : ' ); @@ -12,7 +12,7 @@ export default class User { return input; } - async returnUserReplay() { + async getUserReplay() { const input = await MissionUtils.Console.readLineAsync( '게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.\n' ); From 1df1359422161db6ebc75dfd92c2a6059d56900c Mon Sep 17 00:00:00 2001 From: Boeun Date: Wed, 25 Oct 2023 23:40:47 +0900 Subject: [PATCH 27/29] =?UTF-8?q?style:=20string=20=EB=B0=B0=EC=97=B4=20?= =?UTF-8?q?=EB=B3=80=ED=99=98=20-=20spread=EC=97=90=EC=84=9C=20split?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Computer.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Computer.js b/src/Computer.js index 27698b38c..a31153f29 100644 --- a/src/Computer.js +++ b/src/Computer.js @@ -43,8 +43,9 @@ export default class Computer { }; } calculateBallStrike(expectedNumbers) { - return [...expectedNumbers] - .map((n) => +n) + return expectedNumbers + .split('') + .map(Number) .reduce( (res, cur, idx) => { const newRes = { ...res }; From f3884ecb11810082d4aba25553d4606b95c6136c Mon Sep 17 00:00:00 2001 From: Boeun Date: Wed, 25 Oct 2023 23:45:49 +0900 Subject: [PATCH 28/29] =?UTF-8?q?style:=20=EC=83=81=EC=88=98=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Computer.js | 13 ++++++++----- src/User.js | 11 ++++++++--- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/Computer.js b/src/Computer.js index a31153f29..c940da674 100644 --- a/src/Computer.js +++ b/src/Computer.js @@ -1,14 +1,17 @@ import { MissionUtils } from '@woowacourse/mission-utils'; export default class Computer { - static QUIT = '2'; - static STRIKE = 3; + static REPLAY_CODE = '1'; + static QUIT_CODE = '2'; + static NUM_SIZE = 3; + static MODE_SIZE = 1; + constructor() { this.numbers = []; } pickRandomNumbers() { this.clearNumbers(); - while (this.numbers.length < 3) { + while (this.numbers.length < Computer.NUM_SIZE) { const number = MissionUtils.Random.pickNumberInRange(1, 9); if (!this.numbers.includes(number)) { this.numbers.push(number); @@ -31,7 +34,7 @@ export default class Computer { } } const input = await user.getUserReplay(); - if (input === Computer.QUIT) break; + if (input === Computer.QUIT_CODE) break; } } getMessage(expectedNumbers) { @@ -39,7 +42,7 @@ export default class Computer { const result = this.getResultString(ballStrike); return { result, - success: ballStrike.strike === Computer.STRIKE, + success: ballStrike.strike === Computer.NUM_SIZE, }; } calculateBallStrike(expectedNumbers) { diff --git a/src/User.js b/src/User.js index 3a36cf394..9aabb17d3 100644 --- a/src/User.js +++ b/src/User.js @@ -1,11 +1,16 @@ import { MissionUtils } from '@woowacourse/mission-utils'; +import Computer from './Computer'; export default class User { async getUserNumber() { const input = await MissionUtils.Console.readLineAsync( '숫자를 입력해주세요 : ' ); - if (input.length !== 3 || input.includes('0') || Number.isNaN(input)) { + if ( + input.length !== Computer.NUM_SIZE || + input.includes('0') || + Number.isNaN(input) + ) { throw new Error('[ERROR] 숫자가 잘못된 형식입니다.'); } @@ -17,8 +22,8 @@ export default class User { '게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.\n' ); if ( - input.length !== 1 || - (input !== '1' && input !== '2') || + input.length !== Computer.MODE_SIZE || + (input !== Computer.REPLAY_CODE && input !== Computer.QUIT_CODE) || Number.isNaN(input) ) { throw new Error('[ERROR] 숫자가 잘못된 형식입니다.'); From 5ae3f53dc76cb7a6398ca0d6b1b2d2b1fca802f6 Mon Sep 17 00:00:00 2001 From: Boeun Date: Wed, 25 Oct 2023 23:47:20 +0900 Subject: [PATCH 29/29] =?UTF-8?q?style:=20soft=20tab=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 14 +++--- src/Computer.js | 126 ++++++++++++++++++++++++------------------------ src/User.js | 50 +++++++++---------- 3 files changed, 95 insertions(+), 95 deletions(-) diff --git a/src/App.js b/src/App.js index 608adfe71..52d55ef06 100644 --- a/src/App.js +++ b/src/App.js @@ -2,13 +2,13 @@ import Computer from './Computer.js'; import User from './User.js'; class App { - constructor() { - this.computer = new Computer(); - this.user = new User(); - } - async play() { - await this.computer.playGame(this.user); - } + constructor() { + this.computer = new Computer(); + this.user = new User(); + } + async play() { + await this.computer.playGame(this.user); + } } export default App; diff --git a/src/Computer.js b/src/Computer.js index c940da674..75c629c39 100644 --- a/src/Computer.js +++ b/src/Computer.js @@ -1,72 +1,72 @@ import { MissionUtils } from '@woowacourse/mission-utils'; export default class Computer { - static REPLAY_CODE = '1'; - static QUIT_CODE = '2'; - static NUM_SIZE = 3; - static MODE_SIZE = 1; + static REPLAY_CODE = '1'; + static QUIT_CODE = '2'; + static NUM_SIZE = 3; + static MODE_SIZE = 1; - constructor() { - this.numbers = []; + constructor() { + this.numbers = []; + } + pickRandomNumbers() { + this.clearNumbers(); + while (this.numbers.length < Computer.NUM_SIZE) { + const number = MissionUtils.Random.pickNumberInRange(1, 9); + if (!this.numbers.includes(number)) { + this.numbers.push(number); + } } - pickRandomNumbers() { - this.clearNumbers(); - while (this.numbers.length < Computer.NUM_SIZE) { - const number = MissionUtils.Random.pickNumberInRange(1, 9); - if (!this.numbers.includes(number)) { - this.numbers.push(number); - } + } + async playGame(user) { + MissionUtils.Console.print('숫자 야구 게임을 시작합니다.'); + while (true) { + this.pickRandomNumbers(); + while (true) { + const input = await user.getUserNumber(); + const result = this.getMessage(input); + MissionUtils.Console.print(result.result); + if (result.success) { + MissionUtils.Console.print( + '3개의 숫자를 모두 맞히셨습니다! 게임 종료' + ); + break; } + } + const input = await user.getUserReplay(); + if (input === Computer.QUIT_CODE) break; } - async playGame(user) { - MissionUtils.Console.print('숫자 야구 게임을 시작합니다.'); - while (true) { - this.pickRandomNumbers(); - while (true) { - const input = await user.getUserNumber(); - const result = this.getMessage(input); - MissionUtils.Console.print(result.result); - if (result.success) { - MissionUtils.Console.print( - '3개의 숫자를 모두 맞히셨습니다! 게임 종료' - ); - break; - } - } - const input = await user.getUserReplay(); - if (input === Computer.QUIT_CODE) break; - } - } - getMessage(expectedNumbers) { - const ballStrike = this.calculateBallStrike(expectedNumbers); - const result = this.getResultString(ballStrike); - return { - result, - success: ballStrike.strike === Computer.NUM_SIZE, - }; - } - calculateBallStrike(expectedNumbers) { - return expectedNumbers - .split('') - .map(Number) - .reduce( - (res, cur, idx) => { - const newRes = { ...res }; - if (this.numbers[idx] === cur) newRes.strike += 1; - else if (this.numbers.includes(cur)) newRes.ball += 1; - return newRes; - }, - { ball: 0, strike: 0 } - ); - } - getResultString({ ball, strike }) { - if (ball === 0 && strike === 0) return '낫싱'; - else if (ball === 0) return `${strike}스트라이크`; - else if (strike === 0) return `${ball}볼`; - else return `${ball}볼 ${strike}스트라이크`; - } + } + getMessage(expectedNumbers) { + const ballStrike = this.calculateBallStrike(expectedNumbers); + const result = this.getResultString(ballStrike); + return { + result, + success: ballStrike.strike === Computer.NUM_SIZE, + }; + } + calculateBallStrike(expectedNumbers) { + return expectedNumbers + .split('') + .map(Number) + .reduce( + (res, cur, idx) => { + const newRes = { ...res }; + if (this.numbers[idx] === cur) newRes.strike += 1; + else if (this.numbers.includes(cur)) newRes.ball += 1; + return newRes; + }, + { ball: 0, strike: 0 } + ); + } + getResultString({ ball, strike }) { + if (ball === 0 && strike === 0) return '낫싱'; + else if (ball === 0) return `${strike}스트라이크`; + else if (strike === 0) return `${ball}볼`; + else return `${ball}볼 ${strike}스트라이크`; + } - clearNumbers() { - this.numbers = []; - } + clearNumbers() { + this.numbers = []; + } } diff --git a/src/User.js b/src/User.js index 9aabb17d3..1ccb824fd 100644 --- a/src/User.js +++ b/src/User.js @@ -1,34 +1,34 @@ import { MissionUtils } from '@woowacourse/mission-utils'; import Computer from './Computer'; export default class User { - async getUserNumber() { - const input = await MissionUtils.Console.readLineAsync( - '숫자를 입력해주세요 : ' - ); + async getUserNumber() { + const input = await MissionUtils.Console.readLineAsync( + '숫자를 입력해주세요 : ' + ); - if ( - input.length !== Computer.NUM_SIZE || - input.includes('0') || - Number.isNaN(input) - ) { - throw new Error('[ERROR] 숫자가 잘못된 형식입니다.'); - } - - return input; + if ( + input.length !== Computer.NUM_SIZE || + input.includes('0') || + Number.isNaN(input) + ) { + throw new Error('[ERROR] 숫자가 잘못된 형식입니다.'); } - async getUserReplay() { - const input = await MissionUtils.Console.readLineAsync( - '게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.\n' - ); - if ( - input.length !== Computer.MODE_SIZE || - (input !== Computer.REPLAY_CODE && input !== Computer.QUIT_CODE) || - Number.isNaN(input) - ) { - throw new Error('[ERROR] 숫자가 잘못된 형식입니다.'); - } + return input; + } - return input; + async getUserReplay() { + const input = await MissionUtils.Console.readLineAsync( + '게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.\n' + ); + if ( + input.length !== Computer.MODE_SIZE || + (input !== Computer.REPLAY_CODE && input !== Computer.QUIT_CODE) || + Number.isNaN(input) + ) { + throw new Error('[ERROR] 숫자가 잘못된 형식입니다.'); } + + return input; + } }