timeKeeper.cc
Go to the documentation of this file.
00001 /* timeKeeper.cc
00002  */
00003 #include "osl/game_playing/timeKeeper.h"
00004 #include "osl/stl/stack.h"
00005 
00006 struct osl::game_playing::TimeKeeper::Stack
00007   : public osl::vector<std::pair<int,int> >
00008 {
00009 };
00010 
00011 osl::game_playing::
00012 TimeKeeper::TimeKeeper() : seconds(new Stack())
00013 {
00014   reset(1500, 1500); // default: 25 min
00015 }
00016 
00017 osl::game_playing::
00018 TimeKeeper::TimeKeeper(int black_time, int white_time) 
00019   : seconds(new Stack())
00020 {
00021   reset(black_time, white_time);
00022 }
00023 
00024 osl::game_playing::
00025 TimeKeeper::~TimeKeeper()
00026 {
00027 }
00028 
00029 void osl::game_playing::
00030 TimeKeeper::reset(int black_time, int white_time)
00031 {
00032   seconds->clear();
00033   seconds->push_back(std::make_pair(black_time, white_time));
00034 }
00035 
00036 void osl::game_playing::
00037 TimeKeeper::pushMove(Player turn, int consumed)
00038 {
00039   std::pair<int,int> time_left = seconds->back();
00040   if (turn == BLACK)
00041     time_left.first -= consumed;
00042   else
00043     time_left.second -= consumed;
00044   seconds->push_back(time_left);
00045 }
00046 
00047 void osl::game_playing::
00048 TimeKeeper::popMove()
00049 {
00050   assert(! seconds->empty());
00051   seconds->pop_back();
00052 }
00053 
00054 int osl::game_playing::
00055 TimeKeeper::timeLeft(Player player) const
00056 {
00057   const std::pair<int,int>& time_left = seconds->back();
00058   return (player == BLACK) ? time_left.first : time_left.second;
00059 }
00060 
00061 int osl::game_playing::
00062 TimeKeeper::timeElapsed(Player player) const
00063 {
00064   return timeLimit(player) - timeLeft(player);
00065 }
00066 
00067 int osl::game_playing::
00068 TimeKeeper::timeLimit(Player player) const
00069 {
00070   const std::pair<int,int>& time_left = seconds->front();
00071   return (player == BLACK) ? time_left.first : time_left.second;
00072 }
00073 
00074 /* ------------------------------------------------------------------------- */
00075 // ;;; Local Variables:
00076 // ;;; mode:c++
00077 // ;;; c-basic-offset:2
00078 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines