blackjack/Models/Deck.cs

66 lines
1.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
namespace БлэкДжек.Components // Замените BlazorBlackjack на имя вашего проекта
{
public class Deck
{
public List<Card> Cards { get; private set; }
public Deck()
{
InitializeDeck();
}
private void InitializeDeck()
{
Cards = new List<Card>();
string[] suits = { "♥", "♦", "♣", "♠" };
string[] ranks = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" };
foreach (var suit in suits)
{
foreach (var rank in ranks)
{
int value = 0;
if (int.TryParse(rank, out int numValue))
{
value = numValue;
}
else if (rank == "J" || rank == "Q" || rank == "K")
{
value = 10;
}
else if (rank == "A")
{
value = 11; // Туз по умолчанию 11
}
Cards.Add(new Card(suit, rank, value));
}
}
}
public void Shuffle()
{
Random rng = new Random();
Cards = Cards.OrderBy(c => rng.Next()).ToList();
}
public Card DealCard()
{
if (Cards.Count == 0)
{
// Можно пересоздать и перемешать колоду, если она закончилась
// InitializeDeck();
// Shuffle();
// Или просто вернуть null/выбросить исключение
return null;
}
Card card = Cards[0];
Cards.RemoveAt(0);
return card;
}
}
}