-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
144 lines (128 loc) · 4.25 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* global $ */
/* CREATE KEYBOARD */
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']
function createAlph () {
for (var i = 0; i < alphabet.length; i++) {
$('<div></div>').attr({'class': 'alph', 'id': alphabet[i]}).text(alphabet[i]).appendTo('.alph-wrapper')
}
}
createAlph()
var moviesList = ['the sound of music', 'braveheart', 'jaws', 'titanic', 'casablanca', 'the wizard of oz']
var songsList = ['vogue', 'thriller', 'i will always love you', 'we will rock you', 'i believe i can fly']
var booksList = ['crime and punishment', 'the catcher in the rye', 'the great gatsby', 'the davinci code', 'war and peace']
var cat = $('.cat')
var catList
var wordPlay = ' '
var wordArray = null
var wordCanvas = $('.word')
cat.on('click', setWord)
cat.on('click', setPlaceholder)
cat.on('click', startGame)
/* SET WORD BASED OFF CATEGORY CHOSEN */
function setWord () {
wordCanvas.html(' ')
cat.css('text-decoration', 'none')
$(this).css('text-decoration', 'underline')
catList = $(this).attr('id')
if (catList === 'movies') {
wordPlay = moviesList[Math.floor(Math.random() * moviesList.length)]
} else if (catList === 'songs') {
wordPlay = songsList[Math.floor(Math.random() * songsList.length)]
} else if (catList === 'books') {
wordPlay = booksList[Math.floor(Math.random() * booksList.length)]
}
}
/* SET LINES FOR WORD LETTERS */
function setPlaceholder () {
wordArray = wordPlay.split('')
wordCanvas.html('')
for (let i = 0; i < wordArray.length; i++) {
if (wordArray[i] === ' ') {
$('.word').append(' ')
} else $('.word').append('_')
}
}
/* GAME FUNCTION */
function startGame () {
var alph = $('.alph')
var alphPick
var allPicks = []
var rightPicks = []
var wrongPicks = []
var showRight
var displayWord
var getAnswer = $('#answer')
var endHideElem = $('h2, .word, ul, footer, h1')
var result = $('#result')
var playAgain = $('#play-again')
alph.on('click', alphSelect)
alph.on('click', alphUsed)
alph.on('click', addToList)
alph.on('click', checkPick)
alph.on('click', reveal)
alph.on('click', showMan)
getAnswer.on('click', showAnswer)
getAnswer.on('click', function () { getAnswer.css({'opacity': '0', 'cursor': 'default'}) })
/* STORE GUESSES AND MARK USED */
function alphUsed () { $(this).css({'opacity': '0.2', 'cursor': 'auto'}) }
function alphSelect () { alphPick = $(this).attr('id') }
function addToList () { allPicks.push(alphPick) }
/* CHECK AND STORE IF LETTER IS RIGHT OR WRONG */
function checkPick () {
if (wordArray.includes(alphPick)) {
rightPicks.push(alphPick)
} else {
wrongPicks.push(alphPick)
$('.stand-base').append(alphPick)
}
}
/* SHOW RIGHT LETTERS ON BOARD */
function reveal () {
showRight = wordArray.map((letter) => {
if (allPicks.includes(letter)) {
return letter
} else if (letter === ' ') {
return ' '
} else return '_'
})
displayWord = showRight.join('')
wordCanvas.text(displayWord)
/* SHOW WIN WHEN ALL LETTERS GUESSED */
if (displayWord === wordPlay) {
endHideElem.css('opacity', '0.5')
$('aside').css('opacity', '0.5')
result.text('YOU WIN!')
playAgain.text('play again')
}
}
/* BUILD HANGMAN WITH WRONG PICKS */
function showMan () {
if (wrongPicks.length === 1) {
$('.head').removeClass('hidden')
} else if (wrongPicks.length === 2) {
$('.torso').removeClass('hidden')
} else if (wrongPicks.length === 3) {
$('.left-arm').removeClass('hidden')
} else if (wrongPicks.length === 4) {
$('.right-arm').removeClass('hidden')
} else if (wrongPicks.length === 5) {
$('.left-leg').removeClass('hidden')
} else if (wrongPicks.length === 6) {
$('.right-leg').removeClass('hidden')
getAnswer.text('get answer')
result.text('YOU LOSE')
endHideElem.css('opacity', '0.5')
$('.eyes, .frown').animate({'opacity': '1'}, {'duration': 900})
playAgain.text('play again')
}
}
/* PROVIDE SHOW ANSWER OPTION ON LOSE */
function showAnswer () {
wordCanvas.text(wordPlay)
}
}
/* PLAY GAME AGAIN */
$('#play-again').on('click', replay)
function replay () {
location.reload()
}