Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 90f4b04

Browse filesBrowse files
committed
ch14 revisions
1 parent b98cbcd commit 90f4b04
Copy full SHA for 90f4b04

File tree

Expand file treeCollapse file tree

4 files changed

+39
-53
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+39
-53
lines changed
Open diff view settings
Collapse file

‎Checkstyle.xml‎

Copy file name to clipboardExpand all lines: Checkstyle.xml
-4Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ Checkstyle configuration for Think Java, 2nd Edition.
4343
<property name="validateOnlyOverlapping" value="false"/>
4444
-->
4545
</module>
46-
<module name="ReturnCount"/>
4746
<module name="SimplifyBooleanExpression"/>
4847
<module name="SimplifyBooleanReturn"/>
4948
<module name="StringLiteralEquality"/>
@@ -62,9 +61,6 @@ Checkstyle configuration for Think Java, 2nd Edition.
6261

6362
<!-- See http://checkstyle.sf.net/config_javadoc.html -->
6463
<module name="AtclauseOrder"/>
65-
<!--
66-
<module name="JavadocMethod"/>
67-
-->
6864
<module name="JavadocStyle"/>
6965
<module name="JavadocType"/>
7066
<module name="NonEmptyAtclauseDescription"/>
Collapse file

‎ch14/CardCollection.java‎

Copy file name to clipboardExpand all lines: ch14/CardCollection.java
+30-30Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ public String getLabel() {
2424
return label;
2525
}
2626

27+
/**
28+
* Returns a string representation of the card collection.
29+
*/
30+
public String toString() {
31+
return label + ": " + cards.toString();
32+
}
33+
2734
/**
2835
* Adds the given card to the collection.
2936
*/
@@ -42,40 +49,22 @@ public Card popCard(int i) {
4249
* Removes and returns the last card.
4350
*/
4451
public Card popCard() {
45-
int i = size() - 1;
52+
int i = cards.size() - 1; // from the end of the list
4653
return popCard(i);
4754
}
4855

49-
/**
50-
* Returns the number of cards.
51-
*/
52-
public int size() {
53-
return cards.size();
54-
}
55-
5656
/**
5757
* True if the collection is empty, false otherwise.
5858
*/
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-
}
59+
public boolean isEmpty() {
60+
return cards.isEmpty();
7161
}
7262

7363
/**
74-
* Moves all remaining cards to the given collection.
64+
* Returns the number of cards.
7565
*/
76-
public void dealAll(CardCollection that) {
77-
int n = size();
78-
deal(that, n);
66+
public int size() {
67+
return cards.size();
7968
}
8069

8170
/**
@@ -89,7 +78,7 @@ public Card getCard(int i) {
8978
* Returns the last card.
9079
*/
9180
public Card lastCard() {
92-
int i = size() - 1;
81+
int i = cards.size() - 1;
9382
return cards.get(i);
9483
}
9584

@@ -107,17 +96,28 @@ public void swapCards(int i, int j) {
10796
*/
10897
public void shuffle() {
10998
Random random = new Random();
110-
for (int i = size() - 1; i > 0; i--) {
111-
int j = random.nextInt(i);
99+
for (int i = cards.size() - 1; i > 0; i--) {
100+
int j = random.nextInt(i + 1);
112101
swapCards(i, j);
113102
}
114103
}
115104

116105
/**
117-
* Returns a string representation of the card collection.
106+
* Moves n cards from this collection to the given collection.
118107
*/
119-
public String toString() {
120-
return label + ": " + cards.toString();
108+
public void deal(CardCollection that, int n) {
109+
for (int i = 0; i < n; i++) {
110+
Card card = popCard();
111+
that.addCard(card);
112+
}
113+
}
114+
115+
/**
116+
* Moves all remaining cards to the given collection.
117+
*/
118+
public void dealAll(CardCollection that) {
119+
int n = cards.size();
120+
deal(that, n);
121121
}
122122

123123
}
Collapse file

‎ch14/Eights.java‎

Copy file name to clipboardExpand all lines: ch14/Eights.java
+6-9Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import java.util.Scanner;
22

33
/**
4-
* Simulates a game of Crazy Eights. See
5-
* https://en.wikipedia.org/wiki/Crazy_Eights
6-
* for basic play and scoring rules.
4+
* Simulates a game of Crazy Eights.
5+
* See https://en.wikipedia.org/wiki/Crazy_Eights.
76
*/
87
public class Eights {
98

@@ -21,12 +20,10 @@ public Eights() {
2120
deck.shuffle();
2221

2322
// deal cards to each player
24-
int handSize = 5;
2523
one = new Player("Allen");
26-
deck.deal(one.getHand(), handSize);
27-
24+
deck.deal(one.getHand(), 5);
2825
two = new Player("Chris");
29-
deck.deal(two.getHand(), handSize);
26+
deck.deal(two.getHand(), 5);
3027

3128
// turn one card face up
3229
discardPile = new Hand("Discards");
@@ -44,7 +41,7 @@ public Eights() {
4441
* Returns true if either hand is empty.
4542
*/
4643
public boolean isDone() {
47-
return one.getHand().empty() || two.getHand().empty();
44+
return one.getHand().isEmpty() || two.getHand().isEmpty();
4845
}
4946

5047
/**
@@ -68,7 +65,7 @@ public void reshuffle() {
6865
* Returns a card from the draw pile.
6966
*/
7067
public Card drawCard() {
71-
if (drawPile.empty()) {
68+
if (drawPile.isEmpty()) {
7269
reshuffle();
7370
}
7471
return drawPile.popCard();
Collapse file

‎ch14/Player.java‎

Copy file name to clipboardExpand all lines: ch14/Player.java
+3-10Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,9 @@ public Card drawForMatch(Eights eights, Card prev) {
7070
* Checks whether two cards match.
7171
*/
7272
public static boolean cardMatches(Card card1, Card card2) {
73-
if (card1.getSuit() == card2.getSuit()) {
74-
return true;
75-
}
76-
if (card1.getRank() == card2.getRank()) {
77-
return true;
78-
}
79-
if (card1.getRank() == 8) {
80-
return true;
81-
}
82-
return false;
73+
return card1.getSuit() == card2.getSuit()
74+
|| card1.getRank() == card2.getRank()
75+
|| card1.getRank() == 8;
8376
}
8477

8578
/**

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.