Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[자동차 경주] 전언석 미션 제출합니다. #757

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions __tests__/ApplicationTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"]]
Expand Down
9 changes: 9 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# 구현할 기능 목록 정리

1. 자동차 이름 입력받는 함수 구현
2. 시도할 횟수 입력 받는 함수 구현
3. 무작위 값을 생성하는 함수 구현
4. 무작위 값을 통해 자동차 이동시키는 로직 구현
5. 우승자 출력하는 로직 구현
6. 잘못된 입력값에 대한 throw문 구현
7. test 항목 작성
14 changes: 13 additions & 1 deletion src/App.js
Original file line number Diff line number Diff line change
@@ -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;
17 changes: 17 additions & 0 deletions src/getCarName.js
Original file line number Diff line number Diff line change
@@ -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;
9 changes: 9 additions & 0 deletions src/getRandomNumber.js
Original file line number Diff line number Diff line change
@@ -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;
16 changes: 16 additions & 0 deletions src/getTryCount.js
Original file line number Diff line number Diff line change
@@ -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;
13 changes: 13 additions & 0 deletions src/printWinner.js
Original file line number Diff line number Diff line change
@@ -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;
26 changes: 26 additions & 0 deletions src/startGame.js
Original file line number Diff line number Diff line change
@@ -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;