Created
February 4, 2025 15:30
-
-
Save Veekinex/80fafe057723deaf4d26a3ea08cae07d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace Deck | |
{ | |
internal class Program | |
{ | |
static void Main(string[] args) | |
{ | |
char[] cardsSiuts = { '♠', '♣', '♦', '♥' }; | |
string[] cardsValues = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" }; | |
string playerNickname = "Jim"; | |
Deck playerDeck = new Deck(); | |
Player player = new Player(playerNickname, playerDeck); | |
Deck tableDeck = new Deck(); | |
tableDeck.Initialize(cardsValues, cardsSiuts); | |
tableDeck.Shuffle(); | |
Table table = new Table(player, tableDeck); | |
table.ShowInfo(); | |
if (table.RequestCardsCount(out int cardCount)) | |
table.MoveCardsToPlayer(cardCount); | |
playerDeck.ShowCards(); | |
} | |
} | |
class Table | |
{ | |
public Table(Player player, Deck deck) | |
{ | |
Deck = deck; | |
Player = player; | |
} | |
public Deck Deck { get; private set; } | |
public Player Player { get; private set; } | |
public void ShowInfo() | |
{ | |
Console.WriteLine($"За текущим столом находится игрок {Player.Nickname}, в колоде {Deck.GetNumberOfCards()} карт"); | |
} | |
public bool RequestCardsCount(out int cardsCount) | |
{ | |
Console.Write("Сколько карт передать игроку? "); | |
string userInput = Console.ReadLine(); | |
if (int.TryParse(userInput, out cardsCount)) | |
{ | |
if (cardsCount < 0 || cardsCount > Deck.GetNumberOfCards()) | |
Console.WriteLine("Некорректное количество карт."); | |
else | |
return true; | |
} | |
else | |
{ | |
Console.WriteLine("Неверный ввод."); | |
} | |
return false; | |
} | |
public void MoveCardsToPlayer(int cardsCount) | |
{ | |
for (int i = 0; i < cardsCount; i++) | |
Player.Deck.AddCard(Deck.PullCard()); | |
} | |
} | |
class Player | |
{ | |
public Player(string nickname, Deck deck) | |
{ | |
Nickname = nickname; | |
Deck = deck; | |
} | |
public Deck Deck { get; private set; } | |
public string Nickname { get; private set; } | |
} | |
class Deck | |
{ | |
private List<Card> _cards = new List<Card>(); | |
public void AddCard(Card card) | |
{ | |
_cards.Add(card); | |
} | |
public Card PullCard(int index = 0) | |
{ | |
Card card = null; | |
if (index < _cards.Count && index >= 0) | |
{ | |
card = _cards[index]; | |
_cards.RemoveAt(index); | |
} | |
return card; | |
} | |
public void Shuffle() | |
{ | |
int castlingCount = 100; | |
Random random = new Random(); | |
for (int i = 0; i < castlingCount; i++) | |
{ | |
int firstRandomIndex = random.Next(_cards.Count); | |
int secondRandomIndex = random.Next(_cards.Count); | |
Card card = _cards[firstRandomIndex]; | |
_cards[firstRandomIndex] = _cards[secondRandomIndex]; | |
_cards[secondRandomIndex] = card; | |
} | |
} | |
public void ShowCards() | |
{ | |
foreach (Card card in _cards) | |
{ | |
card.Show(); | |
Console.Write(" "); | |
} | |
Console.WriteLine(); | |
} | |
public void Initialize(string[] cardsValues, char[] cardsSiuts) | |
{ | |
for (int i = 0; i < cardsValues.Length; i++) | |
for (int j = 0; j < cardsSiuts.Length; j++) | |
AddCard(new Card(cardsValues[i], cardsSiuts[j])); | |
} | |
public int GetNumberOfCards() | |
{ | |
return _cards.Count; | |
} | |
} | |
class Card | |
{ | |
private char _suite; | |
private string _value; | |
public Card(string value, char suite) | |
{ | |
_suite = suite; | |
_value = value; | |
} | |
public void Show() | |
{ | |
Console.Write(_value + _suite); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment