From b27dfdbf5ab08655ce59a10d88425fe4de6d72a5 Mon Sep 17 00:00:00 2001 From: chichi Date: Mon, 28 Oct 2024 22:33:44 +0900 Subject: [PATCH 1/9] =?UTF-8?q?docs:=20README.md=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 기능 목록 작성 --- README.md | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e078fd41f..89d538e3a 100644 --- a/README.md +++ b/README.md @@ -1 +1,35 @@ -# javascript-racingcar-precourse +### 자동차 경주 게임 + +초간단 자동차 경주 게임을 구현한다. + +### 프로그램 실행 시 + +- 프로그램 실행 시 ‘경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)’ 가 출력된다 +- 자동차 이름을 쉼표 기준으로 구분하여 입력한다 +- 각각의 자동차 이름은 5글자 이하로 입력한다 +- 올바르게 입력되었을 경우 시도할 횟수를 입력할 수 있다 +- 시도한 횟수만큼 전진 로직을 실행한다 + +### 전진 로직 + +- `Random.pickNumberInRange()`을 활용해 랜덤 값을 추출한다 +- 추출한 랜덤 값이 4 이상일 경우 전진한다 +- 전진할 경우 이전 회차에서 전진한 문자에 - 기호를 추가로 붙여준다 + +### 우승자 발표 + +- 가장 전진거리가 긴 우승자를 출력한다 +- 같은 전진거리인 우승자가 여러명일 경우 쉼표로 구분하여 출력한다 (우승자는 한 명 이상일 수 있다) + +### 에러 처리 + +- 사용자가 잘못된 값을 입력할 경우 "[ERROR]"로 시작하는 메시지와 함께 Error를 발생시킨 후 애플리케이션은 종료되어야 한다. + + - 입력 값이 비어있는 경우 "[ERROR] 입력 값이 비어 있습니다"를 출력한다. + - 구분자가 입력되지 않은 경우 "[ERROR] 구분자가 입력되지 않았습니다"를 출력한다. + - 음수 값이 입력된 경우 "[ERROR] 음수는 입력하실 수 없습니다"를 출력한다. + - 시도 횟수가 입력되지 않은 경우 "[ERROR] 시도 횟수를 입력해 주세요"를 출력한다. + - 시도 횟수 입력이 숫자가 아닌 경우 "[ERROR] 시도 횟수는 숫자만 입력 가능합니다"를 출력한다. + - 자동차 이름을 쉼표로 구분하지 않은 경우 "[ERROR] 자동차 이름을 쉼표로 구분하여 입력해 주세요"를 출력한다. + - 자동차 이름이 5자를 초과한 경우 "[ERROR] 자동차 이름은 5자 이하로 입력해 주세요"를 출력한다. + - 자동차 이름에 공백이 포함된 경우 "[ERROR] 이름에 공백 문자는 사용하실 수 없습니다"를 출력한다. From 64a9e62020012767789dd9c95198b16e4bf46745 Mon Sep 17 00:00:00 2001 From: chichi Date: Mon, 28 Oct 2024 22:36:01 +0900 Subject: [PATCH 2/9] =?UTF-8?q?refactor:=20single=20quotes=20=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 --- __tests__/ApplicationTest.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/__tests__/ApplicationTest.js b/__tests__/ApplicationTest.js index 0260e7e84..77955e6fb 100644 --- a/__tests__/ApplicationTest.js +++ b/__tests__/ApplicationTest.js @@ -1,5 +1,5 @@ -import App from "../src/App.js"; -import { MissionUtils } from "@woowacourse/mission-utils"; +import App from '../src/App.js'; +import { MissionUtils } from '@woowacourse/mission-utils'; const mockQuestions = (inputs) => { MissionUtils.Console.readLineAsync = jest.fn(); @@ -19,18 +19,18 @@ const mockRandoms = (numbers) => { }; const getLogSpy = () => { - const logSpy = jest.spyOn(MissionUtils.Console, "print"); + const logSpy = jest.spyOn(MissionUtils.Console, 'print'); logSpy.mockClear(); return logSpy; }; -describe("자동차 경주", () => { - test("기능 테스트", async () => { +describe('자동차 경주', () => { + test('기능 테스트', async () => { // given const MOVING_FORWARD = 4; const STOP = 3; - const inputs = ["pobi,woni", "1"]; - const logs = ["pobi : -", "woni : ", "최종 우승자 : pobi"]; + const inputs = ['pobi,woni', '1']; + const logs = ['pobi : -', 'woni : ', '최종 우승자 : pobi']; const logSpy = getLogSpy(); mockQuestions(inputs); @@ -46,15 +46,15 @@ describe("자동차 경주", () => { }); }); - test("예외 테스트", async () => { + test('예외 테스트', async () => { // given - const inputs = ["pobi,javaji"]; + const inputs = ['pobi,javaji']; mockQuestions(inputs); // when const app = new App(); // then - await expect(app.run()).rejects.toThrow("[ERROR]"); + await expect(app.run()).rejects.toThrow('[ERROR]'); }); }); From df314fa7f19f72784c4942683db5c409e4757540 Mon Sep 17 00:00:00 2001 From: chichi Date: Mon, 28 Oct 2024 22:51:35 +0900 Subject: [PATCH 3/9] =?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=EC=9E=85?= =?UTF-8?q?=EB=A0=A5=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 10 +++++++++- src/constants/constant.js | 3 +++ src/constants/messages.js | 5 +++++ src/controller/Controller.js | 16 ++++++++++++++++ src/view/View.js | 14 ++++++++++++++ 5 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 src/constants/constant.js create mode 100644 src/constants/messages.js create mode 100644 src/controller/Controller.js create mode 100644 src/view/View.js diff --git a/src/App.js b/src/App.js index 091aa0a5d..ba99ddeca 100644 --- a/src/App.js +++ b/src/App.js @@ -1,5 +1,13 @@ +import Controller from './controller/controller.js'; + class App { - async run() {} + constructor() { + this.controller = new Controller(); + } + + async run() { + await this.controller.execute(); + } } export default App; diff --git a/src/constants/constant.js b/src/constants/constant.js new file mode 100644 index 000000000..0423dfc61 --- /dev/null +++ b/src/constants/constant.js @@ -0,0 +1,3 @@ +export const MOVE_REQUIREMENT = 3; +export const CAR_NAME_LENGTH_LIMIT = 5; +export const DELIMITER = ','; diff --git a/src/constants/messages.js b/src/constants/messages.js new file mode 100644 index 000000000..97743c129 --- /dev/null +++ b/src/constants/messages.js @@ -0,0 +1,5 @@ +export const PROMPT_MESSAGES = Object.freeze({ + INPUT_ADDITION_CAR: + '경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)\n', + INPUT_ATTEMPTS: '시도할 횟수는 몇 회인가요?\n', +}); diff --git a/src/controller/Controller.js b/src/controller/Controller.js new file mode 100644 index 000000000..0a7b6ec9e --- /dev/null +++ b/src/controller/Controller.js @@ -0,0 +1,16 @@ +import { DELIMITER } from '../constants/constant.js'; +import { getCarNames } from '../view/view.js'; + +class Controller { + constructor() { + this.cars = []; + } + + async execute() { + const enteredCarNames = await getCarNames(); + const splittedNames = enteredCarNames.split(DELIMITER); + console.log('splittedNames', splittedNames); + } +} + +export default Controller; diff --git a/src/view/View.js b/src/view/View.js new file mode 100644 index 000000000..670afb172 --- /dev/null +++ b/src/view/View.js @@ -0,0 +1,14 @@ +import { Console } from '@woowacourse/mission-utils'; +import { PROMPT_MESSAGES } from '../constants/messages.js'; + +export const getCarNames = async () => { + const inputs = await Console.readLineAsync( + PROMPT_MESSAGES.INPUT_ADDITION_CAR + ); + return inputs; +}; + +export const getAttempts = async () => { + const attempts = await Console.readLineAsync(PROMPT_MESSAGES.INPUT_ATTEMPTS); + return attempts; +}; From ccdfd0f513b3d1efca6dee3189aeb3d309cf7da3 Mon Sep 17 00:00:00 2001 From: chichi Date: Mon, 28 Oct 2024 23:07:29 +0900 Subject: [PATCH 4/9] =?UTF-8?q?feat:=20=EC=9E=90=EB=8F=99=EC=B0=A8=20?= =?UTF-8?q?=EC=9D=B4=EB=A6=84=20=EC=84=A4=EC=A0=95=20=EC=8B=9C=20=EA=B2=80?= =?UTF-8?q?=EC=A6=9D=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/constants/messages.js | 10 ++++++++++ src/controller/Controller.js | 2 ++ src/utils/utils.js | 15 +++++++++++++++ src/utils/validator.js | 16 ++++++++++++++++ 4 files changed, 43 insertions(+) create mode 100644 src/utils/utils.js create mode 100644 src/utils/validator.js diff --git a/src/constants/messages.js b/src/constants/messages.js index 97743c129..8aa5b62fb 100644 --- a/src/constants/messages.js +++ b/src/constants/messages.js @@ -3,3 +3,13 @@ export const PROMPT_MESSAGES = Object.freeze({ '경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)\n', INPUT_ATTEMPTS: '시도할 횟수는 몇 회인가요?\n', }); + +export const ERROR_MESSAGES = Object.freeze({ + EMPTY_INPUT: '[ERROR] 입력 값이 비어 있습니다.', + MISSING_DELIMITER: '[ERROR] 구분자가 입력되지 않았습니다.', + CAR_NAME_DELIMITER_REQUIRED: + '[ERROR] 자동차 이름을 쉼표로 구분하여 입력해 주세요.', + CAR_NAME_LENGTH_EXCEEDED: '[ERROR] 자동차 이름은 5자 이하로 입력해 주세요', + CAR_NAME_CONTAINS_WHITESPACE: + '[ERROR] 이름에 공백 문자는 사용하실 수 없습니다', +}); diff --git a/src/controller/Controller.js b/src/controller/Controller.js index 0a7b6ec9e..06e082690 100644 --- a/src/controller/Controller.js +++ b/src/controller/Controller.js @@ -1,5 +1,6 @@ import { DELIMITER } from '../constants/constant.js'; import { getCarNames } from '../view/view.js'; +import { validCarName } from '../utils/validator.js'; class Controller { constructor() { @@ -10,6 +11,7 @@ class Controller { const enteredCarNames = await getCarNames(); const splittedNames = enteredCarNames.split(DELIMITER); console.log('splittedNames', splittedNames); + validCarName(splittedNames); } } diff --git a/src/utils/utils.js b/src/utils/utils.js new file mode 100644 index 000000000..9d653685b --- /dev/null +++ b/src/utils/utils.js @@ -0,0 +1,15 @@ +import { CAR_NAME_LENGTH_LIMIT } from '../constants/constant.js'; + +export const utils = { + hasValidCharacter(name) { + const regex = /^[\p{L},\s]*$/u; + return regex.test(name); + }, + isNameLengthLimit(name) { + return name.length <= CAR_NAME_LENGTH_LIMIT; + }, + hasWhitespace(name) { + const regex = /\s/; + return regex.test(name); + }, +}; diff --git a/src/utils/validator.js b/src/utils/validator.js new file mode 100644 index 000000000..6fdd10fe7 --- /dev/null +++ b/src/utils/validator.js @@ -0,0 +1,16 @@ +import { utils } from './utils.js'; +import { ERROR_MESSAGES } from '../constants/messages.js'; + +export const validCarName = (splittedNames) => { + splittedNames.forEach((car) => { + if (!utils.hasValidCharacter(car)) { + throw new Error(ERROR_MESSAGES.CAR_NAME_DELIMITER_REQUIRED); + } + if (!utils.isNameLengthLimit(car)) { + throw new Error(ERROR_MESSAGES.CAR_NAME_LENGTH_EXCEEDED); + } + if (utils.hasWhitespace(car)) { + throw new Error(ERROR_MESSAGES.CAR_NAME_CONTAINS_WHITESPACE); + } + }); +}; From 7fc0d70e92e085b67ddb0f361162c30912249574 Mon Sep 17 00:00:00 2001 From: chichi Date: Mon, 28 Oct 2024 23:12:43 +0900 Subject: [PATCH 5/9] =?UTF-8?q?feat:=20=EC=8B=9C=EB=8F=84=ED=95=A0=20?= =?UTF-8?q?=ED=9A=9F=EC=88=98=EB=A5=BC=20=EC=9E=85=EB=A0=A5=ED=95=A0=20?= =?UTF-8?q?=EC=88=98=20=EC=9E=88=EB=8F=84=EB=A1=9D=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controller/Controller.js | 10 ++++++++-- src/model/Model.js | 8 ++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 src/model/Model.js diff --git a/src/controller/Controller.js b/src/controller/Controller.js index 06e082690..4d6a92884 100644 --- a/src/controller/Controller.js +++ b/src/controller/Controller.js @@ -1,5 +1,6 @@ +import Car from '../model/model.js'; import { DELIMITER } from '../constants/constant.js'; -import { getCarNames } from '../view/view.js'; +import { getAttempts, getCarNames } from '../view/view.js'; import { validCarName } from '../utils/validator.js'; class Controller { @@ -10,8 +11,13 @@ class Controller { async execute() { const enteredCarNames = await getCarNames(); const splittedNames = enteredCarNames.split(DELIMITER); - console.log('splittedNames', splittedNames); validCarName(splittedNames); + this.cars = this.createCars(splittedNames); + const attempts = await getAttempts(); + } + + createCars(splittedNames) { + return splittedNames.map((name) => new Car(name)); } } diff --git a/src/model/Model.js b/src/model/Model.js new file mode 100644 index 000000000..bfaf9d92c --- /dev/null +++ b/src/model/Model.js @@ -0,0 +1,8 @@ +class Car { + constructor(name) { + this.name = name; + this.distance = ''; + } +} + +export default Car; From 5ed42878fc016ffdf13c9ba880b66090809bdcc5 Mon Sep 17 00:00:00 2001 From: chichi Date: Mon, 28 Oct 2024 23:18:04 +0900 Subject: [PATCH 6/9] =?UTF-8?q?feat:=20=EA=B2=8C=EC=9E=84=20=EC=8B=9C?= =?UTF-8?q?=EB=8F=84=20=ED=9A=9F=EC=88=98=EC=9D=98=20=EC=97=90=EB=9F=AC=20?= =?UTF-8?q?=ED=95=B8=EB=93=A4=EB=A7=81=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/constants/messages.js | 3 +++ src/controller/Controller.js | 3 ++- src/utils/utils.js | 6 ++++++ src/utils/validator.js | 12 ++++++++++++ 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/constants/messages.js b/src/constants/messages.js index 8aa5b62fb..f1f493c8c 100644 --- a/src/constants/messages.js +++ b/src/constants/messages.js @@ -7,6 +7,9 @@ export const PROMPT_MESSAGES = Object.freeze({ export const ERROR_MESSAGES = Object.freeze({ EMPTY_INPUT: '[ERROR] 입력 값이 비어 있습니다.', MISSING_DELIMITER: '[ERROR] 구분자가 입력되지 않았습니다.', + NEGATIVE_NUMBER: '[ERROR] 음수는 입력하실 수 없습니다.', + EMPTY_ATTEMPTS: '[ERROR] 시도 횟수를 입력해 주세요', + INVALID_ATTEMPTS: '[ERROR] 시도 횟수는 숫자만 입력 가능합니다', CAR_NAME_DELIMITER_REQUIRED: '[ERROR] 자동차 이름을 쉼표로 구분하여 입력해 주세요.', CAR_NAME_LENGTH_EXCEEDED: '[ERROR] 자동차 이름은 5자 이하로 입력해 주세요', diff --git a/src/controller/Controller.js b/src/controller/Controller.js index 4d6a92884..8eec38c31 100644 --- a/src/controller/Controller.js +++ b/src/controller/Controller.js @@ -1,7 +1,7 @@ import Car from '../model/model.js'; import { DELIMITER } from '../constants/constant.js'; import { getAttempts, getCarNames } from '../view/view.js'; -import { validCarName } from '../utils/validator.js'; +import { validateAttempts, validCarName } from '../utils/validator.js'; class Controller { constructor() { @@ -14,6 +14,7 @@ class Controller { validCarName(splittedNames); this.cars = this.createCars(splittedNames); const attempts = await getAttempts(); + validateAttempts(attempts); } createCars(splittedNames) { diff --git a/src/utils/utils.js b/src/utils/utils.js index 9d653685b..cd13e17af 100644 --- a/src/utils/utils.js +++ b/src/utils/utils.js @@ -12,4 +12,10 @@ export const utils = { const regex = /\s/; return regex.test(name); }, + isNumberType(number) { + return !isNaN(number); + }, + isNegativeNumber(number) { + return number < 0; + }, }; diff --git a/src/utils/validator.js b/src/utils/validator.js index 6fdd10fe7..14a879c99 100644 --- a/src/utils/validator.js +++ b/src/utils/validator.js @@ -14,3 +14,15 @@ export const validCarName = (splittedNames) => { } }); }; + +export const validateAttempts = (attempts) => { + if (!attempts) { + throw new Error(ERROR_MESSAGES.EMPTY_ATTEMPTS); + } + if (!utils.isNumberType(attempts)) { + throw new Error(ERROR_MESSAGES.INVALID_ATTEMPTS); + } + if (utils.isNegativeNumber(attempts)) { + throw new Error(ERROR_MESSAGES.NEGATIVE_NUMBER); + } +}; From 37415f8c1b9873079ad66a7cdffaf57e64fb93c1 Mon Sep 17 00:00:00 2001 From: chichi Date: Mon, 28 Oct 2024 23:23:25 +0900 Subject: [PATCH 7/9] =?UTF-8?q?feat:=20=EC=9E=90=EB=8F=99=EC=B0=A8=20?= =?UTF-8?q?=EC=A0=84=EC=A7=84=20=EB=A1=9C=EC=A7=81=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Random.pickNumberInRange()을 활용해 랜덤 값을 추출 * 추출한 랜덤 값이 4 이상일 경우 전진한다 * 전진할 경우 이전 회차에서 전진한 문자에 - 기호를 추가로 붙여준다 * 시도한 횟수만큼 전진 로직을 실행한다 * 전진 결과를 콘솔로 출력한다 --- src/controller/Controller.js | 16 +++++++++++++++- src/model/Model.js | 8 ++++++++ src/view/View.js | 7 +++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/controller/Controller.js b/src/controller/Controller.js index 8eec38c31..2413d9b8a 100644 --- a/src/controller/Controller.js +++ b/src/controller/Controller.js @@ -1,6 +1,7 @@ import Car from '../model/model.js'; import { DELIMITER } from '../constants/constant.js'; -import { getAttempts, getCarNames } from '../view/view.js'; +import { Random } from '@woowacourse/mission-utils'; +import { getAttempts, getCarNames, displayRaceProgress } from '../view/view.js'; import { validateAttempts, validCarName } from '../utils/validator.js'; class Controller { @@ -15,11 +16,24 @@ class Controller { this.cars = this.createCars(splittedNames); const attempts = await getAttempts(); validateAttempts(attempts); + + for (let attemptIndex = 0; attemptIndex < attempts; attemptIndex++) { + this.startCarRace(this.cars); + displayRaceProgress(this.cars); + } } createCars(splittedNames) { return splittedNames.map((name) => new Car(name)); } + + startCarRace(cars) { + cars.forEach((car) => { + const number = Random.pickNumberInRange(0, 9); + car.race(number); + }); + return cars; + } } export default Controller; diff --git a/src/model/Model.js b/src/model/Model.js index bfaf9d92c..8e7ed1b76 100644 --- a/src/model/Model.js +++ b/src/model/Model.js @@ -1,8 +1,16 @@ +import { MOVE_REQUIREMENT } from '../constants/constant.js'; + class Car { constructor(name) { this.name = name; this.distance = ''; } + + race(pickNumber) { + if (pickNumber > MOVE_REQUIREMENT) { + this.distance = this.distance + '-'; + } + } } export default Car; diff --git a/src/view/View.js b/src/view/View.js index 670afb172..45d7009b2 100644 --- a/src/view/View.js +++ b/src/view/View.js @@ -12,3 +12,10 @@ export const getAttempts = async () => { const attempts = await Console.readLineAsync(PROMPT_MESSAGES.INPUT_ATTEMPTS); return attempts; }; + +export const displayRaceProgress = (cars) => { + cars.forEach((car) => { + Console.print(`${car.name} : ${car.distance}`); + }); + Console.print(`\n`); +}; From 0869e04c24dea966ee3b7fbf3a87dfb5d323ae70 Mon Sep 17 00:00:00 2001 From: chichi Date: Mon, 28 Oct 2024 23:24:32 +0900 Subject: [PATCH 8/9] =?UTF-8?q?docs:=20README=20=EC=88=98=EC=A0=95=20(?= =?UTF-8?q?=EC=A0=84=EC=A7=84=20=EB=A1=9C=EC=A7=81=20=EC=82=AC=ED=95=AD=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 89d538e3a..a33661bf1 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ - `Random.pickNumberInRange()`을 활용해 랜덤 값을 추출한다 - 추출한 랜덤 값이 4 이상일 경우 전진한다 - 전진할 경우 이전 회차에서 전진한 문자에 - 기호를 추가로 붙여준다 +- 매 횟수마다 전진 결과를 콘솔로 출력한다 ### 우승자 발표 From ea651faf8ad7baf49d773319ae9ed2266a5fbd8a Mon Sep 17 00:00:00 2001 From: chichi Date: Mon, 28 Oct 2024 23:29:14 +0900 Subject: [PATCH 9/9] =?UTF-8?q?feat:=20=EC=9A=B0=EC=8A=B9=EC=9E=90=20?= =?UTF-8?q?=EB=B0=9C=ED=91=9C=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 가장 전진거리가 긴 우승자를 출력 * 같은 전진거리인 우승자가 여러명일 경우 쉼표로 구분하여 출력 --- src/controller/Controller.js | 18 +++++++++++++++++- src/view/View.js | 4 ++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/controller/Controller.js b/src/controller/Controller.js index 2413d9b8a..063f5a9ae 100644 --- a/src/controller/Controller.js +++ b/src/controller/Controller.js @@ -1,7 +1,12 @@ import Car from '../model/model.js'; import { DELIMITER } from '../constants/constant.js'; import { Random } from '@woowacourse/mission-utils'; -import { getAttempts, getCarNames, displayRaceProgress } from '../view/view.js'; +import { + getAttempts, + getCarNames, + announceWinners, + displayRaceProgress, +} from '../view/view.js'; import { validateAttempts, validCarName } from '../utils/validator.js'; class Controller { @@ -21,6 +26,9 @@ class Controller { this.startCarRace(this.cars); displayRaceProgress(this.cars); } + + const result = this.getWinners(this.cars); + announceWinners(result.join(',')); } createCars(splittedNames) { @@ -34,6 +42,14 @@ class Controller { }); return cars; } + + getWinners(cars) { + const maxScore = Math.max(...cars.map((car) => car.distance.length)); + + return cars + .filter((car) => car.distance.length === maxScore) + .map((car) => car.name); + } } export default Controller; diff --git a/src/view/View.js b/src/view/View.js index 45d7009b2..e690a418b 100644 --- a/src/view/View.js +++ b/src/view/View.js @@ -13,6 +13,10 @@ export const getAttempts = async () => { return attempts; }; +export const announceWinners = (winner) => { + Console.print(`최종 우승자 : ${winner}`); +}; + export const displayRaceProgress = (cars) => { cars.forEach((car) => { Console.print(`${car.name} : ${car.distance}`);