Index   Main   Namespaces   Classes   Hierarchy   Annotated   Files   Compound   Global   Pages  

SgTimeControl.cpp

Go to the documentation of this file.
00001 //----------------------------------------------------------------------------
00002 /** @file SgTimeControl.cpp
00003     See SgTimeControl.h
00004 */
00005 //----------------------------------------------------------------------------
00006 
00007 #include "SgSystem.h"
00008 #include "SgTimeControl.h"
00009 
00010 #include "SgDebug.h"
00011 #include "SgTimeRecord.h"
00012 
00013 using namespace std;
00014 
00015 //----------------------------------------------------------------------------
00016 
00017 SgTimeControl::~SgTimeControl()
00018 {
00019 }
00020 
00021 //----------------------------------------------------------------------------
00022 
00023 SgDefaultTimeControl::SgDefaultTimeControl()
00024     : m_fastOpenFactor(0.25),
00025       m_fastOpenMoves(0),
00026       m_minTime(0),
00027       m_remainingConstant(1.0)
00028 {
00029 }
00030 
00031 double SgDefaultTimeControl::FastOpenFactor() const
00032 {
00033     return m_fastOpenFactor;
00034 }
00035 
00036 int SgDefaultTimeControl::FastOpenMoves() const
00037 {
00038     return m_fastOpenMoves;
00039 }
00040 
00041 double SgDefaultTimeControl::RemainingConstant() const
00042 {
00043     return m_remainingConstant;
00044 }
00045 
00046 void SgDefaultTimeControl::SetRemainingConstant(double value)
00047 {
00048     m_remainingConstant = value;
00049 }
00050 
00051 void SgDefaultTimeControl::SetFastOpenFactor(double factor)
00052 {
00053     m_fastOpenFactor = factor;
00054 }
00055 
00056 void SgDefaultTimeControl::SetFastOpenMoves(int nummoves)
00057 {
00058     m_fastOpenMoves = nummoves;
00059 }
00060 
00061 void SgDefaultTimeControl::SetMinTime(double mintime)
00062 {
00063     m_minTime = mintime;
00064 }
00065 
00066 double SgDefaultTimeControl::TimeForCurrentMove(const SgTimeRecord& time,
00067                                                 bool quiet)
00068 {
00069     SgBlackWhite toPlay;
00070     int estimatedRemainingMoves;
00071     int movesPlayed;
00072     GetPositionInfo(toPlay, movesPlayed, estimatedRemainingMoves);
00073     const bool useOvertime = time.UseOvertime();
00074     const bool isInOvertime = (useOvertime && time.MovesLeft(toPlay) > 0);
00075     double remainingMoves = 0.0;
00076     double timeForMove = 0.0;
00077     double timeLeft = time.TimeLeft(toPlay);
00078     if (isInOvertime)
00079     {
00080         remainingMoves = time.MovesLeft(toPlay);
00081         timeForMove = timeLeft / remainingMoves - time.Overhead();
00082     }
00083     else
00084     {
00085         double estimatedTotalMoves = movesPlayed + estimatedRemainingMoves;
00086         remainingMoves = m_remainingConstant * estimatedTotalMoves;
00087         if (useOvertime && remainingMoves > estimatedRemainingMoves)
00088             remainingMoves = estimatedRemainingMoves;
00089         remainingMoves = max(remainingMoves, 1.0);
00090         timeForMove = time.TimeLeft(toPlay) / remainingMoves - time.Overhead();
00091         if (useOvertime)
00092         {
00093             double firstOvertimeMove = time.OTPeriod() / time.OTNumMoves();
00094             if (timeForMove < firstOvertimeMove)
00095                 timeForMove = firstOvertimeMove;
00096         }
00097     }
00098 
00099     // Don't use quite as much time for the first few moves, makes no real
00100     // difference in the quality of the moves.
00101     if (m_fastOpenMoves > 0 && movesPlayed <= m_fastOpenMoves)
00102         timeForMove *= m_fastOpenFactor;
00103 
00104     timeForMove = max(timeForMove, m_minTime);
00105 
00106     if (! quiet)
00107     {
00108         SgDebug() << "SgDefaultTimeControl: timeLeft=" << timeLeft;
00109         if (useOvertime)
00110             SgDebug() << '/' << time.MovesLeft(toPlay);
00111         SgDebug() << " remaining=" << remainingMoves << " timeMove="
00112                   << timeForMove << '\n';
00113     }
00114 
00115     return timeForMove;
00116 }
00117 
00118 //----------------------------------------------------------------------------
00119 
00120 SgObjectWithDefaultTimeControl::~SgObjectWithDefaultTimeControl()
00121 {
00122 }
00123 
00124 //----------------------------------------------------------------------------


17 Jun 2010 Doxygen 1.4.7