Skip to content

Commit dbf0613

Browse files
committed
Added test for Exercise 12.3
1 parent c215058 commit dbf0613

3 files changed

Lines changed: 154 additions & 0 deletions

File tree

ch12/Card.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public int compareTo(Card that) {
3535
return 1;
3636
}
3737
if (this.rank < that.rank) {
38+
if (this.rank == 1) // Exercise 12.2
39+
{
40+
return 1;
41+
}
3842
return -1;
3943
}
4044
if (this.rank > that.rank) {

ch12/Deck.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
public class Deck {
2+
3+
public static Card[] makeDeck()
4+
{
5+
Card[] newCardDeck = new Card[52];
6+
int index = 0;
7+
for (int suit = 0; suit < 4; suit ++)
8+
{
9+
for (int rank = 1; rank < 14; rank++)
10+
{
11+
newCardDeck[index++] = new Card(rank, suit);
12+
}
13+
}
14+
return newCardDeck;
15+
}
16+
17+
public static int[] suitHist(Card[] hand)
18+
{
19+
int[] suitHistogram = new int[4];
20+
for (Card card: hand)
21+
{
22+
suitHistogram[card.getSuit()]++;
23+
}
24+
return suitHistogram;
25+
}
26+
27+
public static boolean hasFlush(Card[] hand)
28+
{
29+
int[] suitHistogram = suitHist(hand);
30+
for (int count : suitHistogram)
31+
{
32+
if (count >= 5) {
33+
return true;
34+
}
35+
}
36+
return false;
37+
}
38+
39+
public static boolean hasRoyal(Card[] hand)
40+
{
41+
int[] suitHistogram = suitHist(hand);
42+
for (int suit = 0; suit < 4; suit++)
43+
{
44+
int count = suitHistogram[suit];
45+
if (count >= 5) {
46+
int numRoyalCards = 0;
47+
for (Card card: hand)
48+
{
49+
if (card.getSuit() == suit
50+
&& (card.getRank() >= 10 || card.getRank() == 1))
51+
{
52+
numRoyalCards++;
53+
}
54+
}
55+
if (numRoyalCards >= 5)
56+
{
57+
return true;
58+
}
59+
}
60+
}
61+
return false;
62+
}
63+
64+
public static void main(String[] args)
65+
{
66+
Card[] deck = makeDeck();
67+
}
68+
}

ch12/PokerHandTest.java

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import junit.framework.TestCase;
2+
import org.junit.Test;
3+
4+
/**
5+
* Class to test Exercise 12.3 Poker Hand
6+
*/
7+
public class PokerHandTest extends TestCase
8+
{
9+
public void testPokerHand()
10+
{
11+
Card[] hand = new Card[9];
12+
13+
// No flush, no royal flush
14+
hand[0] = new Card(1, 0);
15+
hand[1] = new Card(2, 1);
16+
hand[2] = new Card(3,2);
17+
hand[3] = new Card(4, 3);
18+
19+
hand[4] = new Card(5, 0);
20+
hand[5] = new Card(6, 1);
21+
hand[6] = new Card(7,2);
22+
hand[7] = new Card(8, 3);
23+
24+
hand[8] = new Card(9, 0);
25+
26+
int[] suitHistogram = Deck.suitHist(hand);
27+
28+
int[] expectedResult = {3, 2, 2, 2};
29+
for(int i = 0; i < 4; i++) {
30+
assertEquals(expectedResult[i], suitHistogram[i]);
31+
}
32+
33+
System.out.println("Suit histogram test passes!");
34+
35+
boolean hasFlush = Deck.hasFlush(hand);
36+
assertFalse(hasFlush);
37+
38+
System.out.println("No flush test passes!");
39+
40+
// Flush
41+
hand[0] = new Card(1, 1);
42+
hand[1] = new Card(2, 1);
43+
hand[2] = new Card(3,1);
44+
hand[3] = new Card(4, 3);
45+
46+
hand[4] = new Card(5, 0);
47+
hand[5] = new Card(6, 1);
48+
hand[6] = new Card(7,1);
49+
hand[7] = new Card(8, 3);
50+
51+
hand[8] = new Card(9, 0);
52+
53+
hasFlush = Deck.hasFlush(hand);
54+
assertTrue(hasFlush);
55+
56+
System.out.println("Has flush test passes!");
57+
58+
boolean hasRoyal = Deck.hasRoyal(hand);
59+
assertFalse(hasRoyal);
60+
61+
System.out.println("No royal flush test passes!");
62+
63+
// Royal Flush
64+
hand[0] = new Card(1, 1);
65+
hand[1] = new Card(13, 1);
66+
hand[2] = new Card(12,1);
67+
hand[3] = new Card(11, 1);
68+
69+
hand[4] = new Card(10, 1);
70+
hand[5] = new Card(6, 1);
71+
hand[6] = new Card(7,1);
72+
hand[7] = new Card(8, 3);
73+
74+
hand[8] = new Card(9, 0);
75+
76+
hasRoyal = Deck.hasRoyal(hand);
77+
assertTrue(hasRoyal);
78+
79+
System.out.println("Has royal flush test passes!");
80+
81+
}
82+
}

0 commit comments

Comments
 (0)