-
Notifications
You must be signed in to change notification settings - Fork 0
/
ControlFrame.cpp
162 lines (135 loc) · 5.4 KB
/
ControlFrame.cpp
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "ControlFrame.h"
#include <string>
using namespace std;
// constructor - sets up all the elements on the frame
ControlFrame::ControlFrame(GameController *gc, Game *g)
: Observer(), gc_(gc), g_(g), controlArea(false, 10), mainControlFrame("Game Controls"), mainControl(true, 10),
mainButtons(true, 10), startButton("Start Game"), endButton("End Game"), seedEntryControl(false, 10),
seedEntryLabel("Seed:"), playerControlFrame("Player Controls"), playerControl(false, 10),
currentPlayer("Current Player: (none)"), playerControlButtons(true, 10), rageButton("Ragequit"),
hintButton("Hint"), statsControlFrame("Stats"), statsControl(true, 10)
{
// main control frame components (start/end, seed entry)
mainButtons.add(startButton);
mainButtons.add(endButton);
mainControl.add(mainButtons);
seedEntry.set_max_length(10);
seedEntry.set_width_chars(10);
seedEntryControl.add(seedEntryLabel);
seedEntryControl.add(seedEntry);
mainControl.add(seedEntryControl);
mainControlFrame.add(mainControl);
// player control frame components
playerControl.add(currentPlayer);
playerControl.add(playerControlButtons);
playerControlButtons.add(rageButton);
playerControlButtons.add(hintButton);
playerControlFrame.add(playerControl);
// stats control frame components
for (int i = 0; i < 4; i++) {
playerInfoLabels[i].set_label(string("Player " + helper::num2str(i+1)).c_str());
playerScores[i].set_label("Score: 0\nDiscards: 0");
playerTypes[i].set_label("Human");
playerTypeBools[i] = false;
playerTypes[i].signal_clicked().connect( sigc::bind (
sigc::mem_fun(*this, &ControlFrame::on_player_type_click), i)
);
playerInfoBoxes[i].add(playerInfoLabels[i]);
playerInfoBoxes[i].add(playerTypes[i]);
statsControl.add(playerInfoBoxes[i]);
}
statsControlFrame.add(statsControl);
// register buttons with clicking events
startButton.signal_clicked().connect( sigc::mem_fun(*this, &ControlFrame::on_start_click) );
endButton.signal_clicked().connect( sigc::mem_fun(*this, &ControlFrame::on_end_click) );
rageButton.signal_clicked().connect( sigc::mem_fun(*this, &ControlFrame::on_ragequit_click) );
hintButton.signal_clicked().connect( sigc::mem_fun(*this, &ControlFrame::on_hint_click) );
endButton.set_sensitive(false);
// finally, add each major part of the control frame
controlArea.add(mainControlFrame);
controlArea.add(playerControlFrame);
controlArea.add(statsControlFrame);
add(controlArea);
// subscribe self to observers
g_->subscribeView(this);
}
// destructor
ControlFrame::~ControlFrame() {
}
// called to update the view
void ControlFrame::notify()
{
// update display of whose turn it is
int currPlayer = g_->getCurrentPlayerId() + 1;
currentPlayer.set_label(string("Current Player: " + helper::num2str(currPlayer)).c_str());
// update score displays
for (int i = 0; i < 4; i++) {
playerScores[i].set_label(string("Score: " + helper::num2str(g_->getScore(i))
+ "\nDiscards: " + helper::num2str(g_->getDiscardCount(i))).c_str());
}
}
// called when start is clicked
void ControlFrame::on_start_click() {
// displays the scores, and removes the human/ai selection
if (!gc_->gameInProgress()) {
for (int i = 0; i < 4; i++) {
playerInfoBoxes[i].remove(playerTypes[i]);
playerInfoBoxes[i].add(playerScores[i]);
}
}
// enables end button, disables start
startButton.set_sensitive(false);
endButton.set_sensitive(true);
// shows all elements on screen
show_all();
// sets the seed, and requests to start the game
string seed_str = seedEntry.get_text();
gc_->startGame(helper::str2num(seed_str), playerTypeBools);
}
// called when end game is clicked
void ControlFrame::on_end_click() {
// disables hint
gc_->setHintRequestedFlag(false);
// displays human/ai selection
if (gc_->gameInProgress()) {
for (int i = 0; i < 4; i++) {
playerInfoBoxes[i].remove(playerScores[i]);
playerInfoBoxes[i].add(playerTypes[i]);
}
}
// enables start game button
startButton.set_sensitive(true);
endButton.set_sensitive(false);
// removes current player counter, and tells controller to end the game
gc_->endGame();
currentPlayer.set_label("Current Player: (none)");
show_all();
}
// Changes given player to the opposite of the current (AI or human), changes to AI when human hits ragequit
void ControlFrame::on_player_type_click(int id) {
playerTypeBools[id] = !playerTypeBools[id];
// sets appropriate label on human/ai button depending on what player changed to
if (playerTypeBools[id]) {
playerTypes[id].set_label("Computer");
}
else {
playerTypes[id].set_label("Human");
}
}
// performs rage quit
void ControlFrame::on_ragequit_click() {
gc_->setHintRequestedFlag(false);
// requests controller to ask model to rage quit
if (gc_->gameInProgress() && !g_->gameDone()) {
int id = g_->getCurrentPlayerId();
on_player_type_click(id);
gc_->rageQuit();
}
}
/// requests controller to ask model for a hint
void ControlFrame::on_hint_click() {
if (gc_->gameInProgress() && !g_->gameDone()) {
gc_->setHintRequestedFlag(true);
g_->makeHintRequest();
}
}