This repository has been archived by the owner on Sep 13, 2022. It is now read-only.
forked from zhengster/Sliding-Tiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiles.js
171 lines (154 loc) · 4.32 KB
/
tiles.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
/**
* FILE NAME: tile-game.js
* WRITTEN BY: Alina Zheng
* DATE: 3/15/20
* PURPOSE: CS204 assignment 5
*/
// 2D array representation of the current occupant of each grid square
var grid = [['a', 'b', 'c'],
['d', 'e', 'f'],
['g', 'h', 'blank']];
// global variables that keep track of the position of the blank tile
var blank_row = 2;
var blank_col = 2;
// global variables that keep track of the total number of rows and columns
var num_row = 3;
var num_col = 3;
// global variables that keep track of the tile to switch the blank with
var tile_row;
var tile_col;
//global array of the four directions
var directions = [right, left, up, down];
/**
* Takes an array, returns a random element
* @param {array} array the array to find a random element of
* @return the random element of the array
*/
function randomElt(array) {
var random_index = Math.floor(array.length * Math.random());
return array[random_index];
}
/**
* Gets the tile to the right of the blank
* @return the tile to the right of the blank
*/
function getBelowTile() {
if (blank_row + 1 < num_row) {
tile_row = blank_row + 1;
tile_col = blank_col;
return grid[tile_row][tile_col];
}
}
/**
* Gets the tile above the blank
* @return the tile above the blank
*/
function getRightTile() {
if (blank_col + 1 < num_col) {
tile_row = blank_row;
tile_col = blank_col + 1;
return grid[tile_row][tile_col];
}
}
/**
* Gets the tile below the blank
* @return the tile below the blank
*/
function getLeftTile() {
if (blank_col - 1 >= 0) {
tile_row = blank_row;
tile_col = blank_col - 1;
return grid[tile_row][tile_col];
}
}
/**
* Gets the tile to the left of the blank
* @return the tile to the left of the blank
*/
function getAboveTile() {
if (blank_row - 1 >= 0) {
tile_row = blank_row - 1;
tile_col = blank_col;
return grid[tile_row][tile_col];
}
}
/**
* Switches the tile positions with the blank positions, updates the grid
* @param {String} dir the direction
*/
function switchTile(dir) {
var tile = grid[tile_row][tile_col];
var blank = grid[blank_row][blank_col];
if (dir == up || dir == down) {
temp = blank_row;
blank_row = tile_row;
tile_row =temp;
}
else {
temp = blank_col;
blank_col = tile_col;
tile_col = temp;
}
grid[tile_row][tile_col] = tile;
grid[blank_row][blank_col] = blank;
console.log(grid);
}
/**
* Takes a direction and determines the tle to slide
* Slides the tile one space in the designated direction
* Swtiches the blank tile and the moved tile in the grid 2D array
* @param {String} dir the direction
*/
function doMove(dir) {
var temp;
var tile_id;
if (dir == right && getLeftTile() != null) {
tile_id = "#" + getLeftTile();
$(tile_id).animate({ left: "+=200px" }, 1000);
switchTile(right);
}
else if (dir == left && getRightTile() != null) {
tile_id = "#" + getRightTile();
$(tile_id).animate({ left: "-=200px" }, 1000);
switchTile(left);
}
else if (dir == up && getBelowTile() != null) {
tile_id = "#" + getBelowTile();
$(tile_id).animate({ top: "-=200px" }, 1000);
switchTile(up);
}
else if (dir == down && getAboveTile() != null) {
tile_id = "#" + getAboveTile();
$(tile_id).animate({ top: "+=200px" }, 1000);
switchTile(down);
}
}
//click handler functions for each of the direction buttons
$("#up").click(function() {doMove(up)});
$("#down").click(function() {doMove(down)});
$("#left").click(function() {doMove(left)});
$("#right").click(function() {doMove(right)});
/**
* Anonymous function, controls movements of tiles based on WASD keys
*/
$(document).keydown(function(e) {
if (e.keyCode == 87) {
doMove(up);
}
else if (e.keyCode == 65) {
doMove(left);
}
else if (e.keyCode == 83) {
doMove(down);
}
else if (e.keyCode == 68) {
doMove(right);
}
});
// click handler function for "scramble" button - WHY IS IT ACTING THIS WAY
$("#scrambleButton").click(function() {
for (var i = 0; i < 30; i++) {
var dir = randomElt(directions);
doMove(dir);
}
});