diff --git a/minji/src/App.js b/minji/src/App.js index f53916abe..ee9f6a3c1 100644 --- a/minji/src/App.js +++ b/minji/src/App.js @@ -1,5 +1,13 @@ +const { BaseballGame } = require("./BaseballGame"); + class App { - play() {} + play() { + const game = new BaseballGame(); + game.start(); + } } +const app = new App(); +app.play(); + module.exports = App; diff --git a/minji/src/BaseballGame.js b/minji/src/BaseballGame.js new file mode 100644 index 000000000..0fd5f8175 --- /dev/null +++ b/minji/src/BaseballGame.js @@ -0,0 +1,95 @@ +const { Console, Random } = require("@woowacourse/mission-utils"); +const { GameConsole } = require("./GameConsole"); + +class BaseballGame { + #answer; + #inputNumbers; + #strikeCnt; + #ballCnt; + #console; + + constructor() { + this.#answer = null; + this.#inputNumbers = null; + this.#strikeCnt = null; + this.#ballCnt = null; + this.#console = new GameConsole(); + } + + start() { + this.#makeRandomNumbers(); + this.#console.printStart(); + this.#getUserInput(); + } + + exit() { + Console.close(); + } + + restart() { + this.#answer = null; + this.#inputNumbers = null; + this.#strikeCnt = null; + this.#ballCnt = null; + this.#makeRandomNumbers(); + this.#getUserInput(); + } + + #checkStrikeCnt() { + let cnt = 0; + for (let i = 0; i < 3; i += 1) { + if (this.#answer[i] === Number(this.#inputNumbers[i])) cnt += 1; + } + this.#strikeCnt = cnt; + } + #checkBallCnt() { + let cnt = 0; + this.#inputNumbers.forEach((v, i) => { + if (Number(v) === this.#answer[0] && i !== 0) cnt++; + if (Number(v) === this.#answer[1] && i !== 1) cnt++; + if (Number(v) === this.#answer[2] && i !== 2) cnt++; + }); + + this.#ballCnt = cnt; + } + #getUserInput() { + this.#console.inputNumbers(this.#userNumberAfterFunc.bind(this)); + } + #userNumberAfterFunc(numbers) { + this.#inputNumbers = numbers.split(""); + this.#matchWithAnswer(); + } + #cmdAfterFunc(cmd) { + if (cmd === "1") this.restart(); + if (cmd === "2") this.exit(); + } + #makeRandomNumbers() { + const computer = []; + while (computer.length < 3) { + const number = Random.pickNumberInRange(1, 9); + if (!computer.includes(number)) { + computer.push(number); + } + } + this.#answer = computer; + console.log(this.#answer); // 확인용 + } + + #matchWithAnswer() { + this.#checkStrikeCnt(); + this.#checkBallCnt(); + + this.#console.printResult(this.#strikeCnt, this.#ballCnt); + + if (this.#strikeCnt === 3) { + this.#console.printEnd(); + this.#console.inputCmd(this.#cmdAfterFunc.bind(this)); + return; + } + this.#getUserInput(); + } +} + +module.exports = { + BaseballGame, +}; diff --git a/minji/src/GameConsole.js b/minji/src/GameConsole.js new file mode 100644 index 000000000..de683d903 --- /dev/null +++ b/minji/src/GameConsole.js @@ -0,0 +1,51 @@ +const { Console } = require("@woowacourse/mission-utils"); +const { validateCmdInput, validateNumbersInput } = require("./Validator"); + +class GameConsole { + #outputs = { + START_MESSAGE: "숫자 야구 게임을 시작합니다.", + REQUIRE_NUMBERS: "숫자를 입력해주세요 : ", + REQUIRE_CMD: "게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.", + END_MESSAGE: "3개의 숫자를 모두 맞히셨습니다! 게임 종료", + }; + + printStart() { + Console.print(this.#outputs.START_MESSAGE); + } + printResult(strike, ball) { + if (strike === 0 && ball === 0) { + Console.print("낫싱"); + return; + } + if (strike === 0) { + Console.print(ball + "볼"); + return; + } + if (ball === 0) { + Console.print(strike + "스트라이크"); + return; + } + Console.print(`${ball}볼 ${strike}스트라이크`); + } + + printEnd() { + Console.print(this.#outputs.END_MESSAGE); + } + + inputNumbers(callback) { + Console.readLine(this.#outputs.REQUIRE_NUMBERS, (numbers) => { + validateNumbersInput(numbers); + return callback(numbers); + }); + } + inputCmd(callback) { + Console.readLine(this.#outputs.REQUIRE_CMD, (cmd) => { + validateCmdInput(cmd); + return callback(cmd); + }); + } +} + +module.exports = { + GameConsole, +}; diff --git a/minji/src/Validator.js b/minji/src/Validator.js new file mode 100644 index 000000000..acd1a1de4 --- /dev/null +++ b/minji/src/Validator.js @@ -0,0 +1,24 @@ +const { Console } = require("@woowacourse/mission-utils"); + +function validateNumbersInput(numbers) { + if (numbers.length != 3) { + Console.close(); + throw new Error("잘못된 입력입니다."); + } + if (isNaN(numbers)) { + throw new Error("숫자만 입력해주세요."); + } + for (idx in numbers) { + if (numbers.indexOf(numbers[idx]) !== Number(idx)) { + throw new Error("중복된 숫자가 있습니다."); + } + } +} + +function validateCmdInput(cmd) { + if (cmd !== "1" && cmd !== "2") { + throw new Error("잘못된 입력입니다."); + } +} + +module.exports = { validateCmdInput, validateNumbersInput }; diff --git a/src/tempCodeRunnerFile.js b/src/tempCodeRunnerFile.js new file mode 100644 index 000000000..0f2a98457 --- /dev/null +++ b/src/tempCodeRunnerFile.js @@ -0,0 +1,2 @@ +const app = new App(); +app.play();