Advertisement
mausha

CardsGame

Aug 2nd, 2017
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.33 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace CardsGame_EXER
  5. {
  6.     public class StartUp
  7.     {
  8.         public static void Main()
  9.         {
  10.             var firstPlayer = new Player(Console.ReadLine());
  11.             var secondPlayer = new Player(Console.ReadLine());
  12.  
  13.             var deck = new List<Card>();
  14.             while (firstPlayer.Cards.Count < 5 || secondPlayer.Cards.Count < 5)
  15.             {
  16.                 var cardInput = Console.ReadLine().Split();
  17.                 try
  18.                 {
  19.                     var card = new Card(cardInput[0], cardInput[cardInput.Length - 1]);
  20.                     if (!deck.Contains(card))
  21.                     {
  22.                         if (firstPlayer.Cards.Count < 5)
  23.                         {
  24.                             firstPlayer.AddCard(card);
  25.                         }
  26.                         else
  27.                         {
  28.                             secondPlayer.AddCard(card);
  29.                         }
  30.                         deck.Add(card);
  31.                     }
  32.                     else
  33.                     {
  34.                         Console.WriteLine("Card is not in the deck.");
  35.                     }
  36.                 }
  37.                 catch (Exception)
  38.                 {
  39.                     Console.WriteLine("No such card exists.");
  40.                 }
  41.             }
  42.  
  43.             var winner = firstPlayer.StrongestCard.CompareTo(secondPlayer.StrongestCard);
  44.             if (winner > 0)
  45.             {
  46.                 Console.WriteLine($"{firstPlayer.Name} wins with {firstPlayer.StrongestCard}.");
  47.             }
  48.             else
  49.             {
  50.                 Console.WriteLine($"{secondPlayer.Name} wins with {secondPlayer.StrongestCard}");
  51.             }
  52.         }
  53.     }
  54. }
  55.  
  56.  
  57. using System.Collections.Generic;
  58. using System.Linq;
  59.  
  60. namespace CardsGame_EXER
  61. {
  62.     public class Player
  63.     {
  64.         private string name;
  65.         private List<Card> cards;
  66.         private Card strongestCard;
  67.  
  68.         public Player(string name)
  69.         {
  70.             this.name = name;
  71.             this.cards = new List<Card>();
  72.             this.strongestCard = new Card("Two", "Clubs");
  73.         }
  74.  
  75.         public string Name => this.name;
  76.         public Card StrongestCard => this.strongestCard;
  77.  
  78.         public List<Card> Cards
  79.         {
  80.             get { return this.cards; }
  81.         }
  82.  
  83.         public void AddCard(Card card)
  84.         {
  85.             this.cards.Add(card);
  86.         }
  87.  
  88.         public void GetStrongesttCard()
  89.         {
  90.             this.strongestCard = this.cards.OrderByDescending(c => c).First();
  91.         }
  92.     }
  93. }
  94.  
  95.  
  96. using System;
  97.  
  98. namespace CardsGame_EXER
  99. {
  100.     public class Card : IComparable<Card>, IEquatable<Card>
  101.     {
  102.         private CardRank rank;
  103.         private CardSuit suit;
  104.  
  105.         public Card(string rank, string suit)
  106.         {
  107.             this.rank = (CardRank)Enum.Parse(typeof(CardRank), rank);
  108.             this.suit = (CardSuit)Enum.Parse(typeof(CardSuit), suit);
  109.         }
  110.  
  111.         public CardRank Rank => this.rank;
  112.         public CardSuit Suit => this.suit;
  113.         public int Power => (int)this.rank + (int)this.suit;
  114.  
  115.         public int CompareTo(Card other)
  116.         {
  117.             if (ReferenceEquals(this, other)) return 0;
  118.             if (ReferenceEquals(null, other)) return 1;
  119.             var result = this.suit.CompareTo(other.suit);
  120.             if (result != 0)
  121.             {
  122.                 return result;
  123.             }
  124.  
  125.             return this.rank.CompareTo(other.rank);
  126.         }
  127.  
  128.         public bool Equals(Card other)
  129.         {
  130.             return this.Power.Equals(other.Power);
  131.         }
  132.  
  133.         public override string ToString()
  134.         {
  135.             return $"{this.rank} of {this.suit}";
  136.         }
  137.     }
  138. }
  139.  
  140.  
  141. namespace CardsGame_EXER
  142. {
  143.     [Type("Enumeration", "Rank", "Provides rank constants for a Card class.")]
  144.     public enum CardRank
  145.     {
  146.         Two = 2,
  147.         Three,
  148.         Four,
  149.         Five,
  150.         Six,
  151.         Seven,
  152.         Eight,
  153.         Nine,
  154.         Ten,
  155.         Jack,
  156.         Queen,
  157.         King,
  158.         Ace
  159.     }
  160. }
  161.  
  162.  
  163. namespace CardsGame_EXER
  164. {
  165.     [Type("Enumeration", "Suit", "Provides suit constants for a Card class.")]
  166.     public enum CardSuit
  167.     {
  168.         Clubs,
  169.         Diamonds = 13,
  170.         Hearts = 26,
  171.         Spades = 39
  172.     }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement