Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Hangman feedback
// ADINA: I noticed you have categories with a name, words, and hint which you have to keep track of by juggling the index of a bunch of different arrays. This is a great opportunity to use a constructor function, which would look like this:
//
// function category( words, hint ) {
// this.words = words;
// this.hint = hint;
// }
// var Films = new category( ["the-matrix", "scarface", "wonder-woman", "finding-dory", "zootopia"], ["Neo", "Cocaine, guns, and a whole lotta swearing", "Feminist superhero", "Ellen's film about a forgetful fish", "Disney's animal cops"] );
// Then you could simply use Films.words to get your array of words, or Films.hint
window.onload = function () {
var alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
't', 'u', 'v', 'w', 'x', 'y', 'z'];
var categories; // Array of topics
var chosenCategory; // Selected category
var getHint ; // Word getHint
var word ; // Selected word
var guess ; // Guess
var guesses = [ ]; // Stored guesses
var lives ; // Lives
var counter ; // Count correct guesses
var space; // Number of spaces in word '-'
// Get elements
var showLives = document.getElementById("mylives");
var showCategory = document.getElementById("categoryName");
var getHint = document.getElementById("hint");
var showClue = document.getElementById("clue");
// create alphabet ul
var buttons = function () {
myButtons = document.getElementById('buttons');
letters = document.createElement('ul');
for (var i = 0; i < alphabet.length; i++) {
letters.id = 'alphabet';
list = document.createElement('li');
list.id = 'letter';
list.innerHTML = alphabet[i];
check();
myButtons.appendChild(letters);
letters.appendChild(list);
}
}
// Select Category
var selectCat = function () {
if (chosenCategory === categories[0]) {
categoryName.innerHTML = "The Chosen Category Is Sad People Topics";
} else if (chosenCategory === categories[1]) {
categoryName.innerHTML = "The Chosen Category Is Films";
} else if (chosenCategory === categories[2]) {
categoryName.innerHTML = "The Chosen Category Is NC Cities";
}
}
// Create guesses ul
result = function () {
wordHolder = document.getElementById('hold');
correct = document.createElement('ul');
for (var i = 0; i < word.length; i++) {
correct.setAttribute('id', 'my-word');
guess = document.createElement('li');
guess.setAttribute('class', 'guess');
if (word[i] === "-") {
guess.innerHTML = "-";
space = 1;
} else {
guess.innerHTML = "_";
}
guesses.push(guess);
wordHolder.appendChild(correct);
correct.appendChild(guess);
}
}
// Show lives
comments = function () {
showLives.innerHTML = "You have " + lives + " incorrect guesses left.";
if (lives < 1) {
showLives.innerHTML = "Game Over";
}
for (var i = 0; i < guesses.length; i++) {
if (counter + space === guesses.length) {
showLives.innerHTML = "You Win!";
}
}
}
// Animate man
var animate = function () {
var drawMe = lives ;
// ADINA: This is a syntax error. Don't need the parentheses
drawArray[drawMe]();
}
// Hangman
canvas = function(){
myStickman = document.getElementById("stickman");
context = myStickman.getContext('2d');
context.beginPath();
context.strokeStyle = "#fff";
context.lineWidth = 2;
};
head = function(){
myStickman = document.getElementById("stickman");
context = myStickman.getContext('2d');
context.beginPath();
context.arc(60, 25, 10, 0, Math.PI*2, true);
context.stroke();
}
draw = function($pathFromx, $pathFromy, $pathTox, $pathToy) {
context.moveTo($pathFromx, $pathFromy);
context.lineTo($pathTox, $pathToy);
context.stroke();
}
frame1 = function() {
draw (0, 150, 150, 150);
};
frame2 = function() {
draw (10, 0, 10, 600);
};
frame3 = function() {
draw (0, 5, 70, 5);
};
frame4 = function() {
draw (60, 5, 60, 15);
};
torso = function() {
draw (60, 36, 60, 70);
};
rightArm = function() {
draw (60, 46, 100, 50);
};
leftArm = function() {
draw (60, 46, 20, 50);
};
rightLeg = function() {
draw (60, 70, 100, 100);
};
leftLeg = function() {
draw (60, 70, 20, 100);
};
drawArray = [rightLeg, leftLeg, rightArm, leftArm, torso, head, frame4, frame3, frame2, frame1];
// OnClick Function
check = function () {
list.onclick = function () {
var guess = (this.innerHTML);
this.setAttribute("class", "active");
this.onclick = null;
for (var i = 0; i < word.length; i++) {
if (word[i] === guess) {
guesses[i].innerHTML = guess;
counter += 1;
}
}
var j = (word.indexOf(guess));
if (j === -1) {
lives -= 1;
comments();
animate();
} else {
comments();
}
}
}
// Play
play = function () {
categories = [
["depression", "covfefe", "apathy", "bad-grades", "ramen-noodles", "death", "nuclear-winter"],
["the-matrix", "scarface", "wonder-woman", "finding-dory", "zootopia"],
["raleigh", "charlotte", "chapel-hill", "winston-salem", "asheboro"]
];
chosenCategory = categories[Math.floor(Math.random() * categories.length)];
word = chosenCategory[Math.floor(Math.random() * chosenCategory.length)];
word = word.replace(/\s/g, "-");
console.log(word);
buttons();
guesses = [ ];
lives = 10;
counter = 0;
space = 0;
result();
comments();
selectCat();
canvas();
}
play();
// Hint
hint.onclick = function() {
hints = [
["It's the sad people life state", "Donald Trump Meme", "An unhealthy way to deal with stress", "What I'll probably recieve on this assignment", "The food of choice for a poor sad person", "What we're all inevitably marching towards", "What'll probably happen with Trump in office"],
["Neo", "Cocaine, guns, and a whole lotta swearing", "Feminist superhero", "Ellen's film about a forgetful fish", "Disney's animal cops"],
["The state capital", "The largest city in the state", "Where we go to class", "Has hosted the demon deacons since 1956", "Host city of a large zoo"]
];
var categoryIndex = categories.indexOf(chosenCategory);
var hintIndex = chosenCategory.indexOf(word);
showClue.innerHTML = "Clue: - " + hints [categoryIndex][hintIndex];
};
// Reset
document.getElementById('reset').onclick = function() {
correct.parentNode.removeChild(correct);
letters.parentNode.removeChild(letters);
showClue.innerHTML = "";
context.clearRect(0, 0, 400, 400);
play();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment