-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetupController.java
More file actions
64 lines (55 loc) · 2.49 KB
/
SetupController.java
File metadata and controls
64 lines (55 loc) · 2.49 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
package cpsc2150.extendedConnectX;
/**
* This class is the controller for our setup screen. The processButtonClick method is called by
* SetupView when someone clicks on the submit button. It is passed in the rows, cols, players and
* the number to win by the view, but it still needs to validate that input. If there are any
* errors it can use the displayError method in the SetupView class to inform the player of the
* error, then wait for them to fix it and resubmit.
* <p>
* If there are no errors it will create a new IGameBoard object (the implementation will depend on
* the size of the game board) to serve as the model, and the ConnectXController and ConnectXView.
* Control is then passed over the the event dispatch thread that will wait for an event to occur
*/
public class SetupController {
private SetupView view;
private int max_size = 20;
private int min_size = 3;
private int min_to_win = 3;
private final int BOARD_CUTOFF = 100;
public SetupController(SetupView v) {
view = v;
}
public void processButtonClick(int rows, int cols, int players, int numWin) {
String errorMsg = "";
if (rows < min_size || rows > max_size) {
errorMsg += "Rows must be between " + min_size + " and " + max_size + ". ";
}
if (cols < min_size || cols > max_size) {
errorMsg += "Columns must be between " + min_size + " and " + max_size + ". ";
}
if (numWin > rows) {
errorMsg += "Can't have more to win than the number of rows. ";
}
if (numWin > cols) {
errorMsg += "Can't have more to win than the number of Columns. ";
}
if (numWin < min_to_win) {
errorMsg += "Number to win must be at least " + min_to_win + ". ";
}
if (!errorMsg.equals("")) {
view.displayError(errorMsg);
} else {
view.closeScreen();
IGameBoard model;
//if the board is too big we'll want the memory efficient version
if (rows * cols > BOARD_CUTOFF) {
model = new GameBoardMem(rows, cols, numWin);
} else {
model = new GameBoard(rows, cols, numWin);
}
ConnectXView tview = new ConnectXView(rows, cols);
ConnectXController tcontroller = new ConnectXController(model, tview, players);
tview.registerObserver(tcontroller);
}
}
}