-
Notifications
You must be signed in to change notification settings - Fork 0
/
BoardState.java
177 lines (165 loc) · 5.37 KB
/
BoardState.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
169
170
171
172
173
174
175
176
177
import java.awt.*;
import java.util.*;
public abstract class BoardState {
/*** Game Information ***/
//Phases
public static final String[] PHASE_NAMES = {"Select a Territory", "Reinforce", "Reinforce", "Attack", "Fortify"};
public static final int CHOOSE_TERR = 0;
public static final int INITIAL_REINF = 1;
public static final int REINF = 2;
public static final int ATTACK = 3;
public static final int FORTIFY = 4;
public static final int MAX_PLAYER = 6;
//Board
public static Territory[] territories;
public static Player[] players;
public static GameBoard BOARD;
public static int startTroops;
public static int reinf;
public static int turn;
public static int phase;
public static int[] dice;
public static Player pTurn(){return players[turn % players.length];}
//Game mode
public static boolean supplyLine;
/*** Set-Up ***/
public static void startGame(){
//Turn
turn = 0;
//Dice
dice = new int[5];
//Territories
territories = new Territory[GameBoard.LOCATIONS.length];
for(int i = 0; i < territories.length; i++)
territories[i] = new Territory(i);
//Player test
players = new Player[6];
startTroops = 8;
supplyLine = true;
players[0] = new HumanPlayer(1, "Alice", Color.RED);
players[1] = new HumanPlayer(2, "Bob", Color.GREEN);
players[2] = new HumanPlayer(2, "Cate", Color.MAGENTA);
players[3] = new HumanPlayer(4, "Daniel", Color.CYAN);
players[4] = new HumanPlayer(5, "Eli", Color.BLUE);
players[5] = new HumanPlayer(6, "Falstaff", Color.ORANGE);
//Distribute territories
randTerr();
phase = 1;
}
public static void randTerr(){
ArrayList<Territory> ter = new ArrayList<Territory>();
for(Territory t: territories)
ter.add(t);
while(ter.size() > 0){
for(Player p: players){
int i = (int)(Math.random() * ter.size());
ter.get(i).occupy(p, 1);
ter.remove(i);
if(ter.size() <= 0)
return;
}
}
}
/*** Process Turns ***/
public static void nxtTurn(){
turn++;
if(phase == INITIAL_REINF){
int i = 0;
while(pTurn().getTroops() >= startTroops && i <= players.length){
turn++;
i++;
}
if(pTurn().getTroops() >= startTroops){
turn = 0;
phase = REINF;
}
}
while(phase != INITIAL_REINF && pTurn().getOccupiedTerritories().size() == 0)
turn++;
if(phase == FORTIFY)
phase = REINF;
if(phase == REINF){
reinf = Math.max(pTurn().getOccupiedTerritories().size() / 3, 3) + pTurn().continentBonus();
pTurn().placeReinforcements();
}
}
/*** Battle ***/
public static void attack(Territory a, Territory d, int t){
//Check for validity
if(t + 1 > a.getTroops() || t > 3)
return;
if(!a.isAdjacent(d) || a.getOccupation() == d.getOccupation())
return;
//Roll dice
dice = new int[5];
for(int i = 0; i < t; i++)
dice[i] = roll();
dice[3] = roll();
if(d.getTroops() > 1)
dice[4] = roll();
Arrays.sort(dice, 0, 3);
Arrays.sort(dice, 3, 5);
//Deal damage
for(int i = 0; i < 2; i++)
if(dice[2 - i] > 0 && dice[4 - i] > 0)
if(dice[2 - i] > dice[4 - i]){
d.getOccupation().troopsL++;
a.getOccupation().troopsK++;
if(d.getTroops() > 1)
d.occupy(d.getOccupation(), d.getTroops() - 1);
else {
d.occupy(a.getOccupation(), t);
a.occupy(a.getOccupation(), a.getTroops() - t);
a.getOccupation().troopPrompt(t);
a.getOccupation().battlesW++;
}
}
else{
d.getOccupation().troopsK++;
a.getOccupation().troopsL++;
a.occupy(a.getOccupation(), a.getTroops() - 1);
}
//Paint menu
if(BOARD.getAttackMenu() != null)
BOARD.getAttackMenu().reset();
}
private static int roll(){
return (int)(Math.random() * Integer.MAX_VALUE) % 6 + 1;
}
/*** Draw Board ***/
public static void paintAdjacent(Graphics g, Territory t){
if(!BOARD.showGraph)
return;
for(int i = 0; i < territories.length; i++)
if(t.isAdjacent(territories[i])){
if(t.getOccupation() == null)
g.setColor(GameBoard.LINE);
else if(t.getOccupation() == territories[i].getOccupation() && !BOARD.flood)
g.setColor(t.getOccupation().getColor());
else
g.setColor(GameBoard.LINE);
((Graphics2D)g).setStroke(new BasicStroke(Territory.RAD() / 5));
if(t.getName().equals("Alaska") && territories[i].getName().equals("Kamchatka")){
g.drawLine(t.getCLocX(), t.getCLocY(), BOARD.getImgCorner()[0] + Territory.RAD() / 5, t.getCLocY());
g.drawLine(territories[i].getCLocX(), territories[i].getCLocY(), BOARD.getImgCorner()[0] + BOARD.getImgDim()[0] - Territory.RAD() / 5, territories[i].getCLocY());
}
else if(t.getName().equals("Kamchatka") && territories[i].getName().equals("Alaska")){
g.drawLine(territories[i].getCLocX(), territories[i].getCLocY(), BOARD.getImgCorner()[0] + Territory.RAD() / 5, territories[i].getCLocY());
g.drawLine(t.getCLocX(), t.getCLocY(), BOARD.getImgCorner()[0] + BOARD.getImgDim()[0] - Territory.RAD() / 5, t.getCLocY());
}
else
g.drawLine(t.getCLocX(), t.getCLocY(), territories[i].getCLocX(), territories[i].getCLocY());
}
}
public static void paint(Graphics g){
//Flood
for(Territory t: territories)
t.floodTerr(g);
//Adjacency graph
for(Territory t: territories)
paintAdjacent(g, t);
//Territories
for(Territory t: territories)
t.paint(g);
}
}