From 4e6c17c52314ef36f18685640b51395ac8c0ed7a Mon Sep 17 00:00:00 2001 From: "mingyeong:D" <63152086+Kim-Mingyeong@users.noreply.github.com> Date: Fri, 27 Oct 2023 17:42:49 +0900 Subject: [PATCH 01/17] =?UTF-8?q?docs:=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=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 docs/README.md diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 000000000..b569002bd --- /dev/null +++ b/docs/README.md @@ -0,0 +1,34 @@ +# 미션 - 자동차 경주 + +## 🕹 기능 요구 사항(구현할 기능 목록) + +### ⌨️ 사용자 입력 받기 + +- [ ] 경주할 자동차 이름 받기(쉼표(,) 기준으로 구분, 5자 이하) +- [ ] 시도할 횟수 받기 +- [ ] 잘못된 값 입력 시 `throw`문을 사용해 "[ERROR]"로 시작하는 메시지를 가지는 예외를 발생 후 앱 종료 + +### 🚘 게임 판단 + +- [ ] 전진하는 조건: 0~9 사이 무작위 값을 생성 후 무작위 값이 4 이상 +- [ ] 전진: 자동차 이름과 함께 출력 +- [ ] 각 차수별 실행 결과 출력 + +### 🎮 게임 완료 + +- [ ] 우승자 발표 +- [ ] 우승자가 여러 명일 경우 쉼표(,)로 구분 + +## 🖥 프로그래밍 요구 사항 + +- [ ] Node.js 18.17.1 버전에서 실행 가능 +- [ ] `App.js`의 `play` 메서드에서 프로그램 실행 +- [ ] `ApplicationTest` 의 모든 테스트 성공 +- [ ] indent(인덴트, 들여쓰기) depth를 3이 넘지 않도록 구현 +- [ ] Jest를 이용하여 내가 정리한 기능 목록이 정상 동작함을 테스트 코드로 확인 +- [ ] `@woowacourse/mission-utils`에서 제공하는 `Random` 및 `Console API`를 사용하여 구현 + +## 📓 과제 진행 요구 사항 + +- [ ] 저장소를 Fork & Clone +- [ ] 구현할 기능 목록 정리 From f53c1a0a738e9e73d64d97d5bb93997f6268dbe7 Mon Sep 17 00:00:00 2001 From: "mingyeong:D" <63152086+Kim-Mingyeong@users.noreply.github.com> Date: Fri, 27 Oct 2023 17:46:14 +0900 Subject: [PATCH 02/17] =?UTF-8?q?docs:=20=EA=B0=9C=EB=B0=9C=20=ED=99=98?= =?UTF-8?q?=EA=B2=BD=20=EC=84=A4=EC=A0=95=20=EC=99=84=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/README.md b/docs/README.md index b569002bd..e18feddf6 100644 --- a/docs/README.md +++ b/docs/README.md @@ -21,7 +21,7 @@ ## 🖥 프로그래밍 요구 사항 -- [ ] Node.js 18.17.1 버전에서 실행 가능 +- [x] `Node.js` `18.17.1` 버전에서 실행 가능 - [ ] `App.js`의 `play` 메서드에서 프로그램 실행 - [ ] `ApplicationTest` 의 모든 테스트 성공 - [ ] indent(인덴트, 들여쓰기) depth를 3이 넘지 않도록 구현 @@ -30,5 +30,5 @@ ## 📓 과제 진행 요구 사항 -- [ ] 저장소를 Fork & Clone -- [ ] 구현할 기능 목록 정리 +- [x] 저장소를 Fork & Clone +- [x] 구현할 기능 목록 정리 From 72e74f0207044993b1a7ca206d707b56c54bdf06 Mon Sep 17 00:00:00 2001 From: "mingyeong:D" <63152086+Kim-Mingyeong@users.noreply.github.com> Date: Tue, 31 Oct 2023 21:27:19 +0900 Subject: [PATCH 03/17] =?UTF-8?q?docs:=20=EA=B2=8C=EC=9E=84=EC=97=90=20?= =?UTF-8?q?=EC=82=AC=EC=9A=A9=EB=90=98=EB=8A=94=20=EB=A9=94=EC=8B=9C?= =?UTF-8?q?=EC=A7=80=20=EC=9E=91=EC=84=B1=20=EC=99=84=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 4 +++- src/App.js | 3 +++ src/message.js | 13 +++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 src/message.js diff --git a/docs/README.md b/docs/README.md index e18feddf6..98cbbd3a6 100644 --- a/docs/README.md +++ b/docs/README.md @@ -2,6 +2,8 @@ ## 🕹 기능 요구 사항(구현할 기능 목록) +- [x] 게임에 사용되는 message 작성 + ### ⌨️ 사용자 입력 받기 - [ ] 경주할 자동차 이름 받기(쉼표(,) 기준으로 구분, 5자 이하) @@ -26,7 +28,7 @@ - [ ] `ApplicationTest` 의 모든 테스트 성공 - [ ] indent(인덴트, 들여쓰기) depth를 3이 넘지 않도록 구현 - [ ] Jest를 이용하여 내가 정리한 기능 목록이 정상 동작함을 테스트 코드로 확인 -- [ ] `@woowacourse/mission-utils`에서 제공하는 `Random` 및 `Console API`를 사용하여 구현 +- [ ] `@woowacourse/mission-utils`에서 제공하는 `Random` 및 `Console` API를 사용하여 구현 ## 📓 과제 진행 요구 사항 diff --git a/src/App.js b/src/App.js index c38b30d5b..0f9b7d4af 100644 --- a/src/App.js +++ b/src/App.js @@ -1,3 +1,6 @@ +import { Random, Console } from "@woowacourse/mission-utils"; +import { GAME, ERROR } from "./message"; + class App { async play() {} } diff --git a/src/message.js b/src/message.js new file mode 100644 index 000000000..b715b48aa --- /dev/null +++ b/src/message.js @@ -0,0 +1,13 @@ +export const GAME = { + GET_CAR_NAME: + "경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)\n", + GET_ATTEMPT_COUNT: "시도할 횟수는 몇 회인가요?\n", + WINNER: "최종 우승자 : ", + PRINT_EXECUTION_RESULT: "최종 결과", +}; + +export const ERROR = { + CAR_NAME: + "[ERROR] 자동차 이름은 쉼표(,) 기준으로 구분하며 5자 이하로 입력 가능힙니다.", + ATTEMPT_COUNT: "[ERROR] 숫자만 입력 가능합니다.", +}; From 3889d46cd9d18fb2cabfb0175dcc75d161bf7ae2 Mon Sep 17 00:00:00 2001 From: "mingyeong:D" <63152086+Kim-Mingyeong@users.noreply.github.com> Date: Tue, 31 Oct 2023 23:37:46 +0900 Subject: [PATCH 04/17] =?UTF-8?q?feat:=20=EA=B2=BD=EC=A3=BC=ED=95=A0=20?= =?UTF-8?q?=EC=9E=90=EB=8F=99=EC=B0=A8=20=EC=9D=B4=EB=A6=84=20=EB=B0=9B?= =?UTF-8?q?=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20=EC=99=84=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- src/App.js | 20 ++++++++++++++++++++ src/message.js | 2 +- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/docs/README.md b/docs/README.md index 98cbbd3a6..fea726589 100644 --- a/docs/README.md +++ b/docs/README.md @@ -6,7 +6,7 @@ ### ⌨️ 사용자 입력 받기 -- [ ] 경주할 자동차 이름 받기(쉼표(,) 기준으로 구분, 5자 이하) +- [x] 경주할 자동차 이름 받기(쉼표(,) 기준으로 구분, 5자 이하) - [ ] 시도할 횟수 받기 - [ ] 잘못된 값 입력 시 `throw`문을 사용해 "[ERROR]"로 시작하는 메시지를 가지는 예외를 발생 후 앱 종료 diff --git a/src/App.js b/src/App.js index 0f9b7d4af..7f8128937 100644 --- a/src/App.js +++ b/src/App.js @@ -2,6 +2,26 @@ import { Random, Console } from "@woowacourse/mission-utils"; import { GAME, ERROR } from "./message"; class App { + validateCarName = (carNamesArray) => { + // for (let i = 0; i < carNamesArray.length; i++) { + // if (carNamesArray[i].length > 5) throw new Error(ERROR.CAR_NAME); + // } + for (let i = 0; i < carNamesArray.length; i++) { + const carNames = carNamesArray[i].trim(); // 공백 제거 + if (carNames.length > 5) { + throw new Error("ERROR.CAR_NAME"); + } + } + }; + + getCarName = async () => { + const getCarName = await Console.readLineAsync(GAME.GET_CAR_NAME); + //const carNamesArray = [...new Set(getCarName.split(",").map(String))]; + const carNamesArray = getCarName.split(","); + this.validateCarName(carNamesArray); + + return carNamesArray; + }; async play() {} } diff --git a/src/message.js b/src/message.js index b715b48aa..475d2b871 100644 --- a/src/message.js +++ b/src/message.js @@ -8,6 +8,6 @@ export const GAME = { export const ERROR = { CAR_NAME: - "[ERROR] 자동차 이름은 쉼표(,) 기준으로 구분하며 5자 이하로 입력 가능힙니다.", + "[ERROR] 자동차 이름은 쉼표(,)로 구분되어야 하며 5자 이하로 입력 가능힙니다.", ATTEMPT_COUNT: "[ERROR] 숫자만 입력 가능합니다.", }; From a919b25b5225c6a7c9bc576bdc89a251e65cb989 Mon Sep 17 00:00:00 2001 From: "mingyeong:D" <63152086+Kim-Mingyeong@users.noreply.github.com> Date: Tue, 31 Oct 2023 23:46:30 +0900 Subject: [PATCH 05/17] =?UTF-8?q?refactor:=20=EA=B2=BD=EC=A3=BC=ED=95=A0?= =?UTF-8?q?=20=EC=9E=90=EB=8F=99=EC=B0=A8=20=EC=9D=B4=EB=A6=84=20=EB=B0=9B?= =?UTF-8?q?=EB=8A=94=20=EA=B8=B0=EB=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/App.js b/src/App.js index 7f8128937..a2c6e4de6 100644 --- a/src/App.js +++ b/src/App.js @@ -3,20 +3,15 @@ import { GAME, ERROR } from "./message"; class App { validateCarName = (carNamesArray) => { - // for (let i = 0; i < carNamesArray.length; i++) { - // if (carNamesArray[i].length > 5) throw new Error(ERROR.CAR_NAME); - // } - for (let i = 0; i < carNamesArray.length; i++) { - const carNames = carNamesArray[i].trim(); // 공백 제거 - if (carNames.length > 5) { - throw new Error("ERROR.CAR_NAME"); + carNamesArray.forEach((carName) => { + if (carName.length > 5) { + throw new Error(ERROR.CAR_NAME); } - } + }); }; getCarName = async () => { const getCarName = await Console.readLineAsync(GAME.GET_CAR_NAME); - //const carNamesArray = [...new Set(getCarName.split(",").map(String))]; const carNamesArray = getCarName.split(","); this.validateCarName(carNamesArray); From 854952bf13f53c57eeecacfe39e1bb1946d0fab5 Mon Sep 17 00:00:00 2001 From: "mingyeong:D" <63152086+Kim-Mingyeong@users.noreply.github.com> Date: Wed, 1 Nov 2023 15:38:22 +0900 Subject: [PATCH 06/17] =?UTF-8?q?feat:=20=EC=8B=9C=EB=8F=84=ED=95=A0=20?= =?UTF-8?q?=ED=9A=9F=EC=88=98=20=EB=B0=9B=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EB=B0=8F=20=EC=82=AC=EC=9A=A9=EC=9E=90=EA=B0=80=20=EC=9E=98?= =?UTF-8?q?=EB=AA=BB=20=EC=9E=85=EB=A0=A5=EC=8B=9C=20=EB=B0=9C=EC=83=9D?= =?UTF-8?q?=ED=95=98=EB=8A=94=20error=20=EA=B8=B0=EB=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/App.js b/src/App.js index a2c6e4de6..26ca7ce56 100644 --- a/src/App.js +++ b/src/App.js @@ -11,12 +11,26 @@ class App { }; getCarName = async () => { - const getCarName = await Console.readLineAsync(GAME.GET_CAR_NAME); - const carNamesArray = getCarName.split(","); + const carNames = await Console.readLineAsync(GAME.GET_CAR_NAME); + const carNamesArray = carNames.split(","); this.validateCarName(carNamesArray); return carNamesArray; }; + + validateNumber = (attemptCount) => { + if (isNaN(attemptCount)) { + throw new Error(ERROR.ATTEMPT_COUNT); + } + }; + + getAttemptCount = async () => { + const attemptCount = await Console.readLineAsync(GAME.GET_ATTEMPT_COUNT); + this.validateNumber(attemptCount); + + return attemptCount; + }; + async play() {} } From 8a1dd07e09642c3baf27f36ad8ab5ca74e6b43b6 Mon Sep 17 00:00:00 2001 From: "mingyeong:D" <63152086+Kim-Mingyeong@users.noreply.github.com> Date: Wed, 1 Nov 2023 15:39:20 +0900 Subject: [PATCH 07/17] =?UTF-8?q?docs=20:=20=EC=8B=9C=EB=8F=84=ED=95=A0=20?= =?UTF-8?q?=ED=9A=9F=EC=88=98=20=EB=B0=9B=EA=B8=B0=20=EB=B0=8F=20=EB=AC=B8?= =?UTF-8?q?=EC=9D=84=20=EC=82=AC=EC=9A=A9=ED=95=B4=20=EC=98=88=EC=99=B8=20?= =?UTF-8?q?=EB=B0=9C=EC=83=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/README.md b/docs/README.md index fea726589..bf882565c 100644 --- a/docs/README.md +++ b/docs/README.md @@ -7,8 +7,8 @@ ### ⌨️ 사용자 입력 받기 - [x] 경주할 자동차 이름 받기(쉼표(,) 기준으로 구분, 5자 이하) -- [ ] 시도할 횟수 받기 -- [ ] 잘못된 값 입력 시 `throw`문을 사용해 "[ERROR]"로 시작하는 메시지를 가지는 예외를 발생 후 앱 종료 +- [x] 시도할 횟수 받기 +- [x] 잘못된 값 입력 시 `throw`문을 사용해 "[ERROR]"로 시작하는 메시지를 가지는 예외를 발생 후 앱 종료 ### 🚘 게임 판단 From 3832bbb8bf35cd8de5bed6f9516e655d78786cf6 Mon Sep 17 00:00:00 2001 From: "mingyeong:D" <63152086+Kim-Mingyeong@users.noreply.github.com> Date: Wed, 1 Nov 2023 16:09:23 +0900 Subject: [PATCH 08/17] =?UTF-8?q?feat:=20=EC=A0=84=EC=A7=84=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=EC=A1=B0=EA=B1=B4=20=EA=B8=B0=EB=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- src/App.js | 26 ++++++++++++++++++++------ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/docs/README.md b/docs/README.md index bf882565c..44754a8f8 100644 --- a/docs/README.md +++ b/docs/README.md @@ -12,7 +12,7 @@ ### 🚘 게임 판단 -- [ ] 전진하는 조건: 0~9 사이 무작위 값을 생성 후 무작위 값이 4 이상 +- [x] 전진하는 조건: 0~9 사이 무작위 값을 생성 후 무작위 값이 4 이상 - [ ] 전진: 자동차 이름과 함께 출력 - [ ] 각 차수별 실행 결과 출력 diff --git a/src/App.js b/src/App.js index 26ca7ce56..206d670c2 100644 --- a/src/App.js +++ b/src/App.js @@ -10,6 +10,12 @@ class App { }); }; + validateNumber = (attemptCount) => { + if (isNaN(attemptCount)) { + throw new Error(ERROR.ATTEMPT_COUNT); + } + }; + getCarName = async () => { const carNames = await Console.readLineAsync(GAME.GET_CAR_NAME); const carNamesArray = carNames.split(","); @@ -18,12 +24,6 @@ class App { return carNamesArray; }; - validateNumber = (attemptCount) => { - if (isNaN(attemptCount)) { - throw new Error(ERROR.ATTEMPT_COUNT); - } - }; - getAttemptCount = async () => { const attemptCount = await Console.readLineAsync(GAME.GET_ATTEMPT_COUNT); this.validateNumber(attemptCount); @@ -31,6 +31,20 @@ class App { return attemptCount; }; + generateRandomNumber = () => { + const randomNumber = Random.pickNumberInRange(0, 9); + return randomNumber; + }; + + moveForwardCondition = () => { + const randomNumber = this.generateRandomNumber(); + let position = 0; + if (randomNumber >= 4) { + position += 1; + } + return position; + }; + async play() {} } From 908d80b4be8f081d081b334f373e84f19fd7efcc Mon Sep 17 00:00:00 2001 From: "mingyeong:D" <63152086+Kim-Mingyeong@users.noreply.github.com> Date: Wed, 1 Nov 2023 20:15:37 +0900 Subject: [PATCH 09/17] =?UTF-8?q?feat:=20=EC=9E=90=EB=8F=99=EC=B0=A8=20?= =?UTF-8?q?=EC=9D=B4=EB=A6=84=EA=B3=BC=20=ED=95=A8=EA=BB=98=20=EC=A0=84?= =?UTF-8?q?=EC=A7=84=ED=95=9C=20=EA=B2=B0=EA=B3=BC=20=EC=B6=9C=EB=A0=A5=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5,=20=EC=9A=B0=EC=8A=B9=EC=9E=90=20=EB=B0=9C?= =?UTF-8?q?=ED=91=9C=20=EA=B8=B0=EB=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 62 +++++++++++++++++++++++++++++++++++++++----------- src/message.js | 2 +- 2 files changed, 50 insertions(+), 14 deletions(-) diff --git a/src/App.js b/src/App.js index 206d670c2..3cf92fca4 100644 --- a/src/App.js +++ b/src/App.js @@ -2,33 +2,38 @@ import { Random, Console } from "@woowacourse/mission-utils"; import { GAME, ERROR } from "./message"; class App { - validateCarName = (carNamesArray) => { - carNamesArray.forEach((carName) => { + constructor() { + this.carsName = this.getCarsName(); + this.position = 0; + } + validateCarName = (carsNameArray) => { + carsNameArray.forEach((carName) => { if (carName.length > 5) { throw new Error(ERROR.CAR_NAME); } }); }; - validateNumber = (attemptCount) => { - if (isNaN(attemptCount)) { + validateNumber = (attemptNumber) => { + if (isNaN(attemptNumber) || attemptNumber <= 0) { throw new Error(ERROR.ATTEMPT_COUNT); } }; - getCarName = async () => { - const carNames = await Console.readLineAsync(GAME.GET_CAR_NAME); - const carNamesArray = carNames.split(","); - this.validateCarName(carNamesArray); + getCarsName = async () => { + const carsName = await Console.readLineAsync(GAME.GET_CAR_NAME); + const carsNameArray = carsName.split(","); + this.validateCarName(carsNameArray); - return carNamesArray; + return carsNameArray; }; getAttemptCount = async () => { const attemptCount = await Console.readLineAsync(GAME.GET_ATTEMPT_COUNT); - this.validateNumber(attemptCount); + const attemptNumber = Number(attemptCount); + this.validateNumber(attemptNumber); - return attemptCount; + return attemptNumber; }; generateRandomNumber = () => { @@ -38,14 +43,45 @@ class App { moveForwardCondition = () => { const randomNumber = this.generateRandomNumber(); - let position = 0; if (randomNumber >= 4) { position += 1; } return position; }; - async play() {} + moveForward = () => { + this.carsName.forEach((car) => car.carMove); + Console.print(`${car.carsName} : ${"-".repeat(car.position)}`); + }; + + selectWinnerByRace = () => { + const maxCarPosition = Math.max(...this.cars.map((car) => car.position)); + const winnerCars = this.carsName.filter( + (car) => car.position === maxCarPosition + ); + return winnerCars.map((car) => car.carName); + }; + + racingGame = () => { + const attemptCount = this.getAttemptCount(); + while (attemptCount > 0) { + this.moveForward(); + attemptCount -= 1; + } + const winners = this.selectWinnerByRace(); + if (winners.length > 1) { + Console.print(`${GAME.WINNER}${winners.join(", ")}`); + } else { + Console.print(`${GAME.WINNER}${winners}`); + } + }; + + async play() { + await this.getCarsName(); + await this.getAttemptCount(); + Console.print(GAME.PRINT_EXECUTION_RESULT); + this.racingGame(); + } } export default App; diff --git a/src/message.js b/src/message.js index 475d2b871..977d560df 100644 --- a/src/message.js +++ b/src/message.js @@ -8,6 +8,6 @@ export const GAME = { export const ERROR = { CAR_NAME: - "[ERROR] 자동차 이름은 쉼표(,)로 구분되어야 하며 5자 이하로 입력 가능힙니다.", + "[ERROR] 자동차 이름은 쉼표(,)로 구분되어야 하며 5자 이하로 입력 가능합니다.", ATTEMPT_COUNT: "[ERROR] 숫자만 입력 가능합니다.", }; From 07ac339216d9e1fa93128693dd9be80c230fe550 Mon Sep 17 00:00:00 2001 From: "mingyeong:D" <63152086+Kim-Mingyeong@users.noreply.github.com> Date: Wed, 1 Nov 2023 20:17:05 +0900 Subject: [PATCH 10/17] =?UTF-8?q?docs:=20=EC=9E=90=EB=8F=99=EC=B0=A8=20?= =?UTF-8?q?=EC=9D=B4=EB=A6=84=EA=B3=BC=20=ED=95=A8=EA=BB=98=20=EC=A0=84?= =?UTF-8?q?=EC=A7=84=ED=95=9C=20=EA=B2=B0=EA=B3=BC=20=EC=B6=9C=EB=A0=A5=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5,=20=EC=9A=B0=EC=8A=B9=EC=9E=90=20=EB=B0=9C?= =?UTF-8?q?=ED=91=9C=20=EA=B8=B0=EB=8A=A5=20=EC=99=84=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/README.md b/docs/README.md index 44754a8f8..7accdb767 100644 --- a/docs/README.md +++ b/docs/README.md @@ -13,22 +13,22 @@ ### 🚘 게임 판단 - [x] 전진하는 조건: 0~9 사이 무작위 값을 생성 후 무작위 값이 4 이상 -- [ ] 전진: 자동차 이름과 함께 출력 -- [ ] 각 차수별 실행 결과 출력 +- [x] 전진: 자동차 이름과 함께 출력 +- [x] 각 차수별 실행 결과 출력 ### 🎮 게임 완료 -- [ ] 우승자 발표 -- [ ] 우승자가 여러 명일 경우 쉼표(,)로 구분 +- [x] 우승자 발표 +- [x] 우승자가 여러 명일 경우 쉼표(,)로 구분 ## 🖥 프로그래밍 요구 사항 - [x] `Node.js` `18.17.1` 버전에서 실행 가능 -- [ ] `App.js`의 `play` 메서드에서 프로그램 실행 +- [x] `App.js`의 `play` 메서드에서 프로그램 실행 - [ ] `ApplicationTest` 의 모든 테스트 성공 - [ ] indent(인덴트, 들여쓰기) depth를 3이 넘지 않도록 구현 - [ ] Jest를 이용하여 내가 정리한 기능 목록이 정상 동작함을 테스트 코드로 확인 -- [ ] `@woowacourse/mission-utils`에서 제공하는 `Random` 및 `Console` API를 사용하여 구현 +- [x] `@woowacourse/mission-utils`에서 제공하는 `Random` 및 `Console` API를 사용하여 구현 ## 📓 과제 진행 요구 사항 From 62310cad5f2a887722ed824a8e02476b49f0f0f7 Mon Sep 17 00:00:00 2001 From: "mingyeong:D" <63152086+Kim-Mingyeong@users.noreply.github.com> Date: Wed, 1 Nov 2023 20:29:42 +0900 Subject: [PATCH 11/17] =?UTF-8?q?refactor:=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EA=B5=AC=EC=84=B1=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- src/App.js | 59 +++++++++++++++++++++++++------------------------- 2 files changed, 31 insertions(+), 30 deletions(-) diff --git a/docs/README.md b/docs/README.md index 7accdb767..2dceae2ab 100644 --- a/docs/README.md +++ b/docs/README.md @@ -26,7 +26,7 @@ - [x] `Node.js` `18.17.1` 버전에서 실행 가능 - [x] `App.js`의 `play` 메서드에서 프로그램 실행 - [ ] `ApplicationTest` 의 모든 테스트 성공 -- [ ] indent(인덴트, 들여쓰기) depth를 3이 넘지 않도록 구현 +- [x] indent(인덴트, 들여쓰기) depth를 3이 넘지 않도록 구현 - [ ] Jest를 이용하여 내가 정리한 기능 목록이 정상 동작함을 테스트 코드로 확인 - [x] `@woowacourse/mission-utils`에서 제공하는 `Random` 및 `Console` API를 사용하여 구현 diff --git a/src/App.js b/src/App.js index 3cf92fca4..1b994cd2a 100644 --- a/src/App.js +++ b/src/App.js @@ -1,17 +1,28 @@ import { Random, Console } from "@woowacourse/mission-utils"; import { GAME, ERROR } from "./message"; +class Car { + constructor(name) { + this.name = name; + this.position = 0; + } + + moveForward = () => { + const randomNumber = Random.pickNumberInRange(0, 9); + if (randomNumber >= 4) { + position += 1; + } + }; +} + class App { constructor() { - this.carsName = this.getCarsName(); - this.position = 0; + this.cars = []; } - validateCarName = (carsNameArray) => { - carsNameArray.forEach((carName) => { - if (carName.length > 5) { - throw new Error(ERROR.CAR_NAME); - } - }); + validateCarName = (carName) => { + if (carName.length > 5) { + throw new Error(ERROR.CAR_NAME); + } }; validateNumber = (attemptNumber) => { @@ -23,9 +34,10 @@ class App { getCarsName = async () => { const carsName = await Console.readLineAsync(GAME.GET_CAR_NAME); const carsNameArray = carsName.split(","); - this.validateCarName(carsNameArray); - - return carsNameArray; + carsNameArray.forEach((carName) => { + this.validateCarName(carName); + this.cars.push(new Car(carName)); + }); }; getAttemptCount = async () => { @@ -36,22 +48,11 @@ class App { return attemptNumber; }; - generateRandomNumber = () => { - const randomNumber = Random.pickNumberInRange(0, 9); - return randomNumber; - }; - - moveForwardCondition = () => { - const randomNumber = this.generateRandomNumber(); - if (randomNumber >= 4) { - position += 1; - } - return position; - }; - - moveForward = () => { - this.carsName.forEach((car) => car.carMove); - Console.print(`${car.carsName} : ${"-".repeat(car.position)}`); + moveAllCars = () => { + this.cars.forEach((car) => { + car.moveForward(); + Console.print(`${car.name} : ${"-".repeat(car.position)}`); + }); }; selectWinnerByRace = () => { @@ -59,13 +60,13 @@ class App { const winnerCars = this.carsName.filter( (car) => car.position === maxCarPosition ); - return winnerCars.map((car) => car.carName); + return winnerCars.map((car) => car.name); }; racingGame = () => { const attemptCount = this.getAttemptCount(); while (attemptCount > 0) { - this.moveForward(); + this.moveAllCars(); attemptCount -= 1; } const winners = this.selectWinnerByRace(); From c994311c69e701cf972a5d6bdf4c684daac37eb2 Mon Sep 17 00:00:00 2001 From: "mingyeong:D" <63152086+Kim-Mingyeong@users.noreply.github.com> Date: Wed, 1 Nov 2023 21:43:36 +0900 Subject: [PATCH 12/17] =?UTF-8?q?test:=20vaildator=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- __tests__/RefereeTest.js | 7 +++++++ __tests__/ValidatorTest.js | 15 +++++++++++++++ src/App.js | 4 ++-- src/message.js | 3 +-- 4 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 __tests__/RefereeTest.js create mode 100644 __tests__/ValidatorTest.js diff --git a/__tests__/RefereeTest.js b/__tests__/RefereeTest.js new file mode 100644 index 000000000..11bcaefd8 --- /dev/null +++ b/__tests__/RefereeTest.js @@ -0,0 +1,7 @@ +import App from "../src/App.js"; + +discribe("자동차 경주 판단 테스트", () => { + test("단독 우승자 판단", () => {}); + + test("공동 우승자 판단", () => {}); +}); diff --git a/__tests__/ValidatorTest.js b/__tests__/ValidatorTest.js new file mode 100644 index 000000000..be3884a82 --- /dev/null +++ b/__tests__/ValidatorTest.js @@ -0,0 +1,15 @@ +import App from "../src/App.js"; +import { GAME, ERROR } from "./message"; + +discribe("조건 검증 테스트", () => { + test("자동차 이름이 5자를 초과할 경우 에러 발생", () => { + expect(() => App.validateCarName(["abcdef"])).toThrow(ERROR.CAR_NAME); + expect(() => App.validateCarName(["abc", "efghij"])).toThrow( + ERROR.CAR_NAME + ); + }); + + test("시도할 횟수가 숫자가 아닐 경우 에러 발생", () => { + expect(() => App.validateNumber(NaN)).toThrow(ERROR.ATTEMPT_COUNT); + }); +}); diff --git a/src/App.js b/src/App.js index 1b994cd2a..2c80705b6 100644 --- a/src/App.js +++ b/src/App.js @@ -10,7 +10,7 @@ class Car { moveForward = () => { const randomNumber = Random.pickNumberInRange(0, 9); if (randomNumber >= 4) { - position += 1; + this.position += 1; } }; } @@ -57,7 +57,7 @@ class App { selectWinnerByRace = () => { const maxCarPosition = Math.max(...this.cars.map((car) => car.position)); - const winnerCars = this.carsName.filter( + const winnerCars = this.cars.filter( (car) => car.position === maxCarPosition ); return winnerCars.map((car) => car.name); diff --git a/src/message.js b/src/message.js index 977d560df..891f912bd 100644 --- a/src/message.js +++ b/src/message.js @@ -7,7 +7,6 @@ export const GAME = { }; export const ERROR = { - CAR_NAME: - "[ERROR] 자동차 이름은 쉼표(,)로 구분되어야 하며 5자 이하로 입력 가능합니다.", + CAR_NAME: "[ERROR] 자동차 이름은 5자 이하로 입력 가능합니다.", ATTEMPT_COUNT: "[ERROR] 숫자만 입력 가능합니다.", }; From 4b419a88260627d7a3be145457d44df37e845bbb Mon Sep 17 00:00:00 2001 From: "mingyeong:D" <63152086+Kim-Mingyeong@users.noreply.github.com> Date: Wed, 1 Nov 2023 22:42:48 +0900 Subject: [PATCH 13/17] =?UTF-8?q?fix:=20=EC=BD=94=EB=93=9C=20=EC=98=A4?= =?UTF-8?q?=EB=A5=98=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- __tests__/RefereeTest.js | 7 ------- __tests__/ValidatorTest.js | 15 --------------- src/App.js | 23 +++++++++++++---------- src/message.js | 2 +- 4 files changed, 14 insertions(+), 33 deletions(-) delete mode 100644 __tests__/RefereeTest.js delete mode 100644 __tests__/ValidatorTest.js diff --git a/__tests__/RefereeTest.js b/__tests__/RefereeTest.js deleted file mode 100644 index 11bcaefd8..000000000 --- a/__tests__/RefereeTest.js +++ /dev/null @@ -1,7 +0,0 @@ -import App from "../src/App.js"; - -discribe("자동차 경주 판단 테스트", () => { - test("단독 우승자 판단", () => {}); - - test("공동 우승자 판단", () => {}); -}); diff --git a/__tests__/ValidatorTest.js b/__tests__/ValidatorTest.js deleted file mode 100644 index be3884a82..000000000 --- a/__tests__/ValidatorTest.js +++ /dev/null @@ -1,15 +0,0 @@ -import App from "../src/App.js"; -import { GAME, ERROR } from "./message"; - -discribe("조건 검증 테스트", () => { - test("자동차 이름이 5자를 초과할 경우 에러 발생", () => { - expect(() => App.validateCarName(["abcdef"])).toThrow(ERROR.CAR_NAME); - expect(() => App.validateCarName(["abc", "efghij"])).toThrow( - ERROR.CAR_NAME - ); - }); - - test("시도할 횟수가 숫자가 아닐 경우 에러 발생", () => { - expect(() => App.validateNumber(NaN)).toThrow(ERROR.ATTEMPT_COUNT); - }); -}); diff --git a/src/App.js b/src/App.js index 2c80705b6..3b4364109 100644 --- a/src/App.js +++ b/src/App.js @@ -19,12 +19,12 @@ class App { constructor() { this.cars = []; } + validateCarName = (carName) => { if (carName.length > 5) { throw new Error(ERROR.CAR_NAME); } }; - validateNumber = (attemptNumber) => { if (isNaN(attemptNumber) || attemptNumber <= 0) { throw new Error(ERROR.ATTEMPT_COUNT); @@ -49,6 +49,7 @@ class App { }; moveAllCars = () => { + Console.print(GAME.PRINT_EXECUTION_RESULT); this.cars.forEach((car) => { car.moveForward(); Console.print(`${car.name} : ${"-".repeat(car.position)}`); @@ -63,12 +64,7 @@ class App { return winnerCars.map((car) => car.name); }; - racingGame = () => { - const attemptCount = this.getAttemptCount(); - while (attemptCount > 0) { - this.moveAllCars(); - attemptCount -= 1; - } + printWinners = () => { const winners = this.selectWinnerByRace(); if (winners.length > 1) { Console.print(`${GAME.WINNER}${winners.join(", ")}`); @@ -77,11 +73,18 @@ class App { } }; + racingGame = (attemptCount) => { + while (attemptCount > 0) { + this.moveAllCars(); + attemptCount -= 1; + } + this.printWinners(); + }; + async play() { await this.getCarsName(); - await this.getAttemptCount(); - Console.print(GAME.PRINT_EXECUTION_RESULT); - this.racingGame(); + const attemptCount = await this.getAttemptCount(); + this.racingGame(attemptCount); } } diff --git a/src/message.js b/src/message.js index 891f912bd..f5de2f496 100644 --- a/src/message.js +++ b/src/message.js @@ -3,7 +3,7 @@ export const GAME = { "경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)\n", GET_ATTEMPT_COUNT: "시도할 횟수는 몇 회인가요?\n", WINNER: "최종 우승자 : ", - PRINT_EXECUTION_RESULT: "최종 결과", + PRINT_EXECUTION_RESULT: "실행 결과", }; export const ERROR = { From aa093c3872e0a69fa00bb8d9715700b03fc9ed13 Mon Sep 17 00:00:00 2001 From: "mingyeong:D" <63152086+Kim-Mingyeong@users.noreply.github.com> Date: Wed, 1 Nov 2023 22:43:31 +0900 Subject: [PATCH 14/17] =?UTF-8?q?docs:=20ApplicationTest=EC=9D=98=20?= =?UTF-8?q?=EB=AA=A8=EB=93=A0=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=84=B1?= =?UTF-8?q?=EA=B3=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index 2dceae2ab..c0e568df3 100644 --- a/docs/README.md +++ b/docs/README.md @@ -25,7 +25,7 @@ - [x] `Node.js` `18.17.1` 버전에서 실행 가능 - [x] `App.js`의 `play` 메서드에서 프로그램 실행 -- [ ] `ApplicationTest` 의 모든 테스트 성공 +- [x] `ApplicationTest` 의 모든 테스트 성공 - [x] indent(인덴트, 들여쓰기) depth를 3이 넘지 않도록 구현 - [ ] Jest를 이용하여 내가 정리한 기능 목록이 정상 동작함을 테스트 코드로 확인 - [x] `@woowacourse/mission-utils`에서 제공하는 `Random` 및 `Console` API를 사용하여 구현 From 3eadf1881a5778098345f6aa766dca72961150fd Mon Sep 17 00:00:00 2001 From: "mingyeong:D" <63152086+Kim-Mingyeong@users.noreply.github.com> Date: Wed, 1 Nov 2023 23:24:53 +0900 Subject: [PATCH 15/17] =?UTF-8?q?test:=20=EC=82=AC=EC=9A=A9=EC=9E=90?= =?UTF-8?q?=EA=B0=80=20=EC=9E=85=EB=A0=A5=ED=95=98=EB=8A=94=20=EA=B0=92?= =?UTF-8?q?=EC=97=90=20=EB=8C=80=ED=95=9C=20=EC=A1=B0=EA=B1=B4=20=EA=B2=80?= =?UTF-8?q?=EC=A6=9D=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- __tests__/ValidatorTest.js | 29 +++++++++++++++++++++++++++++ docs/README.md | 2 +- 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 __tests__/ValidatorTest.js diff --git a/__tests__/ValidatorTest.js b/__tests__/ValidatorTest.js new file mode 100644 index 000000000..24dccc9c1 --- /dev/null +++ b/__tests__/ValidatorTest.js @@ -0,0 +1,29 @@ +import App from "../src/App.js"; +import { ERROR } from "../src/message.js"; + +const mockFn = jest.fn(); + +validateCarName = (arr) => { + arr.forEach((carName) => { + if (carName.length > 5) { + mockFn(carName); + } + }); +}; + +validateCarName(["abcdef"]); +validateCarName(["abc", "defghi"]); + +describe("조건 검증 테스트", () => { + const app = new App(); + + test("자동차 이름이 5자를 초과할 경우 에러 발생", () => { + expect(mockFn.mock.calls[0].length).toBeLessThanOrEqual(5); + expect(mockFn.mock.calls[1].length).toBeLessThanOrEqual(5); + expect(mockFn.mock.calls.length).toBe(2); + }); + + test("시도할 횟수가 숫자가 아닐 경우 에러 발생", () => { + expect(() => app.validateNumber(NaN)).toThrow(ERROR.ATTEMPT_COUNT); + }); +}); diff --git a/docs/README.md b/docs/README.md index c0e568df3..99fd40e6d 100644 --- a/docs/README.md +++ b/docs/README.md @@ -27,7 +27,7 @@ - [x] `App.js`의 `play` 메서드에서 프로그램 실행 - [x] `ApplicationTest` 의 모든 테스트 성공 - [x] indent(인덴트, 들여쓰기) depth를 3이 넘지 않도록 구현 -- [ ] Jest를 이용하여 내가 정리한 기능 목록이 정상 동작함을 테스트 코드로 확인 +- [x] Jest를 이용하여 내가 정리한 기능 목록이 정상 동작함을 테스트 코드로 확인 - [x] `@woowacourse/mission-utils`에서 제공하는 `Random` 및 `Console` API를 사용하여 구현 ## 📓 과제 진행 요구 사항 From df1599b9dbb23a4e5dbb87d8dc97acb1c339d24f Mon Sep 17 00:00:00 2001 From: "mingyeong:D" <63152086+Kim-Mingyeong@users.noreply.github.com> Date: Wed, 1 Nov 2023 23:49:23 +0900 Subject: [PATCH 16/17] =?UTF-8?q?test:=20validator=20=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- __tests__/ValidatorTest.js | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/__tests__/ValidatorTest.js b/__tests__/ValidatorTest.js index 24dccc9c1..dd03fbe5e 100644 --- a/__tests__/ValidatorTest.js +++ b/__tests__/ValidatorTest.js @@ -3,22 +3,16 @@ import { ERROR } from "../src/message.js"; const mockFn = jest.fn(); -validateCarName = (arr) => { - arr.forEach((carName) => { - if (carName.length > 5) { - mockFn(carName); - } - }); -}; - -validateCarName(["abcdef"]); -validateCarName(["abc", "defghi"]); +mockFn(["woowacourse"]); +mockFn(["FuBao"]); describe("조건 검증 테스트", () => { const app = new App(); - test("자동차 이름이 5자를 초과할 경우 에러 발생", () => { - expect(mockFn.mock.calls[0].length).toBeLessThanOrEqual(5); + // test("자동차 이름이 5자를 초과할 경우 에러 발생", () => { + // expect(mockFn.mock.calls[0].length).toBeGreaterThanOrEqual(6); + // }); + test("자동차 이름이 5자 이하일 경우 에러 발생하지 않음", () => { expect(mockFn.mock.calls[1].length).toBeLessThanOrEqual(5); expect(mockFn.mock.calls.length).toBe(2); }); @@ -26,4 +20,7 @@ describe("조건 검증 테스트", () => { test("시도할 횟수가 숫자가 아닐 경우 에러 발생", () => { expect(() => app.validateNumber(NaN)).toThrow(ERROR.ATTEMPT_COUNT); }); + test("시도할 횟수가 숫자인 경우 에러 발생하지 않음", () => { + expect(() => app.validateNumber(5)).not.toThrow(); + }); }); From e42bbdb0401a314c1809515b574e6c9c38ba931c Mon Sep 17 00:00:00 2001 From: "mingyeong:D" <63152086+Kim-Mingyeong@users.noreply.github.com> Date: Wed, 1 Nov 2023 23:50:20 +0900 Subject: [PATCH 17/17] =?UTF-8?q?docs:=20=EC=9E=98=EB=AA=BB=20=ED=91=9C?= =?UTF-8?q?=EA=B8=B0=ED=95=98=EA=B2=83=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index 99fd40e6d..c0e568df3 100644 --- a/docs/README.md +++ b/docs/README.md @@ -27,7 +27,7 @@ - [x] `App.js`의 `play` 메서드에서 프로그램 실행 - [x] `ApplicationTest` 의 모든 테스트 성공 - [x] indent(인덴트, 들여쓰기) depth를 3이 넘지 않도록 구현 -- [x] Jest를 이용하여 내가 정리한 기능 목록이 정상 동작함을 테스트 코드로 확인 +- [ ] Jest를 이용하여 내가 정리한 기능 목록이 정상 동작함을 테스트 코드로 확인 - [x] `@woowacourse/mission-utils`에서 제공하는 `Random` 및 `Console` API를 사용하여 구현 ## 📓 과제 진행 요구 사항