⚾️ 숫자 야구 게임
1~9까지의 숫자 중 중복없이 3개 입력해주세요.
올바른 예) 139
@@ -15,7 +15,8 @@
⚾️ 숫자 야구 게임
📄결과
-
+
+
diff --git a/src/components/makeHint.js b/src/components/makeHint.js
index bc4fce4..d6281f0 100644
--- a/src/components/makeHint.js
+++ b/src/components/makeHint.js
@@ -3,10 +3,8 @@ export function calculateHint(playerInput, comRandom) {
let ball = 0
playerInput.forEach((num, index) => {
- if (num === comRandom[index])
- strike++
- else if (comRandom.includes(num))
- ball++
+ if (num === comRandom[index]) strike++
+ else if (comRandom.includes(num)) ball++
});
console.log('스트라이크 개수: ', strike, ', 볼 개수: ', ball)
diff --git a/src/components/printHint.js b/src/components/printHint.js
index ae57c42..628bf0e 100644
--- a/src/components/printHint.js
+++ b/src/components/printHint.js
@@ -5,13 +5,13 @@ export function classifyHint({ strike, ball}) {
resultElement.textContent = '🎉정답을 맞히셨습니다🎉'
}
else if (strike !== 0 && ball === 0) {
- resultElement.textContent = `${strike} 스트라이크`
+ resultElement.textContent = `${strike}스트라이크`
}
else if (ball !== 0 && strike === 0) {
- resultElement.textContent = `${ball} 볼`
+ resultElement.textContent = `${ball}볼`
}
else if (ball != 0 && strike != 0) {
- resultElement.textContent = `${ball} 볼 ${strike} 스트라이크`
+ resultElement.textContent = `${ball}볼 ${strike}스트라이크`
}
else {
resultElement.textContent = '낫싱'
diff --git a/src/main.js b/src/main.js
index c8dd71b..5c3013f 100644
--- a/src/main.js
+++ b/src/main.js
@@ -3,13 +3,31 @@ import { checkInput } from './components/numberInput.js';
import { calculateHint } from './components/makeHint.js';
import { classifyHint } from './components/printHint.js';
+const gameState = {
+ comRandom: null
+}
+// 숫자를 모두 맞히면 게임이 종료 메시지와 재시작 버튼이 나오는 기능
+function setRestartButton() {
+ const restartButton = document.getElementById('restartButton')
+ restartButton.addEventListener('click', () => window.location.reload())
+}
+
+function gameResult() {
+ const userInput = checkInput()
+ const calHint = calculateHint(userInput, gameState.comRandom)
+ classifyHint(calHint)
+
+ const resultElement = document.getElementById('result')
+ if (resultElement.textContent.includes('🎉정답을 맞히셨습니다🎉')) {
+ const restartButton = document.getElementById('restartButton')
+ restartButton.style.display = 'block'
+ }
+}
+
document.addEventListener('DOMContentLoaded', () => {
- const comRandom = threeRandom()
- console.log('상대방(컴퓨터)의 수: ', comRandom)
-
- document.getElementById('checkButton').addEventListener('click', () => {
- const userInput = checkInput()
- const calHint = calculateHint(userInput, comRandom)
- classifyHint(calHint)
- });
+ gameState.comRandom = threeRandom()
+ console.log('상대방(컴퓨터)의 수: ', gameState.comRandom)
+
+ document.getElementById('checkButton').addEventListener('click', gameResult)
+ setRestartButton()
});
\ No newline at end of file