From 2825973e616cd45a28bdb151e1124264468105df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=B0=95=EC=86=8C=ED=9D=AC?= Date: Mon, 23 Oct 2023 00:41:42 +0900 Subject: [PATCH 1/5] =?UTF-8?q?=F0=9F=93=9D=20docs:=20README.md=20?= =?UTF-8?q?=EA=B3=BC=EC=A0=9C=20=EC=9A=94=EA=B5=AC=EC=82=AC=ED=95=AD=20?= =?UTF-8?q?=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 docs/README.md diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 000000000..fcafe5f91 --- /dev/null +++ b/docs/README.md @@ -0,0 +1,82 @@ +## 1. 기능 구현 +### 1.0 우테코 API 이용 +랜덤세자리 만들기 +```javascript +const computer = []; +while (computer.length < 3) { + const number = MissionUtils.Random.pickNumberInRange(1, 9); + if (!computer.includes(number)) { + computer.push(number); + } +} +``` + +### 1.1 정답 도달 (?) +1. 사용자 인풋이랑 랜덤세자리랑 비교하기 +2. 포함(includes)확인 -> 자리(indexOf)확인 -> ball, strike 개수 저장 +3. ball, strike개수에 따라 메세지 +4. 3스트라이크 선별 -> 추가 메세지 +5. 정답 맞힐 때 까지 라운드 반복실행 + +### 1.2 사용자 상호작용 +1. readline 모듈 추가 +> ```javascript +> // readline 모듈 사용법 +> const readline = require("readline"); +> const rl = readline.createInterface({ +> input: process.stdin, +> output: process.stdout, +> }); +> rl.on("line", (line) => { +> console.log("input: ", line); +> rl.close(); +> }); +> rl.on('close', () => { +> process.exit(); +> }) +> ``` +2. input에 랜덤 세자리 대신 readline을 통해 입력받은 값 넣기 +3. targetNumber를 랜덤 세자리로 설정 + +### 1.3 게임 종료 및 재시작 기능 추가 +1. 게임이 시작되었음을 나타내는 변수 isStarted = true; 추가 +2. round의 리턴값이 'clear'인 경우 를 isStarted = false 로 변경 +3. isStarted = false 인 경우 입력값이 100 이하여도 오류 x +4. 1 -> play() / 2 -> 종료.. + +## 2. 구현 후 +### 2.1 작동 확인 +```javascript +const app = new App(); +app.play(); +``` + +### 2.2 코드 컨벤션 확인 +- `const SNAKE_CASE = 1;` +- `const INPUT = randomThree()` +- [에어비엔비 코드컨벤션 참고](https://github.com/airbnb/javascript/blob/master/README.md) + +### 2.3 하면 안되는 것 +- 외부 라이브러리(jQuery, Lodash 등)를 사용 +- 프로그램 종료 시 process.exit()를 호출 +- 파일, 패키지 이름을 수정하거나 이동 + + +## 3. 제출 전 +### 3.1 TEST 통과 확인 +- npm test +- 모든 테스트가 성공 + +### 3.2 출력값 형식 확인 + +- 시작할 때: `숫자 야구 게임을 시작합니다.` +- default: +`1볼 1스트라이크` +- 하나도 없는 경우: `낫싱` +- 3개의 숫자를 모두 맞힐 경우: +`3스트라이크 +3개의 숫자를 모두 맞히셨습니다! 게임 종료` +- 재시작 여부 묻기: + `게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요. +` +- - 재시작 = 숫자 게임 시작 문구 출력 X \ No newline at end of file From 5bdf52acd68f479bb18c183a3da43914acd371ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=B0=95=EC=86=8C=ED=9D=AC?= Date: Mon, 23 Oct 2023 03:59:31 +0900 Subject: [PATCH 2/5] =?UTF-8?q?=E2=9C=A8=20feat:=201.=20=EA=B8=B0=EB=8A=A5?= =?UTF-8?q?=20=EA=B5=AC=ED=98=84=201.1=20-=201.3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 35 ++++++++----------- src/App.js | 88 +++++++++++++++++++++++++++++++++++++++++++++++- src/App2.js | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 192 insertions(+), 22 deletions(-) create mode 100644 src/App2.js diff --git a/docs/README.md b/docs/README.md index fcafe5f91..481909851 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,6 +1,6 @@ ## 1. 기능 구현 -### 1.0 우테코 API 이용 -랜덤세자리 만들기 +### 1.0 우테코 API 이용 ✅ +랜덤세자리 만들기 ```javascript const computer = []; while (computer.length < 3) { @@ -11,38 +11,31 @@ while (computer.length < 3) { } ``` -### 1.1 정답 도달 (?) +### 1.1 정답 도달 (?) ✅ 1. 사용자 인풋이랑 랜덤세자리랑 비교하기 2. 포함(includes)확인 -> 자리(indexOf)확인 -> ball, strike 개수 저장 3. ball, strike개수에 따라 메세지 4. 3스트라이크 선별 -> 추가 메세지 5. 정답 맞힐 때 까지 라운드 반복실행 -### 1.2 사용자 상호작용 +### 1.2 사용자 상호작용 ✅ 1. readline 모듈 추가 > ```javascript > // readline 모듈 사용법 -> const readline = require("readline"); -> const rl = readline.createInterface({ -> input: process.stdin, -> output: process.stdout, -> }); -> rl.on("line", (line) => { -> console.log("input: ", line); -> rl.close(); -> }); -> rl.on('close', () => { -> process.exit(); -> }) +> import * as readline from 'node:readline/promises'; +> import { stdin as input, stdout as output } from 'node:process'; +> const rl = readline.createInterface({ input, output }); +> const answer = await rl.question('What do you think of Node.js? '); +> console.log(`Thank you for your valuable feedback: ${answer}`); +> rl.close(); > ``` 2. input에 랜덤 세자리 대신 readline을 통해 입력받은 값 넣기 3. targetNumber를 랜덤 세자리로 설정 -### 1.3 게임 종료 및 재시작 기능 추가 -1. 게임이 시작되었음을 나타내는 변수 isStarted = true; 추가 -2. round의 리턴값이 'clear'인 경우 를 isStarted = false 로 변경 -3. isStarted = false 인 경우 입력값이 100 이하여도 오류 x -4. 1 -> play() / 2 -> 종료.. +### 1.3 게임 종료 및 재시작 기능 추가 ✅ +1. round의 리턴값이 'clear'인 경우 종료할지 시작할지를 물음 +2. 입력값에 따라 다음 실행 +3. 1 -> play() / 2 -> 종료.. ## 2. 구현 후 ### 2.1 작동 확인 diff --git a/src/App.js b/src/App.js index c38b30d5b..441e9ed81 100644 --- a/src/App.js +++ b/src/App.js @@ -1,5 +1,91 @@ +import { MissionUtils } from "@woowacourse/mission-utils"; +import * as readline from 'node:readline/promises' +import { stdin as input, stdout as output } from 'node:process' + +// 랜덤세자리 만들기 +const randomThree = () => { + const computer = []; + while (computer.length < 3) { + const number = MissionUtils.Random.pickNumberInRange(1, 9); + if (!computer.includes(number.toString())) { + computer.push(number.toString()); + } + } + return computer; +} + +// 유저 인풋 받기 +const userInput = async () => { + const rl = readline.createInterface({ input, output }) + let userInputValue = await rl.question('숫자를 입력해주세요 : '); + rl.close(); + return userInputValue; +} + +// 타겟넘버랑 유저인풋 비교하기 +const compare = (TARGET_NUMBER, input) => { + let [ball, strike] = [0, 0]; + input.forEach((v,i) => { + if(TARGET_NUMBER.includes(v)){ + if(TARGET_NUMBER.indexOf(v)=== i){ + strike++; + }else{ + ball++; + } + } + }) + return [ball, strike]; +} + +// 라운드 종료 메세지 +const endMessage = (ball, strike) => { + let message = ''; + + message = ball + strike == 0? '낫싱' : + ball == 0? `${strike}스트라이크` : + strike == 0? `${ball}볼` : + `${ball}볼 ${strike}스트라이크`; + + return message; +} + +// 도전 시작 +const round = (TARGET_NUMBER, input) => { + let [ball, strike] = compare(TARGET_NUMBER, input); + console.log(endMessage(ball, strike)); + return strike === 3 ? 'clear' : 'fail'; +} + +// 맞힐 때까지 반복 +const trial = async (TARGET_NUMBER, round) => { + let trialN = await userInput() + trialN = trialN.toString().split(''); + let result = round(TARGET_NUMBER, trialN); + + if(result === 'fail'){ + trial(TARGET_NUMBER, round); + }else if(result === 'clear'){ + console.log('3개의 숫자를 모두 맞히셨습니다! 게임을 새로시작하려면 1을 종료하시려면 2를 입력하세요.') + let restart = userInput(); + return restart; + } +} + class App { - async play() {} + async play() { + console.log('숫자 야구 게임을 시작합니다.') + const TARGET_NUMBER = randomThree(); + let restart = await trial(TARGET_NUMBER, round); + if(restart === '1'){ + return this.play(); + }else if(restart === '2'){ + return; + }else{ + throw new Error('에러!') + } + } } export default App; +const app = new App(); +app.play(); \ No newline at end of file diff --git a/src/App2.js b/src/App2.js new file mode 100644 index 000000000..1d82400b9 --- /dev/null +++ b/src/App2.js @@ -0,0 +1,91 @@ +import { MissionUtils } from "@woowacourse/mission-utils"; +import * as readline from 'node:readline/promises' +import { stdin as input, stdout as output } from 'node:process' + +// 랜덤세자리 만들기 +const randomThree = () => { + const computer = []; + while (computer.length < 3) { + const number = MissionUtils.Random.pickNumberInRange(1, 9); + if (!computer.includes(number.toString())) { + computer.push(number.toString()); + } + } + return computer; +} + +// 유저 인풋 받기 +const userInput = async () => { + const rl = readline.createInterface({ input, output }) + let userInputValue = await rl.question('숫자를 입력해주세요 : '); + rl.close(); + return userInputValue; +} + +// 타겟넘버랑 유저인풋 비교하기 +const compare = (TARGET_NUMBER, input) => { + let [ball, strike] = [0, 0]; + input.forEach((v,i) => { + if(TARGET_NUMBER.includes(v)){ + if(TARGET_NUMBER.indexOf(v)=== i){ + strike++; + }else{ + ball++; + } + } + }) + return [ball, strike]; +} + +// 라운드 종료 메세지 +const endMessage = (ball, strike) => { + let message = ''; + + message = ball + strike == 0? '낫싱' : + ball == 0? `${strike}스트라이크` : + strike == 0? `${ball}볼` : + `${ball}볼 ${strike}스트라이크`; + + return message; +} + +// 도전 시작 +const round = async (TARGET_NUMBER, input) => { + let [ball, strike] = await compare(TARGET_NUMBER, input); + console.log(await endMessage(ball, strike)); + return strike === 3 ? 'clear' : 'fail'; +} + +// 맞힐 때까지 반복 +const trial = async (TARGET_NUMBER, round) => { + let trialN = await userInput() + trialN = trialN.toString().split(''); + let result = await round(TARGET_NUMBER, trialN); + + if(result === 'fail'){ + await trial(TARGET_NUMBER, round); + }else if(result === 'clear'){ + console.log('3개의 숫자를 모두 맞히셨습니다! 게임을 새로시작하려면 1을 종료하시려면 2를 입력하세요.') + let restart = await userInput(); + return restart; + } +} + +// 야구 게임 클래스 선언 +class BaseBall { + async play() { + console.log('숫자 야구 게임을 시작합니다.') + const TARGET_NUMBER = randomThree(); + console.log(TARGET_NUMBER) + let restart = await trial(TARGET_NUMBER, round); + console.log(restart) + if(restart == 1){ + await this.play(); + }else if(restart == 2){ + return; + } + } +} + +let baseball = new BaseBall(); +baseball.play(); \ No newline at end of file From 23a7aabfbdfe27e1f3a38c77fff916f27070808b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=B0=95=EC=86=8C=ED=9D=AC?= Date: Tue, 24 Oct 2023 00:04:30 +0900 Subject: [PATCH 3/5] =?UTF-8?q?=F0=9F=90=9B=20fix:=20trial=20=ED=95=A8?= =?UTF-8?q?=EC=88=98=20undefined=20=EB=B0=98=ED=99=98=20=EC=98=A4=EB=A5=98?= =?UTF-8?q?=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App2.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/App2.js b/src/App2.js index 1d82400b9..de92779e5 100644 --- a/src/App2.js +++ b/src/App2.js @@ -63,7 +63,7 @@ const trial = async (TARGET_NUMBER, round) => { let result = await round(TARGET_NUMBER, trialN); if(result === 'fail'){ - await trial(TARGET_NUMBER, round); + return await trial(TARGET_NUMBER, round); }else if(result === 'clear'){ console.log('3개의 숫자를 모두 맞히셨습니다! 게임을 새로시작하려면 1을 종료하시려면 2를 입력하세요.') let restart = await userInput(); From 45455941494e6dbc492dfb9dee34c74fab9a5e04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=B0=95=EC=86=8C=ED=9D=AC?= Date: Wed, 25 Oct 2023 23:35:47 +0900 Subject: [PATCH 4/5] =?UTF-8?q?=F0=9F=90=9B=20fix:=20=EC=98=88=EC=99=B8=20?= =?UTF-8?q?=EC=83=81=ED=99=A9=20=EC=B2=98=EB=A6=AC=20=EB=B0=8F=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=ED=86=B5=EA=B3=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 64 +++++++++++++++----- package-lock.json | 2 +- package.json | 2 +- src/App.js | 145 ++++++++++++++++++++++++---------------------- src/App2.js | 91 ----------------------------- 5 files changed, 126 insertions(+), 178 deletions(-) delete mode 100644 src/App2.js diff --git a/docs/README.md b/docs/README.md index 481909851..36f1a8f5a 100644 --- a/docs/README.md +++ b/docs/README.md @@ -36,40 +36,74 @@ while (computer.length < 3) { 1. round의 리턴값이 'clear'인 경우 종료할지 시작할지를 물음 2. 입력값에 따라 다음 실행 3. 1 -> play() / 2 -> 종료.. +4. (1024) 오류수정 : trial 재귀함수에서 result가 'fail'일 때 return을 해주지 않아 답을 틀리면 undefined가 return 되는 오류가 있었음! +: 커밋넘버 : #ㅇ -## 2. 구현 후 -### 2.1 작동 확인 +## 2. 구현 후 +### 2.1 작동 확인 ✅ ```javascript const app = new App(); app.play(); ``` -### 2.2 코드 컨벤션 확인 +### 2.2 코드 컨벤션 확인 - 진행 중 ❌ - `const SNAKE_CASE = 1;` -- `const INPUT = randomThree()` +- `const INPUT = createRandomThree()` - [에어비엔비 코드컨벤션 참고](https://github.com/airbnb/javascript/blob/master/README.md) +#### 2.2.1 변수 +- 리터럴 문법 사용 (ex. const array = []; (o) const array = new Array(); (x)) +- 문법은 동사형으로 +#### 2.2.2 객체 +- 동적으로 객체의 키를 만들고 싶다면 객체를 생성하면서 넣어주기 (밖으로 빼지 않고) +```javascript +function getKey(k) { + return `a key named ${k}`; +} + +// bad +const obj = { + id: 5, + name: 'San Francisco', +}; +obj[getKey('enabled')] = true; + +// good +const obj = { + id: 5, + name: 'San Francisco', + [getKey('enabled')]: true, +}; +``` +- 객체 키랑 밸류랑 같으면 하나만 쓰면 됨(?) +- 하나만 쓸거면 위에 묶어놓기 +- no-prototype-builtins (ex.Object.hasOwn(object,key); (o) // Object.hasOwnProperty(key); Object.prototype.hasOwnProperty.call; const has = Object.prototype.hasOwnproperty; has.call(object,key); (x)) +- Object.assign({빈객체}, original, {추가할 객체}); (x) // const copy = { ...original, 추가할키:값} +#### 2.2.3 배열 +- 배열을 복사할 때는 전개구문을 사용 +- 노드리스트 같은 객체를 순회가능한 객체로 만들 때는 전개구문을 사용 + + -### 2.3 하면 안되는 것 +### 2.3 하면 안되는 것 ✅ - 외부 라이브러리(jQuery, Lodash 등)를 사용 - 프로그램 종료 시 process.exit()를 호출 - 파일, 패키지 이름을 수정하거나 이동 ## 3. 제출 전 -### 3.1 TEST 통과 확인 +### 3.1 TEST 통과 확인 ✅ - npm test - 모든 테스트가 성공 -### 3.2 출력값 형식 확인 +### 3.2 출력값 형식 확인 ✅ -- 시작할 때: `숫자 야구 게임을 시작합니다.` +- 시작할 때: `숫자 야구 게임을 시작합니다.`✅ - default: -`1볼 1스트라이크` -- 하나도 없는 경우: `낫싱` +`1볼 1스트라이크`✅ +- 하나도 없는 경우: `낫싱`✅ - 3개의 숫자를 모두 맞힐 경우: -`3스트라이크 -3개의 숫자를 모두 맞히셨습니다! 게임 종료` +`3스트라이크`✅ +`3개의 숫자를 모두 맞히셨습니다! 게임 종료`✅ - 재시작 여부 묻기: - `게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요. -` -- - 재시작 = 숫자 게임 시작 문구 출력 X \ No newline at end of file + `게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.` ✅ +- - 재시작 = 숫자 게임 시작 문구 출력 X ✅ \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index eb481dbbe..26176095a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,7 +15,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" }, "engines": { "node": ">=18.17.1", diff --git a/package.json b/package.json index 85960455f..44fbaf9c4 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/App.js b/src/App.js index 441e9ed81..8acb50222 100644 --- a/src/App.js +++ b/src/App.js @@ -1,91 +1,96 @@ import { MissionUtils } from "@woowacourse/mission-utils"; -import * as readline from 'node:readline/promises' -import { stdin as input, stdout as output } from 'node:process' -// 랜덤세자리 만들기 -const randomThree = () => { - const computer = []; - while (computer.length < 3) { - const number = MissionUtils.Random.pickNumberInRange(1, 9); - if (!computer.includes(number.toString())) { - computer.push(number.toString()); +class App { + createRandomThree() { + const computer = []; + while (computer.length < 3) { + const number = MissionUtils.Random.pickNumberInRange(1, 9); + if (!computer.includes(number.toString())) { + computer.push(number.toString()); + } } + return computer; } - return computer; -} -// 유저 인풋 받기 -const userInput = async () => { - const rl = readline.createInterface({ input, output }) - let userInputValue = await rl.question('숫자를 입력해주세요 : '); - rl.close(); - return userInputValue; -} + async getUserInput() { + const userInputValue = await MissionUtils.Console.readLineAsync('숫자를 입력해주세요 : '); + return userInputValue; + } -// 타겟넘버랑 유저인풋 비교하기 -const compare = (TARGET_NUMBER, input) => { - let [ball, strike] = [0, 0]; - input.forEach((v,i) => { - if(TARGET_NUMBER.includes(v)){ - if(TARGET_NUMBER.indexOf(v)=== i){ - strike++; - }else{ - ball++; + compare(input) { + let [ball, strike] = [0, 0]; + input.forEach((v, i) => { + if (this.TARGET_NUMBER.includes(v)) { + if (this.TARGET_NUMBER.indexOf(v) === i) { + strike++; + } else { + ball++; + } } - } - }) - return [ball, strike]; -} + }) + return [ball, strike]; + } -// 라운드 종료 메세지 -const endMessage = (ball, strike) => { - let message = ''; + getEndMessage(ball, strike) { + const message = ball + strike == 0 ? '낫싱' : + ball == 0 ? `${strike}스트라이크` : + strike == 0 ? `${ball}볼` : + `${ball}볼 ${strike}스트라이크`; - message = ball + strike == 0? '낫싱' : - ball == 0? `${strike}스트라이크` : - strike == 0? `${ball}볼` : - `${ball}볼 ${strike}스트라이크`; + return message; + } - return message; -} + async startTrial(input) { + const [ball, strike] = this.compare(input); + MissionUtils.Console.print(this.getEndMessage(ball, strike)); + return strike === 3 ? 'clear' : 'fail'; + } -// 도전 시작 -const round = (TARGET_NUMBER, input) => { - let [ball, strike] = compare(TARGET_NUMBER, input); - console.log(endMessage(ball, strike)); - return strike === 3 ? 'clear' : 'fail'; -} + async startRound() { + let userTrial = await this.getUserInput(); + userTrial = userTrial.toString(); + const regExpNum = /^[1-9]+$/g; + const regExpSame = /(\d).*\1/g; + if (!userTrial.match(regExpNum) || userTrial.length !== 3 || userTrial.match(regExpSame)) { + throw new Error('[error] 숫자가 잘못된 형식입니다.'); + } else { + userTrial = userTrial.split(''); + } -// 맞힐 때까지 반복 -const trial = async (TARGET_NUMBER, round) => { - let trialN = await userInput() - trialN = trialN.toString().split(''); - let result = round(TARGET_NUMBER, trialN); + const result = await this.startTrial(userTrial); - if(result === 'fail'){ - trial(TARGET_NUMBER, round); - }else if(result === 'clear'){ - console.log('3개의 숫자를 모두 맞히셨습니다! 게임을 새로시작하려면 1을 종료하시려면 2를 입력하세요.') - let restart = userInput(); - return restart; + if (result === 'fail') { + return await this.startRound(); + } else if (result === 'clear') { + MissionUtils.Console.print('3개의 숫자를 모두 맞히셨습니다! 게임 종료\n 게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.'); + const restart = await this.getUserInput(); + return restart; + } + } + + async startNewGame () { + this.TARGET_NUMBER = this.createRandomThree(); + // console.log(this.TARGET_NUMBER) + const restart = await this.startRound(); + if (restart === '1') { + return this.startNewGame(); + } else if (restart === '2') { + MissionUtils.Console.print('게임종료') + return; + } } -} -class App { async play() { - console.log('숫자 야구 게임을 시작합니다.') - const TARGET_NUMBER = randomThree(); - let restart = await trial(TARGET_NUMBER, round); - if(restart === '1'){ - return this.play(); - }else if(restart === '2'){ - return; - }else{ - throw new Error('에러!') + try { + MissionUtils.Console.print('숫자 야구 게임을 시작합니다.'); + this.startNewGame(); + } catch (error) { + throw new Error(error); } } } -export default App; const app = new App(); -app.play(); \ No newline at end of file +app.play(); + +export default App; \ No newline at end of file diff --git a/src/App2.js b/src/App2.js deleted file mode 100644 index de92779e5..000000000 --- a/src/App2.js +++ /dev/null @@ -1,91 +0,0 @@ -import { MissionUtils } from "@woowacourse/mission-utils"; -import * as readline from 'node:readline/promises' -import { stdin as input, stdout as output } from 'node:process' - -// 랜덤세자리 만들기 -const randomThree = () => { - const computer = []; - while (computer.length < 3) { - const number = MissionUtils.Random.pickNumberInRange(1, 9); - if (!computer.includes(number.toString())) { - computer.push(number.toString()); - } - } - return computer; -} - -// 유저 인풋 받기 -const userInput = async () => { - const rl = readline.createInterface({ input, output }) - let userInputValue = await rl.question('숫자를 입력해주세요 : '); - rl.close(); - return userInputValue; -} - -// 타겟넘버랑 유저인풋 비교하기 -const compare = (TARGET_NUMBER, input) => { - let [ball, strike] = [0, 0]; - input.forEach((v,i) => { - if(TARGET_NUMBER.includes(v)){ - if(TARGET_NUMBER.indexOf(v)=== i){ - strike++; - }else{ - ball++; - } - } - }) - return [ball, strike]; -} - -// 라운드 종료 메세지 -const endMessage = (ball, strike) => { - let message = ''; - - message = ball + strike == 0? '낫싱' : - ball == 0? `${strike}스트라이크` : - strike == 0? `${ball}볼` : - `${ball}볼 ${strike}스트라이크`; - - return message; -} - -// 도전 시작 -const round = async (TARGET_NUMBER, input) => { - let [ball, strike] = await compare(TARGET_NUMBER, input); - console.log(await endMessage(ball, strike)); - return strike === 3 ? 'clear' : 'fail'; -} - -// 맞힐 때까지 반복 -const trial = async (TARGET_NUMBER, round) => { - let trialN = await userInput() - trialN = trialN.toString().split(''); - let result = await round(TARGET_NUMBER, trialN); - - if(result === 'fail'){ - return await trial(TARGET_NUMBER, round); - }else if(result === 'clear'){ - console.log('3개의 숫자를 모두 맞히셨습니다! 게임을 새로시작하려면 1을 종료하시려면 2를 입력하세요.') - let restart = await userInput(); - return restart; - } -} - -// 야구 게임 클래스 선언 -class BaseBall { - async play() { - console.log('숫자 야구 게임을 시작합니다.') - const TARGET_NUMBER = randomThree(); - console.log(TARGET_NUMBER) - let restart = await trial(TARGET_NUMBER, round); - console.log(restart) - if(restart == 1){ - await this.play(); - }else if(restart == 2){ - return; - } - } -} - -let baseball = new BaseBall(); -baseball.play(); \ No newline at end of file From a47b84bf0df67a25b0ee14aec091357145574880 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=B0=95=EC=86=8C=ED=9D=AC?= Date: Wed, 25 Oct 2023 23:59:50 +0900 Subject: [PATCH 5/5] =?UTF-8?q?=F0=9F=9A=9A=20restore:=20package,=20packag?= =?UTF-8?q?e-lock?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 4 ++-- package.json | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index 26176095a..9fc0c73cc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,7 +15,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" }, "engines": { "node": ">=18.17.1", @@ -5008,4 +5008,4 @@ } } } -} +} \ No newline at end of file diff --git a/package.json b/package.json index 44fbaf9c4..6b41112d5 100644 --- a/package.json +++ b/package.json @@ -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" @@ -41,4 +41,4 @@ "npm": ">=9.6.7", "node": ">=18.17.1" } -} +} \ No newline at end of file