Newer
Older
/**
* Represent a car in the game. The car can be either horizontal or vertical.
* Also manage movement for that car.
* For horizontal cars the front is to the right, for vertical cars it is to the bottom of the matrix.
*/
public class Car
{
private int number;
private Position front, back;
private Orientation orientation;
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/**
* Create a new car. Front and back are switched if necessary so that the from is to the right and the bottom.
*/
public Car(int number, Position front, Position back, Orientation orientation)
{
this.number = number;
this.orientation = orientation;
if (orientation == Orientation.HORIZONTAL)
{
if (back.getX() < front.getX())
{
this.back = back;
this.front = front;
} else
{
this.back = front;
this.front = back;
}
} else
{
if (back.getY() < front.getY())
{
this.back = back;
this.front = front;
} else
{
this.back = front;
this.front = back;
}
}
}
/**
* Move the car forward
*
* @param board the board on which apply the changes
* @param nbSteps the number of steps this car will move
* @throws RuntimeException If the number of steps is too high
*/
public void moveForward(int[][] board, int nbSteps) throws RuntimeException
{
if (!canMoveForward(board, nbSteps))
throw new RuntimeException("Illegal number of steps!");
if (orientation == Orientation.HORIZONTAL)
{
for (int i = 0; i < nbSteps; i++)
{
board[front.getX() + i + 1][front.getY()] = this.number;
board[back.getX() + i][back.getY()] = -1;
}
front.setX(front.getX() + nbSteps);
back.setX(back.getX() + nbSteps);
} else
{
for (int i = 0; i < nbSteps; i++)
{
board[front.getX()][front.getY() + i + 1] = this.number;
board[back.getX()][back.getY() + i] = -1;
}
front.setY(front.getY() + nbSteps);
back.setY(back.getY() + nbSteps);
}
}
/**
* Move the car backwards
*
* @param board the board on which apply the changes
* @param nbSteps the number of steps this car will move
* @throws RuntimeException If the number of steps is too high
*/
public void moveBackwards(int[][] board, int nbSteps) throws RuntimeException
{
if (!canMoveBackwards(board, nbSteps))
throw new RuntimeException("Illegal number of steps!");
if (orientation == Orientation.HORIZONTAL)
{
for (int i = 0; i < nbSteps; i++)
{
board[back.getX() - i - 1][back.getY()] = this.number;
board[front.getX() - i][front.getY()] = -1;
}
front.setX(front.getX() - nbSteps);
back.setX(back.getX() - nbSteps);
} else
{
for (int i = 0; i < nbSteps; i++)
{
board[back.getX()][back.getY() - i - 1] = this.number;
board[front.getX()][front.getY() - i] = -1;
}
front.setY(front.getY() - nbSteps);
back.setY(back.getY() - nbSteps);
}
}
/**
* @param board The game board.
* @return How many tiles the car can move forward
*/
public int getNbStepsForward(int[][] board)
{
int result = 0;
if (this.orientation == Orientation.HORIZONTAL)
for (int x = this.front.getX() + 1; (x < board.length) && (board[x][this.front.getY()] == -1); x++)
result++;
else
for (int y = this.front.getY() + 1; (y < board.length) && (board[this.front.getX()][y] == -1); y++)
result++;
return result;
}
/**
* @param board The game board.
* @return How many tiles the car can move backwards
*/
public int getNbStepsBackwards(int[][] board)
{
int result = 0;
if (this.orientation == Orientation.HORIZONTAL)
for (int x = this.back.getX() - 1; (x > 0) && (board[x][this.back.getY()] == -1); x--)
result++;
else
for (int y = this.back.getY() - 1; (y > 0) && (board[this.back.getX()][y] == -1); y--)
result++;
return result;
}
/**
* Tell if the car can move forward that many steps
*
* @param board The game board
* @param nbSteps The number of steps
*/
public boolean canMoveForward(int[][] board, int nbSteps)
{
if (this.orientation == Orientation.HORIZONTAL)
{
if (this.front.getX() + nbSteps >= board.length)
return false;
for (int x = this.front.getX() + 1; x - this.front.getX() <= nbSteps; x++)
if (board[x][this.front.getY()] != -1)
return false;
return true;
} else
{
if (this.front.getY() + nbSteps >= board.length)
return false;
for (int y = this.front.getY() + 1; y - this.front.getY() <= nbSteps; y++)
if (board[this.front.getX()][y] != -1)
return false;
return true;
}
}
/**
* Tell if the car can move backwards that many steps
*
* @param board The game board
* @param nbSteps The number of steps
*/
public boolean canMoveBackwards(int[][] board, int nbSteps)
{
if (this.orientation == Orientation.HORIZONTAL)
{
if (this.back.getX() - nbSteps < 0)
return false;
for (int x = this.back.getX() - 1; this.back.getX() - x <= nbSteps; x--)
if (board[x][this.back.getY()] != -1)
return false;
return true;
} else
{
if (this.back.getY() - nbSteps < 0)
return false;
for (int y = this.back.getY() - 1; this.back.getY() - y <= nbSteps; y--)
if (board[this.back.getX()][y] != -1)
return false;
return true;
}
}
public Position getFront()
{
return front;
}
public Position getBack()
{
return back;
}
public Orientation getOrientation()
{
return orientation;
}
public int getNumber()
{
return number;
}
public static enum Orientation