basic mau
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
public class Deck
|
||||
{
|
||||
public List<Card> UnusedDeck = new();
|
||||
public List<Card> UsedDeck = new();
|
||||
|
||||
public Deck()
|
||||
{
|
||||
@@ -37,4 +38,9 @@ public class Deck
|
||||
}
|
||||
return cards;
|
||||
}
|
||||
|
||||
public void AddCardToUsedDeck(Card card)
|
||||
{
|
||||
UsedDeck.Add(card);
|
||||
}
|
||||
}
|
||||
48
Mau/Game.cs
Normal file
48
Mau/Game.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
namespace MauMau_Server.Mau;
|
||||
|
||||
public class Game
|
||||
{
|
||||
public readonly Deck Deck = new();
|
||||
public Card CurrentCard;
|
||||
public List<Card> Hand;
|
||||
|
||||
public Game()
|
||||
{
|
||||
CurrentCard = Deck.DrawCard();
|
||||
Deck.AddCardToUsedDeck(CurrentCard);
|
||||
Hand = Deck.DrawCards(8);
|
||||
}
|
||||
|
||||
public void PlayCard(Card card)
|
||||
{
|
||||
if (!IsCardInHand(Hand, card) || !IsCardPlayable(CurrentCard, card)) return;
|
||||
Deck.AddCardToUsedDeck(card);
|
||||
Hand.Remove(GetSameCardFromHand(Hand, card));
|
||||
CurrentCard = card;
|
||||
}
|
||||
|
||||
private Card GetSameCardFromHand(IEnumerable<Card> hand, Card card)
|
||||
{
|
||||
return hand.FirstOrDefault(handCard => IsSameCardType(handCard, card) && IsSameCardValue(handCard, card));
|
||||
}
|
||||
|
||||
private bool IsCardPlayable(Card currentCard, Card playedCard)
|
||||
{
|
||||
return IsSameCardType(currentCard, playedCard) || IsSameCardValue(currentCard, playedCard);
|
||||
}
|
||||
|
||||
private bool IsCardInHand(IEnumerable<Card> hand, Card card)
|
||||
{
|
||||
return hand.Any(handCard => IsSameCardType(handCard, card) && IsSameCardValue(handCard, card));
|
||||
}
|
||||
|
||||
private static bool IsSameCardType(Card card1, Card card2)
|
||||
{
|
||||
return card1.CardType == card2.CardType;
|
||||
}
|
||||
|
||||
private static bool IsSameCardValue(Card card1, Card card2)
|
||||
{
|
||||
return card1.CardValue == card2.CardValue;
|
||||
}
|
||||
}
|
||||
16
Mau/GameState.cs
Normal file
16
Mau/GameState.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
namespace MauMau_Server.Mau;
|
||||
|
||||
public class GameState
|
||||
{
|
||||
public List<string> Hand { get; set; } = new();
|
||||
public string CurrentCard { get; set; }
|
||||
|
||||
public GameState(Game game)
|
||||
{
|
||||
foreach (var card in game.Hand)
|
||||
{
|
||||
Hand.Add(card.ToString());
|
||||
}
|
||||
CurrentCard = game.CurrentCard.ToString();
|
||||
}
|
||||
}
|
||||
@@ -2,13 +2,13 @@
|
||||
|
||||
public class HandDTO
|
||||
{
|
||||
public List<string> _cards { get; set; } = new();
|
||||
public List<string> Cards { get; set; } = new();
|
||||
|
||||
public HandDTO(List<Card> cards)
|
||||
{
|
||||
foreach (var card in cards)
|
||||
{
|
||||
_cards.Add(card.ToString());
|
||||
Cards.Add(card.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,9 +8,7 @@ namespace MauMau_Server.Websockets;
|
||||
public class Room
|
||||
{
|
||||
private readonly Dictionary<string, WebSocket> _connections = new();
|
||||
private readonly Deck _deck = new();
|
||||
private Card currentCard;
|
||||
private List<Card> hand = new();
|
||||
private Game _game = new();
|
||||
|
||||
public async Task InstantiateConnection(WebSocket socket)
|
||||
{
|
||||
@@ -20,16 +18,15 @@ public class Room
|
||||
|
||||
private async Task HandleConnection(WebSocket socket, string socketId)
|
||||
{
|
||||
hand = _deck.DrawCards(8);
|
||||
SendAsync(socket, JsonSerializer.Serialize(new HandDTO(hand)));
|
||||
BroadcastAsync(JsonSerializer.Serialize(new GameState(_game)));
|
||||
var buffer = EmptyBuffer();
|
||||
var result = await ReceiveAsync(socket, buffer);
|
||||
while (!result.CloseStatus.HasValue)
|
||||
{
|
||||
var slicedBuffer = buffer[0..result.Count];
|
||||
var playedCard = JsonSerializer.Deserialize<CardDTO>(slicedBuffer).ToCard();
|
||||
BroadcastAsync(JsonSerializer.Serialize(playedCard.ToString()));
|
||||
SendAsync(socket, JsonSerializer.Serialize(new HandDTO(hand)));
|
||||
_game.PlayCard(playedCard);
|
||||
BroadcastAsync(JsonSerializer.Serialize(new GameState(_game)));
|
||||
buffer = EmptyBuffer();
|
||||
result = await ReceiveAsync(socket, buffer);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user