-
Notifications
You must be signed in to change notification settings - Fork 0
/
PieceUtils.java~
168 lines (153 loc) · 3.96 KB
/
PieceUtils.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
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
public class PieceUtils
{
public static Piece charToPiece(char c)
{
switch (c)
{
case '^':
return Piece.EMITTER_NORTH;
case '>':
return Piece.EMITTER_EAST;
case 'v':
return Piece.EMITTER_SOUTH;
case '<':
return Piece.EMITTER_WEST;
case '|':
return Piece.LASER_VERTICAL;
case '-':
return Piece.LASER_HORIZONTAL;
case '+':
return Piece.LASER_CROSSED;
case '/':
return Piece.MIRROR_SW_NE;
case '\\':
return Piece.MIRROR_NW_SE;
case '#':
return Piece.WALL;
case 'o':
return Piece.TARGET;
case '@':
return Piece.SNOWMAN;
case ' ':
return Piece.EMPTY;
default:
return null;
}
}
public static Piece[][] charsToPieces(char[] description,
int width, int height)
{
String desc = new String(description);
Piece[][] board = new Piece[width][height];
for (int i = height-1; i >= 0; i--)
{
for (int j = 0; j < width; j++)
{
board[j][i] = charToPiece(desc.charAt(j));
}
desc = desc.substring(width);
}
return board;
}
public static boolean isEmitter(Piece p) {
switch (p) {
case EMITTER_NORTH:
case EMITTER_EAST:
case EMITTER_SOUTH:
case EMITTER_WEST:
return true;
}
return false;
}
public static Coordinate findEmitter(Piece[][] pieces)
{
for (int x = 0; x < pieces.length; x++)
{
for (int y = 0; y < pieces[x].length; y++)
{
if (isEmitter(pieces[x][y]))
return new Coordinate(x, y);
}
}
return null;
}
public static Piece hideLaser(Piece p) {
switch (p) {
case LASER_VERTICAL:
case LASER_HORIZONTAL:
case LASER_CROSSED:
return Piece.EMPTY;
}
return p;
}
public static Piece addLaser(Piece p, boolean isHorizontal)
{
switch (p)
{
case EMPTY:
return (isHorizontal) ? Piece.LASER_HORIZONTAL
: Piece.LASER_VERTICAL;
case LASER_HORIZONTAL:
{
if (!isHorizontal)
return Piece.LASER_CROSSED;
break;
}
case LASER_VERTICAL:
{
if (isHorizontal)
return Piece.LASER_CROSSED;
break;
}
}
return p;
}
public static Coordinate move(Piece p, Coordinate c, int xo, int yo)
{
int x, y;
x = c.getX();
y = c.getY();
switch (p)
{
case EMITTER_NORTH:
return new Coordinate(x , y + 1);
case EMITTER_EAST:
return new Coordinate(x + 1, y);
case EMITTER_SOUTH:
return new Coordinate(x, y - 1);
case EMITTER_WEST:
return new Coordinate(x - 1, y);
case EMPTY:
case LASER_HORIZONTAL:
case LASER_CROSSED:
case LASER_VERTICAL:
return new Coordinate(x + xo, y + yo);
case MIRROR_NW_SE:
{
xo *= -1;
yo *= -1;
}
case MIRROR_SW_NE:
return new Coordinate(x + yo, y + xo);
default:
return c;
}
}
public static Piece rotate(Piece p) {
switch (p) {
case EMITTER_NORTH:
return Piece.EMITTER_EAST;
case EMITTER_EAST:
return Piece.EMITTER_SOUTH;
case EMITTER_SOUTH:
return Piece.EMITTER_WEST;
case EMITTER_WEST:
return Piece.EMITTER_NORTH;
case MIRROR_SW_NE:
return Piece.MIRROR_NW_SE;
case MIRROR_NW_SE:
return Piece.MIRROR_SW_NE;
}
return p;
}
}