-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleView.java
More file actions
113 lines (101 loc) · 3.58 KB
/
Copy pathConsoleView.java
File metadata and controls
113 lines (101 loc) · 3.58 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
import java.util.List;
/**
* ConsoleView is a concrete class which encapsulate rendering of View in console.
*/
public class ConsoleView implements View {
private Controller controller;
private Board board;
private BoardDisplay boardDisplay;
private String error;
/**
* Constructor to create View
*
* @param controller takes inputs from View and updates with as well
* @param board holds application state
* @param boardDisplay holds rendering logic i.e. Simple or Colored
*/
public ConsoleView(Controller controller, Board board, BoardDisplay boardDisplay) {
this.controller = controller;
this.board = board;
this.boardDisplay = boardDisplay;
this.error = "";
}
/**
* Displays welcome message to player.
*/
@Override
public void displayWelcomeMessage() {
int matchLendth = this.board.getMatchLength();
List<Player> players = this.board.getPlayers();
System.out.println("Welcome to Connect " + matchLendth + "\n");
System.out.println("There are " + players.size() + " players");
for (Player player : players) {
System.out.println(player.getName() + ", Token - " + player.getToken());
}
System.out.println("\nTo play the game type in the number of the column you want to drop you counter in");
System.out.println("A player wins by connecting " + matchLendth
+ " counters in a row - vertically, horizontally or diagonally\n");
if (this.error != "") {
System.out.println(Colors.BRIGHT_RED + "Error: " + this.error + Colors.RESET);
}
}
/**
* displayBoard method renders the welcome message and the board as well.
*/
@Override
public void displayBoard() {
System.out.print("\033[H\033[2J");
System.out.flush();
System.out.println("");
this.displayWelcomeMessage();
this.boardDisplay.display(this.board);
}
/**
* displayErrorMessage method is called by Controller to update View in case of error.
*
* @param error contains error message recieved from Controller
*/
@Override
public void displayErrorMessage(String error) {
this.error = error;
}
/**
* getUserInput method recieves player input.
*/
@Override
public String getUserInput() {
Player player = this.board.getCurrentPlayer();
System.out.print("\n" + player.getName() + " to move: ");
String move = player.getMove(this.board.getCoulmns());
System.out.println("");
return move;
}
/**
* play method is the starting point for board rendering and collects player input
* until Game is WON or DRAW.
*/
@Override
public void play() {
while (true) {
this.displayBoard();
String column = this.getUserInput();
boolean result = this.controller.setToken(column);
if (result) {
this.error = "";
Player player = this.board.getCurrentPlayer();
State state = this.board.getState();
if (state == State.DRAW) {
this.displayBoard();
System.out.println("\nGame ended in a DRAW\n");
break;
}
if (state == State.WON) {
this.displayBoard();
System.out.println("\n" + player.getName() + " WON!\n");
break;
}
this.board.getNextPlayer();
}
}
}
}