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();
- Deck deck = new Deck();
- 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.Game(deck.Cards);
- 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 Deck
- {
- public List<Card> Cards = new List<Card>();
- public List<Card> PlayerCards = new List<Card>();
- public List<Card> OpponentCards = new List<Card>();
- private string[] _rank = { "6", "7", "8", "9", "10", "J", "Q", "K", "A" };
- private char[] _suits = { '♣', '♦', '♥', '♠' };
- private int[] _price = { 6, 7, 8, 9, 10, 2, 3, 4, 11 };
- public void FillDeck()
- {
- for (int i = 0; i < _rank.Length; i++)
- {
- for (int j = 0; j < _suits.Length; j++)
- {
- AssignPrice(_rank, _suits, i, j, "6", 0);
- AssignPrice(_rank, _suits, i, j, "7", 1);
- AssignPrice(_rank, _suits, i, j, "8", 2);
- AssignPrice(_rank, _suits, i, j, "9", 3);
- AssignPrice(_rank, _suits, i, j, "10", 4);
- AssignPrice(_rank, _suits, i, j, "J", 5);
- AssignPrice(_rank, _suits, i, j, "Q", 7);
- AssignPrice(_rank, _suits, i, j, "K", 7);
- AssignPrice(_rank, _suits, i, j, "A", 8);
- }
- }
- }
- private void AssignPrice(string[] _rank, char[] _suits, int i, int j, string rank, int increment)
- {
- if (_rank[i] == rank)
- {
- for (int k = increment; k < _price.Length;)
- {
- Cards.Add(new Card(_rank[i], _suits[j], _price[k]));
- break;
- }
- }
- }
- }
- class Card
- {
- public string Rank { get; private set; }
- public char Suit { get; private set; }
- public int Price { get; private set; }
- public List<Card> PlayerCards = new List<Card>();
- public List<Card> OpponentCards = new List<Card>();
- public Card(string rank, char suit, int price)
- {
- Rank = rank;
- Suit = suit;
- Price = price;
- }
- public void ShowCardInfo(Card card, int positionX, int positionY)
- {
- Console.SetCursorPosition(positionX, positionY);
- 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 bool _isRun = true;
- Random random = new Random();
- public void Game(List<Card> cards)
- {
- while (_isRun)
- {
- ConsoleKeyInfo charKey = Console.ReadKey();
- switch (charKey.Key)
- {
- case ConsoleKey.Spacebar:
- PlayerTurn(cards);
- break;
- case ConsoleKey.Enter:
- OpponentTurn(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 PlayerTurn(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 -= 12;
- randomCardPlayer.ShowCardInfo(randomCardPlayer, 0, _infoLinePositionPlayerY);
- _infoLinePositionPlayerY += 1;
- if (_playerCount > 21)
- {
- Console.SetCursorPosition(30, 13);
- Console.ForegroundColor = ConsoleColor.Cyan;
- Console.WriteLine("Перебор. Ты проиграл..");
- Console.ForegroundColor = ConsoleColor.White;
- _isRun = false;
- }
- }
- private void OpponentTurn(List<Card> cards)
- {
- while (_opponentCount < 19)
- {
- 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 -= 12;
- randomCardOpponent.ShowCardInfo(randomCardOpponent, 30, _infoLinePositionOpponentY);
- _infoLinePositionOpponentY += 1;
- 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 > 21 && _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 < 21 || _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 indentLeftX, int positionCardPlayerX, int positionCardPlayerY)
- {
- Console.SetCursorPosition(positionCardPlayerX, positionCardPlayerY);
- DisplayCard(card, indentLeftX);
- }
- private void DisplayCard(Card card, int indentLeftX)
- {
- string symbols = new string(' ', indentLeftX);
- 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