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

[숫자 야구 게임] 손희진 미션 제출합니다. #858

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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,11 @@ 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) 문서를 참고한다.


[구현할 기능 목록]
- 사용자로부터 세자리 숫자 입력 받는다.
- Random.pickNumberInRange() API 를 활용해 정답값을 추출한다.
- 서로 같은 자리에 숫자가 일치하면 스트라이크 , 서로 다른 자리에 숫자가 일치하면 볼의 결과값을 갖는다. 그리고 일치하는 숫자가 없을 시 낫싱의 값을 갖는다.
- 3 스트라이크가 성립되면 게임이 종료되고, 이외의 결과값을 가질시 게임은 반복된다.
- 게임 종료 후, 숫자값을 입력받아 게임을 재 시작 할 수 있다. 이때 1을 입력하면 재시작, 2를 입력하면 종료한다.
86 changes: 85 additions & 1 deletion src/App.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,89 @@
import { MissionUtils } from "@woowacourse/mission-utils";

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

async getPersonResult(computer) {
let Input = await MissionUtils.Console.readLineAsync("숫자를 입력 해 주세요 : ");
let userInput = [...Input].map(Number);

if (userInput.length === 3) {
MissionUtils.Console.print("숫자를 입력 해 주세요 : " + userInput);
this.compareToComputer(userInput, computer);
} else {
MissionUtils.Console.print("숫자를 입력 해 주세요 : " + userInput);
throw new Error("[ERROR] 세자리 숫자를 입력 해 주세요");
}
}

getComputerResult() {
const computer = [];
while (computer.length < 3) {
let number = MissionUtils.Random.pickNumberInRange(1, 9);
if (!computer.includes(number)) {
computer.push(number);
}
}
return computer;
}

compareToComputer(person, computer) {
let strike = 0;
let ball = 0;

for (let i = 0; i < computer.length; i++) {
if (computer[i] == person[i]) {
strike++;
}
for (let j = 0; j < computer.length; j++) {
if (computer[i] == person[j] && i != j) {
ball++;
}
}
}

console.log('person :: ', person, ' c :', computer, strike, ball);

if (strike == 0 && ball == 0) {
MissionUtils.Console.print('낫싱');
this.getPersonResult(computer);
}

if (strike != 0 && ball == 0) {
MissionUtils.Console.print(strike + '스트라이크');
if (strike === 3) {
MissionUtils.Console.print('3개의 숫자를 모두 맞히셨습니다! 게임 종료');
this.reStartGame();
}
}

if (strike === 0 && ball != 0) {
MissionUtils.Console.print(ball + '볼');
this.getPersonResult(computer);
}

if (strike != 0 && ball != 0) {
MissionUtils.Console.print(ball + '볼 ' + strike + '스트라이크');
this.getPersonResult(computer);
}
}

reStartGame() {
MissionUtils.Console.readLineAsync("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.\n")
.then((input) => {
if (input === '1') {
this.play();
} else if (input === '2') {
MissionUtils.Console.print("게임 종료");
} else {
throw new Error("[ERROR] 숫자를 다시 입력하여 주십시오. 게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.");
}
})
}
}

export default App;