forked from quackle/quackle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datamanager.h
202 lines (156 loc) · 5.89 KB
/
datamanager.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
/*
* Quackle -- Crossword game artificial intelligence and analysis tool
* Copyright (C) 2005-2019 Jason Katz-Brown, John O'Laughlin, and John Fultz.
*
* 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/>.
*/
#ifndef QUACKLE_DATAMANAGER_H
#define QUACKLE_DATAMANAGER_H
#include <mutex>
#include <random>
#include <string>
#include "playerlist.h"
using namespace std;
#define QUACKLE_DATAMANAGER Quackle::DataManager::self()
#define QUACKLE_DATAMANAGER_EXISTS Quackle::DataManager::exists()
#define QUACKLE_EVALUATOR Quackle::DataManager::self()->evaluator()
#define QUACKLE_PARAMETERS Quackle::DataManager::self()->parameters()
#define QUACKLE_ALPHABET_PARAMETERS Quackle::DataManager::self()->alphabetParameters()
#define QUACKLE_BOARD_PARAMETERS Quackle::DataManager::self()->boardParameters()
#define QUACKLE_LEXICON_PARAMETERS Quackle::DataManager::self()->lexiconParameters()
#define QUACKLE_STRATEGY_PARAMETERS Quackle::DataManager::self()->strategyParameters()
#define QUACKLE_COMPUTER_PLAYERS Quackle::DataManager::self()->computerPlayers()
namespace Quackle
{
// General singleton type that will be around whenever
// you use libquackle.
// It provides access to lexica (todo), random numbers,
// and all parameters for a game
class AlphabetParameters;
class BoardParameters;
class Evaluator;
class GameParameters;
class LexiconParameters;
class PlayerList;
class StrategyParameters;
class DataManager
{
public:
// sets up singleton, seeds random number generator,
// and creates default parameter instances
DataManager();
~DataManager();
static DataManager *self();
static bool exists();
// Are we in shape to run a game?
// Makes sure there's a lexicon at least.
bool isGood() const;
Evaluator *evaluator();
void setEvaluator(Evaluator *evaluator);
// game and board parameter objects are owned and deleted by
// this data manager
GameParameters *parameters();
void setParameters(GameParameters *parameters);
AlphabetParameters *alphabetParameters();
void setAlphabetParameters(AlphabetParameters *alphabetParameters);
BoardParameters *boardParameters();
void setBoardParameters(BoardParameters *boardParameters);
LexiconParameters *lexiconParameters();
void setLexiconParameters(LexiconParameters *lexiconParameters);
StrategyParameters *strategyParameters();
void setStrategyParameters(StrategyParameters *strategyParameters);
// When the data manager dies or setComputerPlayers is called, it deletes
// all of the computer players pointed to by the players in this list. The
// players' names are the names of the computer players, and the players'
// ids are the ids of the computer players. The last player on the list
// should be considered the best player.
const PlayerList &computerPlayers() const;
void setComputerPlayers(const PlayerList &playerList);
void cleanupComputerPlayers();
// Find a file at datadir/subdir/lexicon/file.
// If this doesn't exist, tries backupLexicon instead of lexicon.
// Returns empty string if the file is not found.
string findDataFile(const string &subDirectory, const string &lexicon, const string &file);
// Find a file at datadir/subdir/file.
// Returns empty string if the file is not found.
string findDataFile(const string &subDirectory, const string &file);
// Returns true if the data file is in user-land.
bool hasUserDataFile(const string &subDirectory, const string &file);
// returns similarly-named file
string makeDataFilename(const string &subDirectory, const string &lexicon, const string &file, bool user);
string makeDataFilename(const string &subDirectory, const string &file, bool user);
void setBackupLexicon(string backupLexicon) { m_backupLexicon = backupLexicon; }
string backupLexicon() { return m_backupLexicon; }
void setAppDataDirectory(string directory) { m_appDataDirectory = directory; }
string appDataDirectory() { return m_appDataDirectory; }
void setUserDataDirectory(string directory) { m_userDataDirectory = directory; }
string userDataDirectory() { return m_userDataDirectory; }
void seedRandomNumbers(unsigned int seed);
void seedRandomNumbers(seed_seq& seed);
int randomInteger(int low, int high);
private:
static DataManager *m_self;
bool fileExists(const string &filename);
string m_appDataDirectory;
string m_userDataDirectory;
// lexicon that has all data files
string m_backupLexicon;
Evaluator *m_evaluator;
GameParameters *m_parameters;
AlphabetParameters *m_alphabetParameters;
BoardParameters *m_boardParameters;
LexiconParameters *m_lexiconParameters;
StrategyParameters *m_strategyParameters;
PlayerList m_computerPlayers;
mt19937_64 m_mersenneTwisterRng;
mutex m_RngMutex;
};
inline DataManager *DataManager::self()
{
return m_self;
}
inline bool DataManager::exists()
{
return m_self != 0;
}
inline Evaluator *DataManager::evaluator()
{
return m_evaluator;
}
inline GameParameters *DataManager::parameters()
{
return m_parameters;
}
inline AlphabetParameters *DataManager::alphabetParameters()
{
return m_alphabetParameters;
}
inline BoardParameters *DataManager::boardParameters()
{
return m_boardParameters;
}
inline LexiconParameters *DataManager::lexiconParameters()
{
return m_lexiconParameters;
}
inline StrategyParameters *DataManager::strategyParameters()
{
return m_strategyParameters;
}
inline const PlayerList &DataManager::computerPlayers() const
{
return m_computerPlayers;
}
}
#endif