From b3afd2221c10e99cd9d29abb0aed441ed2b12604 Mon Sep 17 00:00:00 2001 From: HyeonChae1104 <211063@jnu.ac.kr> Date: Wed, 25 Oct 2023 19:59:39 +0900 Subject: [PATCH 1/2] =?UTF-8?q?docs=20=ED=8C=8C=EC=9D=BC=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=EB=B0=8F=20=ED=95=84=EC=9A=94=ED=95=9C=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EB=AA=A9=EB=A1=9D=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 docs/README.md diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 000000000..88eb15cf3 --- /dev/null +++ b/docs/README.md @@ -0,0 +1,15 @@ +기능 구현 + +- 같은 수가 같은 자리에 있으면 스트라이크, 다른 자리에 있으면 볼, 같은 수가 전혀 없으면 낫싱이란 힌트를 얻고, 그 힌트를 이용해서 먼저 상대방(컴퓨터)의 수를 맞추면 승리한다. + - 예) 상대방(컴퓨터)의 수가 425일 때 + - 123을 제시한 경우 : 1스트라이크 + - 456을 제시한 경우 : 1볼 1스트라이크 + - 789를 제시한 경우 : 낫싱 + +- 위 숫자 야구 게임에서 상대방의 역할을 컴퓨터가 한다. 컴퓨터는 1에서 9까지 서로 다른 임의의 수 3개를 선택한다. 게임 플레이어는 컴퓨터가 생각하고 있는 서로 다른 3개의 숫자를 입력하고, 컴퓨터는 입력한 숫자에 대한 결과를 출력한다. + +- 이 같은 과정을 반복해 컴퓨터가 선택한 3개의 숫자를 모두 맞히면 게임이 종료된다. + +- 게임을 종료한 후 게임을 다시 시작하거나 완전히 종료할 수 있다. + +- 사용자가 잘못된 값을 입력한 경우 throw문을 사용해 예외를 발생시킨후 애플리케이션은 종료되어야 한다. \ No newline at end of file From cf73ba02676e018c18e269c43f4c9d8c5c9915ff Mon Sep 17 00:00:00 2001 From: HyeonChae1104 <211063@jnu.ac.kr> Date: Wed, 25 Oct 2023 23:46:03 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=EC=88=AB=EC=9E=90=20=EC=95=BC=EA=B5=AC=20?= =?UTF-8?q?=EA=B2=8C=EC=9E=84=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 110 insertions(+), 2 deletions(-) diff --git a/src/App.js b/src/App.js index c38b30d5b..3c0c21d57 100644 --- a/src/App.js +++ b/src/App.js @@ -1,5 +1,113 @@ +import { Random, Console } from "@woowacourse/mission-utils"; + +//클래스 정의 class App { - async play() {} + async play() { + playGame() + while(true){ + + let computer = initializeComputer(); + + while (true) { + getAndValidateUserInput(getUserInput()); + + const result = checkGuess(computer, userNumber); + Console.print(result); + + if (result.includes("3스트라이크")) { + Console.print("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); + break; + } + } + const playAgain = await Console.readLineAsync("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); + + if(playAgain === '1'){ + continue; + } + else if (playAgain === '2') { + Console.print("게임 종료"); + return; + } + else{ + throw new Error(`[ERROR] 1 또는 2를 입력하세요`); + } + } + } +} + //숫자야구 시작 +function playGame() { + Console.print('숫자 야구 게임을 시작합니다.'); + } + + //컴퓨터가 1~9 사이 임의의 수 3개를 선택 + function initializeComputer() { + const computer = []; + while (computer.length < 3) { + const number = String(Random.pickNumberInRange(1, 9)); + if (!computer.includes(number)) { + computer.push(number); + } + } + return computer; + } + + // 사용자에게 입력값 받기 + async function getUserInput() { + while (true) { + const userNumber = []; + const number = String(await Console.readLineAsync("숫자를 입력해주세요 : ")); + userNumber.push(number); + return userNumber; + } + } + + + //입력값과 정답값 비교하여 ball/strike 반환 / 낫싱 반환 + function checkGuess(computer, userGuess) { + let strikes = 0; + let balls = 0; + + for (let i = 0; i < 3; i++) { + if (userGuess[i] === computer[i]) { + strikes++; + } else if (computer.includes(userGuess[i])) { + balls++; + } + if (balls > 0) { + result.push(`${balls}볼 `); + } + + if (strikes > 0) { + result.push(`${strikes}스트라이크`); + } + + if (strikes === 0 && balls === 0) { + Console.print('낫싱'); + } + if (strikes === 3) { + Console.print(`3스트라이크\n`); // 3개를 다 맞췄을 때 + } + } + return result.join(' '); + } + +function getAndValidateUserInput(inputNumber) { + const ANSWER_LENGTH = 3; + const NUMBER_RANGE = /^[1-9]+$/; + + if (inputNumber.length !== ANSWER_LENGTH) { + throw new Error("[ERROR] 3자리 숫자를 입력해야 합니다."); + } + + if (new Set(inputNumber).size !== ANSWER_LENGTH) { + throw new Error(`[ERROR] 서로 다른 숫자를 입력해야 합니다.`); + } + + if (!NUMBER_RANGE.test(inputNumber)) { + throw new Error(`[ERROR] 1부터 9까지의 숫자만 입력해야 합니다.`); + } + } -export default App; +const app = new App(); +app.play(); \ No newline at end of file