diff --git a/index.html b/index.html index 5cfc357..cf155bb 100644 --- a/index.html +++ b/index.html @@ -12,10 +12,9 @@

H A N G M A N

-

The word to be guessed goes here

+

Guess left: 5

-
- +
diff --git a/src/index.js b/src/index.js index fea3a99..d864e47 100644 --- a/src/index.js +++ b/src/index.js @@ -1,44 +1,28 @@ -// 1. 알파벳 버튼을 누른다. -// 2-1. 단어에 있으면 위치에 들어가고 목숨이 줄어들지 않고 그림이 바뀌지 않는다. -// 2-2. 단어에 없으면 목숨이 줄어들고 그림이 그림이 바뀐다. -// 3. 그 단어가 비활성화 된다. -// 4. 계속 반복된다. -// 5-1. 답을 맞춘 경우 승리 메세지가 뜬다. -// 5-2. 답을 틀린 경우 패매 메시지가 뜬다. -// 6. 위의 메세지와 함께 다시 시작할건지 묻는 버튼이 생긴다. -let programming_languages = [ - "python", - "javascript", - "mongodb", - "json", - "java", - "html", - "css", - "c", - "csharp", - "golang", - "kotlin", - "php", - "sql", - "ruby", +let fruits = [ + "apple", + "banana", + "grape", + "pear", + "strawberry", + "lemon", + "melon", + "watermelon", + "tomato", + "orange", + "kiwi", + "pineapple", ]; let answer = ""; let life = 5; let guessed = []; let wordStatus = null; - -function $(selector) { - return document.querySelector(selector); -} - -function addClass(element, className) { - element.classList.add(className); -} - -function createElement(tagName) { - return document.createElement(tagName); -} +/* const aaaa = { + answer : "", + life: 5, + guessed: [], + wordStatus: null, +} */ // //랜덤 단어 생성 // const createRandomWords = async () => { @@ -53,73 +37,49 @@ function createElement(tagName) { // // return word; // }; +const gameButton = document.querySelector(".game-button"); +const mistakes = document.querySelector("#mistakes"); + +//랜덤으로 단어를 생성해주는 함수 function randomWord() { - answer = - programming_languages[ - Math.floor(Math.random() * programming_languages.length) - ]; + answer = fruits[Math.floor(Math.random() * fruits.length)]; } -// function generateButtons() { -// let buttonsHTML = "abcdefghijklmnopqrstuvwxyz" -// .split("") -// .map( -// (letter) => -// ` -// -// ` -// ) -// .join(""); - -// document.getElementById("keyboard").innerHTML = buttonsHTML; -// } - +//알파벳을 맞추면 보여주고 틀리면 life를 깎는 함수 function handleGuess(chosenLetter) { guessed.indexOf(chosenLetter) === -1 ? guessed.push(chosenLetter) : null; - document.getElementById(chosenLetter).setAttribute("disabled", true); + console.log(guessed); if (answer.indexOf(chosenLetter) >= 0) { guessedWord(); - checkIfGameWon(); + checkGameWin(); } else if (answer.indexOf(chosenLetter) === -1) { - //mistakes++; - //updateMistakes(); - checkIfGameLost(); - //updateHangmanPicture(); + life--; + checkGameLose(); + mistakes.innerHTML = `${life}`; } } -// function updateHangmanPicture() { -// document.getElementById("hangmanPic").src = "./images/" + mistakes + ".jpg"; -// } +//초기화 하는 함수 +//만들기 -function checkIfGameWon() { +//정답을 맞췄을 때 실행되는 함수 +function checkGameWin() { if (wordStatus === answer) { - document.getElementById("keyboard").innerHTML = "You Won!!!"; + alert("You Win!!"); } } -// function checkIfGameLost() { -// if (mistakes === maxWrong) { -// document.getElementById("wordSpotlight").innerHTML = -// "The answer was: " + answer; -// document.getElementById("keyboard").innerHTML = "You Lost!!!"; -// } -// } +//목숨이 0이 됐을 때 실행되는 함수 +function checkGameLose() { + if (life === 0) { + alert("You Lose!!"); + } +} +//답을 '_'로 바꿔주는 함수 function guessedWord() { + console.log(answer); wordStatus = answer .split("") .map((letter) => (guessed.indexOf(letter) >= 0 ? letter : " _ ")) @@ -128,25 +88,25 @@ function guessedWord() { document.getElementById("wordSpotlight").innerHTML = wordStatus; } -// function updateMistakes() { -// document.getElementById("mistakes").innerHTML = mistakes; -// } - +//리셋하는 함수 function reset() { - //mistakes = 0; + life = 5; guessed = []; - //document.getElementById("hangmanPic").src = "./images/0.jpg"; randomWord(); guessedWord(); - //updateMistakes(); - //generateButtons(); } -//document.getElementById("maxWrong").innerHTML = maxWrong; +//이벤트를 넘겨주는 함수 +function handleKeyDown(event) { + console.log(event); + handleGuess(event.key); +} + +gameButton.addEventListener("click", reset); +window.addEventListener("keydown", handleKeyDown); randomWord(); -//generateButtons(); guessedWord(); // //1~2까지 랜덤 숫자 생성 // function randomNumber() { diff --git a/src/style.css b/src/style.css index 07edee9..544bd3a 100644 --- a/src/style.css +++ b/src/style.css @@ -33,12 +33,14 @@ h1 { } #wordSpotlight { - display: relative; + display: flex; margin: 80px 0 40px 0; background-color: #363239; height: 100px; width: 800px; border-radius: 20px; + justify-content: center; + align-items: center; } p {