Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Numerics;
- using System.Reflection.Emit;
- using System.Runtime.CompilerServices;
- using System.Text;
- namespace Колода_карт
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- bool isRun = true;
- while (isRun)
- {
- GameLogic gameLogic = new GameLogic();
- gameLogic.Gameplay();
- Deck deck = new Deck();
- Player player = new Player();
- Console.CursorVisible = false;
- string symbols = new string('=', 20);
- Console.WriteLine($"\n\n{symbols} Игра в 21! {symbols}\n\nНабери 21 очко, или больше очков, чем у соперника, (но не больше 21)!\n" +
- "Валет - 2 очка, Дама - 3 очка, Король - 4 очка, Туз - 11 очков, остальные по номиналу.");
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.WriteLine("Жми 'ПРОБЕЛ', что бы начать. Жми 'ENTER', когда захочешь передать ход сопернику, для выхода нажми 'ESCAPE'");
- Console.ForegroundColor = ConsoleColor.White;
- //deck.FillDeck();
- //gameLogic.Gameplay(deck._сards);
- Console.SetCursorPosition(60, 8);
- Console.Write("Нажми любую клавишу для продолжения, 'ESCAPE' для выхода");
- ConsoleKeyInfo charKey = Console.ReadKey();
- switch (charKey.Key)
- {
- case ConsoleKey.Escape:
- isRun = false;
- break;
- default:
- break;
- }
- Console.SetCursorPosition(0, 0);
- Console.Clear();
- }
- }
- }
- class CardHolder
- {
- protected List<Card> Cards = new List<Card>();
- public int CardPoints => CalculatePoints();
- private int quantityCoordinatePointIndentLeftX = 12;
- private int infoCardPointLineIndentY = 1;
- private int pointToWin = 21;
- private bool _isRun = true;
- public int CalculatePoints()
- {
- int points = 0;
- for (int i = 0; i < Cards.Count; i++)
- {
- points += Cards[i].Price;
- }
- return points;
- }
- public void ShowCards()
- {
- for (int i = 0; i < Cards.Count; i++)
- {
- Console.Write($"{Cards[i].Rank}{Cards[i].Suit}");
- }
- }
- public void TakeCard(Card card)
- {
- if (card != null)
- {
- Cards.Add(card);
- }
- else
- {
- Console.WriteLine("карт нет");
- }
- }
- }
- class Deck
- {
- private List<Card> _cards = new List<Card>(36);
- public Deck()
- {
- CreateCards();
- }
- public void Shuffle()
- {
- Random random = new Random();
- for (int i = _cards.Count - 1; i >= 1; i--)
- {
- int randomIndexInArray = random.Next(i + 1);
- Card temporaryValue = _cards[randomIndexInArray];
- _cards[randomIndexInArray] = _cards[i];
- _cards[i] = temporaryValue;
- }
- }
- private void CreateCards()
- {
- string[] ranks = { "6", "7", "8", "9", "10", "J", "Q", "K", "A" };
- char[] suits = { '♣', '♦', '♥', '♠' };
- int[] prices = { 6, 7, 8, 9, 10, 2, 3, 4, 11 };
- for (int i = 0; i < ranks.Length; i++)
- {
- for (int j = 0; j < suits.Length; j++)
- {
- Card card = new Card(ranks[i], suits[j], prices[i]);
- _cards.Add(card);
- }
- }
- }
- public Card GetCard()
- {
- Card card = null;
- if (_cards.Count > 0)
- {
- card = _cards[0];
- _cards.Remove(card);
- }
- return card;
- }
- public void SaveCardPosition(Card card, int quantityCoordinatePointToCardX, int positionCardPlayerX, int positionCardPlayerY)
- {
- Console.SetCursorPosition(positionCardPlayerX, positionCardPlayerY);
- DisplayCard(card, quantityCoordinatePointToCardX);
- }
- public void DisplayCard(Card card, int quantityCoordinatePointToCardX)
- {
- string symbols = new string(' ', quantityCoordinatePointToCardX);
- if (card.Rank == "10")
- {
- Console.Write($"\r\n{symbols}┌─────────┐\r\n{symbols}│{card.Rank} │\r\n{symbols}│ │\r\n{symbols}│ │\r\n{symbols}│ {card.Suit} │\r\n{symbols}│ │\r\n{symbols}│ │\r\n{symbols}│ {card.Rank}│\r\n{symbols}└─────────┘");
- }
- else
- {
- Console.Write($"\r\n{symbols}┌─────────┐\r\n{symbols}│ {card.Rank} │\r\n{symbols}│ │\r\n{symbols}│ │\r\n{symbols}│ {card.Suit} │\r\n{symbols}│ │\r\n{symbols}│ │\r\n{symbols}│ {card.Rank} │\r\n{symbols}└─────────┘");
- }
- }
- }
- class Player : CardHolder
- {
- //private List<Card> _playerCards = new List<Card>();
- private int _playerCount = 0;
- private int _infoLinePositionPlayerY = 8;
- private int _indentPlayerCardX = 50;
- private int _positionCardPlayerX = 14;
- private int _positionCardPlayerY = 13;
- //private void CreatePlayerTurn(List<Card> cards)
- //{
- // //int randomIndexPlayer = _random.Next(cards.Count);
- // Card randomCardPlayer = cards[randomIndexPlayer];
- // cards.Remove(randomCardPlayer);
- // _playerCount += randomCardPlayer.Price;
- // //_playerCards.Add(randomCardPlayer);
- // Console.SetCursorPosition(0, 7);
- // Console.ForegroundColor = ConsoleColor.DarkYellow;
- // Console.WriteLine($"Твои карты: Общий счет ({_playerCount})");
- // Console.ForegroundColor = ConsoleColor.White;
- // SaveCardPosition(randomCardPlayer, _indentPlayerCardX, _positionCardPlayerX, _positionCardPlayerY);
- // _indentPlayerCardX -= quantityCoordinatePointIndentLeftX;
- // randomCardPlayer.ShowCardPointInfoPanel(randomCardPlayer, 0, _infoLinePositionPlayerY);
- // _infoLinePositionPlayerY += infoCardPointLineIndentY;
- // if (_playerCount > pointToWin)
- // {
- // Console.SetCursorPosition(30, 13);
- // Console.ForegroundColor = ConsoleColor.Cyan;
- // Console.WriteLine("Перебор. Ты проиграл..");
- // Console.ForegroundColor = ConsoleColor.White;
- // _isRun = false;
- // }
- //}
- }
- class Opponent : CardHolder
- {
- //private List<Card> _opponentCards = new List<Card>();
- private int _opponentCount = 0;
- private int _infoLinePositionOpponentY = 8;
- private int _indentOpponentCardX = 50;
- private int _positionCardOpponentX = 14;
- private int _positionCardOpponentY = 23;
- public void TryMakeTurn(Deck deck)
- {
- while (CardPoints < 16)
- {
- TakeCard(deck.GetCard());
- Console.WriteLine($"Очки {CardPoints}");
- }
- }
- //private void CreateOpponentTurn(List<Card> cards)
- //{
- // int opponentStopPoint = 19;
- // while (_opponentCount < opponentStopPoint)
- // {
- // //int randomIndexOpponent = _random.Next(cards.Count);
- // Card randomCardOpponent = cards[randomIndexOpponent];
- // cards.Remove(randomCardOpponent);
- // _opponentCount += randomCardOpponent.Price;
- // _opponentCards.Add(randomCardOpponent);
- // Console.SetCursorPosition(30, 7);
- // Console.ForegroundColor = ConsoleColor.DarkYellow;
- // Console.WriteLine($"Карты оппонента: Общий счет({_opponentCount})");
- // Console.ForegroundColor = ConsoleColor.White;
- // SaveCardPosition(randomCardOpponent, _indentOpponentCardX, _positionCardOpponentX, _positionCardOpponentY);
- // _indentOpponentCardX -= quantityCoordinatePointIndentLeftX;
- // randomCardOpponent.ShowCardPointInfoPanel(randomCardOpponent, 30, _infoLinePositionOpponentY);
- // _infoLinePositionOpponentY += infoCardPointLineIndentY;
- // Thread.Sleep(500);
- // }
- // if (_playerCount > _opponentCount)
- // {
- // Console.SetCursorPosition(30, 13);
- // string symbols = new string('=', 20);
- // Console.ForegroundColor = ConsoleColor.Green;
- // Console.WriteLine($"{symbols}Ты выиграл!{symbols}");
- // Console.ForegroundColor = ConsoleColor.White;
- // _isRun = false;
- // }
- // else if (_playerCount == _opponentCount)
- // {
- // Console.SetCursorPosition(30, 13);
- // string symbols = new string('=', 20);
- // Console.ForegroundColor = ConsoleColor.Yellow;
- // Console.WriteLine($"{symbols}Ничья{symbols}");
- // Console.ForegroundColor = ConsoleColor.White;
- // _isRun = false;
- // }
- // else if (_opponentCount > pointToWin && _opponentCount > _playerCount)
- // {
- // Console.SetCursorPosition(30, 13);
- // string symbols = new string('=', 20);
- // Console.ForegroundColor = ConsoleColor.Green;
- // Console.WriteLine($"{symbols}Ты выиграл!{symbols}");
- // Console.ForegroundColor = ConsoleColor.White;
- // _isRun = false;
- // }
- // else if (_opponentCount < pointToWin || _opponentCount > _playerCount)
- // {
- // Console.SetCursorPosition(30, 13);
- // string symbols = new string('=', 20);
- // Console.ForegroundColor = ConsoleColor.Cyan;
- // Console.WriteLine($"{symbols}Ты проиграл!{symbols}");
- // Console.ForegroundColor = ConsoleColor.White;
- // _isRun = false;
- // }
- //}
- }
- class Card
- {
- public Card(string rank, char suit, int price)
- {
- Rank = rank;
- Suit = suit;
- Price = price;
- }
- public string Rank { get; private set; }
- public char Suit { get; private set; }
- public int Price { get; private set; }
- public void ShowCardPointInfoPanel(Card card, int positionCardPointX, int positionCardPointY)
- {
- Console.SetCursorPosition(positionCardPointX, positionCardPointY);
- Console.Write($"{card.Rank} {card.Suit} (количество очков {card.Price})");
- }
- }
- class GameLogic
- {
- //private List<Card> _playerCards = new List<Card>();
- //private List<Card> _opponentCards = new List<Card>();
- //private int _playerCount = 0;
- //private int _opponentCount = 0;
- //private int _infoLinePositionPlayerY = 8;
- //private int _infoLinePositionOpponentY = 8;
- //private int _indentPlayerCardX = 50;
- //private int _indentOpponentCardX = 50;
- //private int _positionCardPlayerX = 14;
- //private int _positionCardPlayerY = 13;
- //private int _positionCardOpponentX = 14;
- //private int _positionCardOpponentY = 23;
- //private int quantityCoordinatePointIndentLeftX = 12;
- //private int infoCardPointLineIndentY = 1;
- //private int pointToWin = 21;
- //private bool _isRun = true;
- //private Random _random = new Random();
- bool _isRun = true;
- public void Gameplay()
- {
- Deck deck = new Deck();
- Player player = new Player();
- Opponent opponent = new Opponent();
- while (_isRun)
- {
- ConsoleKeyInfo charKey = Console.ReadKey();
- switch (charKey.Key)
- {
- case ConsoleKey.Spacebar:
- deck.Shuffle();
- Card card = deck.GetCard();
- player.TakeCard(card);
- //player.ShowCards();
- deck.SaveCardPosition(card, 12, 10,2);
- //deck.DisplayCard(card, 10);
- //CreatePlayerTurn(cards);
- break;
- case ConsoleKey.Enter:
- deck.Shuffle();
- opponent.TryMakeTurn(deck);
- deck.SaveCardPosition(deck.GetCard(),12, 10, 20);
- //opponent.ShowCards();
- //CreateOpponentTurn(cards);
- break;
- case ConsoleKey.Escape:
- _isRun = false;
- break;
- default:
- Console.SetCursorPosition(0, 6);
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.WriteLine("Жми 'ПРОБЕЛ', что бы начать. Жми 'ENTER', когда захочешь передать ход сопернику, для выхода нажми 'ESCAPE'");
- Console.ForegroundColor = ConsoleColor.White;
- break;
- }
- }
- }
- //private void CreatePlayerTurn(List<Card> cards)
- //{
- // int randomIndexPlayer = _random.Next(cards.Count);
- // Card randomCardPlayer = cards[randomIndexPlayer];
- // cards.Remove(randomCardPlayer);
- // _playerCount += randomCardPlayer.Price;
- // _playerCards.Add(randomCardPlayer);
- // Console.SetCursorPosition(0, 7);
- // Console.ForegroundColor = ConsoleColor.DarkYellow;
- // Console.WriteLine($"Твои карты: Общий счет ({_playerCount})");
- // Console.ForegroundColor = ConsoleColor.White;
- // SaveCardPosition(randomCardPlayer, _indentPlayerCardX, _positionCardPlayerX, _positionCardPlayerY);
- // _indentPlayerCardX -= quantityCoordinatePointIndentLeftX;
- // randomCardPlayer.ShowCardPointInfoPanel(randomCardPlayer, 0, _infoLinePositionPlayerY);
- // _infoLinePositionPlayerY += infoCardPointLineIndentY;
- // if (_playerCount > pointToWin)
- // {
- // Console.SetCursorPosition(30, 13);
- // Console.ForegroundColor = ConsoleColor.Cyan;
- // Console.WriteLine("Перебор. Ты проиграл..");
- // Console.ForegroundColor = ConsoleColor.White;
- // _isRun = false;
- // }
- //}
- //private void CreateOpponentTurn(List<Card> cards)
- //{
- // int opponentStopPoint = 19;
- // while (_opponentCount < opponentStopPoint)
- // {
- // int randomIndexOpponent = _random.Next(cards.Count);
- // Card randomCardOpponent = cards[randomIndexOpponent];
- // cards.Remove(randomCardOpponent);
- // _opponentCount += randomCardOpponent.Price;
- // _opponentCards.Add(randomCardOpponent);
- // Console.SetCursorPosition(30, 7);
- // Console.ForegroundColor = ConsoleColor.DarkYellow;
- // Console.WriteLine($"Карты оппонента: Общий счет({_opponentCount})");
- // Console.ForegroundColor = ConsoleColor.White;
- // SaveCardPosition(randomCardOpponent, _indentOpponentCardX, _positionCardOpponentX, _positionCardOpponentY);
- // _indentOpponentCardX -= quantityCoordinatePointIndentLeftX;
- // randomCardOpponent.ShowCardPointInfoPanel(randomCardOpponent, 30, _infoLinePositionOpponentY);
- // _infoLinePositionOpponentY += infoCardPointLineIndentY;
- // Thread.Sleep(500);
- // }
- // if (_playerCount > _opponentCount)
- // {
- // Console.SetCursorPosition(30, 13);
- // string symbols = new string('=', 20);
- // Console.ForegroundColor = ConsoleColor.Green;
- // Console.WriteLine($"{symbols}Ты выиграл!{symbols}");
- // Console.ForegroundColor = ConsoleColor.White;
- // _isRun = false;
- // }
- // else if (_playerCount == _opponentCount)
- // {
- // Console.SetCursorPosition(30, 13);
- // string symbols = new string('=', 20);
- // Console.ForegroundColor = ConsoleColor.Yellow;
- // Console.WriteLine($"{symbols}Ничья{symbols}");
- // Console.ForegroundColor = ConsoleColor.White;
- // _isRun = false;
- // }
- // else if (_opponentCount > pointToWin && _opponentCount > _playerCount)
- // {
- // Console.SetCursorPosition(30, 13);
- // string symbols = new string('=', 20);
- // Console.ForegroundColor = ConsoleColor.Green;
- // Console.WriteLine($"{symbols}Ты выиграл!{symbols}");
- // Console.ForegroundColor = ConsoleColor.White;
- // _isRun = false;
- // }
- // else if (_opponentCount < pointToWin || _opponentCount > _playerCount)
- // {
- // Console.SetCursorPosition(30, 13);
- // string symbols = new string('=', 20);
- // Console.ForegroundColor = ConsoleColor.Cyan;
- // Console.WriteLine($"{symbols}Ты проиграл!{symbols}");
- // Console.ForegroundColor = ConsoleColor.White;
- // _isRun = false;
- // }
- //}
- private void SaveCardPosition(Card card, int quantityCoordinatePointToCardX, int positionCardPlayerX, int positionCardPlayerY)
- {
- Console.SetCursorPosition(positionCardPlayerX, positionCardPlayerY);
- DisplayCard(card, quantityCoordinatePointToCardX);
- }
- private void DisplayCard(Card card, int quantityCoordinatePointToCardX)
- {
- string symbols = new string(' ', quantityCoordinatePointToCardX);
- if (card.Rank == "10")
- {
- Console.Write($"\r\n{symbols}┌─────────┐\r\n{symbols}│{card.Rank} │\r\n{symbols}│ │\r\n{symbols}│ │\r\n{symbols}│ {card.Suit} │\r\n{symbols}│ │\r\n{symbols}│ │\r\n{symbols}│ {card.Rank}│\r\n{symbols}└─────────┘");
- }
- else
- {
- Console.Write($"\r\n{symbols}┌─────────┐\r\n{symbols}│ {card.Rank} │\r\n{symbols}│ │\r\n{symbols}│ │\r\n{symbols}│ {card.Suit} │\r\n{symbols}│ │\r\n{symbols}│ │\r\n{symbols}│ {card.Rank} │\r\n{symbols}└─────────┘");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement