-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew_try_general.js
137 lines (111 loc) · 5.44 KB
/
new_try_general.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
function Grid () {
this.dimension = [[false,false,false,false],
[false,false,false,false],
[false,false,false,false],
[false,false,false,false],
[false,false,false,false],
[false,false,false,false],
[false,false,false,false],
[false,false,false,false],
[false,false,false,false],
[false,false,false,false],
[false,false,false,false],
[false,false,false,false],
[false,false,false,false],
[false,false,false,false]];
}
Grid.prototype.display = function() //Function to display the grid
{
alert(grid.dimension[0]+"\n"+grid.dimension[1]+"\n"+grid.dimension[2]+"\n"+grid.dimension[3]+"\n"+grid.dimension[4]+"\n"+grid.dimension[5]+"\n"+grid.dimension[6]+"\n"+grid.dimension[7]);
}
function Shape () {
this.shape_sqr = [[true,true],
[true,true]];
this.size_sqr = 2;
this.current_top_left_corner = [0,0]; //Initializing variables that will hold current pos of sqaure
this.current_bottom_right_corner = [0,0];
}
var grid = new Grid(); //object of class Grid
var shape = new Shape(); //object of class Shape
grid.display();
function enter_sqaure_into_grid() //At the top of the grid, the square is placed
{
for( var i = 0; i < 2; i++ ){ //Specifies the position where the square is placed
for ( var j = 1; j < 3; j++ ){
grid.dimension[i][j] = true;
}
}
shape.current_top_left_corner = [0,1]; //Store the current position of the square
shape.current_bottom_right_corner = [1,2];
}
enter_sqaure_into_grid();
grid.display();
function move_down() //Function that moves sqaure down by 1 block (Anywhere on grid)
{
//First we remove the square from it's current location
// grid.dimension[shape.current_top_left_corner[0]][shape.current_top_left_corner[1]] = false;
// grid.dimension[shape.current_top_left_corner[0]][shape.current_top_left_corner[1]+1] = false;
// grid.dimension[shape.current_bottom_right_corner[0]][shape.current_bottom_right_corner[1]] = false;
// grid.dimension[shape.current_bottom_right_corner[0]][shape.current_bottom_right_corner[1]-1] = false;
for ( var i = shape.current_top_left_corner[0]; i <= shape.current_bottom_right_corner[0]-1;i++){
for ( var j = shape.current_top_left_corner[1]; j <= shape.current_bottom_right_corner[1];j++){
grid.dimension[i][j] = false;
}
}
//Then we shift it one position lower on the grid
grid.dimension[shape.current_top_left_corner[0]+1][shape.current_top_left_corner[1]] = true;
grid.dimension[shape.current_top_left_corner[0]+2][shape.current_top_left_corner[1]] = true;
grid.dimension[shape.current_bottom_right_corner[0]][shape.current_bottom_right_corner[1]] = true;
grid.dimension[shape.current_bottom_right_corner[0]+1][shape.current_bottom_right_corner[1]] = true;
grid.display();
//Update the current position of the square
shape.current_top_left_corner[0]+=1;
shape.current_bottom_right_corner[0] +=1;
console.log(shape.current_top_left_corner);
console.log(shape.current_bottom_right_corner);
}
move_down();
function move_left() //Function that moves sqaure left by 1 block (Anywhere on grid)
{
//First we remove the square from it's current location
grid.dimension[shape.current_top_left_corner[0]][shape.current_top_left_corner[1]] = false;
grid.dimension[shape.current_top_left_corner[0]][shape.current_top_left_corner[1]+1] = false;
grid.dimension[shape.current_bottom_right_corner[0]][shape.current_bottom_right_corner[1]] = false;
grid.dimension[shape.current_bottom_right_corner[0]][shape.current_bottom_right_corner[1]-1] = false;
//Then we shift it left by one position on the grid
grid.dimension[shape.current_top_left_corner[0]][shape.current_top_left_corner[1]-1] = true;
grid.dimension[shape.current_top_left_corner[0]][shape.current_top_left_corner[1]] = true;
grid.dimension[shape.current_bottom_right_corner[0]][shape.current_bottom_right_corner[1]-2] = true;
grid.dimension[shape.current_bottom_right_corner[0]][shape.current_bottom_right_corner[1]-1] = true;
grid.display();
//Update the current position of the square
shape.current_top_left_corner[1]-=1;
shape.current_bottom_right_corner[1] -=1;
console.log(shape.current_top_left_corner);
console.log(shape.current_bottom_right_corner);
}
// move_left();
move_down();
function move_right() //Function that moves sqaure left by 1 block (Anywhere on grid)
{
//First we remove the square from it's current location
grid.dimension[shape.current_top_left_corner[0]][shape.current_top_left_corner[1]] = false;
grid.dimension[shape.current_top_left_corner[0]][shape.current_top_left_corner[1]+1] = false;
grid.dimension[shape.current_bottom_right_corner[0]][shape.current_bottom_right_corner[1]] = false;
grid.dimension[shape.current_bottom_right_corner[0]][shape.current_bottom_right_corner[1]-1] = false;
//Then we shift it right by one position on the grid
grid.dimension[shape.current_top_left_corner[0]][shape.current_top_left_corner[1]+2] = true;
grid.dimension[shape.current_top_left_corner[0]][shape.current_top_left_corner[1]+1] = true;
grid.dimension[shape.current_bottom_right_corner[0]][shape.current_bottom_right_corner[1]] = true;
grid.dimension[shape.current_bottom_right_corner[0]][shape.current_bottom_right_corner[1]+1] = true;
grid.display();
//Update the current position of the square
shape.current_top_left_corner[1]+=1;
shape.current_bottom_right_corner[1] +=1;
console.log(shape.current_top_left_corner);
console.log(shape.current_bottom_right_corner);
}
// move_right();
// move_right();
move_down();
// move_left();