-
Notifications
You must be signed in to change notification settings - Fork 798
[숫자 야구 게임] 한보은 미션 제출합니다. #852
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
base: main
Are you sure you want to change the base?
Changes from all commits
d831b0b
0f94e34
f17380f
12f237b
29736f2
d7353c3
2890ef2
0fc67db
e6d07f5
aab592a
f898cb5
cd10f7d
ce33efd
c9ba2be
d4aac0f
ff32a2e
cd951be
bd94bdb
9e171e7
41e3cc8
f39bd22
6ab159f
d0f3cbd
20bb592
c0d686b
f15170f
1df1359
f3884ec
5ae3f53
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| - ✅랜덤 숫자 세자리 지정하기 | ||
| - ✅사용자 입력받기 | ||
| - ✅컴퓨터 응답 출력하기 | ||
| - ✅잘못된 값 입력 시 에러 발생하기 | ||
| - ✅랜덤 숫자와 사용자 입력 비교하기 | ||
| - ✅게임 종료 시 재시작/종료 입력받기 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,14 @@ | ||
| import Computer from './Computer.js'; | ||
| import User from './User.js'; | ||
|
|
||
| class App { | ||
| async play() {} | ||
| constructor() { | ||
| this.computer = new Computer(); | ||
| this.user = new User(); | ||
| } | ||
| async play() { | ||
| await this.computer.playGame(this.user); | ||
| } | ||
| } | ||
|
|
||
| export default App; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import { MissionUtils } from '@woowacourse/mission-utils'; | ||
|
|
||
| export default class Computer { | ||
| static REPLAY_CODE = '1'; | ||
| static QUIT_CODE = '2'; | ||
| static NUM_SIZE = 3; | ||
| static MODE_SIZE = 1; | ||
|
|
||
| constructor() { | ||
| this.numbers = []; | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. airbnb 스타일 가이드에 따르면 구문의 앞과 블록의 뒤에는 빈 행을 두는 것이 좋다고 해요.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 미처 확인하지 못한 컨벤션이네요..알려주셔서 감사합니다! |
||
| pickRandomNumbers() { | ||
| this.clearNumbers(); | ||
| while (this.numbers.length < Computer.NUM_SIZE) { | ||
| const number = MissionUtils.Random.pickNumberInRange(1, 9); | ||
| if (!this.numbers.includes(number)) { | ||
| this.numbers.push(number); | ||
| } | ||
| } | ||
| } | ||
| async playGame(user) { | ||
| MissionUtils.Console.print('숫자 야구 게임을 시작합니다.'); | ||
| while (true) { | ||
| this.pickRandomNumbers(); | ||
| while (true) { | ||
| const input = await user.getUserNumber(); | ||
| const result = this.getMessage(input); | ||
| MissionUtils.Console.print(result.result); | ||
| if (result.success) { | ||
| MissionUtils.Console.print( | ||
| '3개의 숫자를 모두 맞히셨습니다! 게임 종료' | ||
| ); | ||
| break; | ||
| } | ||
| } | ||
|
Comment on lines
+23
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2주차 때는 좀더 코드를 기능별로 분리해보겠습니다. 조언 감사합니다! |
||
| const input = await user.getUserReplay(); | ||
| if (input === Computer.QUIT_CODE) break; | ||
| } | ||
| } | ||
| getMessage(expectedNumbers) { | ||
| const ballStrike = this.calculateBallStrike(expectedNumbers); | ||
| const result = this.getResultString(ballStrike); | ||
| return { | ||
| result, | ||
| success: ballStrike.strike === Computer.NUM_SIZE, | ||
| }; | ||
| } | ||
| calculateBallStrike(expectedNumbers) { | ||
| return expectedNumbers | ||
| .split('') | ||
| .map(Number) | ||
| .reduce( | ||
| (res, cur, idx) => { | ||
| const newRes = { ...res }; | ||
| if (this.numbers[idx] === cur) newRes.strike += 1; | ||
| else if (this.numbers.includes(cur)) newRes.ball += 1; | ||
| return newRes; | ||
| }, | ||
| { ball: 0, strike: 0 } | ||
| ); | ||
| } | ||
| getResultString({ ball, strike }) { | ||
| if (ball === 0 && strike === 0) return '낫싱'; | ||
| else if (ball === 0) return `${strike}스트라이크`; | ||
| else if (strike === 0) return `${ball}볼`; | ||
| else return `${ball}볼 ${strike}스트라이크`; | ||
|
Comment on lines
+64
to
+66
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 앞의 if문에 return이 있으니 |
||
| } | ||
|
|
||
| clearNumbers() { | ||
| this.numbers = []; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import { MissionUtils } from '@woowacourse/mission-utils'; | ||
| import Computer from './Computer'; | ||
| export default class User { | ||
| async getUserNumber() { | ||
| const input = await MissionUtils.Console.readLineAsync( | ||
| '숫자를 입력해주세요 : ' | ||
| ); | ||
|
|
||
| if ( | ||
| input.length !== Computer.NUM_SIZE || | ||
| input.includes('0') || | ||
| Number.isNaN(input) | ||
| ) { | ||
| throw new Error('[ERROR] 숫자가 잘못된 형식입니다.'); | ||
| } | ||
|
|
||
| return input; | ||
| } | ||
|
|
||
| async getUserReplay() { | ||
| const input = await MissionUtils.Console.readLineAsync( | ||
| '게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.\n' | ||
| ); | ||
| if ( | ||
| input.length !== Computer.MODE_SIZE || | ||
| (input !== Computer.REPLAY_CODE && input !== Computer.QUIT_CODE) || | ||
| Number.isNaN(input) | ||
| ) { | ||
| throw new Error('[ERROR] 숫자가 잘못된 형식입니다.'); | ||
| } | ||
|
|
||
| return input; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이렇게 선언하면 이후에
MissionUtils.Random()을Random()로 줄여서 쓸 수 있어요!