diff --git a/__tests__/ApplicationTest.js b/__tests__/ApplicationTest.js index f93b27c7a..20f106843 100644 --- a/__tests__/ApplicationTest.js +++ b/__tests__/ApplicationTest.js @@ -46,6 +46,58 @@ describe("자동차 경주 게임", () => { }); }); + test("전진-정지-여러번", async () => { + // given + const MOVING_FORWARD = 4; + const STOP = 3; + const inputs = ["pobi,woni", "4"]; + const outputs = [ + "pobi : -", + "pobi : --", + "pobi : ---", + "pobi : ----", + "pobi : -", + "pobi : --", + "pobi : ---", + "pobi : ----", + ]; + const randoms = [ + MOVING_FORWARD, + STOP, + MOVING_FORWARD, + STOP, + MOVING_FORWARD, + STOP, + MOVING_FORWARD, + STOP, + ]; + const logSpy = getLogSpy(); + + mockQuestions(inputs); + mockRandoms([...randoms]); + + // when + const app = new App(); + await app.play(); + + // then + outputs.forEach((output) => { + expect(logSpy).toHaveBeenCalledWith(expect.stringContaining(output)); + }); + }); + + test("음수 입력에 대한 예외 처리", async () => { + const inputs = ["pobi,woni", "-1"]; // 음수 값 입력 + + mockQuestions(inputs); + + // when + const app = new App(); + + // then + await expect(app.play()).rejects.toThrow("[ERROR]"); + }); + test.each([ [["pobi,javaji"]], [["pobi,eastjun"]] diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 000000000..d94e80194 --- /dev/null +++ b/docs/README.md @@ -0,0 +1,9 @@ +# 구현할 기능 목록 정리 + +1. 자동차 이름 입력받는 함수 구현 +2. 시도할 횟수 입력 받는 함수 구현 +3. 무작위 값을 생성하는 함수 구현 +4. 무작위 값을 통해 자동차 이동시키는 로직 구현 +5. 우승자 출력하는 로직 구현 +6. 잘못된 입력값에 대한 throw문 구현 +7. test 항목 작성 \ No newline at end of file diff --git a/src/App.js b/src/App.js index c38b30d5b..b66bffdcf 100644 --- a/src/App.js +++ b/src/App.js @@ -1,5 +1,17 @@ +import getCarName from './getCarName'; +import getTryCount from './getTryCount'; +import printWinner from './printWinner'; +import startGame from './startGame'; + class App { - async play() {} + async play() { + const CAR_NAMES = await getCarName(); + const TRY_COUNT = await getTryCount(); + + const GO_COUNT = await startGame(CAR_NAMES, TRY_COUNT); + + printWinner(CAR_NAMES, GO_COUNT); + } } export default App; diff --git a/src/getCarName.js b/src/getCarName.js new file mode 100644 index 000000000..1877fea7b --- /dev/null +++ b/src/getCarName.js @@ -0,0 +1,17 @@ +import { Console } from '@woowacourse/mission-utils'; + +const getCarName = async function getCarNameByUserInput() { + const CAR_NAME_INPUT = await Console.readLineAsync('경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)'); + + const CAR_NAME_ARRAY = CAR_NAME_INPUT.split(','); + + CAR_NAME_ARRAY.forEach((element) => { + if (element.length > 5) { + throw Error('[ERROR] 입력이 잘못된 형식입니다.'); + } + }); + + return CAR_NAME_ARRAY; +} + +export default getCarName; \ No newline at end of file diff --git a/src/getRandomNumber.js b/src/getRandomNumber.js new file mode 100644 index 000000000..c7f6a3aca --- /dev/null +++ b/src/getRandomNumber.js @@ -0,0 +1,9 @@ +import { Random } from '@woowacourse/mission-utils'; + +const getRandomNumber = async function getRandomNumberWithRandomUtil() { + const RANDOM_NUMBER = await Random.pickNumberInRange(0, 9); + + return RANDOM_NUMBER; +} + +export default getRandomNumber; \ No newline at end of file diff --git a/src/getTryCount.js b/src/getTryCount.js new file mode 100644 index 000000000..a5bb9fea1 --- /dev/null +++ b/src/getTryCount.js @@ -0,0 +1,16 @@ +import { Console } from '@woowacourse/mission-utils'; + +const getTryCount = async function getTryCountByUserInput() { + const TRY_COUNT = await Console.readLineAsync('시도할 횟수는 몇 회인가요?'); + + if ( + (!!isNaN(TRY_COUNT)) + || (TRY_COUNT <= 0) + ) { + throw Error('[ERROR] 숫자가 잘못된 형식입니다.'); + } + + return TRY_COUNT; +} + +export default getTryCount; \ No newline at end of file diff --git a/src/printWinner.js b/src/printWinner.js new file mode 100644 index 000000000..17cd209bf --- /dev/null +++ b/src/printWinner.js @@ -0,0 +1,13 @@ +import { Console } from '@woowacourse/mission-utils'; + +const printWinner = async function printWinnerWithResult(CAR_NAME, GO_COUNT) { + const MAX_GO_COUNT = Math.max(...GO_COUNT); + + const WINNERS = CAR_NAME.filter((_, index) => { + return GO_COUNT[index] === MAX_GO_COUNT; + }); + + Console.print(`최종 우승자 : ${WINNERS.join(', ')}`); +} + +export default printWinner; diff --git a/src/startGame.js b/src/startGame.js new file mode 100644 index 000000000..e0d89e5f6 --- /dev/null +++ b/src/startGame.js @@ -0,0 +1,26 @@ +import { Console } from '@woowacourse/mission-utils'; +import getRandomNumber from './getRandomNumber'; + +const startGame = async function startGameWithCarNameAndTryCount(CAR_NAME, TRY_COUNT) { + Console.print('실행 결과'); + + const GO_COUNT = Array.from({ length: CAR_NAME.length }, () => 0); + + while (TRY_COUNT > 0) { + TRY_COUNT -= 1; + + CAR_NAME.forEach(async (element, index) => { + const RANDOM_NUMBER = await getRandomNumber(); + + if (RANDOM_NUMBER >= 4) { + GO_COUNT[index] += 1; + } + + Console.print(`${element} : ${'-'.repeat(GO_COUNT[index])}`); + }); + } + + return GO_COUNT; +} + +export default startGame; \ No newline at end of file