forked from quackle/quackle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.cpp
125 lines (101 loc) · 3.26 KB
/
player.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
/*
* Quackle -- Crossword game artificial intelligence and analysis tool
* Copyright (C) 2005-2014 Jason Katz-Brown and John O'Laughlin.
*
* This program 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.
*
* This program 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 <iostream>
#include <sstream>
#include "computerplayer.h"
#include "datamanager.h"
#include "player.h"
using namespace Quackle;
Player::Player()
: m_name(MARK_UV("No Name")), m_abbreviatedName(MARK_UV("NoName")), m_id(-1), m_playerType(ComputerPlayerType), m_computerPlayer(0), m_score(0), m_racksAreKnown(true)
{
}
Player::Player(const UVString &name, int playerType, int id)
: m_name(name), m_abbreviatedName(name), m_id(id), m_playerType(playerType), m_computerPlayer(0), m_score(0), m_racksAreKnown(true)
{
}
void Player::addToScore(int addition)
{
m_score += addition;
}
bool Player::positionallyEqual(const Player &otherPlayer) const
{
return rack().equals(otherPlayer.rack()) && score() == otherPlayer.score();
}
UVString Player::storeInformationToString() const
{
UVOStringStream ss;
ss << m_id << ';' << m_playerType << ';' << (m_playerType == ComputerPlayerType? m_computerPlayer->id() : (m_racksAreKnown? 0 : 1)) << ';' << m_name;
return ss.str();
}
void Player::loadInformationFromString(const UVString &info)
{
int i = 0;
UVString whatIsLeft(info);
for (UVString::size_type semicolonIndex; i <= 2; ++i)
{
semicolonIndex = whatIsLeft.find(MARK_UV(';'));
if (semicolonIndex == string::npos)
break;
UVString itemString = whatIsLeft.substr(0, semicolonIndex);
whatIsLeft = whatIsLeft.substr(semicolonIndex + 1);
UVStringStream ss(itemString);
int itemInt;
ss >> itemInt;
switch (i)
{
case 0:
m_id = itemInt;
break;
case 1:
m_playerType = itemInt;
break;
case 2:
switch (m_playerType)
{
case ComputerPlayerType:
if (QUACKLE_DATAMANAGER_EXISTS)
{
bool computerPlayerExists = false;
const Player &computerPlayer = QUACKLE_COMPUTER_PLAYERS.playerForId(itemInt, computerPlayerExists);
if (computerPlayerExists)
m_computerPlayer = computerPlayer.computerPlayer();
}
break;
case HumanPlayerType:
m_racksAreKnown = !itemInt;
break;
}
break;
default:
break;
}
}
m_name = whatIsLeft;
}
Player Player::makePlayerFromString(const UVString &info)
{
Player ret;
ret.loadInformationFromString(info);
return ret;
}
UVOStream &operator<<(UVOStream &o, const Quackle::Player &player)
{
o << (player.type() == Quackle::Player::ComputerPlayerType? MARK_UV("Computer") : MARK_UV("Human")) << " Player " << player.name() << " (id " << player.id() << ")" << " with score " << player.score() << " holding " << player.rack() << " after drawing [" << player.drawnLetters() << "]";
return o;
}