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
Binary file added .DS_Store
Binary file not shown.
25 changes: 25 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# ⚾️ 숫자 야구게임 ⚾️

## ✔︎ 기능 목록
* [E] : 예외 처리 -> throw 문 사용

ex) [ERROR] 숫자가 잘못된 형식입니다.

### (1) 🖥️ 컴퓨터의 숫자 선택
1 ~ 9 까지 서로 다른 임의의 수 3개 선택

### (2) 🙋 플레이어의 숫자 입력
1 ~ 9까지 서로 다른 임의의 수 3개 입력
[E] 3개 미만 or 초과 숫자 입력 시 -> 앱 종료
[E] 동일한 숫자가 있을 시 -> 앱 종료

### (3) 📝 결과 출력
(i) 같은 자리 & 같은 숫자 -> 🔴스트라이크++
(ii) 다른 자리 & 같은 숫자 -> 🟢볼++
(iii) 🔴스트라이크=0 & 🟢볼=0 -> "낫싱"

### (4) 📝 게임 결과
(3)에서 🔴스트라이크=3 이면
(i) 게임 새로 시작 -> 1 입력
(ii) 게임 종료 -> 2 입력
[E] 그 외 숫자 입력 시 -> 앱 종료
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"@babel/core": "^7.23.0",
"@babel/preset-env": "^7.22.20",
"babel-jest": "^29.7.0",
"jest": "29.6.0"
"jest": "^29.6.0"
},
"scripts": {
"test": "jest"
Expand Down
105 changes: 102 additions & 3 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,104 @@
const MissionUtils = require('@woowacourse/mission-utils');

const NUM_LENGTH = 3;

class App {
async play() {}
}
async play() {
MissionUtils.Console.print('숫자 야구 게임을 시작합니다.');
let playGame = true;
while(playGame) {
const computerNum = this.computerPickNum();
await this.startGame(computerNum);
playGame = await this.endGame();
}
}

//(1)컴퓨터 랜덤 숫자 선택
computerPickNum() {
const computer = [];
while (computer.length < NUM_LENGTH) {
const number = MissionUtils.Random.pickNumberInRange(1, 9);
if (!computer.includes(number))
computer.push(number);
}
return computer;
}

//(2)플레이어 숫자 입력
async startGame(computerNum) {
while(true) {
const input = await MissionUtils.Console.readLineAsync('숫자를 입력해주세요 : ');
const userNum = input.split('').map(Number);

this.checkInput(input);

let strike = this.countStrike(computerNum, userNum);
let ball = this.countBall(computerNum, userNum);

const result = this.printResult(ball, strike);
MissionUtils.Console.print(result);

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

}

async checkInput(input) {
const userInput = new Set(input.split('').map(Number));

export default App;
if (input.length !== NUM_LENGTH)
throw Error('[ERROR] 숫자가 잘못된 형식입니다. 3개의 숫자만 입력 가능합니다.');
if ([...userInput].length !== NUM_LENGTH)
throw Error('[ERROR] 숫자가 잘못된 형식입니다. 중복되지 않는 숫자만 입력 가능합니다.');
if (Number.isNaN(input) || input.includes(' '))
throw Error('[ERROR] 숫자가 잘못된 형식입니다. 숫자만 입력 가능합니다.');
}

//(3)결과 출력
countStrike(computerNum, userNum) {
let strike = 0;

for (let i = 0; i < NUM_LENGTH; i++)
if (computerNum[i] === Number(userNum[i]))
strike += 1;

return strike;
}

countBall(computerNum, userNum) {
let ball = 0;

for (let i = 0; i < NUM_LENGTH; i++)
if (computerNum[i] !== Number(userNum[i]) && computerNum.includes(Number(userNum[i])))
ball += 1;

return ball;
}

printResult(ball, strike) {
if (ball !== 0 && strike === 0)
return `${ball}볼`;
if (ball !== 0 && strike !== 0)
return `${ball}볼 ${strike}스트라이크`;
if (ball === 0 && strike !== 0)
return `${strike}스트라이크`;
return '낫싱';
}

//(4)게임 결과
async endGame() {
const askReplay = await MissionUtils.Console.readLineAsync('게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.\n');

if(askReplay === '1')
return true;
else if(askReplay === '2')
return false;
else
throw new Error('[ERROR] 잘못된 형식입니다. 1 또는 2만 입력 가능합니다.');
}

}
export default App;