-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoardController.java
More file actions
55 lines (51 loc) · 1.71 KB
/
Copy pathBoardController.java
File metadata and controls
55 lines (51 loc) · 1.71 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
/**
* BoardController is a concrete class which takes inputs from View and performs validation.
* Once validation is done it updates the model.
*/
public class BoardController implements Controller {
private View view;
private Board board;
private Validator validator;
/**
* Constructor method which creates BoardController object.
*
* @param board Holds application state
* @param boardDisplay it holds the logic of Board rendering
*/
public BoardController(Board board, BoardDisplay boardDisplay) {
this.view = new ConsoleView(this, board, boardDisplay);
this.board = board;
this.validator = new NumericValidator();
this.validator.setValidator(new RangeValidator());
}
/**
* setToken method updates the board with the player Token.
*
* @param column It represents the column entered by end user.
*
* @return true if model update succeed's else false.
*/
@Override
public boolean setToken(String column) {
if (validator.validate(column, 1, this.board.getCoulmns())) {
boolean result = this.board.setToken(Integer.parseInt(column) - 1);
if (result)
return true;
else {
this.view.displayErrorMessage("Input ignored: '" + column + "'\n");
return false;
}
} else {
this.view.displayErrorMessage("Incorrect input: '" + column + "'\n");
return false;
}
}
/**
* play method initializes the model and triggers view rendering.
*/
@Override
public void play() {
this.board.init(new SimpleToken());
this.view.play();
}
}