Skip to content

Commit 4d64a44

Browse files
authored
ch14
1 parent 73d70a7 commit 4d64a44

8 files changed

Lines changed: 624 additions & 450 deletions

File tree

ch14/Card.java

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,91 @@
1-
../ch12/Card.java
1+
/**
2+
* A standard playing card.
3+
*/
4+
public class Card {
5+
6+
public static final String[] RANKS = {
7+
null, "Ace", "2", "3", "4", "5", "6", "7",
8+
"8", "9", "10", "Jack", "Queen", "King"};
9+
10+
public static final String[] SUITS = {
11+
"Clubs", "Diamonds", "Hearts", "Spades"};
12+
13+
private final int rank;
14+
15+
private final int suit;
16+
17+
/**
18+
* Constructs a card of the given rank and suit.
19+
*/
20+
public Card(int rank, int suit) {
21+
this.rank = rank;
22+
this.suit = suit;
23+
}
24+
25+
/**
26+
* Returns a negative integer if this card comes before
27+
* the given card, zero if the two cards are equal, or
28+
* a positive integer if this card comes after the card.
29+
*/
30+
public int compareTo(Card that) {
31+
if (this.suit < that.suit) {
32+
return -1;
33+
}
34+
if (this.suit > that.suit) {
35+
return 1;
36+
}
37+
//in some games, Aces are ranked higher than Kings
38+
if (this.rank == 1 && that.rank == 1) {
39+
return 0;
40+
} else if (this.rank == 1 && that.rank != 1) {
41+
return 1;
42+
} else if (this.rank != 1 && that.rank == 1) {
43+
return -1;
44+
}
45+
if (this.rank < that.rank) {
46+
return -1;
47+
}
48+
if (this.rank > that.rank) {
49+
return 1;
50+
}
51+
return 0;
52+
}
53+
54+
/**
55+
* Returns true if the given card has the same
56+
* rank AND same suit; otherwise returns false.
57+
*/
58+
public boolean equals(Card that) {
59+
return this.rank == that.rank
60+
&& this.suit == that.suit;
61+
}
62+
63+
/**
64+
* Gets the card's rank.
65+
*/
66+
public int getRank() {
67+
return this.rank;
68+
}
69+
70+
/**
71+
* Gets the card's suit.
72+
*/
73+
public int getSuit() {
74+
return this.suit;
75+
}
76+
77+
/**
78+
* Returns the card's index in a sorted deck of 52 cards.
79+
*/
80+
public int position() {
81+
return this.suit * 13 + this.rank - 1;
82+
}
83+
84+
/**
85+
* Returns a string representation of the card.
86+
*/
87+
public String toString() {
88+
return RANKS[this.rank] + " of " + SUITS[this.suit];
89+
}
90+
91+
}

ch14/CardCollection.java

Lines changed: 123 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,123 @@
1-
import java.util.ArrayList;
2-
import java.util.Random;
3-
4-
/**
5-
* A collection of playing cards.
6-
*/
7-
public class CardCollection {
8-
9-
private String label;
10-
private ArrayList<Card> cards;
11-
12-
/**
13-
* Constructs an empty collection.
14-
*/
15-
public CardCollection(String label) {
16-
this.label = label;
17-
this.cards = new ArrayList<Card>();
18-
}
19-
20-
/**
21-
* Returns the label of the card collection.
22-
*/
23-
public String getLabel() {
24-
return label;
25-
}
26-
27-
/**
28-
* Adds the given card to the collection.
29-
*/
30-
public void addCard(Card card) {
31-
cards.add(card);
32-
}
33-
34-
/**
35-
* Removes and returns the card with the given index.
36-
*/
37-
public Card popCard(int i) {
38-
return cards.remove(i);
39-
}
40-
41-
/**
42-
* Removes and returns the last card.
43-
*/
44-
public Card popCard() {
45-
int i = size() - 1;
46-
return popCard(i);
47-
}
48-
49-
/**
50-
* Returns the number of cards.
51-
*/
52-
public int size() {
53-
return cards.size();
54-
}
55-
56-
/**
57-
* True if the collection is empty, false otherwise.
58-
*/
59-
public boolean empty() {
60-
return cards.size() == 0;
61-
}
62-
63-
/**
64-
* Moves n cards from this collection to the given collection.
65-
*/
66-
public void deal(CardCollection that, int n) {
67-
for (int i = 0; i < n; i++) {
68-
Card card = popCard();
69-
that.addCard(card);
70-
}
71-
}
72-
73-
/**
74-
* Moves all remaining cards to the given collection.
75-
*/
76-
public void dealAll(CardCollection that) {
77-
int n = size();
78-
deal(that, n);
79-
}
80-
81-
/**
82-
* Returns the card with the given index.
83-
*/
84-
public Card getCard(int i) {
85-
return cards.get(i);
86-
}
87-
88-
/**
89-
* Returns the last card.
90-
*/
91-
public Card last() {
92-
int i = size() - 1;
93-
return cards.get(i);
94-
}
95-
96-
/**
97-
* Swaps the cards at indexes i and j.
98-
*/
99-
public void swapCards(int i, int j) {
100-
Card temp = cards.get(i);
101-
cards.set(i, cards.get(j));
102-
cards.set(j, temp);
103-
}
104-
105-
/**
106-
* Randomly permute the cards.
107-
*/
108-
public void shuffle() {
109-
Random random = new Random();
110-
for (int i = size() - 1; i > 0; i--) {
111-
int j = random.nextInt(i);
112-
swapCards(i, j);
113-
}
114-
}
115-
116-
/**
117-
* Returns a string representation of the card collection.
118-
*/
119-
public String toString() {
120-
return label + ": " + cards.toString();
121-
}
122-
123-
}
1+
import java.util.ArrayList;
2+
import java.util.Random;
3+
4+
/**
5+
* A collection of playing cards.
6+
*/
7+
public class CardCollection {
8+
9+
private String label;
10+
private ArrayList<Card> cards;
11+
12+
/**
13+
* Constructs an empty collection.
14+
*/
15+
public CardCollection(String label) {
16+
this.label = label;
17+
this.cards = new ArrayList<Card>();
18+
}
19+
20+
/**
21+
* Returns the label of the card collection.
22+
*/
23+
public String getLabel() {
24+
return label;
25+
}
26+
27+
/**
28+
* Adds the given card to the collection.
29+
*/
30+
public void addCard(Card card) {
31+
cards.add(card);
32+
}
33+
34+
/**
35+
* Removes and returns the card with the given index.
36+
*/
37+
public Card popCard(int i) {
38+
return cards.remove(i);
39+
}
40+
41+
/**
42+
* Removes and returns the last card.
43+
*/
44+
public Card popCard() {
45+
int i = size() - 1;
46+
return popCard(i);
47+
}
48+
49+
/**
50+
* Returns the number of cards.
51+
*/
52+
public int size() {
53+
return cards.size();
54+
}
55+
56+
/**
57+
* True if the collection is empty, false otherwise.
58+
*/
59+
public boolean empty() {
60+
return cards.size() == 0;
61+
}
62+
63+
/**
64+
* Moves n cards from this collection to the given collection.
65+
*/
66+
public void deal(CardCollection that, int n) {
67+
for (int i = 0; i < n; i++) {
68+
Card card = popCard();
69+
that.addCard(card);
70+
}
71+
}
72+
73+
/**
74+
* Moves all remaining cards to the given collection.
75+
*/
76+
public void dealAll(CardCollection that) {
77+
int n = size();
78+
deal(that, n);
79+
}
80+
81+
/**
82+
* Returns the card with the given index.
83+
*/
84+
public Card getCard(int i) {
85+
return cards.get(i);
86+
}
87+
88+
/**
89+
* Returns the last card.
90+
*/
91+
public Card last() {
92+
int i = size() - 1;
93+
return cards.get(i);
94+
}
95+
96+
/**
97+
* Swaps the cards at indexes i and j.
98+
*/
99+
public void swapCards(int i, int j) {
100+
Card temp = cards.get(i);
101+
cards.set(i, cards.get(j));
102+
cards.set(j, temp);
103+
}
104+
105+
/**
106+
* Randomly permute the cards.
107+
*/
108+
public void shuffle() {
109+
Random random = new Random();
110+
for (int i = size() - 1; i > 0; i--) {
111+
int j = random.nextInt(i);
112+
swapCards(i, j);
113+
}
114+
}
115+
116+
/**
117+
* Returns a string representation of the card collection.
118+
*/
119+
public String toString() {
120+
return label + ": " + cards.toString();
121+
}
122+
123+
}

ch14/Deck.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
/**
2-
* A deck of playing cards.
3-
*/
4-
public class Deck extends CardCollection {
5-
6-
/**
7-
* Constructs a standard deck of 52 cards.
8-
*/
9-
public Deck(String label) {
10-
super(label);
11-
12-
for (int suit = 0; suit <= 3; suit++) {
13-
for (int rank = 1; rank <= 13; rank++) {
14-
addCard(new Card(rank, suit));
15-
}
16-
}
17-
}
18-
19-
}
1+
/**
2+
* A deck of playing cards.
3+
*/
4+
public class Deck extends CardCollection {
5+
6+
/**
7+
* Constructs a standard deck of 52 cards.
8+
*/
9+
public Deck(String label) {
10+
super(label);
11+
12+
for (int suit = 0; suit <= 3; suit++) {
13+
for (int rank = 1; rank <= 13; rank++) {
14+
addCard(new Card(rank, suit));
15+
}
16+
}
17+
}
18+
19+
}

0 commit comments

Comments
 (0)