Fixed some bugs and added new features that came up while testing:
All checks were successful
Build Mau & Deploy Mau / build (push) Successful in 1m29s
Build Mau & Deploy Mau / deploy (push) Has been skipped

- Check for allowed card type was before the check for the mau cards, which caused incorrect cards to be played and mau cards to be passed to wrong players
- Ending with a special card is now a faulty move, which grants the player 5 fault cards now
- Grabbed cards could be registered as playable as it didnt include next allowed card type
- If the first card is a joker, set the next allowed card type to a random other card type to prevent an instant softlock
- Other cleanup :)
This commit is contained in:
2024-06-12 15:57:09 +02:00
parent 2ef1de9d64
commit 0918b2b3cc
3 changed files with 126 additions and 78 deletions

View File

@@ -9,19 +9,38 @@ namespace MauMau_Server.Mau;
public class Game : RoomType
{
// Helpers
private readonly Deck _deck = new();
private readonly TurnManager _turnManager = new();
public readonly List<Card> MauCardBuffer = new();
public CardType? NextAllowedCardType { get; set; }
// Game state
private readonly List<Card> _mauCardBuffer = new();
private CardType? NextAllowedCardType { get; set; }
// Variables
private const int NumberOfFaultcards = 5;
private const int NumberOfStartCards = 8;
public Game(Room.Room room, IEnumerable<ConnectionInstance> connections) : base(room)
{
// If the current card is a joker, set the next allowed card type to a random card type
if (_deck.CurrentCard.CardType == CardType.JOKER)
{
// In the case the random card type is a joker, try again until it is not
do
{
var cardTypes = Enum.GetValues(typeof(CardType));
var randomIndex = new Random().Next(cardTypes.Length);
NextAllowedCardType = (CardType?)cardTypes.GetValue(randomIndex) ?? CardType.SPADES;
} while (NextAllowedCardType == CardType.JOKER);
}
// Convert all the connections to players
List<Player> players = new();
foreach (var player in connections.Select(connection => new Player(connection)))
{
// Give the new player a hand of cards
var initialHand = _deck.DrawCards(8);
var initialHand = _deck.DrawCards(NumberOfStartCards);
player.GiveCards(initialHand);
players.Add(player);
}
@@ -155,7 +174,7 @@ public class Game : RoomType
{
// If there are cards in the MauCardBuffer, this means there are multiple cards that need to be drawn
// Otherwise, just draw a single card
if (MauCardBuffer.Count > 0)
if (_mauCardBuffer.Count > 0)
{
// Count the amount of cards that need to be drawn
var totalCards = CountMauCardBuffer();
@@ -178,7 +197,7 @@ public class Game : RoomType
player.GiveCard(drawnCard);
// Change the player if the drawn card cannot be played
if (!drawnCard.CanBePlayedOn(_deck.CurrentCard))
if (!CardCanBePlayed(drawnCard))
{
_turnManager.ChangeTurnTo();
}
@@ -217,43 +236,26 @@ public class Game : RoomType
{
return;
}
// Check if there is a specific card type that is allowed to be played
if (NextAllowedCardType != null)
{
// If the card is not the allowed card type, not the same value or a joker, ignore the message
if (playerCard.CardType != NextAllowedCardType && playerCard.CardType != CardType.JOKER && playerCard.CardValue != _deck.CurrentCard.CardValue)
{
return;
}
// Reset the allowed card type, so the next player has the normal same type and same value rules
NextAllowedCardType = null;
} else if (MauCardBuffer.Count > 0)
{
// If there are queued mau cards, the player can only another mau card (or draw)
if (playerCard.CardType != CardType.JOKER && playerCard.CardValue != CardValue.TWO)
{
return;
}
}
else
{
// Check if the card can be played on the current card, if not, ignore the message
if (!playerCard.CanBePlayedOn(_deck.CurrentCard))
{
return;
}
}
if (!CardCanBePlayed(playerCard)) return;
// Remove the card from the player's hand
player.Hand.Remove(playerCard);
// If the player's last played card is a special card, give the player 5 fault cards
if (player.Hand.Count < 1 && playerCard.IsSpecialCard())
{
var faultCards = _deck.DrawCards(NumberOfFaultcards);
player.GiveCards(faultCards);
}
// Add the card to the used deck
_deck.AddCardToUsedDeck(playerCard);
// Reset the allowed card type, so the next player has the normal same type and same value rules
NextAllowedCardType = null;
// Set the new current card
_deck.CurrentCard = playerCard;
// If the player has no cards left, end the game with player as winner
if (player.Hand.Count == 0)
{
EndGame(player);
@@ -280,13 +282,13 @@ public class Game : RoomType
case CardValue.RED:
case CardValue.BLACK:
{
MauCardBuffer.Add(card);
_mauCardBuffer.Add(card);
_turnManager.CurrentPlayer.State = PlayerState.CHOOSE;
break;
}
case CardValue.TWO:
{
MauCardBuffer.Add(card);
_mauCardBuffer.Add(card);
_turnManager.ChangeTurnTo();
break;
}
@@ -319,6 +321,37 @@ public class Game : RoomType
}
}
/**
* <summary>
* Check if the card can be played with the current game state.
* <list type="bullet">
* <item>The player can only play a mau card if there are pending mau cards (or draw)</item>
* <item>If there is a next allowed card type, the player can only play that card type, a joker or a card with the same value as the current card</item>
* <item>Otherwise, the player can play a card that has the same type, same value or is a joker</item>
* </list>
* </summary>
* <returns>True if the given card could be played</returns>
*/
private bool CardCanBePlayed(Card card)
{
// Check if there are pending mau cards played
if (_mauCardBuffer.Count > 0)
{
// If so, the card must be a mau card
return card.IsMauCard();
}
// Check if there is a next allowed card type
if (NextAllowedCardType != null)
{
// If so, the card must be the allowed card type, a joker or the same value as the current card
return card.CardType == NextAllowedCardType || card.CardType == CardType.JOKER || card.CardValue == _deck.CurrentCard.CardValue;
}
// Otherwise, use the normal rules
return card.CanBePlayedOn(_deck.CurrentCard);
}
/**
* <summary>
* Count the amount of cards that need to be drawn from the MauCardBuffer. This method also clears the buffer.
@@ -328,7 +361,7 @@ public class Game : RoomType
private int CountMauCardBuffer()
{
var totalCards = 0;
foreach (var card in MauCardBuffer)
foreach (var card in _mauCardBuffer)
{
if (card.CardType == CardType.JOKER)
{
@@ -342,7 +375,7 @@ public class Game : RoomType
continue;
}
}
MauCardBuffer.Clear();
_mauCardBuffer.Clear();
return totalCards;
}