Skip to content

Commit 368bacf

Browse files
committed
chapter 14
Completed through first two problems of chapter 14
1 parent 02c8171 commit 368bacf

File tree

6 files changed

+163
-15
lines changed

6 files changed

+163
-15
lines changed

ch13/.vscode/launch.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"configurations": [
3+
{
4+
"type": "java",
5+
"name": "CodeLens (Launch) - Deck",
6+
"request": "launch",
7+
"mainClass": "Deck"
8+
},
9+
{
10+
"type": "java",
11+
"name": "CodeLens (Launch) - Test",
12+
"request": "launch",
13+
"mainClass": "Test"
14+
}
15+
]
16+
}

ch13/Deck.java

Lines changed: 81 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import java.util.Arrays;
22
import java.util.Random;
33

4+
45
/**
56
* A deck of playing cards (of fixed size).
67
*/
78
public class Deck {
89

910
private Card[] cards;
11+
private Random random = new Random();
1012

1113
/**
1214
* Constructs a standard deck of 52 cards.
@@ -49,40 +51,71 @@ public void print() {
4951
* Returns a string representation of the deck.
5052
*/
5153
public String toString() {
52-
return Arrays.toString(this.cards);
54+
StringBuilder output = new StringBuilder();
55+
for (Card card : cards) {
56+
output.append(card.toString() + "\n");
57+
}
58+
return output.toString();
59+
//return Arrays.toString(this.cards);
5360
}
5461

5562
/**
5663
* Chooses a random number between low and high, including both.
5764
*/
5865
public int randomInt(int low, int high) {
59-
return 0;
66+
if (high < low) {
67+
int placeholder = low;
68+
low = high;
69+
high = placeholder;
70+
}
71+
int rand = random.nextInt(high - low + 1);
72+
rand += low;
73+
return rand;
6074
}
6175

6276
/**
6377
* Swaps the cards at indexes i and j.
6478
*/
6579
public void swapCards(int i, int j) {
80+
Card card1 = cards[i];
81+
cards[i] = cards[j];
82+
cards[j] = card1;
6683
}
6784

6885
/**
6986
* Randomly permutes the array of cards.
7087
*/
7188
public void shuffle() {
89+
for (int i = 0; i < cards.length; i++) {
90+
int j = randomInt(i, cards.length - 1);
91+
swapCards(i, j);
92+
}
7293
}
7394

7495
/**
7596
* Finds the index of the lowest card
7697
* between low and high inclusive.
7798
*/
7899
public int indexLowest(int low, int high) {
79-
return 0;
100+
int min = low;
101+
for (int i = low+1; i <= high; i++) {
102+
if (cards[i].compareTo(cards[min]) == -1) {
103+
min = i;
104+
}
105+
}
106+
return min;
80107
}
81108

82109
/**
83110
* Sorts the cards (in place) using selection sort.
84111
*/
85112
public void selectionSort() {
113+
int j;
114+
for (int i = 0; i < cards.length; i++) {
115+
if ((j = indexLowest(i, cards.length-1)) >i ) {
116+
swapCards(i, j);
117+
}
118+
}
86119
}
87120

88121
/**
@@ -100,20 +133,63 @@ public Deck subdeck(int low, int high) {
100133
* Combines two previously sorted subdecks.
101134
*/
102135
public static Deck merge(Deck d1, Deck d2) {
103-
return null;
136+
int fullDeck = d1.cards.length + d2.cards.length;
137+
Deck combine = new Deck(fullDeck);
138+
int i = 0;
139+
int j = 0;
140+
for (int k = 0; k < fullDeck; k++) {
141+
if (i >= d1.cards.length) {
142+
combine.cards[k] = d2.cards[j];
143+
j++;
144+
} else if (j >= d2.cards.length) {
145+
combine.cards[k] = d1.cards[i];
146+
i++;
147+
} else if (d1.cards[i].compareTo(d2.cards[j]) == 1) { // d2 < d1
148+
combine.cards[k] = d2.cards[j];
149+
j++;
150+
} else if (d1.cards[i].compareTo(d2.cards[j]) == -1) { // d1 < d2
151+
combine.cards[k] = d1.cards[i];
152+
i++;
153+
}
154+
else { // to avoid freezing i and j if the cards are somehow duplicates
155+
combine.cards[k] = d1.cards[i];
156+
i++;
157+
}
158+
}
159+
160+
return combine;
104161
}
105162

106163
/**
107164
* Returns a sorted copy of the deck using merge sort.
108165
*/
109166
public Deck mergeSort() {
110-
return this;
167+
if (cards.length <= 1) {
168+
return this;
169+
}
170+
Deck d1 = subdeck(0, cards.length / 2 - 1);
171+
Deck d2 = subdeck(cards.length / 2, cards.length - 1);
172+
d1 = d1.mergeSort();
173+
d2 = d2.mergeSort();
174+
return merge(d1, d2);
111175
}
112176

113177
/**
114178
* Reorders the cards (in place) using insertion sort.
115179
*/
116180
public void insertionSort() {
181+
for (int i = 1; i < cards.length; i++) {
182+
for (int k = i; k > 0; k--) {
183+
if (cards[k].compareTo(cards[k-1]) == -1) {
184+
swapCards(k, k-1);
185+
}
186+
}
187+
}
188+
}
189+
190+
public static void main(String[] args) {
191+
Deck deck = new Deck();
192+
System.out.println(deck);
117193
}
118194

119195
}

ch14/.vscode/launch.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"configurations": [
3+
{
4+
"type": "java",
5+
"name": "CodeLens (Launch) - Eights",
6+
"request": "launch",
7+
"mainClass": "Eights"
8+
}
9+
]
10+
}

ch14/Eights.java

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public Eights() {
2525
one = new Player("Allen");
2626
deck.deal(one.getHand(), handSize);
2727

28-
two = new Player("Chris");
28+
two = new SmartPlayer("Chris");
2929
deck.deal(two.getHand(), handSize);
3030

3131
// turn one card face up
@@ -111,8 +111,8 @@ public void takeTurn(Player player) {
111111
Card next = player.play(this, prev);
112112
discardPile.addCard(next);
113113

114-
System.out.println(player.getName() + " plays " + next);
115-
System.out.println();
114+
// System.out.println(player.getName() + " plays " + next);
115+
// System.out.println();
116116
}
117117

118118
/**
@@ -123,23 +123,34 @@ public void playGame() {
123123

124124
// keep playing until there's a winner
125125
while (!isDone()) {
126-
displayState();
127-
waitForUser();
126+
// displayState();
127+
//waitForUser();
128128
takeTurn(player);
129129
player = nextPlayer(player);
130130
}
131131

132132
// display the final score
133-
one.displayScore();
134-
two.displayScore();
133+
//one.displayScore();
134+
//two.displayScore();
135135
}
136136

137+
137138
/**
138139
* Creates the game and runs it.
139140
*/
140141
public static void main(String[] args) {
141-
Eights game = new Eights();
142-
game.playGame();
142+
int[] tally = new int[2];
143+
for (int i = 0; i < 100; i++) {
144+
Eights game = new Eights();
145+
game.playGame();
146+
if (game.one.score() > game.two.score()) {
147+
tally[0]++;
148+
} else if (game.two.score() > game.one.score()) {
149+
tally[1]++;
150+
}
151+
}
152+
System.out.printf("Allen with %d wins, Chris with %d\n",tally[0], tally[1]);
153+
143154
}
144155

145156
}

ch14/Player.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public Card searchForMatch(Card prev) {
5858
public Card drawForMatch(Eights eights, Card prev) {
5959
while (true) {
6060
Card card = eights.draw();
61-
System.out.println(name + " draws " + card);
61+
//System.out.println(name + " draws " + card);
6262
if (cardMatches(card, prev)) {
6363
return card;
6464
}

ch14/SmartPlayer.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
public class SmartPlayer extends Player {
2+
3+
4+
public SmartPlayer(String name) {
5+
super(name);
6+
}
7+
8+
public Card play(Eights eights, Card prev) {
9+
Card card = searchForBetterMatch(prev);
10+
if (card == null) {
11+
card = drawForMatch(eights, prev);
12+
}
13+
return card;
14+
}
15+
16+
public Card searchForBetterMatch(Card prev) {
17+
int bestMatch = -1;
18+
for (int i = 0; i < getHand().size(); i++) {
19+
Card card = getHand().getCard(i);
20+
if (card.getRank() == 8) {
21+
return getHand().popCard(i);
22+
}
23+
if (cardMatches(card, prev)) {
24+
if (bestMatch == -1 || card.getRank() > getHand().getCard(bestMatch).getRank()) {
25+
bestMatch = i;
26+
}
27+
}
28+
}
29+
if (bestMatch > -1) {
30+
return getHand().popCard(bestMatch);
31+
}
32+
return null;
33+
}
34+
35+
}

0 commit comments

Comments
 (0)