-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstarterscript.js
More file actions
105 lines (84 loc) · 2.3 KB
/
starterscript.js
File metadata and controls
105 lines (84 loc) · 2.3 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
var grid = [];
var UP_ARROW = '38';
var DOWN_ARROW = '40';
var LEFT_ARROW = '37';
var RIGHT_ARROW = '39';
//As soon as webpage loads run these two functions
$(document).ready(function(){
setUpBoard();
printBoard();
console.log("Loaded webpage"); //how you do print statements in javascript
});
function setUpBoard(){
// initialize board to have no values
for(var i=0; i<4; i++){
var innergrid = [];
for(var j=0; j<4; j++){
innergrid.push("x");
}
grid.push(innergrid);
}
addTile();
}
function addTile() {
//place a 2 on a random spot in the board
var x = Math.round(Math.random()*3);
var y = Math.round(Math.random()*3);
grid[x][y] = "2";
}
function printBoard(){
var board = '<br/>' + "*--------------*" + '<br/>';
for(var i=0; i<grid.length; i++){
board += "| ";
for(var j=0; j<grid[i].length; j++){
board += grid[i][j] + " | ";
}
board += '<br/>';
board += "*--------------*";
board += '<br/>';
}
//console.log(board);
document.getElementById("container").innerHTML = board;
}
//function gets called anytime a key is pressed
//e is a special variable
// that references the event obeject that reads if the user is interacting with
//the window
document.onkeydown = function(e) {
//makes it work in internet explorer which uses window.event and not e
e = e || window.event;
//keyCode is actually a character value which we convert to a String
//to use triple equals sign
if (e.keyCode == UP_ARROW) {
// up arrow
moveTilesUp();
}
//double equals sign will convert it for us
else if (e.keyCode == DOWN_ARROW) {
// down arrow
console.log("Pressed down");
}
else if (e.keyCode == LEFT_ARROW) {
// left arrow
console.log("Pressed left");
}
else if (e.keyCode == RIGHT_ARROW) {
// right arrow
console.log("Pressed right");
}
printBoard(); //have to recall print board to get the board to update
};
function moveTilesUp()
{
for(var r=0; r < grid.length; r++)
{
for(var c=0; c<grid[r].length; c++)
{
if(r !== 0 && grid[r][c] !== "x" && grid[r-1][c] === "x")
{
grid[r-1][c] = grid[r][c];
grid[r][c] = "x";
}
}
}
}