-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConnect4.java
More file actions
348 lines (296 loc) · 11.5 KB
/
Connect4.java
File metadata and controls
348 lines (296 loc) · 11.5 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
package Dropbox;
/**
* Created by cicean on 9/10/2018.
*/
import java.util.*;
public class Main {
static final String[] PLAYER_SYMBOLS = new String[]{"X", "O", "A", "B", "C", "D", "E", "Y", "Z", "H"};
public final int numRows;
public final int numCols;
private final int[][] board;
private final int humans;
private final int robots;
private final int players;
class Move {
public final int playerIndex;
public final int row;
public final int col;
public Move(int playerIndex, int row, int col) {
this.playerIndex = playerIndex;
this.row = row;
this.col = col;
}
}
public Main(int rows, int cols, int humans, int robots) {
this.board = new int[rows][cols];
for (int i = 0; i < rows; i++) {
Arrays.fill(board[i], -1);
colToIndex.put(i,i);
String col = String.valueOf(i+1);
boardindex.add(i,col);
}
this.numRows = rows;
this.numCols = cols;
this.humans = humans;
this.robots = robots;
this.players = humans + robots;
}
// public Main() {
// }
/**
* This method is called after each move.
*
* @param board = 2 dimensional index of player indexes which are 0 or greater
* @param lastMove = Move object representing the last move made
* @return index of winning player; -1 if no winner detected
*/
public int winningPlayerIndex(int[][] board, Move lastMove) {
// check vertical
int total = 1 + countSame(board, lastMove, 1, 0, 3) + countSame(board, lastMove, -1, 0, 3);
if( total >= 4 ) {
return lastMove.playerIndex;
}
// check horizontal
total = 1 + countSame(board, lastMove, 0, 1, 3) + countSame(board, lastMove, 0, -1, 3);
if( total >= 4 ) {
return lastMove.playerIndex;
}
// check up-and-left diagonal
total = 1 + countSame(board, lastMove, -1, -1, 3) + countSame(board, lastMove, 1, 1, 3);
if( total >= 4 ) {
return lastMove.playerIndex;
}
// check up-and-right diagonal
total = 1 + countSame(board, lastMove, -1, 1, 3) + countSame(board, lastMove, 1, -1, 3);
if( total >= 4 ) {
return lastMove.playerIndex;
}
return -1;
}
public int countSame(int[][] board, Move lastMove, int rowDelta, int colDelta, int maxCount) {
int count = 0;
int currentRow = lastMove.row;
int currentCol = lastMove.col;
int maxRow = board.length;
int maxCol = board[0].length;
while( true ) {
currentRow = currentRow + rowDelta;
currentCol = currentCol + colDelta;
boolean invalidRow = currentRow < 0 || currentRow >= maxRow;
boolean invalidCol = currentCol < 0 || currentCol >= maxCol;
if (invalidRow || invalidCol) {
return count;
}
if (board[currentRow][currentCol] == lastMove.playerIndex) {
count += 1;
if( count >= maxCount ) {
return count;
}
} else {
break;
}
}
return count;
}
private Map<Integer, Integer> colToIndex = new HashMap<>();
private List<String> boardindex = new ArrayList<>();
//init in constructor
//
public String getRobots() {
Random random = new Random();
for (int y = 0; y < board[0].length; y++) {
if (board[0][y] > -1) {
int index = colToIndex.get(y);
if (index != boardindex.size() - 1) {
boardindex.add(index, boardindex.get(boardindex.size() - 1));
int col = Integer.parseInt(boardindex.get(boardindex.size() - 1));
colToIndex.put(col - 1,index);
boardindex.remove(boardindex.size() - 1);
colToIndex.remove(y);
}
}
}
int index = random.nextInt(boardindex.size());
return boardindex.get(index);
}
public String getUserInput(String prompt) {
// create a scanner so we can read the command-line input
Scanner scanner = new Scanner(System.in);
// prompt for the user's name
System.out.print(prompt);
// get their input as a String
String userInput = scanner.next();
return userInput;
}
public void printBoard(int[][] board, String[] playerSymbols) {
System.out.println("\n" + boardTerminalString(board, PLAYER_SYMBOLS) + "\n");
}
public String boardTerminalString(int[][] board, String[] playerSymbols) {
StringBuilder sb = new StringBuilder();
sb.append(" ");
for (int col = 0; col < numCols; col++) {
sb.append(String.format("%1$" + 3 + "s", col + 1 + ""));
}
sb.append("\n");
for (int row = 0; row < numRows; row++) {
sb.append(String.format("%1$" + 3 + "s", row + 1 + ""));
for (int col = 0; col < numCols; col++) {
int playerIndex = board[row][col];
String playerSymbol = playerIndex > -1 ? PLAYER_SYMBOLS[playerIndex] : ".";
playerSymbol = String.format("%1$" + 3 + "s", playerSymbol);
sb.append(playerSymbol);
}
sb.append("\n");
}
return sb.toString();
}
public boolean isValidMove(Move move) {
if (move == null || move.row < 0 || move.col < 0) {
return false;
}
if (move.row >= numRows || move.col >= numCols) {
return false;
}
// can't place over existing piece
if (board[move.row][move.col] > -1) {
return false;
}
// check that we are stacking if not on bottom row
if (move.row < numRows - 1 && board[move.row + 1][move.col] < 0) {
return false;
}
return true;
}
public void updateBoard(int[][] board, Move move) {
board[move.row][move.col] = move.playerIndex;
}
/**
* @param playerIndex
* @param moveString
* @return Move object or null if unable to parse
*/
public Move parseMove(int playerIndex, String moveString) {
try {
//String[] tokens = moveString.split(",");
// if (tokens.length < 2) {
// return null;
// }
//int row = Integer.parseInt(tokens[0].trim());
int col = Integer.parseInt(moveString);
int row = 0;
for (int x = board.length - 1; x >= 0; x--) {
if (board[x][col - 1] == -1) {
row = x;
break;
}
}
System.out.println("playerIndex = " + playerIndex + "," + "row = " + row);
return new Move(playerIndex, row, col - 1);
} catch (Exception e) {
return null;
}
}
public void start() {
int moveCount = 0;
while (true) {
for (int currentPlayerIndex = 0; currentPlayerIndex < players; currentPlayerIndex++) {
printBoard(board, PLAYER_SYMBOLS);
Move move = null;
while (true) {
String moveString;
if (currentPlayerIndex == 1 ) {
moveString = getRobots();
} else {
moveString = getUserInput("Player #" + (currentPlayerIndex + 1) + " Move [ row, col ]: ");
}
move = parseMove(currentPlayerIndex, moveString);
if (isValidMove(move)) {
updateBoard(board, move);
moveCount++;
break;
} else {
System.out.println("INVALID MOVE");
}
}// while
if (currentPlayerIndex == winningPlayerIndex(board, move)) {
printBoard(board, PLAYER_SYMBOLS);
System.out.println("\n\nPlayer #" + (currentPlayerIndex + 1) + " wins!\n\n");
System.exit(0);
}
if (moveCount == numRows * numCols) {
printBoard(board, PLAYER_SYMBOLS);
System.out.println("\n\nIt's a draw!");
System.exit(0);
}
}// for
}// while
}
public List<String> buildOrder(String target, Map<String, List<String>> dependences) {
if (target == null || dependences == null) return null;
Map<String, Integer> indegree = new HashMap<>();
Map<String, List<String>> prebuildtotarget = new HashMap<>();
Queue<String> queue = new LinkedList<>();
Set<String> uniquedependence = new HashSet<>();
for (Map.Entry<String, List<String>> entry : dependences.entrySet()) {
indegree.put(entry.getKey(), entry.getValue().size());
uniquedependence.add(entry.getKey());
for (String prebuilddependence : entry.getValue()) {
prebuildtotarget.computeIfAbsent(prebuilddependence, x -> new ArrayList<String>()).add(entry.getKey());
uniquedependence.add(prebuilddependence);
}
}
for (String dep : uniquedependence) {
if (!indegree.containsKey(dep)) {
queue.offer(dep);
}
}
//System.out.print("length of " + uniquedependence.size());
List<String> results = new ArrayList<>();
while (!queue.isEmpty()) {
String curr = queue.poll();
results.add(curr);
System.out.println("curr = " + curr);
if (!prebuildtotarget.containsKey(curr)) {
continue;
}
for (String next : prebuildtotarget.get(curr)) {
if (indegree.get(next) - 1 == 0) {
queue.offer(next);
}
indegree.put(next, indegree.get(next) - 1);
}
}
//System.out.print("result" + results.size());
return results.size() != uniquedependence.size() ? new ArrayList<>() : results;
}
public static void main(String[] args) throws Throwable {
int rows = int32OrDefault(System.getProperty("rows"), 6);
int cols = int32OrDefault(System.getProperty("cols"), 7);
int humans = int32OrDefault(System.getProperty("humans"), 2);
int robots = int32OrDefault(System.getProperty("robots"), 0);
System.out.println("\n>>>>> Board = " + rows + " rows x " + cols + " cols; Human Players = " + humans + "; Robot Players = " + robots + "\n\n");
Main game = new Main(rows, cols, humans, robots);
game.start();
// Main object = new Main();
// String target = "4";
// Map<String, List<String>> deps = new HashMap<>();
// List<String> a = new ArrayList<>();
// a.add("3");
// a.add("2");
// deps.put("4", a);
// List<String> b = new ArrayList<>();
// b.add("1");
// deps.put("3",b);
// deps.put("2",b);
// List<String> res = object.buildOrder("4",deps);
// String col = String.valueOf(1+1);
// System.out.print(col);
// System.out.print("res" + Arrays.toString(res.toArray()));
}
public static int int32OrDefault(String in, int defaultValue) {
if (in == null || in.trim().length() == 0) {
return defaultValue;
}
return Integer.parseInt(in);
}
}