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
38 changes: 38 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# 숫자 야구

- Front-End/JavaScript

## 구현 기능 목록

---

### 게임 시작

- 게임 시작 메시지 출력
- 컴퓨터(상대)의 랜덤 숫자 생성하기

### 숫자 판단 및 결과 메시지 출력

- 플레이어에게 숫자 입력 받기
- 해당 숫자가 정상적인 값인지 검사
- 정상적이지 않다면 예외 처리
- 정상적이라면 게임 결과 판단
- 맞힌 경우, 숫자 입력 받기
- 1이면 재시작, 2면 종료

### 예외 처리

- 게임 재시작 및 종료
- 사용자 입력이 숫자가 아닌 경우
- 사용자 입력이 1 또는 2가 아닌 경우
- 게임 중
- 사용자 입력이 숫자가 아닌 경우
- 사용자 입력이 다 다른 3자리가 아닌 경우
- 사용자 입력 중 숫자가 1 ~ 9가 아닌 경우

### 그렇다면 필요한 함수는?

- 입력값 정상 여부 검사 함수
- 입력값 라운드 결과 판단 함수
- 재시작하는 함수?
- 종료시키는 함수?
116 changes: 115 additions & 1 deletion src/App.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,119 @@
import { MissionUtils } from "@woowacourse/mission-utils";

class App {
async play() {}
async play() {
// 게임 시작 문구
MissionUtils.Console.print("숫자 야구 게임을 시작합니다.");
const computer = getComputerNum();
await this.startRound(computer);
}

async startRound(computer) {
while (true) {
// 사용자 입력 받기
let input = await MissionUtils.Console.readLineAsync(
"숫자를 입력해주세요 : "
);

input = input.split("").map(Number);

if (input === null) {
throw new Error("[ERROR] 입력은 숫자만 가능합니다.");
}

if (input.includes(NaN)) {
throw new Error("[ERROR] 입력은 숫자만 가능합니다.");
}

if (input.length !== 3) {
throw new Error("[ERROR] 3자리의 숫자를 입력해주세요.");
}

if (input.includes("0")) {
throw new Error("[ERROR] 1이상 9이하 숫자로 구성해주세요.");
}

const inputSet = new Set(input);

if (inputSet.size < 3) {
throw new Error("[ERROR] 중복되지 않는 숫자로 구성해주세요.");
}

const gameResult = await getGameResult(computer, input);
const ballCnt = gameResult[0];
const strikeCnt = gameResult[1];
let resultStr = "";

if (ballCnt > 0) {
resultStr += `${ballCnt}볼 `;
}

if (strikeCnt > 0) {
resultStr += `${strikeCnt}스트라이크`;
}

if (ballCnt === 0 && strikeCnt === 0) {
resultStr += "낫싱";
}

MissionUtils.Console.print(resultStr);

if (strikeCnt === 3) {
MissionUtils.Console.print("3개의 숫자를 모두 맞히셨습니다! 게임 종료");
break;
}
}
this.restartRound();
}

async restartRound() {
const isAgainInput = await MissionUtils.Console.readLineAsync(
"게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.\n"
);

if (isAgainInput === "1") {
await this.play();
} else {
if (isAgainInput !== "2") {
throw new Error("[ERROR] 1 또는 2를 입력하세요.");
}
}
}
}

function getComputerNum() {
// 1. 1 ~ 9 사이어야 함.
// 2. 서로 다른 3자리의 수여야 함.
const computerNum = [];
while (computerNum.length < 3) {
const randomNumber = MissionUtils.Random.pickNumberInRange(1, 9);
if (!computerNum.includes(randomNumber)) {
computerNum.push(randomNumber);
}
}
return computerNum;
}

async function getGameResult(arrComputer, arrPlayer) {
const strike = [];
let ball = 0;

// 스트라이크 먼저 검사
for (let idx = 0; idx < 3; idx++) {
if (arrPlayer[idx] === arrComputer[idx]) {
strike.push(idx);
}

if (!strike.includes(idx) && arrComputer.includes(arrPlayer[idx])) {
ball += 1;
}
}

const result = [ball, strike.length];
return result;
}

const app = new App();
app.play();

export default App;