-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathConnect4.java
More file actions
71 lines (55 loc) · 1.8 KB
/
Copy pathConnect4.java
File metadata and controls
71 lines (55 loc) · 1.8 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
import java.util.Scanner;
/**
* Connect4.java
*
* Controller that plays the connect 4 game.
* @author Delos Chang
*
*/
public class Connect4 {
public static void main(String [] args){
Scanner input = new Scanner(System.in);
String answer = "";
Connect4View view;
// Ask for either text or view
while (!(answer.contains("Text") || answer.contains("Graphic"))){
System.out.println("Text or Graphical View?");
answer = input.nextLine();
}
if (answer.contains("Text")){
view = new Connect4Text();
} else {
view = new Connect4ViewGraphical();
}
Player [] players = new Player[2];
// Initialize the game
// Computer - for computer
String playerName = view.getAnswer("Enter the name of the first player." +
"\n(Include 'Computer' if you want a computer player) ");
if (playerName.contains("Computer")){
int askDepth = view.getIntAnswer("Please enter depth of computer");
players[0] = new ComputerConnect4Player(playerName, askDepth);
} else {
players[0] = new Connect4HumanPlayer(playerName);
}
playerName = view.getAnswer("Enter the name of the second player." +
"\n(Include 'Computer' if you want a computer player) ");
if (playerName.contains("Computer")){
int askDepth = view.getIntAnswer("Please enter depth of computer");
players[1] = new ComputerConnect4Player(playerName, askDepth);
} else {
players[1] = new Connect4HumanPlayer(playerName);
}
Connect4Game state = new Connect4Game(0, players);
view.display(state);
// Hold current game state
while (!state.gameIsOver()){
int move = state.getPlayerToMove().getMove(state, view);
state.makeMove(move);
view.display(state);
}
// The game is over
// declare the winner!
view.reportToUser(state.getPlayers()[1 - state.getPlayerNum()].getName() + " won!");
}
}