-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameController.java
147 lines (132 loc) · 4.04 KB
/
GameController.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import java.util.ArrayList;
import java.security.SecureRandom;
public class GameController
{
private GameModel model;
private GameView view;
public GameController(GameModel m)
{
this.model = m;
}
/**
* Sets the GameView that this GameController is associated with.
* @param v The view to attach to this GameController.
*/
public void setView(GameView v)
{
view = v;
}
/**
* Initializes the game.
*/
public void startGame() {
GameState gamestate = model.getGameState();
Stage stage = gamestate.getStage();
Player whitePlayer = gamestate.getWhitePlayer();
Player blackPlayer = gamestate.getBlackPlayer();
gamestate.setCurrentPlayer(whitePlayer);
gamestate.setStage(Stage.GAME_START);
view.getStatusLabel1().setText("Welcome to Score 4!");
view.getStatusLabel2().setText("You will play against an AI opponent.");
view.getStatusLabel3().setText("Please select a peg.");
advanceRound();
}
/**
* Advances the round to the next stage.
*/
public void advanceRound()
{
GameState gamestate = model.getGameState();
Stage stage = gamestate.getStage();
Player whitePlayer = gamestate.getWhitePlayer();
Player blackPlayer = gamestate.getBlackPlayer();
if (stage == Stage.GAME_START) {
gamestate.setStage(Stage.GET_WHITE_MOVE);
gamestate.setCurrentPlayer(whitePlayer);
} else if (!model.checkWin() && stage == Stage.GET_WHITE_MOVE) {
gamestate.setStage(Stage.GET_BLACK_MOVE);
model.setCurrentPlayer(blackPlayer);
getBlackMove();
} else if (!model.checkWin() && stage == Stage.GET_BLACK_MOVE) {
gamestate.setStage(Stage.GET_WHITE_MOVE);
model.setCurrentPlayer(whitePlayer);
}
if (model.checkWin()) {
showWinMessage();
showResetButton();
}
}
/**
* Gets the move that the white (human) player wishes to play.
* @param x The x coordinate that the white player wishes to play.
* @param y The y coordinate that the white player wishes to play.
*/
public void getWhiteMove(int x, int y)
{
GameState gamestate = model.getGameState();
Stage stage = model.getGameState().getStage();
if (stage == Stage.GET_WHITE_MOVE) {
model.addBead(x, y);
advanceRound();
// view.getStatusLabel3().setText("Your last move is: (" + x + ", " + y + ")");
}
}
/**
* Gets the move that the white (AI) player wishes to play.
* @param x The x coordinate that the white player wishes to play.
* @param y The y coordinate that the white player wishes to play.
*/
public void getBlackMove()
{
GameState gamestate = model.getGameState();
Player blackPlayer = gamestate.getBlackPlayer();
Move move;
move = blackPlayer.getMove(gamestate);
model.addBead(move.getXCoord(), move.getYCoord());
advanceRound();
}
/**
* Displays a win message in the status area in the View.
*/
public void showWinMessage()
{
GameState gamestate = model.getGameState();
Player whitePlayer = gamestate.getWhitePlayer();
Player blackPlayer = gamestate.getBlackPlayer();
if (model.getCurrentPlayer().equals(whitePlayer)) {
view.getStatusLabel1().setText("CONGRULATIONS!");
view.getStatusLabel2().setText("WHITE WINS");
view.getStatusLabel3().setText("");
view.getStatusLabel4().setText("");
} else if (model.getCurrentPlayer().equals(blackPlayer)) {
view.getStatusLabel1().setText("SORRY");
view.getStatusLabel2().setText("BLACK WINS");
view.getStatusLabel3().setText("");
view.getStatusLabel4().setText("");
}
}
/**
* Displays the reset button in the status area in the View.
*/
public void showResetButton()
{
view.getResetButton().setVisible(true);
}
/**
* Hides the reset button in the status area in the View.
*/
public void hideResetButton()
{
view.getResetButton().setVisible(false);
}
/**
* Resets the game.
*/
public void resetGame()
{
GameState gamestate = model.getGameState();
gamestate.getBoard().clearBoard();
startGame();
view.update();
}
}