-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitblock.js
More file actions
336 lines (308 loc) · 11.5 KB
/
Copy pathbitblock.js
File metadata and controls
336 lines (308 loc) · 11.5 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
v03 Todos
2. done: fix menu-to-game so it doesn't erase the board, reload the page, get lost, disappear.
3. done: add New Game button
4. done: added a Last Games list when you replay a board, to see how you did the last few times
v04 Todo
1. server database, saving games to replay, compete
18. timing bonus (not really needed right now)
*/
$(document).ready(function(){
$("#options > div.section").first().addClass("current");
$("a.back").live('touchstart click', function(event) {
var current = $(event.currentTarget).attr("href");
$(".current").removeClass("current").addClass("reverse");
$(current).addClass("current");
});
$(".menu a[href]").live('touchstart click', function(event) {
var link = $(event.currentTarget);
var section = link.closest('div.section');
var prev_element = "#"+(section.removeClass("current").addClass("reverse").attr("id"));
$(link.attr("href")).addClass("current");
$(".current .back").remove();
$(".current .toolbar").prepend("<a href=\""+prev_element+"\" class=\"back\">Backy</a>");
});
$(".menu a[togame]").live('touchstart click', go2game);
$("#go2options").live('touchstart click', go2options);
function go2options(event){
$("#game01").css("display","hide");
$("#options").css("display","block");
$("#options > div.section").first().addClass("current");
}
function go2game(event){
$("#options").css("display","hide");
$("#game01").css("display","block");
//$("#go2options").live('touchstart click', go2options);
}
$("#replay").live('touchstart click', function(event) {
replayLastGame();
showNewBitblockMatrix();
setLastGame();
resetScores();
});
$("#newgame").live('touchstart click', function(event) {
genNewGame();
showNewBitblockMatrix();
resetLastGames();
resetScores();
});
$(".tapper").live('touchstart click', function(event) {
var tapid = $(event.target).attr("id");
var tapcolor = $(this).css("background-color");
var tapccode = $(this).attr("ccode");
//alert(tapid+" "+tapcolor+" "+tapccode);
//$(".matrix").;
//$("#options").show();
var grays = getGraycount();
playTurnMatrixChange(tapccode);
var newgrays = getGraycount();
squarecount = newgrays - grays;
var sqbonus = playTurnSquareCountBonus(squarecount);
//alert(score + " + " + sqbonus + " = " + (score+sqbonus));
score = score + sqbonus;
if(grays < newgrays) {
tapscount += 1;
}
showNewBitblockMatrix();
showScores();
squarecount = 0;
});
var gameover = false;
var score = 0;
var tapscount = 0;
var squarecount = 0; //track num squares killed for each tap
var matrixwidth = 10;
var matrixheight = 5;
var colors = Array("#555555","#864bff","#469bff","#57ffaa","#ffc81b","#ff388b");
var marr = new Array(matrixheight);
var marr2 = new Array(matrixheight);
function genNewGame(){
gameover = false;
for(var i = 0; i < matrixheight; i++){
//marr is the matrix used to display BitBlocks
marr[i] = new Array(matrixwidth);
//marr2 is a matrix used in calculating each modified BitBlocks (each turn)
marr2[i] = new Array(matrixwidth);
for(var j = 0; j < matrixwidth; j++){
var mcellid = "m"+int22char(i)+int22char(j);
var mcellccode = Math.floor(1 + Math.random() * 5);
marr[i][j] = mcellccode;
marr2[i][j] = mcellccode;
var mcellbgcolor = colors[mcellccode];
var mstr = "<div id=\""+ mcellid +"\" class=\"matrix\" ccode=\""+ mcellccode +"\" style=\"background-color:"+ mcellbgcolor +";\">"+mcellccode+"</div>";
$("#bitblocks").append(mstr);
}
var bstr = "<div class=\"b02\"></div>";
$("#bitblocks").append(bstr);
}
//set upper left corner to color 0 (gray)
marr[0][0] = 0;
mcellbgcolor = colors[0];
$("#m0000").css("background-color",mcellbgcolor).html("");
}
//var int22char = function(theint){
function int22char(theint) {
//creates a minimum-two char value for integer numbers.
var newstr = theint;
if(theint < 10){
newstr = "0"+theint;
}
return newstr;
//};
}
function playTurnMatrixChange(tapccode){
//changes the matrix array (marr) in place. marr2 represents the last known state of marr
/*
for(var i = 0; i < matrixheight; i++){
for(var j = 0; j < matrixwidth; j++){
marr2[i][j] = marr[i][j];
}
}
*/
//while(numgrays < newgrays){
//for(var grays = 0; grays <= pregrays; bill = 1 ){
for(var loops=0; loops<matrixheight-1; loops++){ //not efficient... should be recursive to find hidden connected blocks
var pregrays = getGraycount();
for(var i = 0; i < matrixheight; i++){
for(var j = 0; j < matrixwidth; j++){
if(marr[i][j] == 0){
//this is a gray cell, so change it's neighbors' colors
//don't look outside the matrix!
if(i==0){
if(j>0 && j<matrixwidth-1){
if(marr[i][j-1] == tapccode){ score = newScore(tapccode); marr[i][j-1] = 0; }
if(marr[i][j+1] == tapccode){ score = newScore(tapccode); marr[i][j+1] = 0; }
if(marr[i+1][j] == tapccode){ score = newScore(tapccode); marr[i+1][j] = 0; } //assumes matrixheight > 1
} else if(j == 0){
if(marr[i][j+1] == tapccode){ score = newScore(tapccode); marr[i][j+1] = 0; }
if(marr[i+1][j] == tapccode){ score = newScore(tapccode); marr[i+1][j] = 0; } //assumes matrixheight > 1
} else if(j == matrixwidth-1){
if(marr[i][j-1] == tapccode){ score = newScore(tapccode); marr[i][j-1] = 0; }
if(marr[i+1][j] == tapccode){ score = newScore(tapccode); marr[i+1][j] = 0; } //assumes matrixheight > 1
}
} else if(i>0 && i<matrixheight-1){
if(j>0 && j<matrixwidth-1){
if(marr[i][j-1] == tapccode){ score = newScore(tapccode); marr[i][j-1] = 0; }
if(marr[i][j+1] == tapccode){ score = newScore(tapccode); marr[i][j+1] = 0; }
if(marr[i+1][j] == tapccode){ score = newScore(tapccode); marr[i+1][j] = 0; } //assumes matrixheight > 1
if(marr[i-1][j] == tapccode){ score = newScore(tapccode); marr[i-1][j] = 0; } //assumes matrixheight > 1
} else if(j == 0){
if(marr[i][j+1] == tapccode){ score = newScore(tapccode); marr[i][j+1] = 0; }
if(marr[i+1][j] == tapccode){ score = newScore(tapccode); marr[i+1][j] = 0; } //assumes matrixheight > 1
if(marr[i-1][j] == tapccode){ score = newScore(tapccode); marr[i-1][j] = 0; } //assumes matrixheight > 1
} else if(j == matrixwidth-1){
if(marr[i][j-1] == tapccode){ score = newScore(tapccode); marr[i][j-1] = 0; }
if(marr[i+1][j] == tapccode){ score = newScore(tapccode); marr[i+1][j] = 0; } //assumes matrixheight > 1
if(marr[i-1][j] == tapccode){ score = newScore(tapccode); marr[i-1][j] = 0; } //assumes matrixheight > 1
}
} else if(i==matrixheight-1){
if(j>0 && j<matrixwidth-1){
if(marr[i][j-1] == tapccode){ score = newScore(tapccode); marr[i][j-1] = 0; }
if(marr[i][j+1] == tapccode){ score = newScore(tapccode); marr[i][j+1] = 0; }
if(marr[i-1][j] == tapccode){ score = newScore(tapccode); marr[i-1][j] = 0; } //assumes matrixheight > 1
} else if(j == 0){
if(marr[i][j+1] == tapccode){ score = newScore(tapccode); marr[i][j+1] = 0; }
if(marr[i-1][j] == tapccode){ score = newScore(tapccode); marr[i-1][j] = 0; } //assumes matrixheight > 1
} else if(j == matrixwidth-1){
if(marr[i][j-1] == tapccode){ score = newScore(tapccode); marr[i][j-1] = 0; }
if(marr[i-1][j] == tapccode){ score = newScore(tapccode); marr[i-1][j] = 0; } //assumes matrixheight > 1
}
}
}
}
}
var grays = getGraycount();
if(grays == pregrays){
//you're finished, there was no change in the graycount
break;
}
}
//alert("numgrays: "+grays+" numloops: "+loops); //works! loops just enough to solve the graycount efficiently
}
function showNewBitblockMatrix(){
//var newmarr = new Array(matrixheight);
$("#bitblocks").html("");
for(var i = 0; i < matrixheight; i++){
//newmarr[i] = new Array(matrixwidth);
for(var j = 0; j < matrixwidth; j++){
var mcellid = "m"+int22char(i)+int22char(j);
var mcellccode = marr[i][j];
//newmarr[i][j] = mcellccode;
var mcellbgcolor = colors[mcellccode];
var mcellcodevis = mcellccode;
if(mcellccode == 0){
mcellcodevis = "";
}
var mstr = "<div id=\""+ mcellid +"\" class=\"matrix\" ccode=\""+ mcellccode +"\" style=\"background-color:"+ mcellbgcolor +";\">"+mcellcodevis+"</div>";
$("#bitblocks").append(mstr);
}
var bstr = "<div class=\"b02\"></div>";
$("#bitblocks").append(bstr);
}
$("#bitblocks").show();
}
function replayLastGame(){
for(var i = 0; i < matrixheight; i++){
for(var j = 0; j < matrixwidth; j++){
marr[i][j] = marr2[i][j];
}
}
marr[0][0] = 0;
mcellbgcolor = colors[0];
$("#m0000").css("background-color",mcellbgcolor).html("");
}
function showScores(){
//var oldscore = $("#score").html();
//if( (oldscore + 0) == score){
// //no change in score, must have been a bad tap. decrement tap, no change in scores or taps.
// tapscount = tapscount - 1;
//}
var isDone = isMatrixCompleted();
if(isDone){
gameover = true;
var bonus = getBonusScore();
var timebonus = getTimeBonusScore();
//score = score + bonus + timebonus;
//$("#bonus").html("<br/>Tap Bonus: "+bonus+"! Time Bonus: "+timebonus+"!");
$("#bonus").html("<br/>FewTaps Bonus: "+bonus+"!");
$("#fini").html("Finished!<br/>");
var scorestr = score + " + " + bonus + " = " + (score+bonus+timebonus);
$("#score").html(scorestr);
} else {
$("#bonus").html("");
$("#fini").html("...not finished yet.<br/>");
$("#score").html(score);
}
$("#tapscount").html(tapscount);
}
function resetScores(){
score = 0;
tapscount = 0;
$("#score").html("0");
$("#tapscount").html("0");
$("#fini").html("");
$("#bonus").html("");
gameover = false;
}
function isMatrixCompleted(){
//matrix is completed when all is gray (0)
var isDone = false;
var graycount = getGraycount();
if(graycount == matrixwidth * matrixheight) {
isDone = true;
}
return isDone;
}
function getGraycount(){
var graycount = 0;
for(var i = 0; i < matrixheight; i++){
for(var j = 0; j < matrixwidth; j++){
if(marr[i][j] == 0){
graycount = graycount + 1;
}
}
}
return graycount;
}
function setLastGame(){
// prepends #lastgames with a new score
if(gameover){
var bonus = getBonusScore();
var timebonus = getTimeBonusScore();
var scorestr = score + " + " + bonus + " = " + (score+bonus+timebonus);
var laststr = "<li>Score: " + scorestr + " Taps: " + tapscount + "</li>\n";
$("#lastgames").prepend(laststr);
}
}
function resetLastGames(){
// kills #lastgames content
$("#lastgames").html("");
}
function newScore(thisccode){
//tapscount must be incremented here because we hit this before we actually bump it up. score as if the tap has already counted.
var newscore = score;
var newpts = (tapscount+1) * thisccode;
return newscore + newpts;
}
function getBonusScore(){
var tcount = tapscount;
if(tcount > matrixheight * matrixwidth) {
var tcount = matrixheight * matrixwidth;
}
var newpts = (Math.pow( 2*(matrixheight * matrixwidth - tapscount),2 )) / 2;
return Math.floor(newpts);
}
function getTimeBonusScore(){
//need to keep track of time to do this one.
var duration = 20; //seconds
if(duration > 30) {
duration = 30;
}
var tbonus = Math.pow((30 - duration),2);
return Math.floor(tbonus);
}
function playTurnSquareCountBonus(sqcount){
var sqbonus = Math.pow(sqcount,2);
return sqbonus;
}
});