-
Notifications
You must be signed in to change notification settings - Fork 0
/
rack.cpp
136 lines (110 loc) · 3.08 KB
/
rack.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
125
126
127
128
129
130
131
132
133
134
135
/*
* Quackle -- Crossword game artificial intelligence and analysis tool
* Copyright (C) 2005-2006 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 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include <algorithm>
#include <iostream>
#include "datamanager.h"
#include "move.h"
#include "rack.h"
#include "uv.h"
using namespace std;
using namespace Quackle;
LetterString Rack::alphaTiles() const
{
return String::alphabetize(m_tiles);
}
bool Rack::equals(const Rack &rack) const
{
return alphaTiles() == rack.alphaTiles();
}
bool Rack::unload(const LetterString &used)
{
// UVcout << *this << ".unload(" << used << ")" << endl;
LetterString newtiles = m_tiles;
bool ret = true;
LetterString::const_iterator usedEnd(used.end());
for (LetterString::const_iterator usedIt = used.begin(); usedIt != usedEnd; ++usedIt)
{
bool found = false;
LetterString::iterator newEnd(newtiles.end());
for (LetterString::iterator newIt = newtiles.begin(); newIt != newEnd; ++newIt)
{
if (*newIt == *usedIt)
{
*newIt = QUACKLE_NULL_MARK;
found = true;
break;
}
}
if (!found)
ret = false;
}
// UVcout << "newtiles: " << newtiles << endl;
m_tiles.clear();
LetterString::const_iterator newEnd(newtiles.end());
for (LetterString::const_iterator newIt = newtiles.begin(); newIt != newEnd; ++newIt)
if (*newIt != QUACKLE_NULL_MARK)
m_tiles += *newIt;
return ret;
}
bool Rack::contains(const LetterString &used) const
{
return Rack(*this).unload(used);
}
void Rack::shuffle()
{
random_shuffle(m_tiles.begin(), m_tiles.end());
}
int Rack::score() const
{
int ret = 0;
const LetterString::const_iterator end(m_tiles.end());
for (LetterString::const_iterator it = m_tiles.begin(); it != end; ++it)
ret += QUACKLE_ALPHABET_PARAMETERS->score(*it);
return ret;
}
unsigned int Rack::size() const
{
return m_tiles.size();
}
const Rack operator-(const Rack &rack, const Move &move)
{
Rack ret(rack);
ret.unload(move.usedTiles());
return ret;
}
const Rack operator-(const Rack &rack1, const Rack &rack2)
{
Rack ret(rack1);
ret.unload(rack2.tiles());
return ret;
}
UVString Rack::xml() const
{
return MARK_UV("<rack tiles=\"") + QUACKLE_ALPHABET_PARAMETERS->userVisible(m_tiles) + MARK_UV("\" />");
}
UVString Rack::toString() const
{
return QUACKLE_ALPHABET_PARAMETERS->userVisible(m_tiles);
}
UVOStream &operator<<(UVOStream &o, const Rack &rack)
{
o << rack.toString();
return o;
}