playing-card/playing-card-operators/PlayingCard.cs - The class PlayingCard with relational operators. | Lecture 6 - slide 4 : 20 Program 4 |
using System; public enum CardSuite { Spades, Hearts, Clubs, Diamonds }; public enum CardValue { Ace = 1, Two = 2, Three = 3, Four = 4, Five = 5, Six = 6, Seven = 7, Eight = 8, Nine = 9, Ten = 10, Jack = 11, Queen = 12, King = 13}; public class Card{ private CardSuite suite; private CardValue value; public Card(CardSuite suite, CardValue value){ this.suite = suite; this.value = value; } public Card(CardSuite suite, int value){ this.suite = suite; this.value = (CardValue)value; } public Card(int suite, int value){ this.suite = (CardSuite)suite; this.value = (CardValue)value; } public CardSuite Suite{ get { return this.suite; } } public CardValue Value{ get { return this.value; } } public System.Drawing.Color Color{ get{ System.Drawing.Color result; if (suite == CardSuite.Spades || suite == CardSuite.Clubs) result = System.Drawing.Color.Black; else result = System.Drawing.Color.Red; return result; } } public override String ToString(){ return String.Format("Suite:{0}, Value:{1}, Color:{2}", suite, value, Color.ToString()); } public override bool Equals(Object other){ return (this.suite == ((Card)other).suite) && (this.value == ((Card)other).value); } public override int GetHashCode(){ return (int)suite ^ (int)value; } public static bool operator ==(Card c1, Card c2){ return c1.Equals(c2); } public static bool operator !=(Card c1, Card c2){ return !(c1.Equals(c2)); } public static bool operator <(Card c1, Card c2){ bool res; if (c1.suite < c2.suite) res = true; else if (c1.suite == c2.suite) res = (c1.value < c2.value); else res = false; return res; } public static bool operator >(Card c1, Card c2){ return !(c1 < c2) && !(c1 == c2); } }