Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- // Реализована на данный момент игра на количество игроков до 6 человек включая,
- // по классике: 36 карт в колоде, изначально раздача всем карт и выявление козыря, далее кидать можно по 1 карте.
- namespace DurakGame
- {
- class Program
- {
- static void Main()
- {
- Console.WriteLine("Введите количество игроков (до 6 человек включая)\n");
- var countPlayers = int.Parse(Console.ReadLine());
- var newGame = new GameProcess(countPlayers);
- newGame.PlayerMovesInOrder();
- }
- }
- public class GameProcess
- {
- public static List<Card> cardDeck;
- public List<Player> players;
- private Random _random;
- private int _cardsAmount = 36;
- public static Card trump { get; private set; }
- public GameProcess(int playersCount)
- {
- players = new List<Player>();
- for (int i = 0; i < playersCount; i++) { players.Add(new Player()); }
- cardDeck = CreateCardDeck();
- GiveAllCardsToPlayers(players, cardDeck);
- }
- public List<Card> CreateCardDeck()
- {
- cardDeck = new List<Card>();
- int countCardInEachSuit = _cardsAmount / 4;
- for (int i = 0; i < countCardInEachSuit; i++)
- {
- cardDeck.Add(new Card((CardValue)i, CardSuit.hearts));
- cardDeck.Add(new Card((CardValue)i, CardSuit.diamonds));
- cardDeck.Add(new Card((CardValue)i, CardSuit.clubs));
- cardDeck.Add(new Card((CardValue)i, CardSuit.spades));
- }
- ShuffleCards(cardDeck);
- return cardDeck;
- }
- public void ShuffleCards(List<Card> cards)
- {
- _random = new Random();
- for (int i = cards.Count - 1; i >= 1; i--)
- {
- int j = _random.Next(i + 1);
- (cards[j], cards[i]) = (cards[i], cards[j]);
- }
- }
- public void GiveAllCardsToPlayers(List<Player> players, List<Card> cards)
- {
- var currentPlayer = 0;
- var copyCardDeck = new List<Card>();
- copyCardDeck.AddRange(cards.ToArray());
- for (int i = 0; i < copyCardDeck.Count; i++)
- {
- if (players[currentPlayer].cards.Count != 6)
- {
- players[currentPlayer].cards.Add(copyCardDeck[i]);
- cardDeck.Remove(copyCardDeck[i]);
- }
- currentPlayer++;
- if (currentPlayer == players.Count) currentPlayer = 0;
- }
- if (cards.Count == 0)
- {
- trump = copyCardDeck[copyCardDeck.Count - 1];
- cardDeck.Remove(copyCardDeck[copyCardDeck.Count - 1]);
- }
- else
- {
- trump = copyCardDeck[_cardsAmount - cards.Count];
- cardDeck.Remove(copyCardDeck[_cardsAmount - cards.Count]);
- }
- }
- public bool PlayerMovesInOrder()
- {
- var playerName = 0;
- while (true)
- {
- var currentPlayer = players[playerName];
- if (currentPlayer.cards.Count > 0)
- {
- Player.WritePlayerCards(currentPlayer, playerName);
- Console.WriteLine("\nСделайте ход, нажав соответствующий карте номер!");
- if (GameTable.CardsOnTable.Count > 0)
- Console.WriteLine("\nЛибо заберите карту, нажав любое отрицательное число!");
- var throwCard = int.Parse(Console.ReadLine());
- if (throwCard < 0)
- TakeCards(currentPlayer, playerName);
- else
- ThrowCard(currentPlayer, playerName, throwCard);
- }
- else
- {
- Console.WriteLine(playerName + " WON");
- return true;
- }
- playerName++;
- if (playerName == players.Count) playerName = 0;
- }
- }
- public static void TakeCards(Player currentPlayer, int playerName)
- {
- var gameTableCards = new List<Card>();
- gameTableCards.AddRange(GameTable.CardsOnTable.ToArray());
- foreach (var card in gameTableCards)
- {
- currentPlayer.cards.Add(card);
- GameTable.CardsOnTable.Remove(card);
- }
- Player.UpdateTheScreenData(playerName, currentPlayer.cards.Count, "взял карты");
- }
- public static bool ThrowCard(Player currentPlayer, int playerName, int throwCard)
- {
- var selectedСard = currentPlayer.cards[throwCard];
- while (!GameTable.IsMoveMade)
- {
- if (GameTable.IsNeedNewThrow)
- {
- Player.UpdateTheScreenData(playerName, currentPlayer.cards.Count, selectedСard);
- Player.WritePlayerCards(currentPlayer, playerName);
- Console.WriteLine("\nСделайте ход, нажав соответствующий карте номер!");
- if (GameTable.CardsOnTable.Count > 0)
- Console.WriteLine("\nЛибо заберите карту, нажав любое отрицательное число!");
- throwCard = int.Parse(Console.ReadLine());
- selectedСard = currentPlayer.cards[throwCard];
- }
- Player.UpdateTheScreenData(playerName, currentPlayer.cards.Count, selectedСard);
- GameTable.ThrowCard(selectedСard, currentPlayer);
- }
- GameTable.IsMoveMade = false;
- currentPlayer.cards.Remove(selectedСard);
- if (currentPlayer.cards.Count == 0)
- {
- Console.WriteLine(playerName + " WON");
- return true;
- }
- return false;
- }
- }
- public class Player
- {
- public List<Card> cards = new List<Card>();
- public static void WritePlayerCards(Player player, int playerName)
- {
- Console.WriteLine("\n{0} игрок, ваши карты: \n", playerName);
- Console.WriteLine("Козырная масть - {0}\n", GameProcess.trump.suit);
- for (int j = 0; j < player.cards.Count; j++)
- {
- Console.WriteLine(j + ") " + player.cards[j]);
- }
- }
- public static void UpdateTheScreenData(int playerName, int cardsCount, object actOrCard)
- {
- Console.Clear();
- Console.WriteLine("предыдущий игрок\tкол-во карт на руках\tход картой");
- Console.WriteLine("{0}\t\t\t{1}\t\t\t{2}", playerName, cardsCount, actOrCard);
- Console.WriteLine("______________________________________________________________\n");
- }
- }
- public enum CardValue
- {
- six, seven, eight, nine, ten, jack, queen, king, ace
- }
- public enum CardSuit
- {
- hearts, diamonds, clubs, spades
- }
- public class Card
- {
- public readonly CardValue value;
- public readonly CardSuit suit;
- public Card(CardValue value, CardSuit suit)
- {
- this.value = value;
- this.suit = suit;
- }
- public override string ToString() => value.ToString() + " " + suit.ToString();
- }
- public class GameTable
- {
- public static List<Card> CardsOnTable = new List<Card>();
- public static bool IsMoveMade = false;
- public static bool IsNeedNewThrow = false;
- public static void ThrowCard(Card throwCard, Player player)
- {
- if (CardsOnTable.Count == 0)
- Attack(throwCard, player);
- else
- Defend(throwCard);
- }
- public static void Attack(Card throwCard, Player player)
- {
- CardsOnTable.Add(throwCard);
- IsMoveMade = true;
- var cardDeckCount = GameProcess.cardDeck.Count;
- if (GameProcess.cardDeck.Count > 0)
- {
- player.cards.Add(GameProcess.cardDeck[cardDeckCount - 1]);
- GameProcess.cardDeck.Remove(GameProcess.cardDeck[cardDeckCount - 1]);
- }
- }
- public static void Defend(Card throwCard)
- {
- var isTheWrongMove = false;
- TryToBeatCardOnTable(throwCard, isTheWrongMove);
- }
- public static void TryToBeatCardOnTable(Card throwCard, bool isTheWrongMove)
- {
- for (int i = 0; i < CardsOnTable.Count; i++)
- {
- var trumpSuit = GameProcess.trump.suit;
- if ((CardsOnTable[i].suit == throwCard.suit && CardsOnTable[i].value < throwCard.value)
- || (throwCard.suit == trumpSuit && CardsOnTable[i].suit != trumpSuit))
- {
- CardsOnTable.Clear();
- break;
- }
- else
- {
- Console.WriteLine("Эту карту нельзя бросить! Введите другое значение.");
- System.Threading.Thread.Sleep(2000);
- IsNeedNewThrow = true;
- isTheWrongMove = true;
- break;
- }
- }
- IsMoveMade = true;
- if (isTheWrongMove)
- IsMoveMade = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement