Skip to content

Commit 8e0ae52

Browse files
committed
SmartPlayerOld aligned with the current version of the AI. Performance improvement.
1 parent 75e33d7 commit 8e0ae52

File tree

2 files changed

+47
-38
lines changed

2 files changed

+47
-38
lines changed

Source/AI/Santase.AI.SmartPlayer/SmartPlayerOld.cs

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
// Overall strategy can be based on the game score. When opponent is close to the winning the player should be riskier.
1313
internal class SmartPlayerOld : BasePlayer
1414
{
15-
private readonly ICollection<Card> playedCards = new List<Card>();
16-
17-
private readonly OpponentSuitCardsProvider opponentSuitCardsProvider = new OpponentSuitCardsProvider();
15+
private readonly CardTracker cardTracker = new CardTracker();
1816

1917
public override string Name => "Smart Player Old";
2018

@@ -23,50 +21,74 @@ public override PlayerAction GetTurn(PlayerTurnContext context)
2321
// When possible change the trump card as this is almost always a good move
2422
// Changing trump can be non-optimal when:
2523
// 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
2725
// 3. Want to confuse the opponent
2826
if (this.PlayerActionValidator.IsValid(PlayerAction.ChangeTrump(), context, this.Cards))
2927
{
28+
this.cardTracker.ChangeTrumpCard(context.TrumpCard);
3029
return this.ChangeTrump(context.TrumpCard);
3130
}
3231

3332
if (this.CloseGame(context))
3433
{
34+
GlobalStats.GamesClosedByPlayer++;
3535
return this.CloseGame();
3636
}
3737

3838
return this.ChooseCard(context);
3939
}
4040

41-
public override void EndRound()
41+
public override void StartRound(ICollection<Card> cards, Card trumpCard, int myTotalPoints, int opponentTotalPoints)
4242
{
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);
4556
}
4657

4758
public override void EndTurn(PlayerTurnContext context)
4859
{
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);
5167
}
5268

5369
// TODO: Improve close game decision
5470
private bool CloseGame(PlayerTurnContext context)
5571
{
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))
5973
{
60-
GlobalStats.GamesClosedByPlayer++;
74+
return false;
75+
}
76+
77+
if (this.Cards.Count(x => x.Suit == context.TrumpCard.Suit) == 5)
78+
{
79+
return true;
6180
}
6281

63-
return shouldCloseGame;
82+
return false;
6483
}
6584

6685
// TODO: Improve choosing best card to play
6786
private PlayerAction ChooseCard(PlayerTurnContext context)
6887
{
88+
this.cardTracker.TrumpCardSaw(context.TrumpCard);
89+
6990
var possibleCardsToPlay = this.PlayerActionValidator.GetPossibleCardsToPlay(context, this.Cards);
91+
7092
return context.State.ShouldObserveRules
7193
? (context.IsFirstPlayerTurn
7294
? this.ChooseCardWhenPlayingFirstAndRulesApply(context, possibleCardsToPlay)
@@ -88,7 +110,7 @@ private PlayerAction ChooseCardWhenPlayingFirstAndRulesDoNotApply(
88110
}
89111

90112
// 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);
92114
if (cardToWinTheGame != null)
93115
{
94116
return this.PlayCard(cardToWinTheGame);
@@ -97,13 +119,7 @@ private PlayerAction ChooseCardWhenPlayingFirstAndRulesDoNotApply(
97119
// Smallest non-trump card from the shortest opponent suit
98120
var cardToPlay =
99121
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))
107123
.ThenBy(x => x.GetValue())
108124
.FirstOrDefault();
109125
if (cardToPlay != null)
@@ -121,16 +137,10 @@ private PlayerAction ChooseCardWhenPlayingFirstAndRulesApply(
121137
ICollection<Card> possibleCardsToPlay)
122138
{
123139
// 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);
130141

131142
var trumpCard = this.GetCardWhichWillSurelyWinTheTrick(
132143
context.TrumpCard.Suit,
133-
context.CardsLeftInDeck == 0 ? null : context.TrumpCard,
134144
opponentHasTrump);
135145
if (trumpCard != null)
136146
{
@@ -141,7 +151,6 @@ private PlayerAction ChooseCardWhenPlayingFirstAndRulesApply(
141151
{
142152
var possibleCard = this.GetCardWhichWillSurelyWinTheTrick(
143153
suit,
144-
context.CardsLeftInDeck == 0 ? null : context.TrumpCard,
145154
opponentHasTrump);
146155
if (possibleCard != null)
147156
{
@@ -208,13 +217,13 @@ private PlayerAction ChooseCardWhenPlayingSecondAndRulesDoNotApply(
208217
}
209218

210219
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)))
212221
{
213222
return this.PlayCard(new Card(context.TrumpCard.Suit, CardType.Queen));
214223
}
215224

216225
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)))
218227
{
219228
return this.PlayCard(new Card(context.TrumpCard.Suit, CardType.King));
220229
}
@@ -266,16 +275,16 @@ private PlayerAction ChooseCardWhenPlayingSecondAndRulesApply(
266275
}
267276

268277
private Card GetTrumpCardWhichWillSurelyWinTheGame(
269-
Card trumpCard,
278+
CardSuit trumpSuit,
270279
int playerRoundPoints,
271280
ICollection<Card> possibleCardsToPlay)
272281
{
273282
var opponentBiggestTrumpCard =
274-
this.opponentSuitCardsProvider.GetOpponentCards(this.Cards, this.playedCards, trumpCard, trumpCard.Suit)
283+
this.cardTracker.UnknownCards.Where(x => x.Suit == trumpSuit)
275284
.OrderByDescending(x => x.GetValue())
276285
.FirstOrDefault();
277286
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());
279288

280289
var sumOfPoints = 0;
281290
foreach (var myTrumpCard in myBiggestTrumpCards)
@@ -294,7 +303,7 @@ private Card GetTrumpCardWhichWillSurelyWinTheGame(
294303
return null;
295304
}
296305

297-
private Card GetCardWhichWillSurelyWinTheTrick(CardSuit suit, Card trumpCard, bool opponentHasTrump)
306+
private Card GetCardWhichWillSurelyWinTheTrick(CardSuit suit, bool opponentHasTrump)
298307
{
299308
var myBiggestCard =
300309
this.Cards.Where(x => x.Suit == suit).OrderByDescending(x => x.GetValue()).FirstOrDefault();
@@ -304,7 +313,7 @@ private Card GetCardWhichWillSurelyWinTheTrick(CardSuit suit, Card trumpCard, bo
304313
}
305314

306315
var opponentBiggestCard =
307-
this.opponentSuitCardsProvider.GetOpponentCards(this.Cards, this.playedCards, trumpCard, suit)
316+
this.cardTracker.UnknownCards.Where(x => x.Suit == suit)
308317
.OrderByDescending(x => x.GetValue())
309318
.FirstOrDefault();
310319

Source/Santase.Logic/GameMechanics/RoundPlayerInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public RoundPlayerInfo(IPlayer player)
1111
{
1212
this.Player = player;
1313
this.Cards = new CardCollection();
14-
this.TrickCards = new List<Card>();
14+
this.TrickCards = new CardCollection();
1515
this.Announces = new List<Announce>();
1616
this.GameCloser = false;
1717
}

0 commit comments

Comments
 (0)