timeKeeper.cc Source File

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
timeKeeper.cc
Go to the documentation of this file.
1 /* timeKeeper.cc
2 */
3 #include "osl/game_playing/timeKeeper.h"
4 #include "osl/stl/stack.h"
5
6 struct osl::game_playing::TimeKeeper::Stack
7 : public osl::vector<std::pair<int,int> >
8 {
9 };
10
11 osl::game_playing::
12 TimeKeeper::TimeKeeper() : seconds(new Stack())
13 {
14 reset(1500, 1500); // default: 25 min
15 }
16
17 osl::game_playing::
18 TimeKeeper::TimeKeeper(int black_time, int white_time)
19 : seconds(new Stack())
20 {
21 reset(black_time, white_time);
22 }
23
24 osl::game_playing::
25 TimeKeeper::~TimeKeeper()
26 {
27 }
28
29 void osl::game_playing::
30 TimeKeeper::reset(int black_time, int white_time)
31 {
32 seconds->clear();
33 seconds->push_back(std::make_pair(black_time, white_time));
34 }
35
36 void osl::game_playing::
37 TimeKeeper::pushMove(Player turn, int consumed)
38 {
39 std::pair<int,int> time_left = seconds->back();
40 if (turn == BLACK)
41 time_left.first -= consumed;
42 else
43 time_left.second -= consumed;
44 seconds->push_back(time_left);
45 }
46
47 void osl::game_playing::
48 TimeKeeper::popMove()
49 {
50 assert(! seconds->empty());
51 seconds->pop_back();
52 }
53
54 int osl::game_playing::
55 TimeKeeper::timeLeft(Player player) const
56 {
57 const std::pair<int,int>& time_left = seconds->back();
58 return (player == BLACK) ? time_left.first : time_left.second;
59 }
60
61 int osl::game_playing::
62 TimeKeeper::timeElapsed(Player player) const
63 {
64 return timeLimit(player) - timeLeft(player);
65 }
66
67 int osl::game_playing::
68 TimeKeeper::timeLimit(Player player) const
69 {
70 const std::pair<int,int>& time_left = seconds->front();
71 return (player == BLACK) ? time_left.first : time_left.second;
72 }
73
74 /* ------------------------------------------------------------------------- */
75 // ;;; Local Variables:
76 // ;;; mode:c++
77 // ;;; c-basic-offset:2
78 // ;;; End: