diff --git a/README.md b/README.md index 1e7eb35..aa17f50 100644 --- a/README.md +++ b/README.md @@ -1,26 +1,34 @@ # Create a hangman game +Team: +1)Ahmed Mohammed Hassan +2)Mariam +3)Hasar Wshar Ali + ## Overview + Create a hangman game!
Example of what we want to accomplish [here](https://codepen.io/cathydutton/pen/ldazc). You can use it to know exactly what the game is about, but do not copy it. Use your own way. Be as creative as you can! ![Example of hangman game](images/example.jpg) + ### Requirements 1. HTML: - 1. Add buttons that correspond to each letter from a to z. - 2. Add a section that will hold the blanks that are going to be equal to the number of characters for the current word. - 3. Add a div to show the man who's going to be hanged if you lose. - 4. Add a counter from 10 that will decrease by 1 with every wrong guess. - 4. Add a button to play again. + + 1. Add buttons that correspond to each letter from a to z. + 2. Add a section that will hold the blanks that are going to be equal to the number of characters for the current word. + 3. Add a div to show the man who's going to be hanged if you lose. + 4. Add a counter from 10 that will decrease by 1 with every wrong guess. + 5. Add a button to play again. 2. JS: - 1. At the beginning use fetch API to retrieve a random word from this api https://random-word-api.herokuapp.com/word?number=1. Make the blanks equal the number of letters in the random word. - 2. Everytime the user clicks on one of the letters the following should happen: - 1. The random word is search through to find if it contains the clicked letter, if the clicked letter is part of the random word's letters then it gets shown up instead of the space, if not, then the lives counter is decreased by one. In some rare cases a clicked letter corresponds to two letters in the generated word, if that happens then show both words. - 2. The button becomes disabled and should only work once. - 3. If the lives counter reaches 0 then the game is over and the hangman should be HANGED! πŸ‘¨β€πŸ¦±πŸ”ͺ - 4. Clicking on the play again button should do the whole process again. + 1. At the beginning use fetch API to retrieve a random word from this api https://random-word-api.herokuapp.com/word?number=1. Make the blanks equal the number of letters in the random word. + 2. Everytime the user clicks on one of the letters the following should happen: + 1. The random word is search through to find if it contains the clicked letter, if the clicked letter is part of the random word's letters then it gets shown up instead of the space, if not, then the lives counter is decreased by one. In some rare cases a clicked letter corresponds to two letters in the generated word, if that happens then show both words. + 2. The button becomes disabled and should only work once. + 3. If the lives counter reaches 0 then the game is over and the hangman should be HANGED! πŸ‘¨β€πŸ¦±πŸ”ͺ + 4. Clicking on the play again button should do the whole process again. ## Coding Phase @@ -81,11 +89,11 @@ Just as in the **subject**, use the imperative, present tense: β€œchange” not - Don't leave any logs inside the code. - All variables should be `const` except for specific cases where you will need to use `let` - Variables should use camelCase naming convention -- CSS classes should follow BEM naming convention. *You can find more about it [here](http://getbem.com/naming/).* +- CSS classes should follow BEM naming convention. _You can find more about it [here](http://getbem.com/naming/)._ - Leave only one empty line between CSS classes. This also goes for different purpose code blocks (like `imports` and variables under it). - Make sure your naming is right and not confusing i.e. the `navbar` shouldn't be named `header` or when you fetch `movies` your function should return `movies` not `data` - Make sure you clean your imported modules or files that you don't use before committing. The same goes for any variable, function or piece of code not used. -- Don't repeat yourself (DRY). Make sure the code you write is reusable and reduce repetition of information of all kinds. For example, don't write two functions that do the same or almost the same job. *Read more about DRY [here](https://en.wikipedia.org/wiki/Don't_repeat_yourself).* +- Don't repeat yourself (DRY). Make sure the code you write is reusable and reduce repetition of information of all kinds. For example, don't write two functions that do the same or almost the same job. _Read more about DRY [here](https://en.wikipedia.org/wiki/Don't_repeat_yourself)._ If you have issues, google them.
@@ -95,6 +103,5 @@ If you still have issues, talk to your instructors.

- -NOTE: Be creative with the game's mechanics and design!!! -Have fun, and Good luck :D +NOTE: Be creative with the game's mechanics and design!!! +Have fun, and Good luck :D diff --git a/images/1.png b/images/1.png new file mode 100644 index 0000000..6d60f9b Binary files /dev/null and b/images/1.png differ diff --git a/images/10.png b/images/10.png new file mode 100644 index 0000000..ef79a51 Binary files /dev/null and b/images/10.png differ diff --git a/images/2.png b/images/2.png new file mode 100644 index 0000000..327642d Binary files /dev/null and b/images/2.png differ diff --git a/images/3.png b/images/3.png new file mode 100644 index 0000000..e9141c1 Binary files /dev/null and b/images/3.png differ diff --git a/images/4.png b/images/4.png new file mode 100644 index 0000000..fea210d Binary files /dev/null and b/images/4.png differ diff --git a/images/5.png b/images/5.png new file mode 100644 index 0000000..5fa0437 Binary files /dev/null and b/images/5.png differ diff --git a/images/6.png b/images/6.png new file mode 100644 index 0000000..95df6a1 Binary files /dev/null and b/images/6.png differ diff --git a/images/7.png b/images/7.png new file mode 100644 index 0000000..47af828 Binary files /dev/null and b/images/7.png differ diff --git a/images/8.png b/images/8.png new file mode 100644 index 0000000..86598f0 Binary files /dev/null and b/images/8.png differ diff --git a/images/9.png b/images/9.png new file mode 100644 index 0000000..fc49470 Binary files /dev/null and b/images/9.png differ diff --git a/index.html b/index.html index feb3555..5214e5a 100644 --- a/index.html +++ b/index.html @@ -1,14 +1,42 @@ + - - + + Document - + + - +

HANGMAN

+ +

VANILLA JAVASCRIPT HANGMAN GAME

+ +

+ ! You can use 2 helps but each will count as a Mistake and cost you An Attempt ! +
Use the alphabet below to guess the word in less than 10 attempts:

+ +
+ +
+ +
Game Over!
+ +
The Answer was:
+ +
Mistakes: 0 || Found Letters: 0
+ +
+ +
+ +
+ + +
+ \ No newline at end of file diff --git a/scripts/script.js b/scripts/script.js index e69de29..e3d8c67 100644 --- a/scripts/script.js +++ b/scripts/script.js @@ -0,0 +1,172 @@ +String.prototype.replaceAt = function (index, replacement) { + return ( + this.substr(0, index) + + replacement + + this.substr(index + replacement.length) + ); +}; + +const alpha = Array.from(Array(26)).map((e, i) => i + 65); +const alphabet = alpha.map((x) => String.fromCharCode(x).toLowerCase()); + +function randomInteger(min, max) { + return Math.floor(Math.random() * (max - min + 1)) + min; +} + +const letters = document.getElementById("letters"); +const blanks = document.getElementById("blanks"); +const gameover = document.getElementById("gameover"); +const answer = document.getElementById("answer"); +const counters = document.getElementById("counters"); +const hang = document.getElementById("hang"); +const replay = document.getElementById("replay"); +const help = document.getElementById("help"); + +alphabet.forEach((letter, i) => { + + const newButton = document.createElement("button"); + newButton.innerHTML = letter; + newButton.id = letter; + newButton.setAttribute("class", "alphaButton"); + newButton.style.margin = "0px 5px"; + newButton.style.width = "40px"; + newButton.style.height = "40px"; + letters.append(newButton); + + if (i == 12) { + const breaking = document.createElement("br"); + const breaking2 = document.createElement("br"); + letters.append(breaking); + letters.append(breaking2); + } + +}); + +fetch("https://random-word-api.herokuapp.com/word?number=1") + .then(response => response.json()) + .then(json => { + + const word = json[0]; + for (let i = 0; i < word.length; i++) { + blanks.innerHTML += "_ "; + } + + let rightCount = 0; + let falseCount = 0; + let loser = false; + const checkLetter = (e) => { + + const clicked = e.target.innerHTML; + if (e.target.nodeName === "BUTTON") { + + let rightLetter = false; + if (loser == false) { + if (word.includes(clicked)) { + rightLetter = true; + for (let i = 0; i < word.length * 2; i += 2) { + if (word[i / 2] == clicked) { + blanks.innerHTML = blanks.innerHTML.replaceAt(i, clicked); + rightCount++; + } + } + } + } + + if (falseCount < 10) { + e.target.setAttribute("disabled", "true"); + if (rightLetter == false) { + falseCount++; + hang.setAttribute("src", `./images/${falseCount}.png`); + } + counters.innerHTML = + "False: " + falseCount + " || Found Letters: " + rightCount; + } + + if (falseCount == 10) { + gameover.style.display = "block"; + answer.innerHTML += '"' + word + '"'; + answer.style.display = "block"; + loser = true; + falseCount++; + } + + if (loser == false) { + if (rightCount == word.length) { + gameover.innerHTML = "Congratulations πŸŽ‰!"; + gameover.style.color = "darkgreen"; + gameover.style.display = "block"; + answer.innerHTML += '"' + word + '"'; + answer.style.display = "block"; + falseCount = 11; + loser = true; + } + } + + } + + }; + letters.addEventListener("click", checkLetter); + + replay.addEventListener("click", () => { + location.reload(); + }); + + let helpCount = 0; + const helper = () => { + + if (falseCount < 10) { + + helpCount++; + let clicked = word[randomInteger(0, word.length - 1)]; + while (blanks.innerHTML.includes(clicked)) { + clicked = word[randomInteger(0, word.length - 1)]; + } + const buttonDisable = document.getElementById(clicked); + + if (loser == false) { + rightLetter = true; + for (let i = 0; i < word.length * 2; i += 2) { + if (word[i / 2] == clicked) { + blanks.innerHTML = blanks.innerHTML.replaceAt(i, clicked); + rightCount++; + } + } + } + + falseCount++; + hang.setAttribute("src", `./images/${falseCount}.png`); + counters.innerHTML = + "Mistakes: " + falseCount + " || Found Letters: " + rightCount; + + if (falseCount < 10) buttonDisable.setAttribute("disabled", "true"); + + if (falseCount == 10) { + gameover.style.display = "block"; + answer.innerHTML += '"' + word + '"'; + answer.style.display = "block"; + loser = true; + falseCount++; + } + + if (loser == false) { + if (rightCount == word.length) { + gameover.innerHTML = "Congratulations πŸŽ‰!"; + gameover.style.color = "darkgreen"; + gameover.style.display = "block"; + answer.innerHTML += '"' + word + '"'; + answer.style.display = "block"; + falseCount = 11; + loser = true; + } + } + + if (helpCount == 2) { + help.style.display = "none"; + } + + } + + }; + help.addEventListener("click", helper); + + }); \ No newline at end of file diff --git a/styles/style.css b/styles/style.css index e69de29..29a98c5 100644 --- a/styles/style.css +++ b/styles/style.css @@ -0,0 +1,117 @@ +body { + text-align: center; + background-color: #C1D72D; + font-family: "HelveticaNeue-Light", "Helvetica Neue Light", + "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif; +} + +h1, +h2 { + font-family: 'Roboto', sans-serif; + color: white; + font-weight: bold; + margin: 5px 0; +} + +h3 { + color: white; + font-weight: normal; +} + +#letters { + margin: 20px 0px; +} + +.alphaButton { + font-size: larger; + border-radius: 8px; + background-color: white; + color: #C1D72D; + border: 1px solid #C1D72D; +} + +.alphaButton:hover { + cursor: pointer; + border: 1px solid white; + background-color: #C1D72D; + color: white; +} + +.alphaButton:disabled { + opacity: 0.5; + cursor: default; + background-color: white; + color: #C1D72D; + border: 1px solid #C1D72D; +} + +.alphaButton:disabled:hover { + opacity: 0.5; +} + +#blanks { + font-size: xx-large; + margin-top: 30px; + color: white; +} + +#gameover { + margin-top: 10px; + display: none; + color: red; + font-size: x-large; +} + +#answer { + display: none; + color: darkgreen; + font-size: x-large; +} + +#counters { + margin: 10px; + font-size: large; + color: white; +} + +#hangman { + width: 350px; + height: 350px; + margin: 20px auto; + border: 2px dashed white; + background-color: inherit; +} + +#hang { + width: 100%; +} + +.buttons { + display: flex; + justify-content: center; + align-items: center; +} + +#replay, +#help { + display: inline-block; + width: 150px; + height: 80px; + margin: 10px; + font-size: larger; + background-color: #C1D72D; + border: 1px solid white; + border-radius: 10px; + color: white; +} + +#replay:hover { + background-color: white; + color: #C1D72D; +} + +#help:hover { + background-color: #F4623A; + color: #C1D72D; + border: #C1D72D; +} \ No newline at end of file