Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 11, 2023 06:16
Show Gist options
  • Save code-boxx/324b8606b32aa7195375c7dfb19701ee to your computer and use it in GitHub Desktop.
Save code-boxx/324b8606b32aa7195375c7dfb19701ee to your computer and use it in GitHub Desktop.
Javascript Hangman

JAVASCRIPT HANGMAN

https://code-boxx.com/simple-hangman-game-javascript/

IMAGES

hangman

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

/* (A) GAME WRAPPER */
#hangman-wrap {
display: flex;
max-width: 600px;
margin: 0 auto;
background: #ffdfbe;
}
#hangman-left, #hangman-right { padding: 10px; }
#hangman-left { width: 30%; text-align: center; }
#hangman-right { width: 70%; }
/* (B) COMMON INTERFACE */
#hangman-wrap, #hangman-wrap input {
font-family: Impact, sans-serif;
font-size: 20px;
}
#hangman-wrap input {
padding: 5px 20px;
margin: 2px;
background: #e46810;
color: #fff;
border: 0;
}
#hangman-wrap input[type=button]:disabled {
background: #7b7b7b;
color: #ccc;
}
/* (C) LEFT - HANGMAN IMAGE */
#hangman-img {
max-width: 100%;
transition: opacity 0.3s;
opacity: 0;
}
/* (D) RIGHT - CURRENT WORD(S) */
#hangman-words span {
font-size: 30px;
display: inline-block;
margin: 5px;
}
/* (E) RIGHT - AVAILABLE CHARACTERS */
#hangman-char { margin: 10px 0; }
<!DOCTYPE html>
<html>
<head>
<title>Simple Hangman Javascript</title>
<meta charset="utf-8">
<link href="hangman.css" rel="stylesheet">
<script src="hangman.js"></script>
</head>
<body>
<div id="hangman-wrap">
<!-- (A) LEFT : HANGMAN & LIVES -->
<div id="hangman-left">
<img id="hangman-img" src="hangman.png">
<div id="hangman-lives-w">
REMAIN: <span id="hangman-lives"></span>
</div>
</div>
<!-- (B) RIGHT : GAME INTERFACE -->
<div id="hangman-right">
<!-- (B1) CURRENT WORD(S) -->
<div id="hangman-words"></div>
<!-- (B2) AVAILABLE CHARACTERS -->
<div id="hangman-char"></div>
<!-- (B3) RESET -->
<input type="button" value="Reset" id="hangman-reset" disabled>
</div>
</div>
</body>
</html>
var hangman = {
// (A) GAME SETTINGS
// the number of "lives"
guesses : 5,
// available words for guessing
dictionary : ["impress", "incapable", "satisfaction", "develop", "determine"],
// (B) FLAGS
word : null, // current chosen word
wordlen : 0, // word length
rights : 0, // current number of correct words
wrongs : 0, // current number of wrong guesses
// (C) HTML ELEMENTS
hImg : null, // hangman iamge
hWord : null, // current word
hChar : null, // available characters
hLives : null, // lives left
// (D) INIT
init : () => {
// (D1) GET HTML ELEMENTS
hangman.hImg = document.getElementById("hangman-img");
hangman.hWord = document.getElementById("hangman-words");
hangman.hChar = document.getElementById("hangman-char");
hangman.hLives = document.getElementById("hangman-lives");
// (D2) GENERATE AVAILABLE CHARACTERS (A-Z)
for (var i=65; i<91; i++) {
let charnow = document.createElement("input");
charnow.type = "button";
charnow.value = String.fromCharCode(i);
charnow.disabled = true;
charnow.onclick = () => hangman.check(charnow);
hangman.hChar.appendChild(charnow);
}
// (D3) START GAME
let rst = document.getElementById("hangman-reset");
rst.onclick = hangman.reset;
rst.disabled = false;
hangman.reset();
},
// (E) HELPER - TOGGLE ENABLE/DISABLE ALL AVAILABLE CHARACTERS
toggle : disable => {
let all = hangman.hChar.getElementsByTagName("input");
for (var i of all) { i.disabled = disable; }
},
// (F) START/RESET THE GAME
reset : () => {
// (F1) RESET STATS
hangman.rights = 0;
hangman.wrongs = 0;
hangman.hLives.innerHTML = hangman.guesses;
hangman.hImg.style.opacity = 0;
// (F2) CHOOSE A RANDOM WORD FROM THE DICTIONARY
hangman.word = hangman.dictionary[Math.floor(Math.random() * Math.floor(hangman.dictionary.length))];
hangman.word = hangman.word.toUpperCase();
hangman.wordlen = hangman.word.length;
// CHEAT!
// console.log(hangman.word);
// (F3) DRAW THE BLANKS
hangman.hWord.innerHTML = "";
for (var i=0; i<hangman.word.length; i++) {
var charnow = document.createElement("span");
charnow.innerHTML = "_";
charnow.id = "hangword-" + i;
hangman.hWord.appendChild(charnow);
}
// (F4) ENABLE ALL CHARACTERS
hangman.toggle(false);
},
// (G) CHECK IF SELECTED CHARACTER IS IN THE CURRENT WORD
check : char => {
// (G1) CHECK FOR HITS
var index = 0, hits = [];
while (index >= 0) {
index = hangman.word.indexOf(char.value, index);
if (index == -1) { break; }
else {
hits.push(index);
index++;
}
}
// (G2) CORRECT - SHOW THE HITS
if (hits.length > 0) {
// (G2-1) REVEAL WORDS
for (var hit of hits) {
document.getElementById("hangword-" + hit).innerHTML = char.value;
}
// (G2-2) ALL HIT - WIN!
hangman.rights += hits.length;
if (hangman.rights == hangman.wordlen) {
hangman.toggle(true);
alert("YOU WIN!");
}
}
// (G3) WRONG - MINUS LIFE & SHOW HANGMAN
else {
// (G3-1) UPDATE HANGMAN
hangman.wrongs++;
var livesleft = hangman.guesses - hangman.wrongs;
hangman.hLives.innerHTML = livesleft;
hangman.hImg.style.opacity = (1 - (livesleft/hangman.guesses)).toFixed(2);
// (G3-2) RUN OUT OF GUESSES - LOSE!
if (hangman.wrongs == hangman.guesses) {
hangman.toggle(true);
alert("YOU LOSE!");
}
}
// (G4) DISABLE SELECTED CHARACTER
char.disabled = true;
}
};
window.addEventListener("DOMContentLoaded", hangman.init);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment