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

[이동현] up nd down 구현 #9

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
11 changes: 9 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"rules": {},
"env": {
"es6": true,
"node": true
Expand All @@ -8,5 +7,13 @@
"ecmaVersion": "latest",
"sourceType": "module"
},
"extends": ["eslint:recommended", "plugin:prettier/recommended"]
"rules": {
"no-console": "off",
"no-use-before-define": "off",
"no-plusplus": "off",
"no-shadow": "off",
"import/prefer-default-export": "off"
},
Comment on lines +10 to +16
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eslint를 조금 빡빡하게 ? 사용해보는 것도 좋을것 같아요
가령 airbnb 의 lint rule을 도입해본다던지
참고로 off 처리한 rule들은 리즈너블 해보입니다

"extends": ["eslint:recommended", "airbnb-base", "prettier"],
"ignorePatterns": ["node_modules/", "dist/", "build/"]
}
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
"devDependencies": {
"esbuild": "^0.24.2",
"eslint": "^8.57.0",
"eslint-config-airbnb": "^19.0.4",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-prettier": "^10.0.1",
"eslint-plugin-import": "^2.31.0",
"eslint-plugin-prettier": "^5.2.1",
"prettier": "^3.2.5"
}
}
60 changes: 60 additions & 0 deletions src/index.constants.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,74 @@
/**
* 게임에서 사용되는 최대 숫자
* @constant
*/
export const MAX_NUMBER = 50

/**
* 게임에서 사용되는 최소 숫자
* @constant
*/
export const MIN_NUMBER = 1

/**
* 숫자 추측 가능 횟수 제한
* @constant
*/
export const LIMIT_COUNT = 5

/**
* 게임에서 출력되는 메시지 상수 모음
* @constant
*/
export const PRINT = Object.freeze({
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍👍👍 객체로 관리하는것, Object.freeze를 통해 객체 내부의 값들도 상수화 한 부분이 좋네요

/**
* 게임 시작 메세지
* @type {string}
*/
init: `컴퓨터가 1~50 사이의 숫자를 선택했습니다. 숫자를 맞춰보세요.`,

/**
* 사용자가 잘못된 숫자를 입력했을 때 표시되는 에러 메시지
* @type {string}
*/
userInputError: `\x1b[31m${"숫자는 1~50 사이로 입력해주세요."}\x1b[0m`,

/**
* 사용자 입력 프롬프트 메시지
* @type {string}
*/
input: `\x1b[1m\x1b[96m숫자 입력: \x1b[0m`,

/**
* 게임 재시작 여부를 묻는 메시지
* @type {string}
*/
reStart: `\x1b[1m\x1b[96m게임을 다시 시작하시겠습니까? (yes/no): \x1b[0m`,

/**
* 게임 종료 메시지
* @type {string}
*/
end: `\x1b[1m\x1b[96m게임을 종료합니다.\x1b[0m`,

/**
* 정답 메시지 생성 함수
* @param {number} playCount - 사용자가 정답을 맞추는 데 걸린 시도 횟수
* @returns {string} 축하 메시지
*/
answer: (playCount) => `\x1b[1m\x1b[32m${`축하합니다! ${playCount}번 만에 숫자를 맞추셨습니다.`}\x1b[0m`,

/**
* 이전 추측 목록 메시지 생성 함수
* @param {number[]} prevInputList - 사용자가 입력한 이전 숫자 목록
* @returns {string} 이전 추측 메시지
*/
prevGuess: (prevInputList) => `\x1b[93m${`이전 추측: ${prevInputList.join(", ")} \n`}\x1b[0m`,

/**
* 제한 횟수를 초과했을 때의 메시지 생성 함수
* @param {number} answer - 정답 숫자
* @returns {string} 실패 메시지
*/
excced: (answer) => `\x1b[1m\x1b[31m${`5회 초과! 숫자를 맞추지 못했습니다. (정답: ${answer})`}\x1b[0m`,
})
19 changes: 10 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readLineAsync } from "./utils/readLine.util"
import { readLineAsync } from "./utils/readLine"
import { getRandomNumber } from "./index.util"
import { MAX_NUMBER, MIN_NUMBER, LIMIT_COUNT, PRINT } from "./index.constants"

Expand Down Expand Up @@ -38,32 +38,33 @@ async function play() {
play()
}

function isInitialStart(playCount) {
return playCount === 0
// Play Helper Function
function isInitialStart(runCount) {
return runCount === 0
}

function isExceedCount(playCount) {
return playCount >= LIMIT_COUNT
function isExceedCount(runCount) {
return runCount >= LIMIT_COUNT
}

function userInputValidation(value) {
return value >= MIN_NUMBER && value <= MAX_NUMBER
}

function validateUserInput(userInputValue, answer) {
function validateUserInput(userInputValue, correctAnswer) {
let result = false

if (userInputValue > answer) {
if (userInputValue > correctAnswer) {
console.log("\x1b[1m\x1b[95m%s\x1b[0m", "다운")
result = false
}

if (userInputValue < answer) {
if (userInputValue < correctAnswer) {
console.log("\x1b[1m\x1b[95m%s\x1b[0m", "업")
result = false
}

if (userInputValue === answer) {
if (userInputValue === correctAnswer) {
console.log("\x1b[1m\x1b[95m%s\x1b[0m", "정답!")
result = true
}
Expand Down
5 changes: 5 additions & 0 deletions src/index.util.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* @param {number} min 최소값
* @param {number} max 최대값
* @returns
*/
export function getRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min)
}
File renamed without changes.
Loading