Skip to content

Commit 75e33d7

Browse files
committed
Small code refactorings in smart player
1 parent f085446 commit 75e33d7

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,17 @@ public override void EndTurn(PlayerTurnContext context)
6969
// TODO: Improve close game decision
7070
private bool CloseGame(PlayerTurnContext context)
7171
{
72-
var shouldCloseGame = this.PlayerActionValidator.IsValid(PlayerAction.CloseGame(), context, this.Cards)
73-
&& this.Cards.Count(x => x.Suit == context.TrumpCard.Suit) == 5;
72+
if (!this.PlayerActionValidator.IsValid(PlayerAction.CloseGame(), context, this.Cards))
73+
{
74+
return false;
75+
}
76+
77+
if (this.Cards.Count(x => x.Suit == context.TrumpCard.Suit) == 5)
78+
{
79+
return true;
80+
}
7481

75-
return shouldCloseGame;
82+
return false;
7683
}
7784

7885
// TODO: Improve choosing best card to play
@@ -103,7 +110,7 @@ private PlayerAction ChooseCardWhenPlayingFirstAndRulesDoNotApply(
103110
}
104111

105112
// If the player is close to the win => play trump card which will surely win the trick
106-
var cardToWinTheGame = this.GetTrumpCardWhichWillSurelyWinTheGame(context.TrumpCard, context.FirstPlayerRoundPoints, possibleCardsToPlay);
113+
var cardToWinTheGame = this.GetTrumpCardWhichWillSurelyWinTheGame(context.TrumpCard.Suit, context.FirstPlayerRoundPoints, possibleCardsToPlay);
107114
if (cardToWinTheGame != null)
108115
{
109116
return this.PlayCard(cardToWinTheGame);
@@ -134,7 +141,6 @@ private PlayerAction ChooseCardWhenPlayingFirstAndRulesApply(
134141

135142
var trumpCard = this.GetCardWhichWillSurelyWinTheTrick(
136143
context.TrumpCard.Suit,
137-
context.CardsLeftInDeck == 0 ? null : context.TrumpCard,
138144
opponentHasTrump);
139145
if (trumpCard != null)
140146
{
@@ -145,7 +151,6 @@ private PlayerAction ChooseCardWhenPlayingFirstAndRulesApply(
145151
{
146152
var possibleCard = this.GetCardWhichWillSurelyWinTheTrick(
147153
suit,
148-
context.CardsLeftInDeck == 0 ? null : context.TrumpCard,
149154
opponentHasTrump);
150155
if (possibleCard != null)
151156
{
@@ -270,16 +275,16 @@ private PlayerAction ChooseCardWhenPlayingSecondAndRulesApply(
270275
}
271276

272277
private Card GetTrumpCardWhichWillSurelyWinTheGame(
273-
Card trumpCard,
278+
CardSuit trumpSuit,
274279
int playerRoundPoints,
275280
ICollection<Card> possibleCardsToPlay)
276281
{
277282
var opponentBiggestTrumpCard =
278-
this.cardTracker.UnknownCards.Where(x => x.Suit == trumpCard.Suit)
283+
this.cardTracker.UnknownCards.Where(x => x.Suit == trumpSuit)
279284
.OrderByDescending(x => x.GetValue())
280285
.FirstOrDefault();
281286
var myBiggestTrumpCards =
282-
possibleCardsToPlay.Where(x => x.Suit == trumpCard.Suit).OrderByDescending(x => x.GetValue());
287+
possibleCardsToPlay.Where(x => x.Suit == trumpSuit).OrderByDescending(x => x.GetValue());
283288

284289
var sumOfPoints = 0;
285290
foreach (var myTrumpCard in myBiggestTrumpCards)
@@ -298,7 +303,7 @@ private Card GetTrumpCardWhichWillSurelyWinTheGame(
298303
return null;
299304
}
300305

301-
private Card GetCardWhichWillSurelyWinTheTrick(CardSuit suit, Card trumpCard, bool opponentHasTrump)
306+
private Card GetCardWhichWillSurelyWinTheTrick(CardSuit suit, bool opponentHasTrump)
302307
{
303308
var myBiggestCard =
304309
this.Cards.Where(x => x.Suit == suit).OrderByDescending(x => x.GetValue()).FirstOrDefault();

0 commit comments

Comments
 (0)