-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
hangman.js
230 lines (199 loc) · 6.69 KB
/
hangman.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* GCompris - hangman.js
*
* SPDX-FileCopyrightText: 2015 Rajdeep Kaur <rajdeep1994@gmail.com>
*
* Authors:
* Bruno Coudoin <bruno.coudoin@gcompris.net> (GTK+ version)
* Rajdeep kaur <rajdeep51994@gmail.com> (Qt Quick port)
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
.pragma library
.import QtQuick 2.12 as Quick
.import GCompris 1.0 as GCompris
.import "qrc:/gcompris/src/core/core.js" as Core
.import "qrc:/gcompris/src/activities/lang/lang_api.js" as Lang
var currentLevel
var currentSubLevel
var maxLevel
var maxSubLevel
var items
var currentWord
var sp ="_ "
var dataset = null
var lessons
var wordList
var subLevelsLeft
var alreadyTypedLetters
// js strings are immutable, can't replace letter like that...
// http://stackoverflow.com/questions/1431094/how-do-i-replace-a-character-at-a-particular-index-in-javascript
String.prototype.replaceAt = function(index, character) {
return this.substr(0, index) + character + this.substr(index+character.length);
}
function start(items_) {
items = items_
currentLevel = 0;
currentSubLevel = 0;
items.remainingLife = 6;
var locale = GCompris.ApplicationInfo.getVoicesLocale(items.locale)
var resourceUrl = "qrc:/gcompris/src/activities/lang/resource/"
// register the voices for the locale
GCompris.DownloadManager.updateResource(
GCompris.DownloadManager.getVoicesResourceForLocale(locale))
var data = Lang.loadDataset(items.parser, resourceUrl, locale);
dataset = data["dataset"];
items.background.englishFallback = data["englishFallback"];
lessons = Lang.getAllLessons(dataset)
maxLevel = lessons.length
initLevel();
}
function stop() {
items.winTimer.stop();
}
function initLevel() {
// If clicking on previous/next level after winning, we prevent to skip the next word
if(items.winTimer.running) {
items.winTimer.stop();
}
items.bar.level = currentLevel + 1;
var currentLesson = lessons[currentLevel];
wordList = Lang.getLessonWords(dataset, currentLesson);
Core.shuffle(wordList);
maxSubLevel = wordList.length;
items.score.numberOfSubLevels = maxSubLevel;
items.score.visible = true;
subLevelsLeft = []
for(var i in wordList)
subLevelsLeft.push(i)
initSubLevel();
//to set the layout...populate
var letters = new Array();
for (var i = 0; i < wordList.length; i++) {
var word = wordList[i].translatedTxt;
for (var j = 0; j < word.length; j++) {
var letter = word.charAt(j).toLocaleLowerCase();
if (letters.indexOf(letter) === -1)
letters.push(letter);
}
}
letters = GCompris.ApplicationInfo.localeSort(letters, items.locale);
// Remove space character if in list
var indexOfSpace = letters.indexOf(' ')
if(indexOfSpace > -1)
letters.splice(indexOfSpace, 1)
// generate layout from letter map
var layout = new Array();
var row = 0;
var offset = 0;
while (offset < letters.length-1) {
var cols = letters.length <= 10
? letters.length : (Math.ceil((letters.length-offset) / (3 - row)));
layout[row] = new Array();
for (var j = 0; j < cols; j++)
layout[row][j] = { label: letters[j+offset] };
offset += j;
row++;
}
items.keyboard.layout = layout;
}
function processKeyPress(text) {
if(items.remainingLife === 0 || items.goodIcon.visible) {
return
}
text = text.toLocaleLowerCase()
// Check if the character has already been typed
if(alreadyTypedLetters.indexOf(text) !== -1) {
// Character already typed, do nothing
return;
}
// Add the character to the already typed characters
alreadyTypedLetters.push(text);
// add the typed character in the "Attempted characters" text field
createAttemptedText()
// Get all the indices of this letter in the word
var indices = [];
for(var i = 0 ; i < currentWord.length ; i ++) {
if (currentWord[i].toLocaleLowerCase() === text) {
indices.push(i);
}
}
if(indices.length == 0) {
// The letter is not in the word to find
// If no more life, we display the good word and show the bonus
if(--items.remainingLife == 0) {
items.hidden.text = items.goodWord.translatedTxt;
items.playWord()
items.bonus.bad("lion");
return;
}
} else {
// For all the indices found, we replace the "_" by the letter
for(var index = 0 ; index < indices.length ; index ++) {
// Characters in the word displayed are separated by spaces, this is why we do 2*index
items.hidden.text = items.hidden.text.replaceAt(2*indices[index],
currentWord[indices[index]]);
}
}
// If no more '_' in the word to find, we have found all letters, show bonus
if(items.hidden.text.indexOf("_") === -1) {
items.maskThreshold = 0;
items.playWord();
items.hidden.text = items.goodWord.translatedTxt;
items.goodIcon.visible = true
items.winTimer.start();
}
}
function nextLevel() {
if(maxLevel <= ++currentLevel) {
currentLevel = 0
}
currentSubLevel = 0;
initLevel();
}
function previousLevel() {
if(--currentLevel < 0) {
currentLevel = maxLevel - 1
}
currentSubLevel = 0;
initLevel();
}
function initSubLevel() {
// initialize sublevel
if(items.score.currentSubLevel < items.score.numberOfSubLevels)
items.score.currentSubLevel = currentSubLevel + 1;
else
items.score.visible = false
items.goodWordIndex = subLevelsLeft.pop()
items.ok.visible = false
items.goodIcon.visible = false
items.goodWord = wordList[items.goodWordIndex]
items.wordImage.changeSource(items.goodWord.image);
items.remainingLife = 6;
alreadyTypedLetters = new Array();
currentWord = items.goodWord.translatedTxt;
items.hidden.text = ""
createAttemptedText()
for(var i = 0; i < currentWord.length ; ++ i) {
if(currentWord[i] == " ") {
items.hidden.text = items.hidden.text + " " + " "
} else {
items.hidden.text = items.hidden.text + sp;
}
}
}
function createAttemptedText() {
alreadyTypedLetters.sort()
items.guessedText.text = qsTr("Attempted: %1").arg(alreadyTypedLetters.join(", "))
}
function nextSubLevel() {
if( ++currentSubLevel >= maxSubLevel) {
currentSubLevel = 0;
nextLevel();
} else {
initSubLevel();
}
}
function focusTextInput() {
if (!GCompris.ApplicationInfo.isMobile && items && items.textinput)
items.textinput.forceActiveFocus();
}