From f0c25a7fd28a1ac716ec9ac8f108e44db2bf7579 Mon Sep 17 00:00:00 2001 From: yunseong0404 <333665@naver.com> Date: Fri, 10 May 2024 16:00:45 +0900 Subject: [PATCH 1/9] =?UTF-8?q?feat.html=20=EA=B5=AC=EC=A1=B0=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.html | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/index.html b/index.html index b021b5c..c986097 100644 --- a/index.html +++ b/index.html @@ -3,10 +3,44 @@ - + baseball-game + + + -
- +
+

⚾숫자 야구 게임

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

📄결과

+
+
+ +
+

🎉정답을 맞추셨습니다🎉

+
게임을 새로 시작하시겠습니까?
+ + +
+
+ +
+

게임이 종료되었습니다😊

+
+
+ From 82108ef88ee82b47ee36534ce910508ef296fe09 Mon Sep 17 00:00:00 2001 From: yunseong0404 <333665@naver.com> Date: Fri, 10 May 2024 16:01:11 +0900 Subject: [PATCH 2/9] =?UTF-8?q?feat.=EC=88=AB=EC=9E=90=20=EC=9C=A0?= =?UTF-8?q?=ED=9A=A8=EC=84=B1=20=EA=B2=80=EC=82=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/checkNumber.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/modules/checkNumber.js diff --git a/src/modules/checkNumber.js b/src/modules/checkNumber.js new file mode 100644 index 0000000..4afb248 --- /dev/null +++ b/src/modules/checkNumber.js @@ -0,0 +1,31 @@ + +function checkNumber(e, inputEl) { + let content = inputEl.value; + let lastValue = e.data; + + + if( isNaN(lastValue) ){ + alert("숫자를 입력해 주세요."); + inputEl.value = content.slice(0, -1); + return; + } + else if( content.length > 3 ){ + alert("세자리 수를 입력해 주세요."); + inputEl.value = content.slice(0, -1); + return; + } + else if( content.slice(0, -1).includes(lastValue) ){ + alert("중복되지 않는 수를 입력해 주세요."); + inputEl.value = content.slice(0, -1); + return; + } + else if( lastValue == 0 ){ + alert("1~9까지의 수를 입력해 주세요."); + inputEl.value = content.slice(0, -1); + return; + } + + return true; +} + +export { checkNumber }; \ No newline at end of file From 57beec4e196eaca986fd6954d3a9f32642b6f26b Mon Sep 17 00:00:00 2001 From: yunseong0404 <333665@naver.com> Date: Fri, 10 May 2024 16:01:27 +0900 Subject: [PATCH 3/9] =?UTF-8?q?feat.=EA=B2=8C=EC=9E=84=20=EC=A2=85?= =?UTF-8?q?=EB=A3=8C=20=EA=B8=B0=EB=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/finishGame.js | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/modules/finishGame.js diff --git a/src/modules/finishGame.js b/src/modules/finishGame.js new file mode 100644 index 0000000..5188c2c --- /dev/null +++ b/src/modules/finishGame.js @@ -0,0 +1,10 @@ +function finishGame() { + const runBoard = document.querySelector(".runGame"); + const finishBoard = document.querySelector(".finishGame"); + + // window.close(); + runBoard.style.display = "none"; + finishBoard.style.display = "block"; +} + +export { finishGame }; \ No newline at end of file From 37c0a9c7482ea3c66ae6018cf3cbf8b896cb7087 Mon Sep 17 00:00:00 2001 From: yunseong0404 <333665@naver.com> Date: Fri, 10 May 2024 16:01:49 +0900 Subject: [PATCH 4/9] =?UTF-8?q?feat.=EA=B2=B0=EA=B3=BC=20=EB=B0=98?= =?UTF-8?q?=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/getResult.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/modules/getResult.js diff --git a/src/modules/getResult.js b/src/modules/getResult.js new file mode 100644 index 0000000..dc0813d --- /dev/null +++ b/src/modules/getResult.js @@ -0,0 +1,25 @@ +function getResult(computer, userNum) { + + let strike = 0; + let ball = 0; + const userArr = userNum.split('').map(Number); + + //count strike + for(let i=0; i Date: Fri, 10 May 2024 16:03:58 +0900 Subject: [PATCH 5/9] =?UTF-8?q?feat.=EB=9E=9C=EB=8D=A4=20=EC=88=AB?= =?UTF-8?q?=EC=9E=90=20=EC=83=9D=EC=84=B1=20=EA=B8=B0=EB=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/makeRandom.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/modules/makeRandom.js diff --git a/src/modules/makeRandom.js b/src/modules/makeRandom.js new file mode 100644 index 0000000..8d7ae28 --- /dev/null +++ b/src/modules/makeRandom.js @@ -0,0 +1,16 @@ +//랜덤 수 생성 함수 +function makeRandom() { + let computer = []; + + while(computer.length <3){ + let random = Math.floor(Math.random() * 9) + 1; + + if(!computer.includes(random)){ + computer.push(random); + } + } + + return computer; +} + +export { makeRandom }; \ No newline at end of file From 19ad6ffe4e3fb7869d73fa4f1eae06495750a1c0 Mon Sep 17 00:00:00 2001 From: yunseong0404 <333665@naver.com> Date: Fri, 10 May 2024 16:04:15 +0900 Subject: [PATCH 6/9] =?UTF-8?q?feat.=EA=B2=B0=EA=B3=BC=20=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5=20=EA=B8=B0=EB=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/showResult.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/modules/showResult.js diff --git a/src/modules/showResult.js b/src/modules/showResult.js new file mode 100644 index 0000000..2d853c9 --- /dev/null +++ b/src/modules/showResult.js @@ -0,0 +1,13 @@ +function showResult(resultArr) { + if(resultArr[0]==0 && resultArr[1]==0){ + return "낫싱"; + } + else if(resultArr[1] == 0) { + return (resultArr[0] + "스트라이크"); + } else { + return (resultArr[1] + "볼 " + resultArr[0] + "스트라이크"); + } + +} + +export { showResult }; \ No newline at end of file From 532cb024d3d733ec7423fcd3354514082bdd3a05 Mon Sep 17 00:00:00 2001 From: yunseong0404 <333665@naver.com> Date: Fri, 10 May 2024 16:04:30 +0900 Subject: [PATCH 7/9] =?UTF-8?q?main.css=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.css | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/main.css diff --git a/src/main.css b/src/main.css new file mode 100644 index 0000000..63ae7dd --- /dev/null +++ b/src/main.css @@ -0,0 +1,13 @@ +.form { + display: flex; + align-items: stretch; +} + +#input { + margin-right: 5px; +} + +#restartBtn { + margin-top: 15px; +} + \ No newline at end of file From b30c300e554a0b0b3fed0171f1be1348fede24ec Mon Sep 17 00:00:00 2001 From: yunseong0404 <333665@naver.com> Date: Fri, 10 May 2024 16:06:09 +0900 Subject: [PATCH 8/9] =?UTF-8?q?main.js=20=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/main.js b/src/main.js index e69de29..4ba57ef 100644 --- a/src/main.js +++ b/src/main.js @@ -0,0 +1,48 @@ +import { checkNumber } from "./modules/checkNumber"; +import { finishGame } from "./modules/finishGame"; +import { getResult } from "./modules/getResult"; +import { makeRandom } from "./modules/makeRandom"; +import { showResult } from "./modules/showResult"; + +const resultBoard = document.querySelector(".result"); +const successBoard = document.querySelector(".success"); +const finishBoard = document.querySelector(".finishGame"); +const inputEl = document.getElementById("input"); + + +successBoard.style.display = "none"; +finishBoard.style.display = "none"; + +//컴퓨터 랜덤 수 생성 +const computer = makeRandom(); +console.log(computer); + +//입력 수 검사 +inputEl.addEventListener('input', e => checkNumber(e, inputEl)); + +//숫자 제출 +document.getElementById("submitBtn").addEventListener("click", function() { + + if(inputEl.value.length == 3){ + const score = getResult(computer, inputEl.value); + const result = showResult(score); + + if(score[0] != 3){ + document.getElementById("score").innerHTML += `
${inputEl.value} : ${result}
`; + } else { + resultBoard.style.display = "none"; + successBoard.style.display = "block"; + } + } else { + alert("세자리 숫자를 입력해주세요."); + } + +}) + + +//프로그램 완전히 종료 +document.getElementById("endBtn").addEventListener("click", function() { + finishGame(); +}); + + From 0fdf341ce25208c9297e573a7274dbd23cd109ca Mon Sep 17 00:00:00 2001 From: yunseong0404 <333665@naver.com> Date: Fri, 10 May 2024 16:06:24 +0900 Subject: [PATCH 9/9] =?UTF-8?q?README.md=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8b2833c..0e8edfc 100644 --- a/README.md +++ b/README.md @@ -1 +1,14 @@ -# javascript-baseball-precourse \ No newline at end of file +# baseball-game + +# 구현 기능 목록 +- 3자리의 랜덤 숫자를 생성한다. + +- 게임 플레이어의 3자리 숫자를 입력 받는다. + +- 게임 플레이어 숫자의 유효성을 검사한다. + +- 컴퓨터의 수와 플레이어의 수를 비교하여 스트라이크와 볼을 카운트한다. + +- 정답을 맞춘 경우 결과를 출력한다. + +- 게임 종료시 재시작을 버튼 출력한다. \ No newline at end of file