-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Homework Added
- Loading branch information
Showing
30 changed files
with
1,255 additions
and
0 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
High-Quality-Code/12. Test-Driven Development/Demo/Card.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Poker | ||
{ | ||
public class Card : ICard, IComparable<Card> | ||
{ | ||
public CardFace Face { get; private set; } | ||
public CardSuit Suit { get; private set; } | ||
|
||
|
||
public Card(CardFace face, CardSuit suit) | ||
{ | ||
this.Face = face; | ||
this.Suit = suit; | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
string cardFace = ConvertCardFaceToString(); | ||
char cardSuit = ConvertCardSuitToChar(); | ||
|
||
string completeCard = cardFace + cardSuit; | ||
|
||
return completeCard; | ||
} | ||
|
||
private string ConvertCardFaceToString() | ||
{ | ||
string cardFace; | ||
|
||
if ((int)this.Face <= 10) | ||
{ | ||
cardFace = ((int)this.Face).ToString(); | ||
} | ||
else | ||
{ | ||
cardFace = (this.Face.ToString()[0]).ToString(); | ||
} | ||
|
||
return cardFace; | ||
} | ||
|
||
private char ConvertCardSuitToChar() | ||
{ | ||
char cardSuit; | ||
|
||
switch (this.Suit) | ||
{ | ||
case CardSuit.Clubs: | ||
cardSuit = '\u2663'; | ||
break; | ||
case CardSuit.Diamonds: | ||
cardSuit = '\u2666'; | ||
break; | ||
case CardSuit.Hearts: | ||
cardSuit = '\u2665'; | ||
break; | ||
case CardSuit.Spades: | ||
cardSuit = '\u2660'; | ||
break; | ||
default: | ||
throw new InvalidOperationException("Invalid suit " + this.Suit); | ||
} | ||
return cardSuit; | ||
} | ||
|
||
public override bool Equals(object obj) | ||
{ | ||
Card otherCard = obj as Card; | ||
|
||
if (otherCard == null) | ||
{ | ||
return false; | ||
} | ||
|
||
return this.Face == otherCard.Face && this.Suit == otherCard.Suit; | ||
} | ||
|
||
public override int GetHashCode() | ||
{ | ||
return ((int)this.Face * Enum.GetNames(typeof(CardSuit)).Length + (int)this.Suit); | ||
} | ||
|
||
public int CompareTo(Card otherCard) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
High-Quality-Code/12. Test-Driven Development/Demo/CardFace.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace Poker | ||
{ | ||
public enum CardFace | ||
{ | ||
Two = 2, | ||
Three = 3, | ||
Four = 4, | ||
Five = 5, | ||
Six = 6, | ||
Seven = 7, | ||
Eight = 8, | ||
Nine = 9, | ||
Ten = 10, | ||
Jack = 11, | ||
Queen = 12, | ||
King = 13, | ||
Ace = 14 | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
High-Quality-Code/12. Test-Driven Development/Demo/CardSuit.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace Poker | ||
{ | ||
public enum CardSuit | ||
{ | ||
Clubs = 1, // ♣ | ||
Diamonds = 2, // ♦ | ||
Hearts = 3, // ♥ | ||
Spades = 4 // ♠ | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
High-Quality-Code/12. Test-Driven Development/Demo/CheckCards.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Poker | ||
{ | ||
public class CheckCards | ||
{ | ||
public bool AreSameSuit(IHand pokerHand) | ||
{ | ||
return pokerHand.Cards.GroupBy(card => card.Suit).Count() == 1; | ||
} | ||
|
||
public int FaceRepetitionsCount(IHand pokerHand) | ||
{ | ||
return pokerHand.Cards.GroupBy(card => card.Face).Select(group => group.Count()).Max(); | ||
} | ||
|
||
public int NumberOfPairs(IHand pokerHand) | ||
{ | ||
return pokerHand.Cards.GroupBy(card => card.Face).Select(group => group.Count()).Where(rep => rep == 2).Count(); | ||
} | ||
|
||
public bool AreConsecutiveCards(IHand pokerHand) | ||
{ | ||
var uniqueFaceValues = pokerHand.Cards.Select(card => card.Face).Distinct(); | ||
|
||
bool areConsecutive = ((uniqueFaceValues.Max() - uniqueFaceValues.Min()) == 4); | ||
|
||
if (!areConsecutive) | ||
{ | ||
bool isHighestFive = (uniqueFaceValues.Take(4).Max() == CardFace.Five); | ||
bool isLowestAce = (uniqueFaceValues.Max() == CardFace.Ace); | ||
|
||
areConsecutive = isHighestFive && isLowestAce; | ||
} | ||
|
||
return areConsecutive; | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
High-Quality-Code/12. Test-Driven Development/Demo/Hand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Poker | ||
{ | ||
public class Hand : IHand | ||
{ | ||
public IList<ICard> Cards { get; private set; } | ||
|
||
public Hand(IList<ICard> cards) | ||
{ | ||
this.Cards = cards; | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
StringBuilder hand = new StringBuilder(); | ||
foreach (var card in this.Cards) | ||
{ | ||
hand.Append(card.ToString()); | ||
} | ||
|
||
return hand.ToString(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Poker | ||
{ | ||
public interface ICard | ||
{ | ||
CardFace Face { get; } | ||
CardSuit Suit { get; } | ||
string ToString(); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
High-Quality-Code/12. Test-Driven Development/Demo/IHand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Poker | ||
{ | ||
public interface IHand | ||
{ | ||
IList<ICard> Cards { get; } | ||
string ToString(); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
High-Quality-Code/12. Test-Driven Development/Demo/IPokerHandsChecker.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
namespace Poker | ||
{ | ||
/// <summary> | ||
/// Interface for checking poker hands as defined in http://en.wikipedia.org/wiki/List_of_poker_hands. | ||
/// </summary> | ||
public interface IPokerHandsChecker | ||
{ | ||
bool IsValidHand(IHand hand); | ||
bool IsStraightFlush(IHand hand); | ||
bool IsFourOfAKind(IHand hand); | ||
bool IsFullHouse(IHand hand); | ||
bool IsFlush(IHand hand); | ||
bool IsStraight(IHand hand); | ||
bool IsThreeOfAKind(IHand hand); | ||
bool IsTwoPair(IHand hand); | ||
bool IsOnePair(IHand hand); | ||
bool IsHighCard(IHand hand); | ||
int CompareHands(IHand firstHand, IHand secondHand); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
High-Quality-Code/12. Test-Driven Development/Demo/Poker.Tests/CardTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System; | ||
|
||
namespace Poker.Tests | ||
{ | ||
[TestClass] | ||
public class CardTests | ||
{ | ||
[TestMethod] | ||
public void TestAceOfSpades() | ||
{ | ||
string expectedCard = "A♠"; | ||
Card aceOfSpades = new Card(CardFace.Ace, CardSuit.Spades); | ||
|
||
Assert.AreEqual(expectedCard, aceOfSpades.ToString()); | ||
} | ||
|
||
[TestMethod] | ||
public void TestKingOfHearts() | ||
{ | ||
string expectedCard = "K♥"; | ||
Card kingOfHearts = new Card(CardFace.King, CardSuit.Hearts); | ||
|
||
Assert.AreEqual(expectedCard, kingOfHearts.ToString()); | ||
} | ||
|
||
[TestMethod] | ||
public void TestTenOfDiamonds() | ||
{ | ||
string expectedCard = "10♦"; | ||
Card tenOfDiamonds = new Card(CardFace.Ten, CardSuit.Diamonds); | ||
|
||
Assert.AreEqual(expectedCard, tenOfDiamonds.ToString()); | ||
} | ||
|
||
[TestMethod] | ||
public void TestSevenOfClubs() | ||
{ | ||
string expectedCard = "7♣"; | ||
Card sevenOfClubs = new Card(CardFace.Seven, CardSuit.Clubs); | ||
|
||
Assert.AreEqual(expectedCard, sevenOfClubs.ToString()); | ||
} | ||
|
||
[TestMethod] | ||
public void TestNineOfHearts() | ||
{ | ||
string expectedCard = "9♥"; | ||
Card nineOfHearts = new Card(CardFace.Nine, CardSuit.Hearts); | ||
|
||
Assert.AreEqual(expectedCard, nineOfHearts.ToString()); | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
High-Quality-Code/12. Test-Driven Development/Demo/Poker.Tests/HandTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Poker.Tests | ||
{ | ||
[TestClass] | ||
public class HandTests | ||
{ | ||
[TestMethod] | ||
public void TestHandToString_First() | ||
{ | ||
string expectedHand = "K♣J♥10♠6♥2♠"; | ||
|
||
Card firstCard = new Card(CardFace.King, CardSuit.Clubs); | ||
Card secondCard = new Card(CardFace.Jack, CardSuit.Hearts); | ||
Card thirdCard = new Card(CardFace.Ten, CardSuit.Spades); | ||
Card fourthCard = new Card(CardFace.Six, CardSuit.Hearts); | ||
Card fifthCard = new Card(CardFace.Two, CardSuit.Spades); | ||
|
||
Hand sampleHand = new Hand(new Card[] { firstCard, secondCard, thirdCard, fourthCard, fifthCard }); | ||
|
||
Assert.AreEqual(expectedHand, sampleHand.ToString()); | ||
} | ||
|
||
[TestMethod] | ||
public void TestHandToString_Second() | ||
{ | ||
string expectedHand = "4♣Q♥A♠9♦6♠"; | ||
|
||
Card firstCard = new Card(CardFace.Four, CardSuit.Clubs); | ||
Card secondCard = new Card(CardFace.Queen, CardSuit.Hearts); | ||
Card thirdCard = new Card(CardFace.Ace, CardSuit.Spades); | ||
Card fourthCard = new Card(CardFace.Nine, CardSuit.Diamonds); | ||
Card fifthCard = new Card(CardFace.Six, CardSuit.Spades); | ||
|
||
Hand sampleHand = new Hand(new Card[] { firstCard, secondCard, thirdCard, fourthCard, fifthCard }); | ||
|
||
Assert.AreEqual(expectedHand, sampleHand.ToString()); | ||
} | ||
} | ||
} |
Oops, something went wrong.