-
Notifications
You must be signed in to change notification settings - Fork 0
/
inference.go
446 lines (408 loc) · 14.6 KB
/
inference.go
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
package main
type Inference struct {
//facts
trumpsInGame []Card
cardsPlayed []Card
declarerVoidSuit map[string]bool
opp1VoidSuit map[string]bool
opp2VoidSuit map[string]bool
// beliefs
declarerVoidSuitB map[string]bool
opp1VoidSuitB map[string]bool
opp2VoidSuitB map[string]bool
declarerVoidCards []Card
opp1VoidCards []Card
opp2VoidCards []Card
}
func makeInference() Inference {
return Inference{
[]Card{},
[]Card{},
map[string]bool{
CLUBS: false,
SPADE: false,
HEART: false,
CARO: false,
},
map[string]bool{
CLUBS: false,
SPADE: false,
HEART: false,
CARO: false,
},
map[string]bool{
CLUBS: false,
SPADE: false,
HEART: false,
CARO: false,
},
map[string]bool{
CLUBS: false,
SPADE: false,
HEART: false,
CARO: false,
},
map[string]bool{
CLUBS: false,
SPADE: false,
HEART: false,
CARO: false,
},
map[string]bool{
CLUBS: false,
SPADE: false,
HEART: false,
CARO: false,
},
[]Card{},
[]Card{},
[]Card{},
}
}
func (s *Inference) cloneInference() Inference {
newI := makeInference()
newI.trumpsInGame = make([]Card, len(s.trumpsInGame))
copy(newI.trumpsInGame, s.trumpsInGame)
newI.cardsPlayed = make([]Card, len(s.cardsPlayed))
copy(newI.cardsPlayed, s.cardsPlayed)
newI.declarerVoidSuit = map[string]bool{
CLUBS: s.declarerVoidSuit[CLUBS],
SPADE: s.declarerVoidSuit[SPADE],
HEART: s.declarerVoidSuit[HEART],
CARO: s.declarerVoidSuit[CARO],
}
newI.opp1VoidSuit = map[string]bool{
CLUBS: s.opp1VoidSuit[CLUBS],
SPADE: s.opp1VoidSuit[SPADE],
HEART: s.opp1VoidSuit[HEART],
CARO: s.opp1VoidSuit[CARO],
}
newI.opp2VoidSuit = map[string]bool{
CLUBS: s.opp2VoidSuit[CLUBS],
SPADE: s.opp2VoidSuit[SPADE],
HEART: s.opp2VoidSuit[HEART],
CARO: s.opp2VoidSuit[CARO],
}
// beliefs
newI.declarerVoidSuitB = map[string]bool{
CLUBS: s.declarerVoidSuitB[CLUBS],
SPADE: s.declarerVoidSuitB[SPADE],
HEART: s.declarerVoidSuitB[HEART],
CARO: s.declarerVoidSuitB[CARO],
}
newI.opp1VoidSuitB = map[string]bool{
CLUBS: s.opp1VoidSuitB[CLUBS],
SPADE: s.opp1VoidSuitB[SPADE],
HEART: s.opp1VoidSuitB[HEART],
CARO: s.opp1VoidSuitB[CARO],
}
newI.opp2VoidSuitB = map[string]bool{
CLUBS: s.opp2VoidSuitB[CLUBS],
SPADE: s.opp2VoidSuitB[SPADE],
HEART: s.opp2VoidSuitB[HEART],
CARO: s.opp2VoidSuitB[CARO],
}
newI.declarerVoidCards = make([]Card, len(s.declarerVoidCards))
copy(newI.declarerVoidCards, s.declarerVoidCards)
newI.opp1VoidCards = make([]Card, len(s.opp1VoidCards))
copy(newI.opp1VoidCards, s.opp1VoidCards)
newI.opp2VoidCards = make([]Card, len(s.opp2VoidCards))
copy(newI.opp2VoidCards, s.opp2VoidCards)
return newI
}
func (s *Inference) getDeclarerVoidSuit() map[string]bool {
return map[string]bool{
CLUBS: s.declarerVoidSuit[CLUBS] || s.declarerVoidSuitB[CLUBS],
SPADE: s.declarerVoidSuit[SPADE] || s.declarerVoidSuitB[SPADE],
HEART: s.declarerVoidSuit[HEART] || s.declarerVoidSuitB[HEART],
CARO: s.declarerVoidSuit[CARO] || s.declarerVoidSuitB[CARO],
}
}
func (s *Inference) getOpp1VoidSuit() map[string]bool {
return map[string]bool{
CLUBS: s.opp1VoidSuit[CLUBS] || s.opp1VoidSuitB[CLUBS],
SPADE: s.opp1VoidSuit[SPADE] || s.opp1VoidSuitB[SPADE],
HEART: s.opp1VoidSuit[HEART] || s.opp1VoidSuitB[HEART],
CARO: s.opp1VoidSuit[CARO] || s.opp1VoidSuitB[CARO],
}
}
func (s *Inference) getOpp2VoidSuit() map[string]bool {
return map[string]bool{
CLUBS: s.opp2VoidSuit[CLUBS] || s.opp2VoidSuitB[CLUBS],
SPADE: s.opp2VoidSuit[SPADE] || s.opp2VoidSuitB[SPADE],
HEART: s.opp2VoidSuit[HEART] || s.opp2VoidSuitB[HEART],
CARO: s.opp2VoidSuit[CARO] || s.opp2VoidSuitB[CARO],
}
}
// Remove beliefs concerning opp player, about cards belonging the cards argument.
func (s *SuitState) detractBeliefs(opp string, cards []Card) {
getSuit := func(suit string) int {
return len(filter(cards, func(c Card) bool {
return getSuit(s.trump, c) == suit
}))
}
switch opp {
case "decl":
for _, suit := range suits {
if getSuit(suit) > 0 {
s.declarerVoidSuitB[suit] = false
}
}
// s.declarerVoidSuitB = map[string]bool{
// CLUBS: false,
// SPADE: false,
// HEART: false,
// CARO: false,
// }
s.declarerVoidCards = remove(s.declarerVoidCards, cards...)
debugTacticsLog("Updated beliefs: %v, %v", voidString(s.declarerVoidSuitB), s.declarerVoidCards)
case "opp1":
for _, suit := range suits {
if getSuit(suit) > 0 {
s.opp1VoidSuitB[suit] = false
s.declarer.getInference().opp1VoidSuitB[suit] = false
}
}
// s.opp1VoidSuitB = map[string]bool{
// CLUBS: false,
// SPADE: false,
// HEART: false,
// CARO: false,
// }
s.opp1VoidCards = remove(s.opp1VoidCards, cards...)
debugTacticsLog("Updated beliefs: %v, %v", voidString(s.opp1VoidSuitB), s.opp1VoidCards)
case "opp2":
for _, suit := range suits {
if getSuit(suit) > 0 {
s.opp2VoidSuitB[suit] = false
}
}
// s.opp2VoidSuitB = map[string]bool{
// CLUBS: false,
// SPADE: false,
// HEART: false,
// CARO: false,
// }
s.opp2VoidCards = remove(s.opp2VoidCards, cards...)
debugTacticsLog("Updated beliefs: %v, %v", voidString(s.opp2VoidSuitB), s.opp2VoidCards)
}
}
func opponentDoesNotHaveAnyLowerCards(s *SuitState, p PlayerI, card Card) {
if p.getName() == s.opp1.getName() && opponentIsLosingTrick(s, p, card, true) {
if noHigherCard(s, true, s.declarer.getHand(), card) {
debugTacticsLog("TRICK: %v, Card: %v\n", s.trick, card)
debugTacticsLog("INFERENCE: ***************DECLARER***************\n")
debugTacticsLog("INFERENCE: Opp1 does not have any lower cards \n")
debugTacticsLog("INFERENCE: Void on suit \n")
debugTacticsLog("INFERENCE: **************************************\n")
s.declarer.getInference().opp1VoidSuitB[s.follow] = true
debugTacticsLog("SuitState: %v, Player: %v, Card %v\n", s, p, card)
}
}
}
func notFollowingSuit(s *SuitState, p PlayerI, card Card) {
// Player VOID on suit
if len(s.trick) > 0 && getSuit(s.trump, card) != getSuit(s.trump, s.trick[0]) {
debugTacticsLog("TRICK: %v, Card: %v\n", s.trick, card)
debugTacticsLog("INFERENCE: **************************************\n")
debugTacticsLog("INFERENCE: Not following tricks \n")
debugTacticsLog("INFERENCE: Void on suit \n")
debugTacticsLog("INFERENCE: **************************************\n")
if p.getName() == s.declarer.getName() {
s.declarerVoidSuit[getSuit(s.trump, s.trick[0])] = true
}
if p.getName() == s.opp1.getName() {
s.opp1VoidSuit[getSuit(s.trump, s.trick[0])] = true
}
if p.getName() == s.opp2.getName() {
s.opp2VoidSuit[getSuit(s.trump, s.trick[0])] = true
}
}
}
func playingAFullTrumpOnALosingTrick(s *SuitState, p PlayerI, card Card) {
if p.getName() != s.declarer.getName() && s.follow == s.trump {
if getSuit(s.trump, card) == s.trump && (card.Rank == "A" || card.Rank == "10") {
if opponentIsLosingTrick(s, p, card, false) {
// TODO:
debugTacticsLog("INFERENCE: **************************************\n")
debugTacticsLog("INFERENCE: Playing a full Trump on a losing trick\n")
debugTacticsLog("INFERENCE: Is the last of the player \n")
debugTacticsLog("INFERENCE: **************************************\n")
if p.getName() == s.opp1.getName() {
s.opp1VoidSuitB[s.trump] = true
}
if p.getName() == s.opp2.getName() {
s.opp2VoidSuitB[s.trump] = true
}
}
}
}
}
func playingAFullTrumpOnALosingTrickDecl(s *SuitState, p PlayerI, card Card) {
fullOne := card.equals(Card{s.follow, "10"}) || card.equals(Card{s.follow, "A"})
if len(s.trick) > 0 {
if p.getName() == s.declarer.getName() && fullOne && !s.greater(card, s.trick...) {
debugTacticsLog("INFERENCE: **************************************\n")
debugTacticsLog("INFERENCE: Declarer void on suit %v \n", s.follow)
debugTacticsLog("INFERENCE: **************************************\n")
s.declarerVoidSuitB[s.follow] = true
}
}
}
func losingASmearedTrick(s *SuitState, p PlayerI, card Card) {
pcard := s.trick[1]
fullOne := pcard.equals(Card{s.follow, "10"}) || pcard.equals(Card{s.follow, "A"})
// debugTacticsLog("%v\n", s.trick)
if fullOne && s.greater(s.trick[0], pcard) && s.greater(s.trick[0], card) {
for _, c := range makeTrumpDeck(s.trump) {
if s.greater(c, s.trick[0]) {
s.opp2VoidCards = append(s.opp2VoidCards, c)
}
}
debugTacticsLog("INFERENCE: **************************************\n")
debugTacticsLog("INFERENCE: Losing a SMEARED TRICK: opp2 void Cards %v \n", s.opp2VoidCards)
debugTacticsLog("INFERENCE: **************************************\n")
}
}
func winingASmearedTrick(s *SuitState, p PlayerI, card Card) {
pcard := s.trick[1]
fullOne := pcard.equals(Card{s.follow, "10"}) || pcard.equals(Card{s.follow, "A"})
// debugTacticsLog("%v\n", s.trick)
if fullOne && s.greater(s.trick[0], pcard) && s.greater(card, s.trick[0]) {
for _, c := range makeTrumpDeck(s.trump) {
if s.greater(c, s.trick[0]) && s.greater(card, c) {
s.opp2VoidCards = append(s.opp2VoidCards, c)
}
}
debugTacticsLog("INFERENCE: **************************************\n")
debugTacticsLog("INFERENCE: Wining a SMEARED TRICK with more than enough: opp2 void Cards %v \n", s.opp2VoidCards)
debugTacticsLog("INFERENCE: **************************************\n")
}
}
func opponent_DoesNot_Play_a_FullTRUMP_on_a_Winned_TRICK_DOES_NOT_HAVE_A10(s *SuitState, p PlayerI, card Card) {
fullOne := card.equals(Card{s.follow, "10"}) || card.equals(Card{s.follow, "A"})
// debugTacticsLog("%v\n", s.trick)
if !fullOne && s.greater(s.trick[1], s.trick[0]) {
s.opp2VoidCards = append(s.opp2VoidCards, Card{s.follow, "10"}, Card{s.follow, "A"})
debugTacticsLog("INFERENCE: **************************************\n")
debugTacticsLog("INFERENCE: Not SMEARING A/10: opp2 void Cards %v \n", s.opp2VoidCards)
debugTacticsLog("INFERENCE: **************************************\n")
}
}
func analysePlay(s *SuitState, p PlayerI, card Card) {
opponentDoesNotHaveAnyLowerCards(s, p, card)
notFollowingSuit(s, p, card)
playingAFullTrumpOnALosingTrick(s, p, card)
playingAFullTrumpOnALosingTrickDecl(s, p, card)
if len(s.trick) == 2 && s.follow == s.trump && p.getName() == s.opp2.getName() {
losingASmearedTrick(s, p, card)
winingASmearedTrick(s, p, card)
opponent_DoesNot_Play_a_FullTRUMP_on_a_Winned_TRICK_DOES_NOT_HAVE_A10(s, p, card)
}
if len(s.trick) == 2 {
condition1 := s.trick[0].Rank == "A" && s.greater(s.trick[0], s.trick[1]) && players[0].getName() == partner(s, p).getName()
condition2 := s.trick[1].equals(Card{s.follow, "A"}) && s.greater(s.trick[1], s.trick[0]) && players[1].getName() == partner(s, p).getName()
if condition1 || condition2 {
if getSuit(s.trump, card) == s.follow && cardValue(card) < 10 {
voidCards := []Card{}
voidCards = append(voidCards, Card{s.follow, "10"})
if cardValue(card) < 4 {
voidCards = append(voidCards, Card{s.follow, "K"})
}
if cardValue(card) < 3 {
voidCards = append(voidCards, Card{s.follow, "D"})
}
debugTacticsLog("INFERENCE: **************************************\n")
debugTacticsLog("INFERENCE: Player does not have the cards %v \n", voidCards)
debugTacticsLog("INFERENCE: **************************************\n")
if p.getName() == s.opp1.getName() {
s.opp1VoidCards = append(s.opp1VoidCards, voidCards...)
}
if p.getName() == s.opp2.getName() {
s.opp2VoidCards = append(s.opp2VoidCards, voidCards...)
}
}
}
// TODO
// only opp1 can know that if he does not have the 10
// inference from a point of view of the player is needed.
// TODO
// test: TestInference_Declarer_A10_at_Declarer DISABLED
condition0 := getSuit(s.trump, s.trick[0]) != s.trump
condition1 = s.trick[0].Rank == "K" && getSuit(s.trump, s.trick[1]) == s.follow && players[0].getName() == s.opp1.getName()
condition2 = getSuit(s.trump, card) == s.follow
condition3 := s.greater(s.trick[0], s.trick[1]) && s.greater(s.trick[0], card)
if condition0 && condition1 && condition2 && condition3 {
debugTacticsLog("INFERENCE: **************************************\n")
debugTacticsLog("INFERENCE: Partner and Declarer go under \n")
debugTacticsLog("INFERENCE: Partner has 10 and declarer A \n")
debugTacticsLog("INFERENCE: **************************************\n")
// s.opp1VoidCards = append(s.opp1VoidCards, Card{s.follow, "A"}) // This OR
// s.opp2VoidCards = append(s.opp2VoidCards, Card{s.follow, "A"}) // this
s.declarerVoidCards = append(s.declarerVoidCards, Card{s.follow, "10"})
}
// if !card.equals(Card{s.follow, "10"}) {
// debugTacticsLog("INFERENCE: **************************************\n")
// debugTacticsLog("INFERENCE: Player does not have the %v \n", Card{s.follow, "10"})
// debugTacticsLog("INFERENCE: **************************************\n")
// if p.getName() == s.opp1.getName() {
// s.opp1VoidCards = append(s.opp1VoidCards, Card{s.follow, "10"})
// }
// if p.getName() == s.opp2.getName() {
// s.opp2VoidCards = append(s.opp2VoidCards, Card{s.follow, "10"})
// }
// }
condition1 = s.greater(s.trick[0], s.trick[1]) && players[0].getName() == s.declarer.getName()
condition2 = s.greater(s.trick[1], s.trick[0]) && players[1].getName() == s.declarer.getName()
if condition1 || condition2 {
if !s.greater(card, s.trick...) && cardValue(card) > 0 && card.Rank != "J" {
debugTacticsLog("INFERENCE: **************************************\n")
debugTacticsLog("INFERENCE: Player does not have lower than %v \n", card)
debugTacticsLog("INFERENCE: **************************************\n")
i := 0
r := ""
for i, r = range ranks {
if card.Rank == r {
break
}
}
for j := i + 1; j < len(ranks); j++ {
c := Card{card.Suit, ranks[j]}
if !in(s.cardsPlayed, c) {
if p.getName() == s.opp1.getName() {
s.opp1VoidCards = append(s.opp1VoidCards, c)
}
if p.getName() == s.opp2.getName() {
s.opp2VoidCards = append(s.opp2VoidCards, c)
}
}
}
}
}
}
}
func opponentIsLosingTrick(s *SuitState, p PlayerI, card Card, lookSkat bool) bool {
dIndex := 0
for dIndex = range players {
if players[dIndex].getName() == s.declarer.getName() {
break
}
}
for i, c := range s.trick {
if s.greater(c, card) && players[i].getName() == s.declarer.getName() {
if len(s.trick) == 1 && noHigherCard(s, false, p.getHand(), c) {
return true
}
if len(s.trick) == 2 {
other := 0
if dIndex == 0 {
other = 1
}
if s.greater(s.trick[dIndex], s.trick[other]) {
return true
}
}
}
}
return false
}