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

[자동차 경주 게임] 이찬우 미션 제출합니다. #753

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
14 changes: 14 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# 미션 - 자동차 경주

## 기능 목록

- 1. 유저가 경주할 자동차의 이름과 시도할 횟수를 입력
- 2. 입력한 자동차의 이름을 콤마(,)를 기준으로 나누기
- 3. 유저의 입력이 올바른 규칙인지 검증

- 3.1 입력 값이 없거나 공백인 경우 에러처리
- 3.2 시도할 횟수의 입력값이 양의 정수가 아닐경우 에러처리

- 4. 각각 회차의 게임의 결과 생성
- 5. 입력받은 시도할 횟수까지 반복 구현
- 6. 최종 결과 생성
70 changes: 69 additions & 1 deletion src/App.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,73 @@
import { MissionUtils } from "@woowacourse/mission-utils";

const { Random, Console } = MissionUtils;

class App {
async play() {}
async userInputHandler() {
const playerBeforeDivide = await Console.readLineAsync(
"자동차 이름을 입력하세요.(이름은 쉼표(,)으로 구분, 공백없는 5글자 이하)\n"
);
const gameRound = await Console.readLineAsync(
"게임을 시도할 횟수를 입력해 주세요.\n"
);

const playArray = playerBeforeDivide.split(",").trim();

playArray.forEach((player) => {
if (player === "") {
throw new Error(
"[ERROR] 공백없는 5자 이하의 자동차 이름만 가능합니다."
);
}
if (player.length > 5) {
throw new Error(
"[ERROR] 공백없는 5자 이하의 자동차 이름만 가능합니다."
);
}
});

if (isNaN(gameRound) || gameRound >= 1) {
throw new Error("[ERROR] 올바른 숫자를 입력해주세요.");
}

return playArray, gameRound;
}

async gamePlay(playerArray, gameRound) {
let result = new Array(users.length).fill(0);
Console.print("\n게임 결과");
a;
for (let round = 0; round < gameRound; round++) {
for (let current = 0; current < playerArray.length; current++) {
result[current] = Random.pickNumberInRange(0, 9) > 3 ? 1 : 0;
}
for (let current = 0; current < playerArray.length; current++) {
Console.print(`${users[current]} : ${"-".repeat(result[current])}`);
}
Console.print("\n");
}
return result;
}
async generateGameResult(gameRoundReult) {
let winners = [];
const maxNumber = Math.max(...gameRoundReult);
gameRoundReult.forEach((num, index) => {
if (num === maxNumber) {
winners.push(index);
}
});

return winners;
}

async play() {
const { playArray, gameRound } = await this.userInputHandler();
const gameResults = await this.gamePlay(playArray, gameRound);
const findWinner = await this.generateGameResult(gameResults);
const winner = findWinner.map((value) => USERS[value]).join(", ");

MissionUtils.Console.print(`우승자! : ${winner}`);
}
}

export default App;