1 /* ColorCode, a free MasterMind clone with built in solver
2 * Copyright (C) 2009 Dirk Laebisch
3 * http://www.laebisch.com/
4 *
5 * ColorCode is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * ColorCode is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with ColorCode. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "ccgame.h"
20 #include "settings.h"
21
22 using namespace std;
23
CCGame()24 CCGame::CCGame()
25 {
26 mUserNm = "";
27 mTime = 0;
28 mScore = 0;
29
30 mGameNo = 0;
31 mGuessCnt = 0;
32 mHintCnt = 0;
33 mDuration = 0;
34
35 mColorCnt = 0;
36 mPegCnt = 0;
37 mDoubles = 0;
38
39 mGameMode = ColorCode::MODE_HVM;
40 }
41
CCGame(const QString & str)42 CCGame::CCGame(const QString &str)
43 {
44 QStringList fields = str.split(QString("|"));
45 if (fields.size() == 11)
46 {
47 SetUserName(QUrl::fromPercentEncoding(fields.at(0).toUtf8()));
48 bool ok;
49 mTime = fields.at(1).toInt(&ok);
50 mScore = fields.at(2).toInt(&ok);
51
52 mGameNo = fields.at(3).toUInt(&ok);
53 mGuessCnt = fields.at(4).toInt(&ok);
54 mHintCnt = fields.at(5).toInt(&ok);
55 mDuration = fields.at(6).toUInt(&ok);
56
57 mGameMode = fields.at(7).toInt(&ok);
58 mColorCnt = fields.at(8).toInt(&ok);
59 mPegCnt = fields.at(9).toInt(&ok);
60 mDoubles = fields.at(10).toInt(&ok);
61 }
62 else
63 {
64 mUserNm = "";
65 mTime = 0;
66 mScore = 0;
67
68 mGameNo = 0;
69 mGuessCnt = 0;
70 mHintCnt = 0;
71 mDuration = 0;
72
73 mGameMode = ColorCode::MODE_HVM;
74 mColorCnt = 0;
75 mPegCnt = 0;
76 mDoubles = 0;
77 }
78 }
79
CCGame(const QString & unm,const uint t,const int sc,const uint gameno,const int gcnt,const int hcnt,const uint dur,const int gmode,const int ccnt,const int pcnt,const int d)80 CCGame::CCGame(const QString &unm, const uint t, const int sc, const uint gameno, const int gcnt, const int hcnt, const uint dur, const int gmode, const int ccnt, const int pcnt, const int d)
81 {
82 mUserNm = unm;
83 mTime = t;
84 mScore = sc;
85
86 mGameNo = gameno;
87 mGuessCnt = gcnt;
88 mHintCnt = hcnt;
89 mDuration = dur;
90
91 mGameMode = gmode;
92 mColorCnt = ccnt;
93 mPegCnt = pcnt;
94 mDoubles = d;
95 }
96
operator ==(const CCGame & other) const97 bool CCGame::operator==(const CCGame &other) const
98 {
99 return ( GetUserName() == other.GetUserName()
100 && mTime == other.mTime
101 && mScore == other.mScore
102
103 && mGameNo == other.mGameNo
104 && mGuessCnt == other.mGuessCnt
105 && mHintCnt == other.mHintCnt
106 && mDuration == other.mDuration
107
108 && mGameMode == other.mGameMode
109 && mColorCnt == other.mColorCnt
110 && mPegCnt == other.mPegCnt
111 && mDoubles == other.mDoubles );
112 }
113
operator !=(const CCGame & other) const114 bool CCGame::operator!=(const CCGame &other) const
115 {
116 return !(*this == other);
117 }
118
GetUserName() const119 QString CCGame::GetUserName() const
120 {
121 return mUserNm;
122 }
123
SetUserName(QString unm)124 void CCGame::SetUserName(QString unm)
125 {
126 QRegExp rx("[^\\S ]+");
127 unm.replace(rx, "");
128 unm = unm.trimmed();
129 unm.truncate(Settings::MAX_LENGTH_USERNAME);
130
131 mUserNm = unm;
132 }
133
GetTime() const134 uint CCGame::GetTime() const
135 {
136 return mTime;
137 }
138
GetScore() const139 int CCGame::GetScore() const
140 {
141 return mScore;
142 }
143
GetDatePartStr() const144 QString CCGame::GetDatePartStr() const
145 {
146 return QDateTime::fromTime_t(mTime).date().toString(Qt::ISODate);
147 }
148
GetTimePartStr() const149 QString CCGame::GetTimePartStr() const
150 {
151 return QDateTime::fromTime_t(mTime).time().toString(Qt::ISODate);
152 }
153
GetDateTimeStr() const154 QString CCGame::GetDateTimeStr() const
155 {
156 return QDateTime::fromTime_t(mTime).toString(Qt::TextDate);
157 }
158
GetCmbStr() const159 QString CCGame::GetCmbStr() const
160 {
161 return QString::number(mGameNo) + QString(" (") + QString::number(mDoubles) + QString("|") + QString::number(mPegCnt) + QString("|") + QString::number(mColorCnt) + QString(") ") + GetDateTimeStr();
162 }
163
Anonymize()164 void CCGame::Anonymize()
165 {
166 SetUserName("");
167 mHintCnt = mGuessCnt = mDuration = mScore = 0;
168 }
169
ToString() const170 QString CCGame::ToString() const
171 {
172 QStringList slist = QStringList();
173 slist << QString(QUrl::toPercentEncoding(mUserNm));
174 slist << QString::number(mTime);
175 slist << QString::number(mScore);
176
177 slist << QString::number(mGameNo);
178 slist << QString::number(mGuessCnt);
179 slist << QString::number(mHintCnt);
180 slist << QString::number(mDuration);
181
182 slist << QString::number(mGameMode);
183 slist << QString::number(mColorCnt);
184 slist << QString::number(mPegCnt);
185 slist << QString::number(mDoubles);
186 return slist.join("|");
187 }
188
HasSameSettings(const CCGame & g) const189 bool CCGame::HasSameSettings(const CCGame &g) const
190 {
191 return (mGameNo == g.mGameNo && mColorCnt == g.mColorCnt && mPegCnt == g.mPegCnt && mDoubles == g.mDoubles);
192 }
193
IsValidGame() const194 bool CCGame::IsValidGame() const
195 {
196 return ( mGameNo > 0
197 && mColorCnt >= ColorCode::MIN_COLOR_CNT && mColorCnt <= ColorCode::MAX_COLOR_CNT
198 && mPegCnt >= 2 && mPegCnt <= 5
199 && (mDoubles == 1 || mDoubles == 0) );
200 }
201