Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace CardsGame_EXER
- {
- public class StartUp
- {
- public static void Main()
- {
- var firstPlayer = new Player(Console.ReadLine());
- var secondPlayer = new Player(Console.ReadLine());
- var deck = new List<Card>();
- while (firstPlayer.Cards.Count < 5 || secondPlayer.Cards.Count < 5)
- {
- var cardInput = Console.ReadLine().Split();
- try
- {
- var card = new Card(cardInput[0], cardInput[cardInput.Length - 1]);
- if (!deck.Contains(card))
- {
- if (firstPlayer.Cards.Count < 5)
- {
- firstPlayer.AddCard(card);
- }
- else
- {
- secondPlayer.AddCard(card);
- }
- deck.Add(card);
- }
- else
- {
- Console.WriteLine("Card is not in the deck.");
- }
- }
- catch (Exception)
- {
- Console.WriteLine("No such card exists.");
- }
- }
- var winner = firstPlayer.StrongestCard.CompareTo(secondPlayer.StrongestCard);
- if (winner > 0)
- {
- Console.WriteLine($"{firstPlayer.Name} wins with {firstPlayer.StrongestCard}.");
- }
- else
- {
- Console.WriteLine($"{secondPlayer.Name} wins with {secondPlayer.StrongestCard}");
- }
- }
- }
- }
- using System.Collections.Generic;
- using System.Linq;
- namespace CardsGame_EXER
- {
- public class Player
- {
- private string name;
- private List<Card> cards;
- private Card strongestCard;
- public Player(string name)
- {
- this.name = name;
- this.cards = new List<Card>();
- this.strongestCard = new Card("Two", "Clubs");
- }
- public string Name => this.name;
- public Card StrongestCard => this.strongestCard;
- public List<Card> Cards
- {
- get { return this.cards; }
- }
- public void AddCard(Card card)
- {
- this.cards.Add(card);
- }
- public void GetStrongesttCard()
- {
- this.strongestCard = this.cards.OrderByDescending(c => c).First();
- }
- }
- }
- using System;
- namespace CardsGame_EXER
- {
- public class Card : IComparable<Card>, IEquatable<Card>
- {
- private CardRank rank;
- private CardSuit suit;
- public Card(string rank, string suit)
- {
- this.rank = (CardRank)Enum.Parse(typeof(CardRank), rank);
- this.suit = (CardSuit)Enum.Parse(typeof(CardSuit), suit);
- }
- public CardRank Rank => this.rank;
- public CardSuit Suit => this.suit;
- public int Power => (int)this.rank + (int)this.suit;
- public int CompareTo(Card other)
- {
- if (ReferenceEquals(this, other)) return 0;
- if (ReferenceEquals(null, other)) return 1;
- var result = this.suit.CompareTo(other.suit);
- if (result != 0)
- {
- return result;
- }
- return this.rank.CompareTo(other.rank);
- }
- public bool Equals(Card other)
- {
- return this.Power.Equals(other.Power);
- }
- public override string ToString()
- {
- return $"{this.rank} of {this.suit}";
- }
- }
- }
- namespace CardsGame_EXER
- {
- [Type("Enumeration", "Rank", "Provides rank constants for a Card class.")]
- public enum CardRank
- {
- Two = 2,
- Three,
- Four,
- Five,
- Six,
- Seven,
- Eight,
- Nine,
- Ten,
- Jack,
- Queen,
- King,
- Ace
- }
- }
- namespace CardsGame_EXER
- {
- [Type("Enumeration", "Suit", "Provides suit constants for a Card class.")]
- public enum CardSuit
- {
- Clubs,
- Diamonds = 13,
- Hearts = 26,
- Spades = 39
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement