-
Notifications
You must be signed in to change notification settings - Fork 0
/
alphabetparameters.h
413 lines (321 loc) · 10.3 KB
/
alphabetparameters.h
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
* 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
*/
#ifndef QUACKLE_ALPHABETPARAMETERS_H
#define QUACKLE_ALPHABETPARAMETERS_H
#include <vector>
#include <map>
#include "fixedstring.h"
#include "uv.h"
using namespace std;
#define QUACKLE_MAXIMUM_ALPHABET_SIZE 55
#define QUACKLE_MINIMUM_ALPHABET_SIZE 1
// Quackle's alphabet scheme includes a way to represent blanks
// in two ways: either as the blank letter or as the letter plus
// the blank offset
// we officially define null but others are only euglenids+i...
#define QUACKLE_NULL_MARK_TEXT MARK_UV(" ")
#define QUACKLE_NULL_MARK 0
#define QUACKLE_BLANK_MARK 1 // let this be '?'
#define QUACKLE_PLAYED_THRU_MARK 2 // let this be '.'
#define QUACKLE_PLAYTHRU_START_MARK 3 // let this be '('
#define QUACKLE_PLAYTHRU_END_MARK 4 // let this be ')'
#define QUACKLE_FIRST_LETTER 5
#define QUACKLE_BLANK_OFFSET QUACKLE_MAXIMUM_ALPHABET_SIZE
namespace Quackle
{
// Letter & LetterString are internally encoded strings mapped
// from UVString (UV == user visible) to 1-n indices. The are intended
// to be language independent whereas UVStrings are language dependent.
#define LETTER_STRING_MAXIMUM_LENGTH FIXED_STRING_MAXIMUM_LENGTH
typedef char Letter;
typedef FixedLengthString LetterString;
typedef std::string LongLetterString;
typedef std::vector<LetterString> WordList;
namespace String
{
LetterString left(const LetterString &letterString, int number);
LetterString alphabetize(const LetterString &letterString);
LetterString clearBlankness(const LetterString &letterString);
LetterString setBlankness(const LetterString &letterString);
// turns string like .ANELINn into .ANELIN?
LetterString usedTiles(const LetterString &letterString);
// allocate a countsArray of size
// QUACKLE_FIRST_LETTER + QUACKLE_MAXIMUM_ALPHABET_SIZE
void counts(const LetterString &letterString, char *countsArray);
void counts(const LongLetterString &letterString, char *countsArray);
inline Letter back(const LetterString &letterString)
{
return *(letterString.end() - 1);
}
inline void pop_back(LetterString &letterString)
{
letterString.erase(letterString.end() - 1);
}
inline Letter front(const LetterString &letterString)
{
return *letterString.begin();
}
inline void pop_front(LetterString &letterString)
{
letterString.erase(letterString.begin());
}
inline LetterString allButFront(const LetterString &letterString)
{
return letterString.substr(1, letterString.length() - 1);
}
}
class LetterParameter
{
public:
LetterParameter();
LetterParameter(Letter letter);
LetterParameter(Letter letter, const UVString &text, const UVString &blankText, int score = 0, int count = 0, bool isVowel = false);
UVString text() const;
void setText(const UVString &text);
UVString blankText() const;
void setBlankText(const UVString &blankText);
Letter letter() const;
void setLetter(Letter letter);
int score() const;
void setScore(int score);
// number of doodads in the bag
int count() const;
void setCount(int count);
bool isVowel() const;
void setVowel(bool isVowel);
private:
Letter m_letter;
UVString m_text;
UVString m_blankText;
int m_score;
int m_count;
bool m_isVowel;
};
inline LetterParameter::LetterParameter()
: m_letter(QUACKLE_NULL_MARK), m_score(0), m_isVowel(false)
{
}
inline LetterParameter::LetterParameter(Letter letter)
: m_letter(letter), m_score(0), m_isVowel(false)
{
}
inline LetterParameter::LetterParameter(Letter letter, const UVString &text, const UVString &blankText, int score, int count, bool isVowel)
: m_letter(letter), m_text(text), m_blankText(blankText), m_score(score), m_count(count), m_isVowel(isVowel)
{
}
inline UVString LetterParameter::text() const
{
return m_text;
}
inline void LetterParameter::setText(const UVString &text)
{
m_text = text;
}
inline UVString LetterParameter::blankText() const
{
return m_blankText;
}
inline void LetterParameter::setBlankText(const UVString &blankText)
{
m_blankText = blankText;
}
inline Letter LetterParameter::letter() const
{
return m_letter;
}
inline void LetterParameter::setLetter(Letter letter)
{
m_letter = letter;
}
inline int LetterParameter::score() const
{
return m_score;
}
inline void LetterParameter::setScore(int score)
{
m_score = score;
}
inline int LetterParameter::count() const
{
return m_count;
}
inline void LetterParameter::setCount(int count)
{
m_count = count;
}
inline bool LetterParameter::isVowel() const
{
return m_isVowel;
}
inline void LetterParameter::setVowel(bool isVowel)
{
m_isVowel = isVowel;
}
typedef vector<LetterParameter> Alphabet;
class AlphabetParameters
{
public:
AlphabetParameters();
// Returns how many letters there are (excludes blanks and null letter).
// Thus this return value is how many letters there are inclusively between
// QUACKLE_FIRST_LETTER and lastLetter()
int length() const;
// first letter in alphabet -- same as the #define
Letter firstLetter() const;
// the last letter in the alphabet -- all letters after this are blanks
Letter lastLetter() const;
void setAlphabet(const Alphabet &alphabet);
const LetterParameter& letterParameter(Letter letter) const;
void setLetterParameter(Letter letter, const LetterParameter &letterParameter);
// get an alphabet with blank and null values set; note
// this empty alphabet specifies no blanks in the bag
static Alphabet emptyAlphabet();
// useful for setting number of blanks
void setCount(Letter letter, int count);
// useful for setting score of blanks
void setScore(Letter letter, int score);
// whether letter is between firstLetter + blank_offset
// and lastLetter + blank_offset
bool isBlankLetter(Letter letter) const;
// returns letter if it's not a blank, or a plain
// (nonblank) version of letter otherwise
Letter clearBlankness(Letter letter) const;
LetterString clearBlankness(const LetterString &letterString) const;
// returns letter if it's a blank, or a blank
// version of letter otherwise
Letter setBlankness(Letter letter) const;
// whether letter is between first and last letter
bool isPlainLetter(Letter letter) const;
// whether letter is a blank or plain letter or not
bool isSomeLetter(Letter letter) const;
int count(Letter letter) const;
int score(Letter letter) const;
bool isVowel(Letter letter) const;
// alphabet-based conversion facilities:
// LetterString -> UVString
UVString userVisible(const LetterString &letterString) const;
UVString userVisible(Letter letter) const;
// UVString -> LetterString. Letters that could not be encoded are
// stored in leftover if it is non-null.
LetterString encode(const UVString &word, UVString *leftover = 0) const;
// a convenience field; this is unused by libquackle
string alphabetName() const;
void setAlphabetName(const string &name);
// finds a file in the alphabets data directory
static string findAlphabetFile(const string &alphabet);
protected:
void updateLength();
int m_length;
Alphabet m_alphabet;
Alphabet::const_iterator m_firstLetterIterator;
typedef map<UVString, int> LetterLookupMap;
LetterLookupMap m_letterLookup;
string m_alphabetName;
};
inline int AlphabetParameters::length() const
{
return m_length;
}
inline Letter AlphabetParameters::firstLetter() const
{
return QUACKLE_FIRST_LETTER;
}
inline Letter AlphabetParameters::lastLetter() const
{
return QUACKLE_FIRST_LETTER + m_length - 1;
}
inline const LetterParameter& AlphabetParameters::letterParameter(Letter letter) const
{
assert(letter >= 0 && (unsigned int)letter < m_alphabet.size());
return m_alphabet[letter];
}
inline bool AlphabetParameters::isBlankLetter(Letter letter) const
{
return letter > lastLetter();
}
inline Letter AlphabetParameters::clearBlankness(Letter letter) const
{
if (isBlankLetter(letter))
return letter - QUACKLE_BLANK_OFFSET;
else
return letter;
}
inline Letter AlphabetParameters::setBlankness(Letter letter) const
{
if (!isBlankLetter(letter))
return letter + QUACKLE_BLANK_OFFSET;
else
return letter;
}
inline bool AlphabetParameters::isPlainLetter(Letter letter) const
{
return letter >= firstLetter() && letter <= lastLetter();
}
inline bool AlphabetParameters::isSomeLetter(Letter letter) const
{
return isBlankLetter(letter) || isPlainLetter(letter);
}
inline int AlphabetParameters::count(Letter letter) const
{
return letterParameter(letter).count();
}
inline int AlphabetParameters::score(Letter letter) const
{
return letterParameter(letter).score();
}
inline bool AlphabetParameters::isVowel(Letter letter) const
{
return letterParameter(letter).isVowel();
}
inline string AlphabetParameters::alphabetName() const
{
return m_alphabetName;
}
inline void AlphabetParameters::setAlphabetName(const string &name)
{
m_alphabetName = name;
}
class EnglishAlphabetParameters : public AlphabetParameters
{
public:
EnglishAlphabetParameters();
};
}
#ifdef QUACKLE_USE_OWN_LETTERSTRING
inline Quackle::LetterString operator+(const Quackle::LetterString &letterString1, const Quackle::LetterString &letterString2)
{
Quackle::LetterString ret(letterString1);
return ret += letterString2;
}
inline Quackle::LetterString operator+(const Quackle::LetterString &letterString, Quackle::Letter letter)
{
Quackle::LetterString ret;
ret += letterString;
return ret += letter;
}
inline Quackle::LetterString operator+(Quackle::Letter letter, const Quackle::LetterString &letterString)
{
Quackle::LetterString ret(letter);
return ret += letterString;
}
#endif // QUACKLE_USE_OWN_LETTERSTRING
UVOStream &operator<<(UVOStream& o, const Quackle::LetterParameter &letterParameter);
UVOStream &operator<<(UVOStream& o, const Quackle::Alphabet &alphabet);
#endif