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

[숫자 야구 게임] 김명빈 미션 제출합니다. #849

Open
wants to merge 2 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
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,33 @@ while (computer.length < 3) {
- 미션은 [javascript-baseball](https://github.com/woowacourse-precourse/javascript-baseball-6/) 저장소를 Fork & Clone해 시작한다.
- **기능을 구현하기 전 `docs/README.md`에 구현할 기능 목록을 정리**해 추가한다.
- 과제 진행 및 제출 방법은 [프리코스 과제 제출](https://github.com/woowacourse/woowacourse-docs/tree/master/precourse) 문서를 참고한다.

# 구현 기능 목록
<랜덤 숫자 생성 (컴퓨터 역할)>
- 오직 3자리 숫자
- 1~9 사이
- 서로 다름, 중복 X

<입력 (사용자 역할)>
- 오직 3자리 숫자
- 1~9 사이
- 서로 다름, 중복 X

<결과 확인>
- 컴퓨터와 사용자의 수 비교 후 힌트 출력
- 같은 수가 같은 자리 -> 스트라이크
- 같은 수가 다른 자리 -> 볼
- 같은 수 전혀 없음 -> 낫싱

- 볼, 스트라이크 동시 발생 -> 볼, 스트라이크 순서로 출력
- 낫싱 발생 -> 입력 (사용자 역할) 반복

<진행>
- 3개의 숫자 모두 맞히면 게임 종료
- 게임 종료 후 다시 시작 or 완전 종료 선택

<예외>
- 사용자가 잘못된 값 입력 시 예외 발생시켜 앱 종료
- 숫자가 아님
- 중복된 숫자가 있음
- 3자리 이상 숫자임
60 changes: 58 additions & 2 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,61 @@
import { MissionUtils } from "@woowacourse/mission-utils";

class App {
async play() {}
async play() {
let choice = '1';
MissionUtils.Console.print('숫자 야구 게임을 시작합니다.');

const com = [];
while (com.length < 3) {
const num = MissionUtils.Random.pickNumberInRange(1, 9);
if (!com.includes(num)) {
com.push(num);
}
}

while (choice === '1') {
let player;
try {
player = await MissionUtils.Console.readLineAsync('숫자를 입력해 주세요 : ');

if (!/^[0-9]{3}$/.test(player)) {
throw new Error('[ERROR] 숫자가 아닙니다.');
}

player = player.split('').map(Number);
if (player.length !== 3 || new Set(player).size !== 3) {
throw new Error('[ERROR] 입력한 숫자가 올바르지 않습니다.');
}

let sCnt = 0,
bCnt = 0;
for (let i = 0; i < 3; i++) {
if (com[i] == player[i]) {
sCnt++;
} else if (com.includes(player[i])) {
bCnt++;
}
}

let result = '';
if (bCnt > 0) result += bCnt + '볼 ';
if (sCnt > 0) result += sCnt + '스트라이크';
if (bCnt === 0 && sCnt === 0) result += '낫싱';

MissionUtils.Console.print(result);

if (sCnt === 3) {
MissionUtils.Console.print(sCnt + '개의 숫자를 모두 맞히셨습니다! 게임 종료');
MissionUtils.Console.print('게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.');
choice = await MissionUtils.Console.readLineAsync();
} else if (choice === '2') {
break;
}
} catch (error) {
throw error;
}
}
}
}

export default App;
export default App;