forked from sangmichaelxie/giraffe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchessclock.cpp
124 lines (109 loc) · 2.12 KB
/
chessclock.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
/*
Copyright (C) 2015 Matthew Lai
Giraffe is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Giraffe is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "chessclock.h"
#include "util.h"
ChessClock::ChessClock(Mode mode, int numMoves, double baseTime, double inc)
: m_mode(mode),
m_numMoves(numMoves),
m_baseTime(baseTime),
m_inc(inc),
m_numMovesMade(0),
m_endTime(0.0),
m_timeLeftWhenStopped(baseTime),
m_running(false)
{
Reset();
}
void ChessClock::Reset()
{
m_running = false;
m_numMovesMade = 0;
if (m_mode == CONVENTIONAL_INCREMENTAL_MODE)
{
m_timeLeftWhenStopped = m_baseTime;
}
else
{
m_timeLeftWhenStopped = m_inc;
}
}
double ChessClock::GetReading() const
{
if (m_running)
{
return m_endTime - CurrentTime();
}
else
{
return m_timeLeftWhenStopped;
}
}
void ChessClock::Start()
{
if (!m_running)
{
m_running = true;
m_endTime = CurrentTime() + m_timeLeftWhenStopped;
}
}
void ChessClock::Stop()
{
if (m_running)
{
m_timeLeftWhenStopped = m_endTime - CurrentTime();
m_running = false;
}
}
void ChessClock::MoveMade()
{
if (m_mode == CONVENTIONAL_INCREMENTAL_MODE)
{
double extraTime = m_inc;
if (m_numMovesMade == m_numMoves && m_numMoves != 0)
{
extraTime += m_baseTime;
m_numMovesMade = 0;
}
if (m_running)
{
m_endTime += extraTime;
}
else
{
m_timeLeftWhenStopped += extraTime;
}
}
else
{
if (m_running)
{
m_endTime = CurrentTime() + m_inc;
}
else
{
m_timeLeftWhenStopped = m_inc;
}
}
}
void ChessClock::AdjustTime(double time)
{
if (m_running)
{
m_endTime = CurrentTime() + time;
}
else
{
m_timeLeftWhenStopped = time;
}
}