-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscripts.js
186 lines (154 loc) · 4.52 KB
/
scripts.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
'use strict'
const lives = 10;
const playButton = document.querySelector('#play');
const wordPlace = document.querySelector('#word');
const checkButton = document.querySelector('#checkButton');
const letterInput = document.querySelector('#letterInput');
const gameStatus = document.querySelector('#gameStatus');
const livesPlace = document.querySelector('#lives');
const gameArea = document.querySelector('#gameArea');
let chosenWord;
let chosenWordArray;
let hiddenWordArray;
let hiddenWordDisplay = '';
let guessedLetters = [];
let remainingLives;
let wordsToChoose = ['woda', 'kamieniarz', 'spacja', 'babcia'];
wordsToChoose = wordsToChoose.map(word => word.toUpperCase());
// Hangman
const myHangman = document.getElementById("hangman");
const context = myHangman.getContext('2d');
context.beginPath();
context.strokeStyle = "#000";
context.lineWidth = 2;
function startHangman() {
playButton.classList.add("d-none");
gameArea.classList.remove("d-none");
remainingLives = lives;
writeInDocument('', gameStatus);
setChosenWord();
setDocumentState();
resetHangman();
}
function checkLetter() {
let value = letterInput.value.toUpperCase();
checkIfGuessed(value);
setDocumentStateAfterGuess();
checkForWinningsOrLooses();
}
function checkForWinningsOrLooses() {
let wordIsGuessed = !hiddenWordArray.some(letter => letter === '_');
let livesLeft = remainingLives === 0;
if (wordIsGuessed) {
writeInDocument('Wygrałeś!', gameStatus);
}
if (livesLeft) {
writeInDocument('Przegrałeś! Spróbuj jeszcze raz', gameStatus);
}
if (wordIsGuessed || livesLeft) {
playButton.classList.remove("d-none");
}
}
function checkIfGuessed(value) {
if (guessedLetters.some(letter => letter === value)) {
writeInDocument('Było!', gameStatus);
remainingLives--;
animateHangman();
} else if (chosenWordArray.some(v => v === value)) {
chosenWordArray.forEach((letter, index) => {
if (letter === value) {
hiddenWordArray[index] = value;
}
});
writeInDocument('Zgadłeś!', gameStatus);
guessedLetters.push(value);
} else {
writeInDocument('Nie zgadłeś', gameStatus);
remainingLives--;
animateHangman();
}
}
function chooseWord(wordsArray) {
return wordsArray[Math.floor(Math.random() * wordsToChoose.length)];
}
function removeWordFromArray(word, wordsArray) {
let index = wordsArray.indexOf(word);
wordsArray.splice(index, 1);
}
function setChosenWord() {
chosenWord = chooseWord(wordsToChoose);
removeWordFromArray(chosenWord, wordsToChoose);
chosenWordArray = Array.from(chosenWord);
hiddenWordArray = Array.from(chosenWord).map(letter => { return '_' });
guessedLetters = [];
}
function setHiddenWordDisplay() {
hiddenWordDisplay = '';
hiddenWordArray.forEach(letter => {
hiddenWordDisplay = hiddenWordDisplay + ' ' + letter;
});
}
function setDocumentState() {
setHiddenWordDisplay();
writeInDocument(remainingLives, livesPlace);
writeInDocument(hiddenWordDisplay, wordPlace);
}
function setDocumentStateAfterGuess() {
letterInput.value = null;
setDocumentState();
}
function writeInDocument(word, node) {
node.innerHTML = word;
}
function animateHangman() {
let drawMe = remainingLives;
drawArray[drawMe]();
}
function resetHangman() {
context.clearRect(0, 0, myHangman.width, myHangman.height);
context.beginPath();
}
function draw($pathFromx, $pathFromy, $pathTox, $pathToy) {
context.moveTo($pathFromx, $pathFromy);
context.lineTo($pathTox, $pathToy);
context.stroke();
}
const head = function () {
context.beginPath();
context.arc(60, 25, 10, 0, Math.PI * 2, true);
context.stroke();
}
const frame1 = function () {
draw(0, 150, 150, 150);
};
const frame2 = function () {
draw(10, 0, 10, 600);
};
const frame3 = function () {
draw(0, 5, 70, 5);
};
const frame4 = function () {
draw(60, 5, 60, 15);
};
const torso = function () {
draw(60, 36, 60, 70);
};
const rightArm = function () {
draw(60, 46, 100, 50);
};
const leftArm = function () {
draw(60, 46, 20, 50);
};
const rightLeg = function () {
draw(60, 70, 100, 100);
};
const leftLeg = function () {
draw(60, 70, 20, 100);
};
const drawArray = [rightLeg, leftLeg, rightArm, leftArm, torso, head, frame4, frame3, frame2, frame1];
playButton.addEventListener('click', () => {
startHangman();
});
checkButton.addEventListener('click', () => {
checkLetter();
});