-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConnect4GeniusBot.java
More file actions
165 lines (154 loc) · 4.54 KB
/
Connect4GeniusBot.java
File metadata and controls
165 lines (154 loc) · 4.54 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
import java.util.*;
import wheels.users.*;
public class Connect4GeniusBot {
private Connect4Board infoBoard;
private int next_move, team;
public Connect4GeniusBot(Connect4Board infoBoard, int team){
this.infoBoard = infoBoard;
this.team = team;
}
public void makeMove() {
if (!infoBoard.isFilled()) {
infoBoard.makeMove(this.getBestPossibleMove());
}
}
private int[] getPossibleMoves(int[][] tempBoard){
int size = 0;
for (int c = 0; c < 7; c++){
if (canMove(c, tempBoard))
size++;
}
int[] ans = new int[size];
int index = 0;
for (int c = 0; c < 7; c++){
if (canMove(c, tempBoard)){
ans[index++] = c;
}
}
return ans;
}
public boolean canMove(int c, int[][] tempBoard){
return tempBoard[0][c] == 0;
}
public void printBoard(int[][] grid) {
for (int r = 0; r < 6; r++) {
for (int c = 0; c < 7; c++) {
if (grid[r][c] == 0) {
System.out.print("e");
} else if (grid[r][c] == 1) {
System.out.print("r");
} else if (grid[r][c] == 2){
System.out.print("y");
}
else {
System.out.print("q");
}
}
System.out.println();
}
System.out.println();
}
private int getBestPossibleMove(){
int[] next_possible_moves = getPossibleMoves(infoBoard.getIntArray());
int[][] tempInfoBoard = infoBoard.getIntArray();
int ans = -1;
int highest_val = -1001;
for (int c : next_possible_moves){
this.makeMove(c, tempInfoBoard, team);
if (this.isWinner(tempInfoBoard, team)){
return c;
}
int tempAns = getBestMoveHelper((team % 2) + 1, tempInfoBoard);
if (tempAns > highest_val){
ans = c;
highest_val = tempAns;
}
this.makeMove(c, tempInfoBoard, 0);
}
return ans;
}
public boolean isWinner(int[][] grid, int team){
boolean ans = false;
// check for a horizontal win
for (int r=0; r<6; r++) {
for (int c=0; c<4; c++) {
if (grid[r][c] == team &&
grid[r][c] == grid[r][c+1] &&
grid[r][c] == grid[r][c+2] &&
grid[r][c] == grid[r][c+3]) {
ans = true;
}
}
}
// check for a vertical win
for (int r=0; r<3; r++) {
for (int c=0; c<7; c++) {
if (grid[r][c] == team &&
grid[r][c] == grid[r + 1][c] &&
grid[r][c] == grid[r + 2][c] &&
grid[r][c] == grid[r + 3][c]) {
ans = true;
}
}
}
// check for a diagonal win (positive slope)
for (int r=0; r<3; r++) {
for (int c=0; c<4; c++) {
if (grid[r][c] == team &&
grid[r][c] == grid[r+1][c+1] &&
grid[r][c] == grid[r+2][c+2] &&
grid[r][c] == grid[r+3][c+3]) {
ans = true;
}
}
}
// check for a diagonal win (negative slope)
for (int r=3; r<6; r++) {
for (int c=0; c<4; c++) {
if (grid[r][c] == team &&
grid[r][c] == grid[r-1][c+1] &&
grid[r][c] == grid[r-2][c+2] &&
grid[r][c] == grid[r-3][c+3]) {
ans = true;
}
}
}
return ans;
}
public boolean makeMove(int col, int[][] grid, int team) {
if (team == 0){
for (int i = 0; i < 6; i++){
if (grid[i][col] != 0){
grid[i][col] = 0;
return true;
}
}
}
for (int i = 5; i >= 0; i--) {
if (grid[i][col] == 0) {
grid[i][col] = team;
return true;
}
}
return false;
}
private int getBestMoveHelper(int player, int[][] tempBoard){
int[] next_possible_moves = getPossibleMoves(tempBoard);
if (next_possible_moves.length == 0)
return 0;
int bestValue = -1000;
int other_player = (player % 2) + 1;
for (int col : next_possible_moves){
makeMove(col, tempBoard, player);
if (isWinner(tempBoard, player)){
makeMove(col, tempBoard, 0);
return 1000;
}
bestValue = Math.max(bestValue, -getBestMoveHelper(other_player, tempBoard));
makeMove(col, tempBoard, 0);
if (bestValue == 1000)
break;
}
return bestValue;
}
}