-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSilkBag.java
81 lines (72 loc) · 2.18 KB
/
SilkBag.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
/**
* Creates a silk bag to be used in the game
* @author Joe Sell
* @version 1.0
*/
public class SilkBag {
private TileType[] actionTiles = {TileType.DoubleMove, TileType.BackTrack, TileType.Ice, TileType.Fire};
private TileType[] placeableTiles = {TileType.Straight, TileType.Corner, TileType.TShaped};
private int[] numActionTiles = {1, 1, 1, 1};
private int[] numPlaceableTiles = {1, 1, 1};
public SilkBag(int[] numActionTiles, int[] numPlaceableTiles) {
this.numActionTiles = numActionTiles;
this.numPlaceableTiles = numPlaceableTiles;
}
/**
* Draws a tile from the bag
*/
public TileType draw() {
int numTiles = lstTotal(numActionTiles, numActionTiles.length) + lstTotal(numPlaceableTiles, numPlaceableTiles.length);
if (numTiles == 0) {
return null;
} else {
int tileNum = randInt(numTiles);
if (tileNum <= lstTotal(numActionTiles, numActionTiles.length)) {
int tileType = pickType(numActionTiles, numActionTiles.length);
numActionTiles[tileType] -= 1;
return actionTiles[tileType];
} else {
int tileType = pickType(numPlaceableTiles, numPlaceableTiles.length);
numPlaceableTiles[tileType] -= 1;
return placeableTiles[tileType];
}
}
}
/**
* Draws a placeable tile from the bag
*/
public TileType drawPlaceable() {
int tileType = pickType(numPlaceableTiles, numPlaceableTiles.length);
numPlaceableTiles[tileType] -= 1;
return placeableTiles[tileType];
}
public int[] getNumActionTiles() {
return numActionTiles;
}
public int[] getNumPlaceableTiles() {
return numPlaceableTiles;
}
private static int pickType(int[] tiles, int tileTypes) {
int tileNum = randInt(lstTotal(tiles, tiles.length));
int prevTiles = 0;
for(int i = 0; i < tileTypes ; i++) {
if(tileNum <= tiles[i] + prevTiles) {
return i;
} else {
prevTiles += tiles[i];
}
}
return 4;
}
private static int randInt(int a) {
int x = ((int)(Math.random() * (a))) + 1;
return x;
}
private static int lstTotal(int[] tiles, int tileTypes) {
int total = 0;
for(int i = 0; i < tileTypes ; i++) {
total += tiles[i];
}
return total;
}
}