Skip to content
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
30 changes: 30 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
### 자동차 이름 입력

1. 게임 시작 메세지를 출력

2. 경주할 자동차 이름을 입력
2.1 이름은 쉼표(,) 기준으로 구분
2.2 이름은 5자 이하로 입력

### 게임 시도 횟수 입력

3. 시도할 횟수를 묻는 메세지 출력

4. 횟수를 입력
4.1 숫자가 아닌 값은 에러 처리

### 레이싱 게임 실행

5. 각각의 자동차마다 무작위 값을 받는다.
5.1 무작위 값이 4이상일 경우에만 전진

6. 입력된 횟수만큼 반복

### 실행 결과

7. 각각의 자동차가 전진한 실행 결과를 출력

### 최종 우승자

8. 레이싱 게임을 완료후에 최종 우승자출력
8.1 중복된 우승자가 있을 수 있습니다.
120 changes: 119 additions & 1 deletion src/App.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,123 @@
import { MissionUtils } from "@woowacourse/mission-utils";
import { Console } from "@woowacourse/mission-utils";

class Car {

constructor(name) {
this.name = name;
this.count = 0;
}

plusCount() {
this.count = this.count + 1;
}
}

class App {
async play() {}

async play() {
const ObjectList = await this.getCarName();
const NUMBER = await this.userTryNumber();

Console.print('\n실행 결과');

for (let i =0; i < NUMBER; i++){
this.getRandomNumber(ObjectList);
}

Console.print(this.finalWinner(ObjectList));

return;
}

async getCarName() {

let carName = await Console.readLineAsync('경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)');

carName = carName.trim().split(',');
carName.forEach(element => {
if(element.includes(" ") || !isNaN(element)) {
throw new Error("[ERROR]");
}
});

const OBJECT_LIST = [];
carName.forEach((element, index) => {
OBJECT_LIST[index] = new Car(element);
});


return OBJECT_LIST;
}

async userTryNumber() {

let answer = await Console.readLineAsync('시도할 횟수는 몇 회인가요?');
if (isNaN(answer)) throw new Error("[ERROR]");

answer = Number(answer);

return answer;
}

getRandomNumber(ObjectList) {

for (let j = 0; j < ObjectList.length; j++) {
let random = MissionUtils.Random.pickNumberInRange(0, 9);

if (random >= 4) {
ObjectList[j].plusCount();
}
}
this.printCar(ObjectList);

return;
}

printCar(ObjectList) {

for(let i=0; i<ObjectList.length; i++){
Console.print(`${ObjectList[i].name} : ${"-".repeat(ObjectList[i].count)}` );
}
Console.print(' ');

return;
}

finalWinner(ObjectList) {

let rank = Array.from({length: ObjectList.length}, () => 1);
let winner = '최종 우승자 : ';

for(let i = 0; i< ObjectList.length; i++){
rank = this.getRank(ObjectList[i], rank);
}

for(let i = 0, flag = 0; i < ObjectList.length; i++) {
if(rank[i] === 1 && flag === 0) {
flag = 1;
winner += ObjectList[i].name;
continue;
}
if(rank[i] === 1 && flag === 1) {
winner += ', ';
winner += ObjectList[i].name;
}
}

return winner;
}
getRank(ObjectList, rank) {

for(let j = 0; j< ObjectList.length; j++){
if(ObjectList.count < ObjectList[j].count) {
rank++;
}
}

return rank;
}
}


export default App;
17 changes: 17 additions & 0 deletions src/myTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import App from "../src/App.js";

describe("App 테스트", () => {
describe("getCarName 테스트", () => {
test("받은 데이터 객체의 배열로 변경", () => {
const result = App.getCarName("lee,yun,lim");

expect(result).toEqual(
[
Car { name: 'lee', count: 0 },
Car { name: 'yun', count: 0 },
Car { name: 'lim', count: 0 }
]
);
});
});
});