From 0bd70e42d0f54b1b9d5ef746d25b3461bc36ebea Mon Sep 17 00:00:00 2001 From: yeramm <213187@jnu.ac.kr> Date: Mon, 6 May 2024 12:35:08 +0900 Subject: [PATCH 1/4] =?UTF-8?q?feat:=20=EC=9B=B9=EC=82=AC=EC=9D=B4?= =?UTF-8?q?=ED=8A=B8=20=ED=85=8D=EC=8A=A4=ED=8A=B8=EC=99=80=20=EC=88=AB?= =?UTF-8?q?=EC=9E=90=EB=A5=BC=20=EC=9E=85=EB=A0=A5=ED=95=98=EB=8A=94=20?= =?UTF-8?q?=ED=85=8D=EC=8A=A4=ED=8A=B8=EB=B0=95=EC=8A=A4=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 숫자야구를 설명하는 텍스트와 그 숫자를 입력하는 텍스트박스 추가 --- index.html | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/index.html b/index.html index b021b5c..aa5243a 100644 --- a/index.html +++ b/index.html @@ -3,9 +3,24 @@ - + 숫자 야구 +

⚾ 숫자 야구 게임

+

+ 1~9까지의 수를 중복없이 3개 입력해주세요.
+ 올바른 예) 139
+ 틀린 예) 122
+

+
+ + +
+

📃 결과

+
+ +
+
From 1607545bc7ada869d10ba4724129856d2f05be74 Mon Sep 17 00:00:00 2001 From: yeramm <213187@jnu.ac.kr> Date: Mon, 6 May 2024 12:37:26 +0900 Subject: [PATCH 2/4] =?UTF-8?q?feat:=20=EC=97=90=EB=9F=AC=EB=A9=94?= =?UTF-8?q?=EC=8B=9C=EC=A7=80=20=EC=B6=9C=EB=A0=A5=ED=95=A8=EC=88=98=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 사용자가 잘못된 값을 입력할 경우 에러메시지를 출력하는 함수 추가. --- src/main.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main.js b/src/main.js index e69de29..a2a98ed 100644 --- a/src/main.js +++ b/src/main.js @@ -0,0 +1,8 @@ +// 사용자가 잘못된 값을 입력할 경우 에러메세지를 출력하는 함수 +function validateInput(input) { + if (input.length !== 3) return false; + const numbers = input.split("").map(Number); + return numbers.every((num, index) => { + return !isNaN(num) && num >= 1 && num <= 9 && numbers.indexOf(num) === index; + }); +} \ No newline at end of file From d277cb96103c5b58bbcd2bd6e487071feda09abf Mon Sep 17 00:00:00 2001 From: yeramm <213187@jnu.ac.kr> Date: Mon, 6 May 2024 12:39:19 +0900 Subject: [PATCH 3/4] =?UTF-8?q?feat:=20=EC=88=AB=EC=9E=90=20=EC=95=BC?= =?UTF-8?q?=EA=B5=AC=20=EA=B2=8C=EC=9E=84=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 비밀 숫자를 생성하고 사용자가 텍스트박스에 숫자를 입력하면 스트라이크, 볼, 낫싱을 출력하는 함수 추가 --- src/main.js | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/src/main.js b/src/main.js index a2a98ed..aaffd44 100644 --- a/src/main.js +++ b/src/main.js @@ -5,4 +5,50 @@ function validateInput(input) { return numbers.every((num, index) => { return !isNaN(num) && num >= 1 && num <= 9 && numbers.indexOf(num) === index; }); -} \ No newline at end of file +} + +let secretNumber; // 비밀숫자 + +//비밀 숫자 생성함수 +function generateSecretNumber() { + let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]; + let secret = []; + for (let i = 0; i < 3; i++) { + const index = Math.floor(Math.random() * numbers.length); + secret.push(numbers[index]); + numbers.splice(index, 1); + } + return secret; +} + +// 사용자의 추측 함수 +function checkGuess() { + const guessInput = document.getElementById("guessInput").value; + if (!validateInput(guessInput)) { + alert("1부터 9까지 서로 다른 수로 이루어진 3자리의 수를 입력하세요."); + return; + } + + const guess = guessInput.split("").map(Number); + let strike = 0; + let ball = 0; + for (let i = 0; i < 3; i++) { + if (guess[i] === secretNumber[i]) { + strike++; + } else if (secretNumber.includes(guess[i])) { + ball++; + } + } + + const output = document.getElementById("output"); + if (strike === 3) { + output.textContent = "\u{1F389}정답을 맞추셨습니다!\u{1F389}"; + document.getElementById("restartButton").style.display = "block"; + } else if (strike === 0 && ball === 0) { + output.textContent = "낫싱"; + } else { + output.textContent = `${ball}볼 ${strike}스트라이크`; + } + } + + \ No newline at end of file From 1542443f62599732a81f702a94b6e36e429fba7d Mon Sep 17 00:00:00 2001 From: yeramm <213187@jnu.ac.kr> Date: Mon, 6 May 2024 12:41:22 +0900 Subject: [PATCH 4/4] =?UTF-8?q?feat:=20=EA=B2=8C=EC=9E=84=20=EC=9E=AC?= =?UTF-8?q?=EC=8B=9C=EC=9E=91=20=ED=95=A8=EC=88=98=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 게임에서 이겼을 경우 종료 문구와 함께 재시작 버튼이 표시되는 함수, 재시작 버튼 함수 추가 --- src/main.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/main.js b/src/main.js index aaffd44..accdbb3 100644 --- a/src/main.js +++ b/src/main.js @@ -51,4 +51,16 @@ function checkGuess() { } } - \ No newline at end of file + +// 게임 재시작 함수 +function restartGame() { + secretNumber = generateSecretNumber(); + document.getElementById("output").textContent = ""; + document.getElementById("guessInput").value = ""; + document.getElementById("restartButton").style.display = "none"; + } + + document.getElementById("guessButton").addEventListener("click", checkGuess); + document.getElementById("restartButton").addEventListener("click", restartGame); + + secretNumber = generateSecretNumber(); // 게임 시작 시 비밀 숫자 생성 \ No newline at end of file