-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConnectFour.java
248 lines (197 loc) · 7.45 KB
/
ConnectFour.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
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
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ConnectFour extends Game implements Endable {
// SINGLETON
private static ConnectFour connectFourInstance = null;
// ATTRIBUTES
private Player playerTurn;
private final List<String> tileOptions = new ArrayList<>();
// CONSTRUCTOR + SINGLETON PROVIDER
private ConnectFour() {
super();
// initialize tile options
this.tileOptions.add("X");
this.tileOptions.add("O");
// initialize board
super.setGameBoard(new ConnectFourBoard());
// initialize players
super.getPlayers().add(new Player(1, this.tileOptions.get(0)));
super.getPlayers().add(new Player(2, this.tileOptions.get(1)));
this.playerTurn = super.getPlayers().get(1);
}
public static synchronized ConnectFour getInstance() {
if (connectFourInstance == null)
connectFourInstance = new ConnectFour();
return connectFourInstance;
}
// GETTER + SETTER
public Player getPlayerTurn() { return playerTurn; }
public void setPlayerTurn(Player playerTurn) { this.playerTurn = playerTurn; }
// ABSTRACT METHOD IMPLEMENTATIONS
@Override
public void gameLoop(Scanner scanner) {
System.out.println("CONNECT FOUR RULES");
System.out.println(" 1. First player to match 25 tiles (or more) wins!");
System.out.println(" 2. Matches exists with 4 (or more) tiles in a row.");
System.out.println(" 3. Matches can be diagonal, horizontal, and vertical.");
System.out.println(" 4. Please determine who will play Player 1 and Player 2 amongst yourselves.\n");
boolean gameRunning = true;
while (gameRunning) {
this.switchPlayer();
super.display();
try {
// Pause for 0.5 seconds to give user the chance to look at board
Thread.sleep(500);
} catch (InterruptedException e) {}
this.takeTurn(scanner);
gameRunning = !this.isGameOver();
}
this.end();
}
@Override
public void takeTurn(Scanner scanner) {
// announce scores
for (Player player : super.getPlayers()) {
System.out.println("PLAYER " + player.getID() + " SCORE: " + player.getScore());
}
// announce turn
System.out.println("\nPLAYER "
+ this.getPlayerTurn().getID()
+ ", place down a tile ["
+ this.getPlayerTurn().getDisplay() + "]");
// get move from user + check validity
String move = this.getMove(scanner);
// do the move
this.executeMove(move);
// check for matches & update status of game
this.checkMatch();
}
@Override
public String getMove(Scanner scanner) {
// choose a Tile and check if a valid move
ConnectFourBoard board = getConnectFourBoard();
if (!(board == null)) {
String move = null;
boolean validMove = false;
while (!validMove) {
move = this.choose(scanner);
validMove = board.isValidMove(String.valueOf(move));
}
return move;
}
return null; // MAJOR ISSUE IF THIS CODE IS REACHED
}
@Override
public String choose(Scanner scanner) {
// ensures that the chosen move is a valid column number
ConnectFourBoard board = this.getConnectFourBoard();
if (!(board == null)) {
int maxColumns = board.getColumns();
int minColumns = 1;
int move = super.parseNumber(scanner, "Enter a column: ", minColumns, maxColumns);
return String.valueOf(move);
}
return null; // MAJOR ISSUE IF THIS CODE IS REACHED
}
@Override
public void executeMove(String move) {
List<Tile> moves = new ArrayList<>();
ConnectFourBoard board = this.getConnectFourBoard();
if (!(board == null)) {
// return the Tile at top of the chosen column
moves.add(board.getGameBoard().get(0).get(Integer.parseInt(move)-1));
board.execute(moves, this.getPlayerTurn());
}
}
@Override
public void checkMatch() {
ConnectFourBoard board = this.getConnectFourBoard();
if (!(board == null)) {
List<Tile> matches;
do {
// match the tiles
matches = board.checkMatches();
if (matches != null) {
// add points to the players
this.addPlayerPoint(matches);
// make the Tiles disappear
board.removeMatchedTiles(matches);
}
} while (matches != null);
}
}
@Override
public void addPlayerPoint(List<Tile> tiles) {
for (Tile tile : tiles) {
for (Player player : super.getPlayers()) {
if (tile.getDisplay().equals(player.getDisplay())) {
player.addPoint();
}
}
}
}
@Override
public boolean isGameOver() {
ConnectFourBoard board = this.getConnectFourBoard();
if (!(board == null)) {
boolean isScore25 = checkPlayerScore();
boolean isFull = board.isFull();
return (isScore25 || isFull);
}
return true; // ERROR: board is not ConnectFourBoard type
}
@Override
public boolean checkPlayerScore() {
// if a score is greater than or equal to 25 then game is over = winner found
for (Player player: super.getPlayers()) {
if (player.getScore() >= 25) {
return true;
}
}
return false;
}
@Override
public void end() {
System.out.println("\n===================================================\n");
System.out.println("CONNECT FOUR: GAME OVER");
List<Player> winner = this.findHighestScore();
if (winner.size() > 1) {
System.out.println("TIE!");
System.out.println("Score :" + winner.get(0).getScore() + "\n");
}
System.out.println("PLAYER " + winner.get(0).getID() + " WINS!");
System.out.println("Score :" + winner.get(0).getScore() + "\n");
connectFourInstance = null;
}
// HELPER FUNCTIONS
private ConnectFourBoard getConnectFourBoard() {
if (super.getGameBoard() instanceof ConnectFourBoard) {
return (ConnectFourBoard) super.getGameBoard();
}
return null;
}
private void switchPlayer() {
int currentPlayerID = (this.getPlayerTurn().getID() % super.getPlayers().size()) + 1;
for (Player player : super.getPlayers()) {
if (player.getID() == currentPlayerID) {
this.setPlayerTurn(player);
break;
}
}
}
private List<Player> findHighestScore() {
List<Player> playersWithHighestScore = new ArrayList<>();
int highestScore = 0;
for (Player player: super.getPlayers()) {
if (player.getScore() > highestScore) {
playersWithHighestScore.clear();
playersWithHighestScore.add(player);
highestScore = player.getScore();
} else if (player.getScore() == highestScore) {
playersWithHighestScore.add(player);
}
}
return playersWithHighestScore;
}
}