From 56034b921f16cfdc0d6e592e6ce65ac51a977e99 Mon Sep 17 00:00:00 2001 From: DTieman Date: Fri, 19 Apr 2024 22:10:21 +0200 Subject: [PATCH] Added the ability to choose your cardtype when playing a jack or the newly added joker --- Mau/Card.cs | 7 +++++-- Mau/Deck.cs | 7 +++++++ Mau/Game.cs | 35 +++++++++++++++++++++++++++++++++-- Mau/GameState.cs | 4 +++- Mau/Player.cs | 4 ++-- Mau/PlayerState.cs | 8 ++++++++ 6 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 Mau/PlayerState.cs diff --git a/Mau/Card.cs b/Mau/Card.cs index 7e5ad7e..b9b48e4 100644 --- a/Mau/Card.cs +++ b/Mau/Card.cs @@ -30,7 +30,8 @@ public enum CardType SPADES, HEARTS, DIAMONDS, - CLUBS + CLUBS, + JOKER } public enum CardValue @@ -47,5 +48,7 @@ public enum CardValue JACK, QUEEN, KING, - ACE + ACE, + RED, + BLACK } \ No newline at end of file diff --git a/Mau/Deck.cs b/Mau/Deck.cs index b2b6ff5..9a74f8d 100644 --- a/Mau/Deck.cs +++ b/Mau/Deck.cs @@ -9,8 +9,15 @@ public class Deck { foreach (CardType cardType in Enum.GetValues(typeof(CardType))) { + if (cardType == CardType.JOKER) + { + UnusedDeck.Add(new Card(cardType, CardValue.RED)); + UnusedDeck.Add(new Card(cardType, CardValue.BLACK)); + continue; + } foreach (CardValue cardValue in Enum.GetValues(typeof(CardValue))) { + if (cardValue is CardValue.RED or CardValue.BLACK) continue; UnusedDeck.Add(new Card(cardType, cardValue)); } } diff --git a/Mau/Game.cs b/Mau/Game.cs index 4477b5f..2e0b95a 100644 --- a/Mau/Game.cs +++ b/Mau/Game.cs @@ -46,6 +46,17 @@ public class Game PlayCard(player, card); break; } + case "CHOOSE": + var choice = action.Data; + if (!Enum.TryParse(choice, out CardType cardType)) + { + break; + } + CurrentCard = new Card(cardType, CardValue.JACK); + CurrentPlayer.State = PlayerState.WAIT; + CurrentPlayer = CurrentCard.CardType == CardType.JOKER ? GetNextPlayer() : GetNextPlayer(2); + CurrentPlayer.State = PlayerState.TURN; + break; case "DRAW": DrawCard(player); break; @@ -66,6 +77,18 @@ public class Game { switch (card.CardValue) { + case CardValue.RED: + case CardValue.BLACK: + { + var nextPlayer = GetNextPlayer(); + var cardsToDraw = Deck.DrawCards(5); + foreach (var drawnCard in cardsToDraw) + { + nextPlayer.Hand.Add(drawnCard); + } + CurrentPlayer.State = PlayerState.CHOOSE; + break; + } case CardValue.TWO: { var nextPlayer = GetNextPlayer(); @@ -74,14 +97,18 @@ public class Game { nextPlayer.Hand.Add(drawnCard); } + CurrentPlayer.State = PlayerState.WAIT; CurrentPlayer = GetNextPlayer(2); + CurrentPlayer.State = PlayerState.TURN; break; } case CardValue.SEVEN: case CardValue.KING: break; case CardValue.EIGHT: + CurrentPlayer.State = PlayerState.WAIT; CurrentPlayer = GetNextPlayer(2); + CurrentPlayer.State = PlayerState.TURN; break; case CardValue.ACE: if (Players.Count > 2) @@ -90,13 +117,15 @@ public class Game CurrentPlayer = GetNextPlayer(); } break; + case CardValue.JACK: + CurrentPlayer.State = PlayerState.CHOOSE; + break; case CardValue.THREE: case CardValue.FOUR: case CardValue.FIVE: case CardValue.SIX: case CardValue.NINE: case CardValue.TEN: - case CardValue.JACK: case CardValue.QUEEN: default: CurrentPlayer = GetNextPlayer(); @@ -108,6 +137,8 @@ public class Game { player.Hand.Add(Deck.DrawCard()); CurrentPlayer = GetNextPlayer(); + player.State = PlayerState.WAIT; + CurrentPlayer.State = PlayerState.TURN; } private Player GetNextPlayer(int numberOfPlayers = 1) @@ -135,7 +166,7 @@ public class Game private static bool IsCardPlayable(Card currentCard, Card playedCard) { - return IsSameCardType(currentCard, playedCard) || IsSameCardValue(currentCard, playedCard); + return IsSameCardType(currentCard, playedCard) || IsSameCardValue(currentCard, playedCard) || playedCard.CardType == CardType.JOKER; } private static bool IsCardInHand(IEnumerable hand, Card card) diff --git a/Mau/GameState.cs b/Mau/GameState.cs index 5f510c1..470fd70 100644 --- a/Mau/GameState.cs +++ b/Mau/GameState.cs @@ -3,6 +3,7 @@ public class GameState { public string PlayerName { get; set; } + public string CurrentState { get; set; } public List Hand { get; set; } = new(); public string CurrentCard { get; set; } public string CurrentPlayer { get; set; } @@ -12,6 +13,7 @@ public class GameState { var p = game.GetPlayer(playerId); PlayerName = p.Connection.ConnectionId; + CurrentState = p.State.ToString(); foreach (var card in p.Hand) { Hand.Add(card.ToString()); @@ -23,6 +25,6 @@ public class GameState } CurrentCard = game.CurrentCard.ToString(); - CurrentPlayer = game.CurrentPlayer.Connection.ConnectionId;; + CurrentPlayer = game.CurrentPlayer.Connection.ConnectionId; } } \ No newline at end of file diff --git a/Mau/Player.cs b/Mau/Player.cs index 0fbec58..0c269c3 100644 --- a/Mau/Player.cs +++ b/Mau/Player.cs @@ -1,11 +1,11 @@ -using System.Net.WebSockets; -using MauMau_Server.Websockets; +using MauMau_Server.Websockets; namespace MauMau_Server.Mau; public class Player { public ConnectionInstance Connection { get; set; } + public PlayerState State { get; set; } = PlayerState.WAIT; public List Hand { get; set; } = new(); public Player(ConnectionInstance connection) diff --git a/Mau/PlayerState.cs b/Mau/PlayerState.cs new file mode 100644 index 0000000..a52669c --- /dev/null +++ b/Mau/PlayerState.cs @@ -0,0 +1,8 @@ +namespace MauMau_Server.Mau; + +public enum PlayerState +{ + TURN, + CHOOSE, + WAIT +} \ No newline at end of file