-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnect4.java
190 lines (163 loc) · 5.2 KB
/
Connect4.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
178
179
180
181
182
183
184
185
186
187
188
189
190
import java.util.LinkedList;
import java.util.List;
public class Connect4 {
//create board state
int[][] board = new int[6][7];
//create List to track moves
List<String> moveHistory = new LinkedList<String>();
int winner = 0;
boolean player1 = true;
public Connect4() {
resetGame();
}
//method for placing a piece given player and column
public boolean putPiece(int column) {
int x = 5;
int y = column;
while (board[x][y] != 0){
x--;
}
if (x >= 0) {
if (player1) {
board[x][y] = 1;
String entry = Integer.toString(1) + " " + Integer.toString(x) + " " + Integer.toString(y);
moveHistory.add(entry);
} else {
board[x][y] = 2;
String entry = Integer.toString(2) + " " + Integer.toString(x) + " " + Integer.toString(y);
moveHistory.add(entry);
}
}
player1 = !player1;
return true;
}
public int getPiece(int j, int i) {
return board[i][j];
}
//method for reading history entries + extracting values from them
public int getPlayer() {
String entry = moveHistory.get(moveHistory.size()-1);
String[] split = entry.split(" ");
return Integer.parseInt(split[0]);
}
public int getX() {
String entry = moveHistory.get(moveHistory.size()-1);
String[] split = entry.split(" ");
return Integer.parseInt(split[1]);
}
public int getY() {
String entry = moveHistory.get(moveHistory.size()-1);
String[] split = entry.split(" ");
return Integer.parseInt(split[2]);
}
//method for undoing a move (removes from history and clears location on board
public void undo() {
if (!checkwin()) {
int x = getX();
int y = getY();
moveHistory.remove(moveHistory.size()-1);
board[x][y] = 0;
player1 = !player1;
}
}
//method for resetting board and history
public void resetGame() {
moveHistory.clear();
winner = 0;
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) {
board[i][j] = 0;
}
}
}
//checks for a win
public boolean checkwin() {
int x = getX();
int y = getY();
int player = getPlayer();
int count = 0;
//horizontal check
for (int i = 0; i < 6; i++) {
if (board[x][i] == player) {
count++;
} else {
count = 0;
}
if (count >= 4) {
winner = player;
return true;
}
}
//vertical check
for (int i = 0; i < 6; i++) {
if (board[i][y] == player) {
count++;
} else {
count = 0;
}
if (count >= 4) {
winner = player;
return true;
}
}
//diagonal check like this: /
for (int i = 3; i < 6; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == player && board[i-1][j+1] == player
&& board[i-2][j+2] == player && board[i-3][j+3] == player) {
return true;
}
}
}
//diagonal check like this: \
for (int i = 3; i < 6; i++) {
for (int j = 3; j < 7; j++) {
if (board[i][j] == player && board[i-1][j-1] == player
&& board[i-2][j-2] == player && board[i-3][j-3] == player) {
return true;
}
}
}
return false;
}
//method for printing the board
public void printGameState() {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
System.out.print(board[i][j]);
if (j < 7) {
System.out.print(" | ");
}
}
if (i < 6) {
System.out.println("\n---------------------------");
}
}
}
public int getWinner() {
return winner;
}
public int getMoves () {
return moveHistory.size();
}
/* public static void main(String[] args) {
Board b = new Board();
b.putPiece(1);
b.printGameState();
b.putPiece(2);
b.printGameState();
b.putPiece(1);
b.printGameState();
b.putPiece(2);
b.printGameState();
b.putPiece(1);
b.printGameState();
b.putPiece(2);
b.printGameState();
b.putPiece(1);
b.printGameState();
System.out.println();
b.checkwin();
System.out.println("The winner is player " + b.getWinner());
} */
}