forked from quackle/quackle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluator.cpp
165 lines (134 loc) · 4.78 KB
/
evaluator.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
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
/*
* 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/>.
*/
#include "board.h"
#include "datamanager.h"
#include "game.h"
#include "evaluator.h"
#include "strategyparameters.h"
#include "catchall.h"
using namespace Quackle;
double Evaluator::equity(const GamePosition &position, const Move &move) const
{
(void) position;
return move.effectiveScore();
}
double Evaluator::playerConsideration(const GamePosition &position, const Move &move) const
{
(void) position;
(void) move;
return 0;
}
double Evaluator::sharedConsideration(const GamePosition &position, const Move &move) const
{
(void) position;
(void) move;
return 0;
}
double Evaluator::leaveValue(const LetterString &leave) const
{
(void) leave;
return 0;
}
////////////
double ScorePlusLeaveEvaluator::equity(const GamePosition &position, const Move &move) const
{
return playerConsideration(position, move) + sharedConsideration(position, move) + move.effectiveScore();
}
double ScorePlusLeaveEvaluator::playerConsideration(const GamePosition &position, const Move &move) const
{
return leaveValue((position.currentPlayer().rack() - move).tiles());
}
double ScorePlusLeaveEvaluator::sharedConsideration(const GamePosition &position, const Move &move) const
{
(void) position;
(void) move;
return 0;
}
double ScorePlusLeaveEvaluator::leaveValue(const LetterString &leave) const
{
LetterString alphabetized = String::alphabetize(leave);
if (QUACKLE_STRATEGY_PARAMETERS->hasSuperleaves() && QUACKLE_STRATEGY_PARAMETERS->superleave(alphabetized))
return QUACKLE_STRATEGY_PARAMETERS->superleave(alphabetized);
double value = 0;
if (!leave.empty())
{
double synergy = 0;
LetterString uniqleave;
if (QUACKLE_STRATEGY_PARAMETERS->hasWorths())
for (const auto& leaveIt : leave)
value += QUACKLE_STRATEGY_PARAMETERS->tileWorth(leaveIt);
if (QUACKLE_STRATEGY_PARAMETERS->hasSyn2())
for (unsigned int i = 0; i < alphabetized.length() - 1; ++i)
if (alphabetized[i] == alphabetized[i + 1])
value += QUACKLE_STRATEGY_PARAMETERS->syn2(alphabetized[i], alphabetized[i]);
uniqleave += alphabetized[0];
for (unsigned int i = 1; i < alphabetized.length(); ++i)
if (uniqleave[uniqleave.length() - 1] != alphabetized[i])
uniqleave += alphabetized[i];
if (uniqleave.length() >= 2 && QUACKLE_STRATEGY_PARAMETERS->hasSyn2())
{
for (unsigned int i = 0; i < uniqleave.length() - 1; ++i)
for (unsigned int j = i + 1; j < uniqleave.length(); ++j)
synergy += QUACKLE_STRATEGY_PARAMETERS->syn2(uniqleave[i], uniqleave[j]);
// TODO handle the Q
bool holding_bad_tile = false;
for (unsigned int i = 0; i < uniqleave.length(); ++i) {
if (QUACKLE_STRATEGY_PARAMETERS->tileWorth(uniqleave[i]) < -5.5) {
holding_bad_tile = true;
}
}
if ((synergy > 3.0) && !holding_bad_tile) {
synergy += 1.5 * (synergy - 3.0);
}
value += synergy;
}
}
int vowels = 0;
int cons = 0;
for (const auto& leaveIt : leave)
{
if (leaveIt != QUACKLE_BLANK_MARK)
{
if (QUACKLE_ALPHABET_PARAMETERS->isVowel(leaveIt))
vowels++;
else
cons++;
}
}
const float vcvalues[8][8] =
{
{ 0.0, 0.0, -1.0, -2.5, -5.0, -8.5, -13.5, -18.5},
{ -1.0, 0.0, 0.5, 0.0, -1.5, -5.0, -10.0, 0.0},
{ -3.5, -1.0, 0.5, 1.5, -1.5, -3.0, 0.0, 0.0},
{ -7.0, -3.5, -0.5, 2.5, 0.0, 0.0, 0.0, 0.0},
{-10.0, -6.5, -3.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{-13.5, -11.5, -8.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{-18.5, -16.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{-23.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
};
#ifdef DEBUG_BOARD
UVcout << QUACKLE_ALPHABET_PARAMETERS->userVisible(leave) << " has " << vowels << " vowels, " << cons << " cons. value of " << vcvalues[vowels][cons] << endl;
#endif
value += vcvalues[vowels][cons];
if (value < -40)
value = -40;
#ifdef DEBUG_BOARD
UVcout << "leave " << QUACKLE_ALPHABET_PARAMETERS->userVisible(leave) << " worth " << value << " uniq " << QUACKLE_ALPHABET_PARAMETERS->userVisible(uniqleave) << " synergy " << synergy << endl;
#endif
return value;
}