-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
181 lines (172 loc) · 4.79 KB
/
index.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
document.addEventListener(
'touchmove',
function(event) {
event.preventDefault()
},
false
)
$(window).bind('touchstart', function(e) {
var now = new Date().getTime()
var lastTouch =
$(this).data('lastTouch') ||
now + 1 /** the first time this will make delta a negative number */
var delta = now - lastTouch
if (delta < 200 && delta > 0) {
// the second touchend event happened within half a second. Here is where we invoke the double tap code
mouseDoubleClick(e)
} else {
// A click/touch action could be invoked here but wee need to wait half a second before doing so.
mouseClick(e)
}
$(this).data('lastTouch', now)
})
function makeTree() {
dictionary = _.filter(dictionary, function(a) {
return a.length >= MIN_LENGTH && a.length <= MAX_LENGTH
})
_.each(dictionary, function(_word) {
temp = tree
for (var j = 0; j < _word.length; j++) {
// where should the character be listed at
index = _word[j] // .charCodeAt(0)-65;
// is this number in the list yet?
last = temp.indexOf(index) + 1
if (last == 0) {
// if not, add the ascii code and push an empty array after it
last = temp.push(index)
temp.push([j + 1, temp])
}
// now temp is holding the new array or old one for this word
temp = temp[last]
}
// push something to indicated if you are up to here, you have a valid word
if (!_.contains(temp, 'YES')) {
// make sure it's not a duplicated word
temp.push('YES')
}
// if tree has yes then take it as a word
})
}
var high_score = ''
var top_score = ''
var dictionary = easyList
var tree = [0, null]
function supports_html5_storage() {
try {
return 'localStorage' in window && window['localStorage'] !== null
} catch (e) {
return false
}
}
function newGame() {
tries = 0
resetGuessList()
do {
tries++
if (tries > 10000) break
init()
var board = puzzle.slice(0)
for (var i = 0; i < COLUMNS; i++) {
board[i] = board[i].slice(0)
}
if (wordList.length > 0) {
isConnected(board, wordList[0].startx, wordList[0].starty)
}
notConnected = false
for (var i = 0; i < COLUMNS; i++) {
for (var j = 0; j < ROWS; j++) {
if (board[i][j] != null && board[i][j] != -1) {
notConnected = true
break
}
}
}
} while (notConnected || wordCount != $('#number_of_words:checked').val())
timing = true
time = 0
if (navigator.onLine)
$.ajax({
url: 'topscores.php',
dataType: 'json',
async: true,
data: { wordcount: wordCount, fullname: $('#fullname').val() },
success: function(data) {
high_score = data.HS
top_score = data.TS
drawPuzzle()
$('#loading_panel').hide()
},
error: function() {
console.log('There was an error fetching topscores!')
$('#loading_panel').hide()
}
})
else {
drawPuzzle()
$('#loading_panel').hide()
}
}
$(window).load(function() {
makeTree()
$('#setting_panel').hide()
if (supports_html5_storage()) {
if (localStorage.fullname) $('#fullname').val(localStorage.fullname)
if (localStorage.email) $('#email').val(localStorage.email)
}
window.ondblclick = mouseDoubleClick
function mouseMove(e) {
if (e.originalEvent) e = e.originalEvent
e = e || window.event
if (!e.which && e.button) {
if (e.button & 1) e.which = 1
else if (e.button & 4) e.which = 2
else if (e.button & 2) e.which = 3
}
if (e.buttons != undefined) button = e.buttons
else button = e.which
if (button) {
// scroll if scrollbar is there
var x = e.touches ? e.touches[0].pageX : e.pageX
var y = e.touches ? e.touches[0].pageY : e.pageY
x = x - $('#canvas').offset().left
y = y - $('#canvas').offset().top
_.each(events, function(ev) {
if (
ev[0] <= x &&
x <= ev[0] + ev[2] &&
ev[1] <= y &&
y <= ev[1] + ev[3] &&
'scroll_event' == ev[5]
) {
ev[4](x, y)
drawPuzzle()
}
})
}
}
$(window).bind('click', mouseClick)
window.onmousemove = mouseMove
$('#saveButton')[0].onclick = function() {
if ($('#submit_details').is(':checked')) {
if ($('#fullname').val() == '') {
alert('Please fill the name field')
return false
}
if (supports_html5_storage()) {
localStorage.fullname = $('#fullname').val()
localStorage.email = $('#email').val()
}
}
$('#setting_panel').hide()
}
newGame()
})
function isConnected(board, i, j) {
if (board[i] && board[i][j] && board[i][j] != -1) {
board[i][j] = -1
isConnected(board, i - 1, j)
isConnected(board, i + 1, j)
isConnected(board, i, j - 1)
isConnected(board, i, j + 1)
}
}