/
KnowledgeBase.cpp
307 lines (266 loc) · 6.94 KB
/
KnowledgeBase.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
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
#include "KnowledgeBase.h"
#include <iostream>
#include <iomanip>
#include <cassert>
using namespace std;
KnowledgeBase::KnowledgeBase( int iKitID, int iPlayerID )
{
i_KitID = iKitID;
i_ThisPlayerID = iPlayerID;
for (int iCard = 0; iCard < NUM_CARDS; ++iCard)
{
for (int iPlayer = 0; iPlayer < NUM_PLAYERS; ++iPlayer)
{
ai_StateMatrix[iCard][iPlayer] = STATE::NOT_CHECKED;
ai_NotCheckedCardCount[iPlayer] = ai_NotCheckedCardCount[iPlayer] + 1;
}
}
for (int i=0; i<NUM_PLAYERS; ++i)
{
ai_HaveCardCount[i] = 0;
ai_ProbablyCardCount[i] = 0;
ai_UsedToCardCount[i] = 0;
ai_NotCheckedCardCount[i] = 0;
}
//Kit Strength parameters
map_KitStrengthParams[KIT_PARAM::MAJOR] = 3;
map_KitStrengthParams[KIT_PARAM::MINOR] = 2;
map_KitStrengthParams[KIT_PARAM::CARDS_KNOWN_1] = 1;
map_KitStrengthParams[KIT_PARAM::CARDS_KNOWN_2] = 2;
map_KitStrengthParams[KIT_PARAM::CARDS_KNOWN_3] = 4;
map_KitStrengthParams[KIT_PARAM::CARDS_KNOWN_4] = 7;
map_KitStrengthParams[KIT_PARAM::CARDS_KNOWN_5] = 10;
}
KnowledgeBase::~KnowledgeBase()
{
}
void KnowledgeBase::InitKB( Set<Card> oSet )
{
oSet.Begin();
while( oSet.HasNext() )
{
Card* pCard = oSet.GetNext();
if ( i_KitID == ( pCard->GetID() & (SUIT::SUIT_MASK | KIT::KIT_MASK) ) )
{
SetState(i_ThisPlayerID, pCard->GetID(), STATE::HAVE);
for(int i=0; i<NUM_PLAYERS; ++i)
{
if ( i != i_ThisPlayerID )
{
SetState(i, pCard->GetID(), STATE::DONT_HAVE);
}
}
}
}
// There cannot be NOT_CHECKED states in this player's positions, turn them to DONT_HAVE
for (int i = 0; i < NUM_CARDS; ++i)
{
if ( GetState(i_ThisPlayerID, GetCardIDFromIndex(i)) == STATE::NOT_CHECKED )
{
SetState(i_ThisPlayerID, GetCardIDFromIndex(i), STATE::DONT_HAVE);
}
}
}
void KnowledgeBase::SetState( int iPlayerID, int iCardID, int iState )
{
assert( iPlayerID >= 0 && iPlayerID <= 5 );
assert( iState >= 0 && iState <= 4 );
int iCardIndex = GetCardIndex(iCardID);
int iPrevState = ai_StateMatrix[iCardIndex][iPlayerID];
if ( iPrevState == STATE::NOT_CHECKED && iState == STATE::HAVE )
{
// state change from NOT_CHECKED to HAVE
ai_NotCheckedCardCount[iPlayerID] = ai_NotCheckedCardCount[iPlayerID] - 1;
ai_HaveCardCount[iPlayerID] = ai_HaveCardCount[iPlayerID] + 1;
}
else if ( iPrevState == STATE::NOT_CHECKED && iState == STATE::PROBABLY_HAVE )
{
// state change from NOT_CHECKED to PROBABLY_HAVE
ai_NotCheckedCardCount[iPlayerID] = ai_NotCheckedCardCount[iPlayerID] - 1;
ai_ProbablyCardCount[iPlayerID] = ai_ProbablyCardCount[iPlayerID] + 1;
}
else if ( iPrevState == STATE::NOT_CHECKED && iState == STATE::DONT_HAVE )
{
// state change from NOT_CHECKED to DONT_HAVE
ai_NotCheckedCardCount[iPlayerID] = ai_NotCheckedCardCount[iPlayerID] - 1;
}
else if ( iPrevState == STATE::USED_TO_HAVE && iState == STATE::PROBABLY_HAVE )
{
// state change from USED_TO_HAVE to PROBABLY_HAVE
ai_ProbablyCardCount[iPlayerID] = ai_ProbablyCardCount[iPlayerID] + 1;
ai_UsedToCardCount[iPlayerID] = ai_UsedToCardCount[iPlayerID] - 1;
}
else if ( iPrevState == STATE::PROBABLY_HAVE && iState == STATE::USED_TO_HAVE )
{
// state change from PROBABLY_HAVE to USED_TO_HAVE
ai_ProbablyCardCount[iPlayerID] = ai_ProbablyCardCount[iPlayerID] - 1;
ai_UsedToCardCount[iPlayerID] = ai_UsedToCardCount[iPlayerID] + 1;
}
else if ( iPrevState == STATE::PROBABLY_HAVE && iState == STATE::HAVE )
{
// inference condition
ai_HaveCardCount[iPlayerID] = ai_HaveCardCount[iPlayerID] + 1;
ai_ProbablyCardCount[iPlayerID] = ai_ProbablyCardCount[iPlayerID] - 1;
}
else if ( iPrevState == STATE::HAVE && iState == STATE::DONT_HAVE )
{
// if a card is taken from the player, reduce the count by one
ai_HaveCardCount[iPlayerID] = ai_HaveCardCount[iPlayerID] - 1;
}
else if ( iPrevState == STATE::DONT_HAVE && iState == STATE::HAVE )
{
// if a card is added, increase the count by one
ai_HaveCardCount[iPlayerID] = ai_HaveCardCount[iPlayerID] + 1;
}
ai_StateMatrix[iCardIndex][iPlayerID] = iState;
}
int KnowledgeBase::GetState( int iPlayerID, int iCardID )
{
assert( iPlayerID >= 0 && iPlayerID <= 5 );
int iCardIndex = GetCardIndex(iCardID);
return ai_StateMatrix[iCardIndex][iPlayerID];
}
int KnowledgeBase::GetCardIndex( int iCardID )
{
int iCardVal = (iCardID & CARD::CARD_MASK);
if ( iCardVal < CARD::NINE ) // MINOR
{
return (iCardVal - 1);
}
return (iCardVal - 7);
}
void KnowledgeBase::DebugPrint()
{
bool bArrowHead = false;
for(int i = 0; i < NUM_PLAYERS; ++i)
{
if ( i == i_ThisPlayerID )
{
if (i == 0)
cout << setw(10);
else
cout << setw(12);
cout << "*";
bArrowHead = true;
}
else
{
if (bArrowHead)
{
cout << setw(12);
bArrowHead = false;
}
else
{
if (i == 0)
cout << setw(10);
else
cout << setw(12);
cout << " ";
}
}
}
cout << endl;
for(int i = 0; i < NUM_PLAYERS; ++i)
{
cout << setw(10);
cout << "[P " << i+1 << "]";
}
cout << endl << endl;
for (int iCard = 0; iCard < NUM_CARDS; ++iCard)
{
int iCardID = GetCardIDFromIndex(iCard);
cout << SYMBOL::GetCardSymbol(iCardID, false);
if ( (iCardID & CARD::CARD_MASK) == CARD::TEN )
cout << setw(4);
else
cout << setw(5);
for (int iPlayer = 0; iPlayer < NUM_PLAYERS; ++iPlayer)
{
int iState = ai_StateMatrix[iCard][iPlayer];
switch(iState)
{
case STATE::NOT_CHECKED:
cout << ".";
break;
case STATE::DONT_HAVE:
cout << "X";
break;
case STATE::HAVE:
cout << "H";
break;
case STATE::PROBABLY_HAVE:
cout << "P";
break;
case STATE::USED_TO_HAVE:
cout << "U";
break;
}
if (iPlayer < NUM_PLAYERS - 1) cout << setw(12);
}
cout << endl << endl;
}
}
int KnowledgeBase::GetCardIDFromIndex( int iIndex )
{
int iKit = (i_KitID & KIT::KIT_MASK);
if (iKit == KIT::MAJOR)
{
return ( i_KitID | (iIndex + 7) );
}
//MINOR
return ( i_KitID | (iIndex + 1) );
}
bool KnowledgeBase::CanDropKit()
{
int iCardCout = 0;
for (int i=0; i < NUM_PLAYERS; ++i)
{
iCardCout += ai_HaveCardCount[i];
}
return ( iCardCout == NUM_CARDS ? true : false );
}
int KnowledgeBase::GetKitStrength()
{
int iStrength = 1;
if ( (i_KitID & KIT::KIT_MASK) == KIT::MAJOR )
{
iStrength *= map_KitStrengthParams[KIT_PARAM::MAJOR];
}
else
{
iStrength *= map_KitStrengthParams[KIT_PARAM::MINOR];
}
int iCardCout = 0;
for (int i=0; i < NUM_PLAYERS; ++i)
{
iCardCout += ai_HaveCardCount[i];
}
switch(iCardCout)
{
case 0:
iStrength = 0;
break;
case 1:
iStrength *= map_KitStrengthParams[KIT_PARAM::CARDS_KNOWN_1];
break;
case 2:
iStrength *= map_KitStrengthParams[KIT_PARAM::CARDS_KNOWN_2];
break;
case 3:
iStrength *= map_KitStrengthParams[KIT_PARAM::CARDS_KNOWN_3];
break;
case 4:
iStrength *= map_KitStrengthParams[KIT_PARAM::CARDS_KNOWN_4];
break;
case 5:
iStrength *= map_KitStrengthParams[KIT_PARAM::CARDS_KNOWN_5];
break;
case 6:
iStrength *= map_KitStrengthParams[KIT_PARAM::CARDS_KNOWN_6];
break;
default:
iStrength *= -1;
}
return iStrength;
}