-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
377 lines (358 loc) · 12.2 KB
/
Board.java
File metadata and controls
377 lines (358 loc) · 12.2 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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/**
* @author Edgar Ghahramanyan <edgarquill@gmail.com>
* @version Version 1
* @since 1.6
*/
import java.awt.Graphics2D;
import java.awt.Color;
import java.util.List;
import java.util.ArrayList;
import java.util.Random;
public class Board
{
// CONSTANTS
public static int TOP = 1;
public static int TOP_RIGHT = 2;
public static int RIGHT = 3;
public static int BOTTOM_RIGHT = 4;
public static int BOTTOM = 5;
public static int BOTTOM_LEFT = 6;
public static int LEFT = 7;
public static int TOP_LEFT = 8;
public static int FIRST = 0;
public static int SECOND = 1;
public static int THIRD = 2;
public static int FOURTH = 3;
// PRIVATE VALUES
private List<Field> fields;
private int id_counter;
private int field_size;
private Random fate;
/*
* Initializer for the Board.
* It is not enough to just initialize, do stuff.
*/
public Board()
{
this.fields = new ArrayList<Field>();
this.fate = new Random();
this.id_counter = 0;
this.field_size = 40;
}
/*
* Adds a field to the board.
* Board will manage the field id,
* but you should know that field ids are incremental starting from 0.
*/
public void addField()
{
Field field = new Field();
field.setId(this.id_counter);
this.fields.add(field);
this.id_counter++;
}
/*
* Will add whatever field you pass to it.
* The id of the field will be owerwritten with what board choses.
* @param field field that you want to add to the board.
*/
public void addField(Field field)
{
field.setId(this.id_counter);
this.fields.add(field);
this.id_counter++;
}
/*
* In all honesty it is pretty much the same as using getFieldById,
* But if you decide to change the id of the field after you add it,
* and you forgot the id, then use position I guess.
* I really don't see how you would use it.
* @param num position of the field in the list.
* @return field object which was at the position you asked for, null if position you asked for is wrong.
*/
public Field getField(int num)
{
if(num < 0 || num >= this.getBoardSize())
return null;
return this.fields.get(num);
}
/*
* Returns the last field added to the list. Not sure why you would want it, but here ya go.
* @return last field object in the list.
*/
public Field getLastField()
{
return this.fields.get(this.fields.size() - 1);
}
/*
* Returns the current number of fields on the board.
* @return the size of the board.
*/
public int getBoardSize()
{
return this.fields.size();
}
/*
* Returns the List of field which is the whole game board.
* Why you would want this function is beyond me.
* Anyways enjoy :)
* @return the list of fields which represents the whole game board.
*/
public List<Field> getGameBoard()
{
return this.fields;
}
/*
* Returns the field that has the id passed.
* If the id does not exist however, you will receive null in return for the field object.
* @param id the id of the field you are searching for in the board.
* @return the field if found, null otherwise.
*/
public Field getFieldById(int id)
{
for(int i = 0; i < this.getBoardSize(); i++)
if(this.fields.get(i).getId() == id)
return this.fields.get(i);
return null;
}
/*
* Connects two fields together.
* The direction also is used for the calculations of the x and y coordinates.
* If someone awesome out there could only make better graphics,
* the stupid direction thing would be unnecessary.
* The fields must exist, otherwise they will not be connected.
* @param id_a id of the first field.
* @param id_b id of the second field to which field one will be connected to.
* @param direction relative position of second field to first field.
* @return true if the fields were connected, false otherwise.
*/
public boolean connect(int id_a, int id_b, int direction)
{
// SOME INIT VALUES
Field a, b;
a = b = new Field();
boolean a_found, b_found;
a_found = b_found = false;
// SEARCH FOR THE FIELDS ON THE BOARD
for(int i = 0; i < this.getBoardSize(); i++)
{
if(this.fields.get(i).getId() == id_a)
{
a = this.fields.get(i);
a_found = true;
}
else if(this.fields.get(i).getId() == id_b)
{
b_found = true;
b = this.fields.get(i);
}
}
// IF BOTH FIELDS WERE FOUND
if(a_found && b_found)
{
// IF ADDING IS IMPOSSIBLE DUE TO SOME WIRD ERROR
// IF IT IS THEN IT IS YOUR FAULT! FIX IT AND STOP MESSING UP!!! >:(
if(!a.addField(b))
return false;
if(!b.addField(a))
return false;
// SET THE COORDINATES ACCORDING TO DIRECTION
if(direction == TOP)
{
b.setX(a.getX());
b.setY(a.getY() - this.field_size);
}
else if(direction == TOP_RIGHT)
{
b.setX(a.getX() + this.field_size);
b.setY(a.getY() - this.field_size);
}
else if(direction == RIGHT)
{
b.setX(a.getX() + this.field_size);
b.setY(a.getY());
}
else if(direction == BOTTOM_RIGHT)
{
b.setX(a.getX() + this.field_size);
b.setY(a.getY() + this.field_size);
}
else if(direction == BOTTOM)
{
b.setX(a.getX());
b.setY(a.getY() + this.field_size);
}
else if(direction == BOTTOM_LEFT)
{
b.setX(a.getX() - this.field_size);
b.setY(a.getY() + this.field_size);
}
else if(direction == LEFT)
{
b.setX(a.getX() - this.field_size);
b.setY(a.getY());
}
else if(direction == TOP_LEFT)
{
b.setX(a.getX() - this.field_size);
b.setY(a.getY() - this.field_size);
}
return true;
}
return false;
}
/*
* Draws the game board to the graphics passed to it. Only draws the squares of fields, and nothing else.
* @param g the graphics to which to draw the game board.
*/
public void drawFields(Graphics2D g)
{
// WILL EVENTUALLY CHANGE THESE CONSTANT NUMBERS
for(int i = 0; i < this.getBoardSize(); i++)
{
if(i < 7)
this.fields.get(i).draw(g, field_size, Color.WHITE);
else if(i < 16)
this.fields.get(i).draw(g, field_size, Color.CYAN);
else if(i < 24)
this.fields.get(i).draw(g, field_size, Color.MAGENTA);
else if(i < 33)
this.fields.get(i).draw(g, field_size, Color.YELLOW);
else
this.fields.get(i).draw(g, field_size, Color.PINK);
}
}
/*
* Draws the info of the fields on the graphics.
* Call this after you called drawFields().
* @param g graphics to which the fields info will be drawn on.
*/
public void drawInfo(Graphics2D g)
{
g.setColor(Color.BLACK);
for(int i = 0; i < this.getBoardSize(); i++)
this.fields.get(i).drawInfo(g);
}
/*
* Draws the piece on the board. First player goes to top left,
* second player goes to top right,
* third to the bottom left,
* and fourth to the bottom right.
* @param g graphics to which the piece will be drawn on.
* @param piece the piece which you want to draw on the graphics.
* @param spot which place the player is in.
*/
public void drawPiece(Graphics2D g, Piece piece, int spot)
{
if(spot == FIRST)
{
g.setColor(piece.getColor());
g.fillRect(piece.getField().getX(), piece.getField().getY(), this.field_size/2, this.field_size/2);
g.setColor(Color.DARK_GRAY);
g.drawRect(piece.getField().getX(), piece.getField().getY(), this.field_size/2, this.field_size/2);
}
else if(spot == SECOND)
{
g.setColor(piece.getColor());
g.fillRect(piece.getField().getX()+this.field_size/2, piece.getField().getY(), this.field_size/2, this.field_size/2);
g.setColor(Color.DARK_GRAY);
g.drawRect(piece.getField().getX()+this.field_size/2, piece.getField().getY(), this.field_size/2, this.field_size/2);
}
else if(spot == THIRD)
{
g.setColor(piece.getColor());
g.fillRect(piece.getField().getX(), piece.getField().getY()+this.field_size/2, this.field_size/2, this.field_size/2);
g.setColor(Color.DARK_GRAY);
g.drawRect(piece.getField().getX(), piece.getField().getY()+this.field_size/2, this.field_size/2, this.field_size/2);
}
else if(spot == FOURTH)
{
g.setColor(piece.getColor());
g.fillRect(piece.getField().getX()+this.field_size/2, piece.getField().getY()+this.field_size/2, this.field_size/2, this.field_size/2);
g.setColor(Color.DARK_GRAY);
g.drawRect(piece.getField().getX()+this.field_size/2, piece.getField().getY()+this.field_size/2, this.field_size/2, this.field_size/2);
}
}
/*
* Rolls the dice.
* @return random number from 1 to 6.
*/
public int roll()
{
return (this.fate.nextInt(6)+1);
}
/*
* Returns a random number from min(inclusive) to max(inclusive).
* @return a random number within the range of min max (both inclusive).
*/
public int getRandom(int min, int max)
{
return (this.fate.nextInt(max - min+1)+min);
}
/*
* Draws the dice on the game board.
* Will not draw anything if dice is not within range of 1 to 6.
* @param g graphics to which to draw the dice to.
* @param dice what is the current dice value.
*/
public void drawDice(Graphics2D g, int dice)
{
if(dice == 1)
{
g.setColor(Color.WHITE);
g.fillRect(550, 400, field_size, field_size);
g.setColor(Color.BLACK);
g.fillOval(567, 417, 6, 6);
}
else if(dice == 2)
{
g.setColor(Color.WHITE);
g.fillRect(550, 400, field_size, field_size);
g.setColor(Color.BLACK);
g.fillOval(557, 407, 6, 6);
g.fillOval(577, 427, 6, 6);
}
else if(dice == 3)
{
g.setColor(Color.WHITE);
g.fillRect(550, 400, field_size, field_size);
g.setColor(Color.BLACK);
g.fillOval(577, 407, 6, 6);
g.fillOval(567, 417, 6, 6);
g.fillOval(557, 427, 6, 6);
}
else if(dice == 4)
{
g.setColor(Color.WHITE);
g.fillRect(550, 400, field_size, field_size);
g.setColor(Color.BLACK);
g.fillOval(557, 407, 6, 6);
g.fillOval(557, 427, 6, 6);
g.fillOval(577, 407, 6, 6);
g.fillOval(577, 427, 6, 6);
}
else if(dice == 5)
{
g.setColor(Color.WHITE);
g.fillRect(550, 400, field_size, field_size);
g.setColor(Color.BLACK);
g.fillOval(557, 407, 6, 6);
g.fillOval(557, 427, 6, 6);
g.fillOval(567, 417, 6, 6);
g.fillOval(577, 407, 6, 6);
g.fillOval(577, 427, 6, 6);
}
else if(dice == 6)
{
g.setColor(Color.WHITE);
g.fillRect(550, 400, field_size, field_size);
g.setColor(Color.BLACK);
g.fillOval(557, 407, 6, 6);
g.fillOval(557, 427, 6, 6);
g.fillOval(557, 417, 6, 6);
g.fillOval(577, 417, 6, 6);
g.fillOval(577, 407, 6, 6);
g.fillOval(577, 427, 6, 6);
}
}
}