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
37 changes: 37 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# 요구사항 정리

## 기능 요구 사항

기본적으로 1부터 9까지 서로 다른 수로 이루어진 3자리의 수를 맞추는 게임이다.

같은 수가 같은 자리에 있으면 스트라이크, 다른 자리에 있으면 볼, 같은 수가 전혀 없으면 낫싱이란 힌트를 얻고, 그 힌트를 이용해서 먼저 상대방(컴퓨터)의 수를 맞추면 승리한다.
예) 상대방(컴퓨터)의 수가 425일 때
123을 제시한 경우 : 1스트라이크
456을 제시한 경우 : 1볼 1스트라이크
789를 제시한 경우 : 낫싱
위 숫자 야구 게임에서 상대방의 역할을 컴퓨터가 한다. 컴퓨터는 1에서 9까지 서로 다른 임의의 수 3개를 선택한다. 게임 플레이어는 컴퓨터가 생각하고 있는 서로 다른 3개의 숫자를 입력하고, 컴퓨터는 입력한 숫자에 대한 결과를 출력한다.
이 같은 과정을 반복해 컴퓨터가 선택한 3개의 숫자를 모두 맞히면 게임이 종료된다.
게임을 종료한 후 게임을 다시 시작하거나 완전히 종료할 수 있다.
사용자가 잘못된 값을 입력한 경우 throw문을 사용해 예외를 발생시킨후 애플리케이션은 종료되어야 한다.

### 입력

- 서로 다른 3자리의 수
- 게임이 끝난 경우 재시작/종료를 구분하는 1과 2 중 하나의 수

### 출력

- 출력
- 입력한 수에 대한 결과를 볼, 스트라이크 개수로 표시
- 1볼 1스트라이크
- 하나도 없는 경우
- 낫싱
- 3개의 숫자를 모두 맞힐 경우
- 3스트라이크
- 3개의 숫자를 모두 맞히셨습니다! 게임 종료
- 게임 시작 문구 출력
- 숫자 야구 게임을 시작합니다.

## 프로그래밍 요구 사항

## 과제 진행 요구 사항
107 changes: 106 additions & 1 deletion src/App.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,110 @@
import { Console, Random } from "@woowacourse/mission-utils";

class App {
async play() {}
#answer = [];

constructor() {
this.#init();
}

async play() {
while (true) {
const input = await Console.readLineAsync("숫자를 입력해주세요 : ");
this.#validateInput(input);
const [strikes, balls] = this.#createResponse(input);

if (strikes === 0 && balls === 0) {
Console.print("낫싱");
continue;
}

const strikesString = strikes === 0 ? '' : `${strikes}스트라이크`
const ballsString = balls === 0 ? '' : `${balls}볼 `
const response = `${ballsString}${strikesString}`;
Console.print(response);

if (strikes < 3) {
continue;
}

Console.print("3개의 숫자를 모두 맞히셨습니다! 게임 종료");
const command = await Console.readLineAsync("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.");
if (command === '1') {
this.#init();
continue;
}
return;
}
}

#init() {
this.#answer = this.#generateAnswer();
Console.print("숫자 야구 게임을 시작합니다.");
}

#validateInput(input) {
if (input.length !== 3) {
throw new Error("[ERROR] 3글자가 아닌 입력입니다.");
}
for (const char of input) {
if (char < '1' || char > '9') {
throw new Error("[ERROR] 숫자가 아닌 입력이 포함되어 있습니다.");
}
}
}

/**
* @param {string} input
*/
#createResponse(input) {
const [ a, b, c ] = this.#answer.map(Number);
const [ x, y, z ] = [ ...input ].map(Number);

let strikes = 0;
let balls = 0;

if (a === x) {
strikes++;
}
if (b === y) {
strikes++;
}
if (c === z) {
strikes++;
}

if (a === y) {
balls++;
}
if (a === z) {
balls++;
}
if (b === x) {
balls++;
}
if (b === z) {
balls++;
}
if (c === x) {
balls++;
}
if (c === y) {
balls++;
}
return [strikes, balls];
}

#generateAnswer() {
const answer = [];
while (answer.length < 3) {
const number = Random.pickNumberInRange(1, 9);
if (!answer.includes(number)) {
answer.push(number);
}
}
return answer;
}

}

export default App;