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

[자동차 경주] 이채영 미션 제출합니다. #456

Open
wants to merge 6 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
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
# javascript-racingcar-precourse
# 2주차 - 자동차 경주
## 구현할 기능 목록

- [x] 시작 메세지 출력
- [x] 자동차 이름 입력 받고 처리
- [x] 이동 입력 받기
- [ ] 주어진 조건에 맞추어 전진하기
- [ ] 우승자 출력하기(한 명, 여러 명)
- [ ] error 처리
- [x] 사용자 이름이 5자 이상인 경우
- [x] 시도할 횟수로 양수가 들어오지 않은 경우
- [x] 입력이 올바른 형태가 아닌 경우
35 changes: 34 additions & 1 deletion src/App.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
import { MissionUtils } from "@woowacourse/mission-utils";
import splitInput from "./splitInput.js";
import validNumber from "./validNumber.js";

class App {
async run() {}
async run() {
// 1. 자동차 이름 입력 처리
const INPUT = await MissionUtils.Console.readLineAsync(
"경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)\n"
);
const CARNAMES = splitInput(INPUT);

// 2. 이동 거리 처리
const NUMINPUT = await MissionUtils.Console.readLineAsync(
"시도할 횟수는 몇 회인가요?\n"
);
const TIMES = validNumber(NUMINPUT);

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

// 3. 경기 진행
var player1 = 0;
var player2 = 0;
var player3 = 0;
var i=0;
while (i<TIMES) {
player1=MissionUtils.Random.pickNumberInRange(0, 9);
player2=MissionUtils.Random.pickNumberInRange(0, 9);
player3=MissionUtils.Random.pickNumberInRange(0, 9);
if(playGame(player1, player2, player3)) {
MissionUtils.Console.print("");
}
i++
}
}
}

export default App;
7 changes: 7 additions & 0 deletions src/playGame.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { MissionUtils } from "@woowacourse/mission-utils";
function playGame(p1, p2, p3) {
if (p1>3 && p2>3 && p3>3) {
return true;
}
return false;
}
18 changes: 18 additions & 0 deletions src/splitInput.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { MissionUtils } from "@woowacourse/mission-utils";
import validInput from "./validInput.js"

function splitInput(targetInput) {
const CARNAMES = targetInput.split(",");
try {
const CARNAMES = targetInput.split(",");
if (validInput(CARNAMES)) {
return CARNAMES;
} else {
throw new Error("[ERROR] 올바른 입력 형식 ");
}
} catch {
MissionUtils.Console.print("[ERROR] 올바른 입력 형식이 아닙니다.");
}
}

export default splitInput;
12 changes: 12 additions & 0 deletions src/validInput.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { MissionUtils } from "@woowacourse/mission-utils";
function validInput (cars) {
for(let i=0;i<cars.length;i++){
if (cars[i].length>5) {
// MissionUtils.Console.print(cars[i]);
return false
}
}
return true
};

export default validInput;
14 changes: 14 additions & 0 deletions src/validNumber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { MissionUtils } from "@woowacourse/mission-utils";

function validNumber(num) {
const parsedNum = parseInt(num);

if (isNaN(parsedNum) || parsedNum <= 0) {
MissionUtils.Console.print("[ERROR] 양수를 입력해주세요.");
}

return parsedNum;
}

export default validNumber;