-
Notifications
You must be signed in to change notification settings - Fork 0
/
tjam.js
404 lines (322 loc) · 10.8 KB
/
tjam.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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
$(document).ready(function() {
//class board
function board() {
//currently only ten colors, will add more.
this.colors = ["red","blue","green","orange","yellow","purple","black","pink","lightblue","star"];
//will hold array of car objects
this.carInfo = new Array();
this.err = "<span class='error'>X</span>";
//when car array is made and applied, give color.
this.addColor = function() {
var car = $(".car");
var cars = car.length;
for (var i = 0; i < cars; i++) {
if (i == cars - 1) {
car.eq(i).addClass("star");
break;
}
car.eq(i).addClass(this.colors[i]);
}
};
//put cars into the game board
this.putCars = function() {
var board = $("#game");
var car;
for (var i = 0; i < this.carInfo.length; i++) {
car = document.createElement("div");
$car = $(car);
$car.addClass("car");
$car.attr("data-name",i);
if (this.carInfo[i].orientation == "h")
$car.attr("data-or","horizontal");
else
$car.attr("data-or","vertical");
//classes are immutable here, so color, length, and orientation are all classes.
$car.attr("data-len",this.carInfo[i].len); //give length
$car.addClass(this.colors[i]); // color
//positions are 0 indexed, and imagined as the first quadrant of a cartestian graph.
$car.attr("style","bottom:" + this.carInfo[i].vPos + "00px;"); //vert position
$car.attr("style",$car.attr("style") + "left:" + this.carInfo[i].hPos + "00px; "); // horizontal pos
$($car).appendTo(board);
}
this.addColor();
this.compile();
};
//adds left and right buttons based on proximity to edges.
this.compile = function() {
var rightBtn = "<span class='right-btn' data-change='100'></span>";
var leftBtn = "<span class='left-btn' data-change='-100'></span>";
var upBtn = "<span class='up-btn' data-change='100'></span>";
var downBtn = "<span class='down-btn' data-change='-100'></span>";;
$("[data-or=horizontal]").each(function() {
var $this = $(this);
$this.empty();
var left = parseInt($this.css("left"));
if (left < 400 && $this.attr("data-len") == 2) // if it's not too far right and it's only two long, give a right btn
$(rightBtn).appendTo($this);
else if (left < 300 && $this.attr("data-len") == 3) //if length = 3, has to be at least three from edge.
$(rightBtn).appendTo($this);
if (left > 0)
$(leftBtn).appendTo($this);
});
$("[data-or=vertical]").each(function() {
var $this = $(this);
$this.empty();
var bottom = parseInt($this.css("bottom"));
if (bottom < 400 && $this.attr("data-len") == 2)
$(upBtn).appendTo($this);
else if (bottom < 300 && $this.attr("data-len") == 3)
$(upBtn).appendTo($this);
if (bottom > 0)
$(downBtn).appendTo($this);
});
};
//uses expected coordinates to check for a colission.
this.checkColission = function(xPos, yPos, len, orientation) {
xPos /= 100;
xPos = Math.floor(xPos);
yPos /= 100;
yPos = Math.floor(yPos);
var desired = {"x": xPos,"y": yPos};
//compare the current car's proposed new space to the other cars on the board
for (var i = 0; i < this.carInfo.length; i++) {
var car = this.carInfo[i];
var checkX = car.hPos;
var checkY = car.vPos;
var checkLen = car.len;
var checkOrient = car.orientation;
// the two or three coordinates of each car are computed,
//then compared the the new square of the car being moved.
var coords = [{"x": checkX,"y": checkY}];
if (checkOrient == "h"){
coords.push({"x": checkX + (checkLen-1), "y": checkY});
if (checkLen == 3)
coords.push({"x": checkX + (checkLen-2), "y": checkY});
}
else{
coords.push({"x": checkX , "y": checkY + (checkLen-1)});
if (checkLen == 3)
coords.push({"x": checkX , "y": checkY + (checkLen-2)});
}
for (var c = 0; c < coords.length; c++) {
if (desired.x == coords[c].x && desired.y == coords[c].y)
return false;
}
}
return true;
};
this.checkWin = function(xPos, yPos) {
return xPos == 400 && yPos == 300;
}
this.error = function($button) {
$button.parent().append(board.err);
var err = $button.parent().find(".error");
if ($button.hasClass("right-btn"))
err.css({"right":"0", "top":"10px"});
else if ($button.hasClass("left-btn"))
err.css({"left":"0","top":"10px"});
else if ($button.hasClass("up-btn"))
err.css({"top":"0","left":"10px"});
else
err.css({"bottom":"0","left":"10px"});
err.hide().fadeIn("slow").delay(250).fadeOut("slow", function() {
$(this).remove();
});
}
}
//each car object is stored in the board with its coords, length, and orientation
function car(hPos, vPos, orientation, len) {
this.hPos = hPos;
this.vPos = vPos;
this.orientation = orientation;
this.len = len;
}
//the four boards from the assignment.
function preMade() {
this.board1 = [
{ hPos: 0,vPos: 2,orientation: 'h',len: 3},
{ hPos: 2,vPos: 3,orientation: 'v',len: 3},
{ hPos: 4,vPos: 5,orientation: 'h',len: 2},
{ hPos: 5,vPos: 0,orientation: 'v',len: 3},
{ hPos: 0,vPos: 3,orientation: 'h',len: 2}
];
this.board2 = [
{ hPos: 0,vPos: 0,orientation: 'h',len: 3},
{ hPos: 0,vPos: 1,orientation: 'v',len: 2},
{ hPos: 1,vPos: 1,orientation: 'h',len: 2},
{ hPos: 3,vPos: 0,orientation: 'v',len: 2},
{ hPos: 4,vPos: 0,orientation: 'v',len: 2},
{ hPos: 5,vPos: 0,orientation: 'v',len: 3},
{ hPos: 2,vPos: 2,orientation: 'v',len: 3},
{ hPos: 3,vPos: 2,orientation: 'h',len: 2},
{ hPos: 5,vPos: 3,orientation: 'v',len: 2},
{ hPos: 0,vPos: 3,orientation: 'h',len: 2}
];
this.board3 = [
{ hPos: 0,vPos: 1,orientation: 'h',len: 3},
{ hPos: 1,vPos: 0,orientation: 'h',len: 2},
{ hPos: 2,vPos: 2,orientation: 'v',len: 3},
{ hPos: 0,vPos: 5,orientation: 'h',len: 3},
{ hPos: 3,vPos: 2,orientation: 'h',len: 2},
{ hPos: 3,vPos: 4,orientation: 'v',len: 2},
{ hPos: 4,vPos: 0,orientation: 'v',len: 2},
{ hPos: 5,vPos: 2,orientation: 'v',len: 2},
{ hPos: 5,vPos: 4,orientation: 'v',len: 2},
{ hPos: 0,vPos: 3,orientation: 'h',len: 2}
];
this.board4 = [
{ hPos: 0,vPos: 0,orientation: 'v',len: 2},
{ hPos: 1,vPos: 0,orientation: 'v',len: 2},
{ hPos: 2,vPos: 0,orientation: 'v',len: 2},
{ hPos: 0,vPos: 2,orientation: 'h',len: 3},
{ hPos: 2,vPos: 3,orientation: 'v',len: 2},
{ hPos: 4,vPos: 0,orientation: 'h',len: 2},
{ hPos: 4,vPos: 1,orientation: 'h',len: 2},
{ hPos: 4,vPos: 2,orientation: 'v',len: 3},
{ hPos: 5,vPos: 2,orientation: 'v',len: 2},
{ hPos: 0,vPos: 3,orientation: 'h',len: 2}
];
}
var preMade_boards = new preMade();
var board = new board(); //initialize a new board
//when an input is made, limit to one char, and go to next input.
$(".carData").keyup(function() {
var maxLen = 1;
if ($(this).val().length > maxLen) {
$(this).val($(this).val().substring(0,1));
}
$(this).next().focus();
if ($(this).index() == $(".carData").length - 1)
$(".carData").eq(0).focus();
});
$(".addCar").on("click",function() {
//the inputs have class carData
var carData = $(".carData");
var hPos, vPos, orientation, len;
var flag = false;
for (var i = 0; i < carData.length; i++) { //check if all fields are entered correctly.
var carVal = carData.eq(i).val();
if (carVal == "") {
flag = true;
break;
}
//if good input, set values and push to array of cars
else if (i == 0) {
if (carVal > 5) {
flag = true;
break;
}
hPos = carVal;
}
else if (i == 1) {
if (carVal > 5) {
flag = true;
break;
}
vPos = carVal;
}
else if (i == 2) {
if (carVal != "h" && carVal != "v"){
flag = true;
break;
}
orientation = carVal;
}
else {
if (carVal != 2 && carVal != 3) {
flag = true;
break;
}
len = carVal;
board.carInfo.push(new car(hPos,vPos, orientation, len));
}
carData.eq(i).val("");
}
if (flag)
alert("Your inputs are malformed. Integers [0,4] for X and Y, h or v for O, and 2 or 3 for L.");
else {
$(".preMade").slideUp();
$("#game").children(":not(.middleBorder)").remove();
board.putCars();
}
});
$(".finish").on("click",function() {
//hide input console when done
$(".wrapInputs, .options").slideUp();
$(".preMade").slideUp();
$("#game").children(":not(.middleBorder)").remove();
board.putCars();
});
//when going left, check for colissions
$(".right-btn, .left-btn").live("click",function() {
var $this = $(this);
var left = parseInt($this.parent().css("left"));
var change = parseInt($this.attr("data-change"));
var yPos = parseInt($this.parent().css("bottom"));
var len = parseInt($this.parent().attr("data-len"));
var orientation = $this.parent().attr("data-or");
var name = parseInt($this.parent().attr("data-name"));
var valid;
if ($this.hasClass("right-btn"))
valid = board.checkColission((left+100*(len-1)+change), yPos, len, orientation);
else
valid = board.checkColission((left+change), yPos, len, orientation);
if (valid) {
//if the move is valid, change the car's data in the array, and move it on the board
$this.parent().css("left", left + change + "px");
board.compile();
board.carInfo[name].hPos = (left + change)/100;
//if the car moved right, check a win.
if ($this.hasClass("right-btn")) {
if (board.checkWin(left + 100, yPos)) {
alert("You've won! Play another game! I dare you.")
document.location.reload(true);
}
}
}
else
board.error($this);
});
$(".up-btn, .down-btn").live("click",function() {
var $this = $(this);
var left = parseInt($this.parent().css("left"));
var change = parseInt($this.attr("data-change"));
var yPos = parseInt($this.parent().css("bottom"));
var len = $this.parent().attr("data-len");
var name = parseInt($this.parent().attr("data-name"));
var orientation = $this.parent().attr("data-or");
if ($this.hasClass("up-btn"))
valid = board.checkColission(left, yPos+100*(len-1)+change, len, orientation);
else
valid = board.checkColission(left, yPos+change, len, orientation);
if (valid) {
$this.parent().css("bottom", yPos + change + "px");
board.compile();
board.carInfo[name].vPos = (yPos + change)/100;
}
else
board.error($this);
});
//if the user picks a board, set the board's car info to the premade car info, then generate the board.
$(".preMade").find("li").on("click",function() {
var boardChoice = parseInt($(this).attr("data-board"));
switch(boardChoice) {
case 1:
board.carInfo = preMade_boards.board1;
break;
case 2:
board.carInfo = preMade_boards.board2;
break;
case 3:
board.carInfo = preMade_boards.board3;
break;
case 4:
board.carInfo = preMade_boards.board4;
break;
case 5:
board.carInfo = preMade_boards.board5;
break;
}
$(".finish").trigger("click");
});
});