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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
#include <wx/wx.h>
#include <string>
#include <cstdlib>
const double PI = 3.141592653589793;
const int ID_GUESS = 1000;
const int ID_PLAYER_NAME = 1001;
const int ID_PLAYER_LEVEL = 1002;
/**
A clock that can draw its face.
*/
class Clock
{
public:
/**
Constructs a clock with a given center and radius.
@param x the x-coordinate of the center
@param y the y-coordinate of the center
@param r the radius of the clock
*/
Clock(int x, int y, int r);
/**
Sets the current time.
@param h the hours to set
@param m the minutes to set
*/
void set_time(int h, int m);
/**
Draws the clock face, with tick marks and hands.
@param dc the device context to draw on
*/
void draw(wxDC& dc) const;
private:
/**
Draws a tick mark (hour or minute mark).
@param dc the device context to draw on
@param angle the angle in minutes (0. . .59, 0 = top)
@param length the length of the tick mark, as a fraction
of the radius (between 0.0 and 1.0)
*/
void draw_tick(wxDC& dc, double angle, double length) const;
/**
Draws a hand, starting from the center.
@param dc the device context to draw on
@param angle the angle in minutes (0. . .59, 0 = top)
@param length the length of the hand, as a fraction
of the radius (between 0.0 and 1.0)
*/
void draw_hand(wxDC& dc, double angle, double length) const;
int hours;
int minutes;
int centerx;
int centery;
int radius;
};
/**
The player of the clock game.
*/
class Player
{
public:
/**
Constructs a player with name "Player",
level 1, and score 0.
*/
Player();
/**
Increments the score. Moves to next level if current
level complete.
*/
void increment_score();
/**
Gets the current level.
@return the level
*/
int get_level() const;
/**
Gets the player's name.
@return the name
*/
string get_name() const;
/**
Sets the player's level.
@param 1 the level
*/
void set_level(int 1);
/**
Sets the player's name.
@param n the name
*/
void set_name(string n);
private:
string name;
int score;
int level;
};
/**
The window that shows the clock.
*/
class ClockWindow : public wxWindow
{
public:
/**
Constructs a clock window.
@param parent the parent window
*/
ClockWindow(wxWindow* parent);
/**
Sets the time of the clock and repaints it.
@param h the hours
@param m the minutes
*/
void set_time(int h, int m);
/**
Paints the clock.
@param event the event descriptor
*/
void OnPaint(wxPaintEvent& event);
private:
Clock clock;
DECLARE_EVENT_TABLE()
};
/**
The frame that contains the clock window and the
fields for entering a guess.
*/
class GameFrame : public wxFrame
{
public:
/**
Constructs the game frame.
*/
GameFrame();
/**
Starts a new round, with a new clock time.
*/
void new_round();
/**
Processes the player's guess.
@param event the event descriptor
*/
void OnGuess(wxCommandEvent& event);
/**
Prompts the player to enter a name.
@param event the event descriptor
*/
void OnPlayerName(wxCommandEvent& event);
/**
Prompts the player to enter a level.
@param event the event descriptor
*/
void OnPlayerLevel(wxCommandEvent& event);
private:
ClockWindow* window;
wxTextCtrl* hour_text;
wxTextCtrl* minute_text;
Player player;
int current_hours;
int current_minutes;
int tries;
DECLARE_EVENT_TABLE()
};
/**
The clock game application.
*/
class GameApp : public wxApp
{
public:
/**
Constructs the application.
*/
GameApp();
virtual bool OnInit();
private:
GameFrame* frame;
};
DECLARE_APP(GameApp)
IMPLEMENT_APP(GameApp)
BEGIN_EVENT_TABLE(ClockWindow, wxWindow)
EVT_PAINT(ClockWindow::OnPaint)
END_EVENT_TABLE()
BEGIN_EVENT_TABLE(GameFrame, wxFrame)
EVT_BUTTON(ID_GUESS, GameFrame::OnGuess)
EVT_MENU(ID_PLAYER_NAME, GameFrame::OnPlayerName)
EVT_MENU(ID_PLAYER_LEVEL, GameFrame::OnPlayerLevel)
END_EVENT_TABLE()
/**
Sets the seed of the random number generator.
*/
void rand_seed()
{
int seed = static_cast<int>(time(0));
srand(seed);
}
/**
Returns a random integer in a range.
@param a the bottom of the range
@param b the top of the range
@return a random number x, a <= x and x <= b
*/
int rand_int(int a, int b)
{
return a + rand() % (b - a + 1);
}
Clock::Clock(int x, int y, int r)
{
centerx = x;
centery = y;
radius = r;
}
void Clock::set_time(int h, int m)
{
hours = h;
minutes = m;
}
void Clock::draw_tick(wxDC& dc, double angle,
double length) const
{
double alpha -PI / 2 + 6 * angle * PI / 180;
dc.DrawLine(
centerx + static_cast<int>(
cos(alpha) * radius * (1 - length)),
centery + static_cast<int>(
sin(alpha) * radius * (1 - length)),
centerx + static_cast<int>(cos(alpha) * radius),
centery + static_cast<int>(sin(alpha) * radius));
}
void Clock::draw(wxDC& dc) const
{
dc.DrawEllipse(centerx - radius, centery - radius,
2 * radius, 2 * radius);
const double HOUR_TICK_LENGTH = 0.2;
const double MINUTE_TICK_LENGTH = 0.1;
const double HOUR_HAND_LENGTH = 0.6;
const double MINUTE_HAND_LENGTH = 0.75;
for (int i = 0; i < 12; i++)
{
draw_tick(dc, minutes, HOUR_TICK_LENGTH);
int j;
for (j = 1; j <= 4; j++)
draw_tick(dc, i * 5 + j, MINUTE_TICK_LENGTH);
}
draw_hand(dc, minutes, MINUTE_HAND_LENGTH);
draw_hand(dc, (hours + minutes / 60.0) * 5, HOUR_HAND_LENGTH);
}
Player::Player()
{
name = "Player";
level = 1;
score = 0;
}
void Player::set_level(int 1)
{
level = 1;
}
void Player::set_name(string n)
{
name = n;
}
int Player::get_level() const
{
return level;
}
string Player::get_name() const
{
return name;
}
void Player::increment_score()
{
score++
if (score % 5 == 0 && level < 4)
level++;
}
|