12
12
// Overall strategy can be based on the game score. When opponent is close to the winning the player should be riskier.
13
13
internal class SmartPlayerOld : BasePlayer
14
14
{
15
- private readonly ICollection < Card > playedCards = new List < Card > ( ) ;
16
-
17
- private readonly OpponentSuitCardsProvider opponentSuitCardsProvider = new OpponentSuitCardsProvider ( ) ;
15
+ private readonly CardTracker cardTracker = new CardTracker ( ) ;
18
16
19
17
public override string Name => "Smart Player Old" ;
20
18
@@ -23,50 +21,74 @@ public override PlayerAction GetTurn(PlayerTurnContext context)
23
21
// When possible change the trump card as this is almost always a good move
24
22
// Changing trump can be non-optimal when:
25
23
// 1. Current player is planning to close the game and don't want to give additional points to his opponent
26
- // 2. The player will close the game and you will give him additional points by giving him bigger trump card instead of 9
24
+ // 2. The other player will close the game and you will give him additional points by giving him bigger trump card instead of 9
27
25
// 3. Want to confuse the opponent
28
26
if ( this . PlayerActionValidator . IsValid ( PlayerAction . ChangeTrump ( ) , context , this . Cards ) )
29
27
{
28
+ this . cardTracker . ChangeTrumpCard ( context . TrumpCard ) ;
30
29
return this . ChangeTrump ( context . TrumpCard ) ;
31
30
}
32
31
33
32
if ( this . CloseGame ( context ) )
34
33
{
34
+ GlobalStats . GamesClosedByPlayer ++ ;
35
35
return this . CloseGame ( ) ;
36
36
}
37
37
38
38
return this . ChooseCard ( context ) ;
39
39
}
40
40
41
- public override void EndRound ( )
41
+ public override void StartRound ( ICollection < Card > cards , Card trumpCard , int myTotalPoints , int opponentTotalPoints )
42
42
{
43
- this . playedCards . Clear ( ) ;
44
- base . EndRound ( ) ;
43
+ base . StartRound ( cards , trumpCard , myTotalPoints , opponentTotalPoints ) ;
44
+
45
+ this . cardTracker . Clear ( ) ;
46
+ foreach ( var card in cards )
47
+ {
48
+ this . cardTracker . UnknownCards . Remove ( card ) ;
49
+ }
50
+ }
51
+
52
+ public override void AddCard ( Card card )
53
+ {
54
+ base . AddCard ( card ) ;
55
+ this . cardTracker . UnknownCards . Remove ( card ) ;
45
56
}
46
57
47
58
public override void EndTurn ( PlayerTurnContext context )
48
59
{
49
- this . playedCards . Add ( context . FirstPlayedCard ) ;
50
- this . playedCards . Add ( context . SecondPlayedCard ) ;
60
+ if ( context . CardsLeftInDeck == 2 )
61
+ {
62
+ this . cardTracker . UnknownCards . Add ( context . TrumpCard ) ;
63
+ }
64
+
65
+ this . cardTracker . CardPlayed ( context . FirstPlayedCard ) ;
66
+ this . cardTracker . CardPlayed ( context . SecondPlayedCard ) ;
51
67
}
52
68
53
69
// TODO: Improve close game decision
54
70
private bool CloseGame ( PlayerTurnContext context )
55
71
{
56
- var shouldCloseGame = this . PlayerActionValidator . IsValid ( PlayerAction . CloseGame ( ) , context , this . Cards )
57
- && this . Cards . Count ( x => x . Suit == context . TrumpCard . Suit ) == 5 ;
58
- if ( shouldCloseGame )
72
+ if ( ! this . PlayerActionValidator . IsValid ( PlayerAction . CloseGame ( ) , context , this . Cards ) )
59
73
{
60
- GlobalStats . GamesClosedByPlayer ++ ;
74
+ return false ;
75
+ }
76
+
77
+ if ( this . Cards . Count ( x => x . Suit == context . TrumpCard . Suit ) == 5 )
78
+ {
79
+ return true ;
61
80
}
62
81
63
- return shouldCloseGame ;
82
+ return false ;
64
83
}
65
84
66
85
// TODO: Improve choosing best card to play
67
86
private PlayerAction ChooseCard ( PlayerTurnContext context )
68
87
{
88
+ this . cardTracker . TrumpCardSaw ( context . TrumpCard ) ;
89
+
69
90
var possibleCardsToPlay = this . PlayerActionValidator . GetPossibleCardsToPlay ( context , this . Cards ) ;
91
+
70
92
return context . State . ShouldObserveRules
71
93
? ( context . IsFirstPlayerTurn
72
94
? this . ChooseCardWhenPlayingFirstAndRulesApply ( context , possibleCardsToPlay )
@@ -88,7 +110,7 @@ private PlayerAction ChooseCardWhenPlayingFirstAndRulesDoNotApply(
88
110
}
89
111
90
112
// If the player is close to the win => play trump card which will surely win the trick
91
- var cardToWinTheGame = this . GetTrumpCardWhichWillSurelyWinTheGame ( context . TrumpCard , context . FirstPlayerRoundPoints , possibleCardsToPlay ) ;
113
+ var cardToWinTheGame = this . GetTrumpCardWhichWillSurelyWinTheGame ( context . TrumpCard . Suit , context . FirstPlayerRoundPoints , possibleCardsToPlay ) ;
92
114
if ( cardToWinTheGame != null )
93
115
{
94
116
return this . PlayCard ( cardToWinTheGame ) ;
@@ -97,13 +119,7 @@ private PlayerAction ChooseCardWhenPlayingFirstAndRulesDoNotApply(
97
119
// Smallest non-trump card from the shortest opponent suit
98
120
var cardToPlay =
99
121
possibleCardsToPlay . Where ( x => x . Suit != context . TrumpCard . Suit )
100
- . OrderBy (
101
- x =>
102
- this . opponentSuitCardsProvider . GetOpponentCards (
103
- this . Cards ,
104
- this . playedCards ,
105
- context . TrumpCard ,
106
- x . Suit ) . Count )
122
+ . OrderBy ( x => this . cardTracker . UnknownCards . Count ( y => y . Suit == x . Suit ) )
107
123
. ThenBy ( x => x . GetValue ( ) )
108
124
. FirstOrDefault ( ) ;
109
125
if ( cardToPlay != null )
@@ -121,16 +137,10 @@ private PlayerAction ChooseCardWhenPlayingFirstAndRulesApply(
121
137
ICollection < Card > possibleCardsToPlay )
122
138
{
123
139
// Find card that will surely win the trick
124
- var opponentHasTrump =
125
- this . opponentSuitCardsProvider . GetOpponentCards (
126
- this . Cards ,
127
- this . playedCards ,
128
- context . CardsLeftInDeck == 0 ? null : context . TrumpCard ,
129
- context . TrumpCard . Suit ) . Any ( ) ;
140
+ var opponentHasTrump = this . cardTracker . UnknownCards . Any ( x => x . Suit == context . TrumpCard . Suit ) ;
130
141
131
142
var trumpCard = this . GetCardWhichWillSurelyWinTheTrick (
132
143
context . TrumpCard . Suit ,
133
- context . CardsLeftInDeck == 0 ? null : context . TrumpCard ,
134
144
opponentHasTrump ) ;
135
145
if ( trumpCard != null )
136
146
{
@@ -141,7 +151,6 @@ private PlayerAction ChooseCardWhenPlayingFirstAndRulesApply(
141
151
{
142
152
var possibleCard = this . GetCardWhichWillSurelyWinTheTrick (
143
153
suit ,
144
- context . CardsLeftInDeck == 0 ? null : context . TrumpCard ,
145
154
opponentHasTrump ) ;
146
155
if ( possibleCard != null )
147
156
{
@@ -208,13 +217,13 @@ private PlayerAction ChooseCardWhenPlayingSecondAndRulesDoNotApply(
208
217
}
209
218
210
219
if ( possibleCardsToPlay . Contains ( new Card ( context . TrumpCard . Suit , CardType . Queen ) )
211
- && this . playedCards . Contains ( new Card ( context . TrumpCard . Suit , CardType . King ) ) )
220
+ && this . cardTracker . PlayedCards . Contains ( new Card ( context . TrumpCard . Suit , CardType . King ) ) )
212
221
{
213
222
return this . PlayCard ( new Card ( context . TrumpCard . Suit , CardType . Queen ) ) ;
214
223
}
215
224
216
225
if ( possibleCardsToPlay . Contains ( new Card ( context . TrumpCard . Suit , CardType . King ) )
217
- && this . playedCards . Contains ( new Card ( context . TrumpCard . Suit , CardType . Queen ) ) )
226
+ && this . cardTracker . PlayedCards . Contains ( new Card ( context . TrumpCard . Suit , CardType . Queen ) ) )
218
227
{
219
228
return this . PlayCard ( new Card ( context . TrumpCard . Suit , CardType . King ) ) ;
220
229
}
@@ -266,16 +275,16 @@ private PlayerAction ChooseCardWhenPlayingSecondAndRulesApply(
266
275
}
267
276
268
277
private Card GetTrumpCardWhichWillSurelyWinTheGame (
269
- Card trumpCard ,
278
+ CardSuit trumpSuit ,
270
279
int playerRoundPoints ,
271
280
ICollection < Card > possibleCardsToPlay )
272
281
{
273
282
var opponentBiggestTrumpCard =
274
- this . opponentSuitCardsProvider . GetOpponentCards ( this . Cards , this . playedCards , trumpCard , trumpCard . Suit )
283
+ this . cardTracker . UnknownCards . Where ( x => x . Suit == trumpSuit )
275
284
. OrderByDescending ( x => x . GetValue ( ) )
276
285
. FirstOrDefault ( ) ;
277
286
var myBiggestTrumpCards =
278
- possibleCardsToPlay . Where ( x => x . Suit == trumpCard . Suit ) . OrderByDescending ( x => x . GetValue ( ) ) ;
287
+ possibleCardsToPlay . Where ( x => x . Suit == trumpSuit ) . OrderByDescending ( x => x . GetValue ( ) ) ;
279
288
280
289
var sumOfPoints = 0 ;
281
290
foreach ( var myTrumpCard in myBiggestTrumpCards )
@@ -294,7 +303,7 @@ private Card GetTrumpCardWhichWillSurelyWinTheGame(
294
303
return null ;
295
304
}
296
305
297
- private Card GetCardWhichWillSurelyWinTheTrick ( CardSuit suit , Card trumpCard , bool opponentHasTrump )
306
+ private Card GetCardWhichWillSurelyWinTheTrick ( CardSuit suit , bool opponentHasTrump )
298
307
{
299
308
var myBiggestCard =
300
309
this . Cards . Where ( x => x . Suit == suit ) . OrderByDescending ( x => x . GetValue ( ) ) . FirstOrDefault ( ) ;
@@ -304,7 +313,7 @@ private Card GetCardWhichWillSurelyWinTheTrick(CardSuit suit, Card trumpCard, bo
304
313
}
305
314
306
315
var opponentBiggestCard =
307
- this . opponentSuitCardsProvider . GetOpponentCards ( this . Cards , this . playedCards , trumpCard , suit )
316
+ this . cardTracker . UnknownCards . Where ( x => x . Suit == suit )
308
317
. OrderByDescending ( x => x . GetValue ( ) )
309
318
. FirstOrDefault ( ) ;
310
319
0 commit comments