-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCardC.java
More file actions
82 lines (71 loc) · 2.18 KB
/
Copy pathCardC.java
File metadata and controls
82 lines (71 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* Think Java v.6 - Exercise 12.3
*
* In Poker a "flush" is a hand that contains five or more cards of the same
* suit. A hand can contain any number of cards.
*
* (1) Write a method called suitHist that takes an array of cards as a
* parameter and that returns a histogram of the suits in the hand. Your
* solution should only traverse the array once.
*
* (2) Write a method called hasFlush that takes an array of cards as a
* parameter and returns true if the hand contains a flush (and false otherwise)
*
* @author Unai de la O
*/
import java.util.Arrays;
public class CardC {
public static final String[] RANKS = {
null, "Ace", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "Jack", "Queen", "King"};
public static final String[] SUITS = {
"Clubs", "Diamonds", "Hearts", "Spades"};
private final int rank;
private final int suit;
/**
* Constructs a card of the given rank and suit.
*/
public CardC(int rank, int suit) {
this.rank = rank;
this.suit = suit;
}
/**
* Returns a string representation of the card.
*/
public String toString() {
return RANKS[this.rank] + " of " + SUITS[this.suit];
}
/**
* Returns a suits histogram of a hand.
*/
public static int[] suitHist(CardC[] cards) {
int[] histogram = new int[4];
for (int i = 0; i < cards.length; i++) {
histogram[cards[i].suit]++;
}
return histogram;
}
/**
* Returns true if a flush exists; returns false if not.
*/
public static boolean hasFlush(CardC[] cards) {
for (int i = 0; i < 4; i++) {
if (suitHist(cards)[i] >= 5)
return true;
}
return false;
}
//MAIN
public static void main(String[] args) {
CardC[] manoPoker = new CardC[5];
manoPoker[0] = new CardC(3, 2);
manoPoker[1] = new CardC(11, 1);
manoPoker[2] = new CardC(9, 3);
manoPoker[3] = new CardC(5, 1);
manoPoker[4] = new CardC(7, 1);
int[] histogramaPalos = suitHist(manoPoker);
System.out.println(Arrays.toString(histogramaPalos));
boolean esColor = hasFlush(manoPoker);
System.out.println(esColor);
}
}