-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSShape.java
125 lines (104 loc) · 2.29 KB
/
SShape.java
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
public class SShape {
int colOrigin;
int rowOrigin;
int col0;
int row0;
int col1;
int row1;
int col2;
int row2;
int direction; // 0: EAST 1: SOUTH 2: WEST 3: NORTH
Grid grid;
public void initPointsFromOrigin(int initCol, int initRow) {
colOrigin = initCol;
rowOrigin = initRow;
col0 = initCol - 1;
row0 = initRow;
col1 = initCol;
row1 = initRow - 1;
col2 = initCol + 1;
row2 = initRow - 1;
direction = 0;
}
public boolean moveDown() {
boolean left =
grid.isEmpty(col0, row0 + 1) &&
grid.isInBounds(col0, row0 + 1);
boolean down =
grid.isEmpty(colOrigin, rowOrigin + 1) &&
grid.isInBounds(colOrigin, rowOrigin + 1);
boolean right =
grid.isEmpty(col2, row2 + 1) &&
grid.isInBounds(col2, row2 + 1);
if (left && down && right) {
++rowOrigin;
++row0;
++row1;
++row2;
return true;
}
return false;
}
public boolean moveLeft() {
boolean left =
grid.isEmpty(col0 - 1, row0) &&
grid.isInBounds(col0 - 1, row0);
if (left) {
--colOrigin;
--col0;
--col1;
--col2;
return true;
}
return false;
}
public boolean moveRight() {
boolean rightTop =
grid.isEmpty(col2 + 1, row0) &&
grid.isInBounds(col2 + 1, row0);
boolean rightBottom =
grid.isEmpty(colOrigin + 1, row0) &&
grid.isInBounds(colOrigin + 1, row0);
if (rightTop && rightBottom) {
++colOrigin;
++col0;
++col1;
++col2;
return true;
}
return false;
}
public boolean rotate() {
// TODO: do not allow the shape to rotate if the cells are not empty.
if (direction == 0) {
--row0;
++col0;
++row1;
++col1;
row2 = row2 + 2;
direction = 1;
} else if (direction == 1) {
++row0;
++col0;
--col1;
++row1;
col2 = col2 - 2;
direction = 2;
} else if (direction == 2) {
++row0;
--col0;
--row1;
--col1;
row2 = row2 - 2;
direction = 3;
} else if (direction == 3) {
--row0;
--col0;
--row1;
++col1;
col2 = col2 + 2;
direction = 0;
}
return true;
}
}