-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.java
68 lines (60 loc) · 2.41 KB
/
Game.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
import java.util.ArrayList;
/**
* Game class used to control the running of the Connect4 game
* @author Henry Prosser
*/
public class Game {
// Instance fields
private boolean win = false;
private boolean draw = false;
// Constructor
public Game() {
System.out.println("Welcome to Connect 4");
System.out.println("There are 2 players red and yellow");
System.out.println("Player 1 is Red, Player 2 is Yellow");
System.out.println("To play the game type in the number of the column you want to drop your counter in");
System.out.println("A player wins by connecting 4 counters in a row - vertically, horizontally or diagonally");
System.out.println("");
playGame();
}
// Method for running the game
private void playGame() {
Board board = new Board();
Player player1 = new HumanPlayer("Red", 'r');
Player player2 = new BotPlayer("Yellow", 'y');
ArrayList<Player> players = new ArrayList<>();
players.add(player1);
players.add(player2);
int currentPlayer = 0;
board.printBoard();
while(!win && !draw){
Player current = players.get(currentPlayer%2);
try{
String userInput = current.getUserInput();
int move = Integer.parseInt(userInput);
boolean placed = board.placeCounter(current.getPlayerCounter(),move);
// If counter is not placed, repeat the go
if(!placed){
currentPlayer--;
}
boolean hasWon = board.checkWin(current.getPlayerCounter());
boolean isDraw = board.checkDraw();
// Checks if current player has won
if(hasWon){
win = true;
System.out.println("Player " + current.getPlayerName() + " wins!!!");
}
// Checks if the game is a draw
else if(isDraw){
draw = true;
System.out.println("It's a draw!");
}
currentPlayer++;
}
/* Exception handling to catch for wrong inputs & prompts user to try again */
catch(NumberFormatException | ArrayIndexOutOfBoundsException e){
System.out.println("Invalid input. Please enter a valid column number");
}
}
}
}