-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHexed.java
More file actions
273 lines (244 loc) · 7.87 KB
/
Copy pathHexed.java
File metadata and controls
273 lines (244 loc) · 7.87 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
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Stack;
public class Hexed {
HexBoard hex = new HexBoard();
char firstMove;
char teamColor;
char turn;
int redScore;
int greenScore;
boolean initialMove = true;
State state;
Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
Hexed program = new Hexed();
program.run();
}
public void run() {
start();
}
public void start() {
hex.generateGrid();
System.out.print("====Start Game====\n");
System.out.print("Column: ");
int col = Integer.parseInt(sc.nextLine());
System.out.print("Row: ");
int row = Integer.parseInt(sc.nextLine());
System.out.print("Color[g,r]: ");
char color = sc.nextLine().charAt(0);
hex.initializeGame(col, row, color);
System.out.print("First Move[g,r]: ");
firstMove = sc.nextLine().charAt(0);
turn = firstMove;
System.out.println("==================");
System.out.print("Team Color[g,r]: ");
teamColor = sc.nextLine().charAt(0);
hex.printHexGrid();
System.out.println("==================");
boolean cont = true;
int utility = (teamColor==color)? -100 : 100;
state = new State(utility,null,teamColor,hex,null);
int limit = 6;
do {
int[] numberOfMoves = playGame(limit,state);
int teamNum = numberOfMoves[0];
int opNum = numberOfMoves[1];
if (bothHexed(teamNum, opNum) == true) {
cont = false;
}
hex.printHexGrid();
System.out.println("===Scores===\nRed: " + redScore + "\nGreen: " + greenScore);
} while (cont);
}
public Index makeMove(State state, int limit){
Stack<State> frontier = new Stack<State>();
ArrayList<State> children = generateChildren(state);
Index move = null;
int maxUtility = -100;
for(int i=0;i<children.size();i++){
frontier.push(children.get(i));
}
do{
State currState = frontier.pop();
if(currState.getDepth()<=limit){
children = generateChildren(currState);
for(int i=0; i<children.size(); i++){
frontier.push(children.get(i));
}
if((currState.getMove()== null && currState.getParent().getMove()==null) || currState.getDepth() == limit){
currState.setUtility(computeUtility(currState.getBoard()));
while(currState.getParent()!=state){
if(currState.getParent().getIden()==teamColor){
if(currState.getParent().getUtility() < currState.getUtility()) currState.getParent().setUtility(currState.getUtility());
}
else{
if(currState.getParent().getUtility() > currState.getUtility()) currState.getParent().setUtility(currState.getUtility());
}
currState = currState.getParent();
if(currState.getParent() == state){
maxUtility = Math.max(maxUtility, currState.getUtility());
if(maxUtility == currState.getUtility()){
move = currState.getMove();
}
}
}
}
}
}while(!frontier.isEmpty());
return move;
}
public ArrayList<State> generateChildren(State parent){
ArrayList<State> children = new ArrayList<State>();
ArrayList<Index> legalMoves = parent.getBoard().generateMoves(parent.getIden());
char childColor = parent.getBoard().negateColor(parent.getIden());
if(legalMoves.size()==0){
int utility = (parent.getIden()==teamColor)? 100 : -100;
HexBoard childBoard = createBoard(parent.getBoard().getHex());
children.add(new State(utility,parent,childColor,childBoard,null));
}
else {
for(int i=0; i<legalMoves.size(); i++){
int utility = (parent.getIden()==teamColor)? 100 : -100;
HexBoard childBoard = createBoard(parent.getBoard().getHex());
ArrayList<Index> moves = getChosenMove(legalMoves, legalMoves.get(i).getCol(), legalMoves.get(i).getRow());
childBoard.getHex()[legalMoves.get(i).getCol()][legalMoves.get(i).getRow()].setColor(childColor);
childBoard.fixSandwichedColor(moves, parent.getIden());
children.add(new State(utility,parent,childColor,childBoard,legalMoves.get(i)));
}
}
return children;
}
public int computeUtility(HexBoard board){
char oppColor = board.negateColor(teamColor);
int oppTiles = 0;
int teamTiles = 0;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 7; j++) {
if(board.getHex()[i][j].getColor() == teamColor) teamTiles++;
if(board.getHex()[i][j].getColor() == oppColor) oppTiles++;
}
}
return teamTiles - oppTiles;
}
public HexBoard createBoard(HexCell[][] hexcells){
HexBoard newBoard = new HexBoard();
HexCell[][] cells = new HexCell[9][7];
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 7; j++) {
cells[i][j] = new HexCell();
cells[i][j].setCol(i);
cells[i][j].setRow(j);
cells[i][j].setColor(hexcells[i][j].getColor());
cells[i][j].setValidCell(hexcells[i][j].isValidCell());
}
}
newBoard.setHex(cells);
return newBoard;
}
public void generateScores() {
redScore = 0;
greenScore = 0;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 7; j++) {
if (hex.hex[i][j].getColor() == 'r') {
redScore++;
} else if (hex.hex[i][j].getColor() == 'g') {
greenScore++;
}
}
}
}
public int[] playGame(int limit,State state) {
ArrayList<Index> opponentMoves = new ArrayList<Index>();
ArrayList<Index> teamMoves = new ArrayList<Index>();
ArrayList<Index> moves = new ArrayList<Index>();
if (teamColor == turn) {// our turn
teamMoves = hex.generateMoves(turn);
hex.showPossibleMoves(teamMoves);
if (isHexed(teamMoves)) {
turn = hex.negateColor(turn);
opponentMoves = hex.generateMoves(turn);
hex.showPossibleMoves(opponentMoves);
} else {
limit += 2;
Index move = makeMove(state,limit);
if(move==null) move = teamMoves.get(0);
moves = getChosenMove(teamMoves, move.getCol(), move.getRow());
System.out.println("\nMove:");
System.out.println("col: " + move.getCol() + " ; row:" + move.getRow());
hex.hex[move.getCol()][move.getRow()].setColor(teamColor);
hex.fixSandwichedColor(moves, teamColor);
generateScores();
turn = hex.negateColor(turn);
}
} else {
opponentMoves = hex.generateMoves(turn);
hex.showPossibleMoves(opponentMoves);
if (isHexed(opponentMoves)) {
turn = hex.negateColor(turn);
teamMoves = hex.generateMoves(turn);
hex.showPossibleMoves(teamMoves);
} else {
opponentMoves = hex.generateMoves(turn);
Index opMove = opponentMoves.get((int) (Math.random() * opponentMoves.size()-1));
int opCol = opMove.getCol();
int opRow = opMove.getRow();
for (int x = 0; x < opponentMoves.size(); x++) {
if (opponentMoves.get(x).getCol() == opCol && opponentMoves.get(x).getRow() == opRow) {
opMove = new Index(opCol, opRow, opponentMoves.get(x).getDirection());
}
}
hex.hex[opCol][opRow].setColor(hex.negateColor(teamColor));
moves = getChosenMove(opponentMoves, opCol, opRow);
hex.fixSandwichedColor(moves, hex.negateColor(teamColor));
generateScores();
turn = hex.negateColor(turn);
}
}
state = new State(-100,state,turn,hex,null);
int[] numMoves = { teamMoves.size(), opponentMoves.size() };
return numMoves;
}
public ArrayList<Index> getChosenMove(ArrayList<Index> pMoves, int col, int row) {
ArrayList<Index> moves = new ArrayList<Index>();
for (int i = 0; i < pMoves.size(); i++) {
if (col == pMoves.get(i).getCol() && row == pMoves.get(i).getRow()) {
moves.add(pMoves.get(i));
}
}
return moves;
}
public boolean isHexed(ArrayList<Index> moves) {
if (moves.size() == 0) {
System.out.println("Hexed!");
return true;
} else {
return false;
}
}
public boolean bothHexed(int team, int op) {
if (team == 0 && op == 0) {
System.out.println("Game Ends!");
return true;
} else {
return false;
}
}
public int evaluate(HexBoard board) {
return firstMove;
}
public boolean isMovesLeft(HexBoard board) {
if (board.bothHexed()) {
return false;
}
return true;
}
public Index findBestMove(HexBoard board) {
Index bestMove = new Index();
int bestVal = -1000;
bestMove.row = -1;
bestMove.col = -1;
return bestMove;
}
}