-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
188 lines (147 loc) · 4.48 KB
/
Board.java
File metadata and controls
188 lines (147 loc) · 4.48 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
/**
* Keeps track of the game board's state
*
* TILE ID LIST:
*
* 0: Water
* 1: Plains/Sheep
* 2: Forest
* 3: Bricks
* 4: Wheat
* 5: Ore
* 6: Sand
*/
import java.util.Random;
public class Board {
// constants for the dimensions
public final int GAME_ROWS = 5;
// which tiles are where
int[][] board = {
{ 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0 }
};
// which numbers are on each tile
int[][] numbers = {
{ 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0 }
};
// which settlement spaces are already filled with settlements
int[][] settlements = {
{ 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0 }
};
// same thing but for cities
int[][] cities = {
{ 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0 }
};
// which road spaces are already filled
int[][] roads = {
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 }
};
/**
* Generate a new board
*/
public Board() {
// board dimensions are 3 then 4 then 5 then 4 then 3 tiles
// generate a new board!
generateBoard();
// now place the numbers
generateNumbers();
}
/**
* Creates a board. Randomly fills it, but limits the count each tile can have
*/
private void generateBoard() {
// the count of how many of each tile are currently placed.
int[] tilesPlaced = { 0, 0, 0, 0, 0, 0, 0 };
int[] maxValues = { 0, 4, 4, 3, 4, 3, 1 };
// create an instance of random for generation
Random random = new Random();
// now fill up each space
for (int y = 0; y < board.length; y++) {
for (int x = 0; x < board[y].length; x++) {
// if we're at the centre tile, place the desert
if (x == 2 && y == 2) {
board[y][x] = 6;
} else {
// otherwise, loop until we generate a valid number to place
int tileToPlace;
do {
// pick a random number between one and five (valid tiles to place)
// 4 + 1 so we have between 1 and 5
tileToPlace = random.nextInt(5) + 1;
// then check if there's still those tiles left to be placed
} while (tilesPlaced[tileToPlace] >= maxValues[tileToPlace]);
// finally, we can set the tile and increment the counter
board[y][x] = tileToPlace;
tilesPlaced[tileToPlace]++;
}
}
}
}
/**
* Using the same process as generateBoard, fill each space with a roll number
*/
private void generateNumbers() {
// the count of how many of each number are currently placed.
int[] numbersPlaced = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] maxValues = { 0, 0, 1, 2, 2, 2, 2, 0, 2, 2, 2, 2, 1 };
// create an instance of random for generation
Random random = new Random();
// now fill up each space
for (int y = 0; y < numbers.length; y++) {
for (int x = 0; x < numbers[y].length; x++) {
// if we're at the centre tile, place the null tile
if (x == 2 && y == 2) {
numbers[y][x] = 0;
} else {
// loop until we generate a valid number to place
int numberToPlace;
do {
// pick a random number between zero and 12 (valid tiles to place)
// anything invalid will not be able to continue so we're good with this
numberToPlace = random.nextInt(13);
// then check if there's still those tiles left to be placed
} while (numbersPlaced[numberToPlace] >= maxValues[numberToPlace]);
// finally, we can set the number and increment the counter
numbers[y][x] = numberToPlace;
numbersPlaced[numberToPlace]++;
}
}
}
}
/**
* Get one row from the board
*
* @param row The index of the row to get
* @return The row
*/
public int[] getRow(int row) {
return board[row];
}
}