From eaf96b152df96ba3af4eef74917d3fff37665eee Mon Sep 17 00:00:00 2001 From: Chris Mayfield Date: Fri, 29 Apr 2016 16:07:13 -0400 Subject: [PATCH] review ch12..ch14 --- ch12/Card.java | 1 + ch12/CardTable.java | 11 +++--- ch12/Search.java | 66 ++++++++++++++++++++---------------- ch13/Deck.java | 36 ++++++++++++++++++++ ch13/Test.java | 1 + ch14/CardCollection.java | 72 ++++++++++++++++++---------------------- ch14/Deck.java | 1 + ch14/Eights.java | 62 +++++++++++++++++----------------- ch14/Hand.java | 1 + ch14/Player.java | 8 +---- ch14/Test.java | 8 ++--- 11 files changed, 151 insertions(+), 116 deletions(-) diff --git a/ch12/Card.java b/ch12/Card.java index 7bd1202..e7a0661 100644 --- a/ch12/Card.java +++ b/ch12/Card.java @@ -79,4 +79,5 @@ public int position() { public String toString() { return RANKS[this.rank] + " of " + SUITS[this.suit]; } + } diff --git a/ch12/CardTable.java b/ch12/CardTable.java index 5b2b8ca..727d307 100644 --- a/ch12/CardTable.java +++ b/ch12/CardTable.java @@ -7,13 +7,10 @@ import javax.swing.JFrame; public class CardTable extends Canvas { + private Image[][] images; private int cardWidth, cardHeight; - // this long is here to suppress a warning; you can read about it at - // http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html - static final long serialVersionUID = 1; - /** * Creates a CardTable. * cardset is the name of the folder that contains the card images. @@ -29,7 +26,8 @@ public CardTable(String cardset) { char c = suits.charAt(suit); for (int rank = 1; rank <= 13; rank++) { - String s = String.format("%s/%02d%c.gif", cardset, rank, c); + String s = String.format("%s/%02d%c.gif", + cardset, rank, c); images[rank][suit] = new ImageIcon(s).getImage(); } } @@ -79,7 +77,7 @@ public void paint(Graphics g) { public static void main(String[] args) { // make the frame - JFrame frame = new JFrame(); + JFrame frame = new JFrame("Card Table"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // add the CardTable @@ -91,4 +89,5 @@ public static void main(String[] args) { frame.pack(); frame.setVisible(true); } + } diff --git a/ch12/Search.java b/ch12/Search.java index 3cfe2d1..2f83023 100644 --- a/ch12/Search.java +++ b/ch12/Search.java @@ -3,6 +3,30 @@ */ public class Search { + /** + * Make an array of 52 cards. + */ + public static Card[] makeDeck() { + Card[] cards = new Card[52]; + int index = 0; + for (int suit = 0; suit <= 3; suit++) { + for (int rank = 1; rank <= 13; rank++) { + cards[index] = new Card(rank, suit); + index++; + } + } + return cards; + } + + /** + * Displays the given deck of cards. + */ + public static void printDeck(Card[] cards) { + for (int i = 0; i < cards.length; i++) { + System.out.println(cards[i]); + } + } + /** * Sequential search. */ @@ -24,14 +48,14 @@ public static int binarySearch(Card[] cards, Card target) { while (low <= high) { System.out.println(low + ", " + high); - int mid = (low + high) / 2; // step 1 + int mid = (low + high) / 2; // step 1 int comp = cards[mid].compareTo(target); - if (comp == 0) { // step 2 + if (comp == 0) { // step 2 return mid; - } else if (comp < 0) { // step 3 + } else if (comp < 0) { // step 3 low = mid + 1; - } else { // step 4 + } else { // step 4 high = mid - 1; } } @@ -41,40 +65,25 @@ public static int binarySearch(Card[] cards, Card target) { /** * Binary search (recursive version). */ - public static int binarySearchRec(Card[] cards, Card target, - int low, int high) { + public static int binarySearch(Card[] cards, Card target, + int low, int high) { System.out.println(low + ", " + high); if (high < low) { return -1; } - int mid = (low + high) / 2; // step 1 + int mid = (low + high) / 2; // step 1 int comp = cards[mid].compareTo(target); - if (comp == 0) { // step 2 + if (comp == 0) { // step 2 return mid; - } else if (comp < 0) { // step 3 - return binarySearchRec(cards, target, mid + 1, high); - } else { // step 4 - return binarySearchRec(cards, target, low, mid - 1); + } else if (comp < 0) { // step 3 + return binarySearch(cards, target, mid + 1, high); + } else { // step 4 + return binarySearch(cards, target, low, mid - 1); } } - /** - * Make an array of 52 cards. - */ - public static Card[] makeDeck() { - Card[] cards = new Card[52]; - int index = 0; - for (int suit = 0; suit <= 3; suit++) { - for (int rank = 1; rank <= 13; rank++) { - cards[index] = new Card(rank, suit); - index++; - } - } - return cards; - } - /** * Demonstrates how to call the search methods. */ @@ -96,7 +105,8 @@ public static void main(String[] args) { System.out.println(); System.out.println("Recursive binary search"); - System.out.println(binarySearch(cards, jack)); + System.out.println(binarySearch(cards, jack, 0, 51)); System.out.println(); } + } diff --git a/ch13/Deck.java b/ch13/Deck.java index 864d52e..742120e 100644 --- a/ch13/Deck.java +++ b/ch13/Deck.java @@ -45,12 +45,40 @@ public void print() { } } + /** + * Returns a string representation of the deck. + */ + public String toString() { + return Arrays.toString(this.cards); + } + + /** + * Chooses a random number between low and high, including both. + */ + public int randomInt(int low, int high) { + return 0; + } + + /** + * Swaps the cards at indexes i and j. + */ + public void swapCards(int i, int j) { + } + /** * Randomly permutes the array of cards. */ public void shuffle() { } + /** + * Finds the index of the lowest card + * between low and high inclusive. + */ + public int indexLowest(int low, int high) { + return 0; + } + /** * Sorts the cards (in place) using selection sort. */ @@ -68,6 +96,13 @@ public Deck subdeck(int low, int high) { return sub; } + /** + * Combines two previously sorted subdecks. + */ + public static Deck merge(Deck d1, Deck d2) { + return null; + } + /** * Returns a sorted copy of the deck using merge sort. */ @@ -80,4 +115,5 @@ public Deck mergeSort() { */ public void insertionSort() { } + } diff --git a/ch13/Test.java b/ch13/Test.java index c5431ee..5f49ef1 100644 --- a/ch13/Test.java +++ b/ch13/Test.java @@ -39,4 +39,5 @@ public static void main(String[] args) { deck.insertionSort(); checkSorted(deck); } + } diff --git a/ch14/CardCollection.java b/ch14/CardCollection.java index 5129496..f9c3be2 100644 --- a/ch14/CardCollection.java +++ b/ch14/CardCollection.java @@ -18,44 +18,46 @@ public CardCollection(String label) { } /** - * Returns the label. + * Returns the label of the card collection. */ public String getLabel() { return label; } /** - * Returns the number of cards. + * Adds the given card to the collection. */ - public int size() { - return cards.size(); + public void addCard(Card card) { + cards.add(card); } /** - * True if the collection is empty, false otherwise. + * Removes and returns the card with the given index. */ - public boolean empty() { - return cards.size() == 0; + public Card popCard(int i) { + return cards.remove(i); } /** - * Randomly permute the cards. + * Removes and returns the last card. */ - public void shuffle() { - Random random = new Random(); - for (int i = size() - 1; i > 0; i--) { - int j = random.nextInt(i); - swapCards(i, j); - } + public Card popCard() { + int i = size() - 1; + return popCard(i); } /** - * Swaps the cards at indexes i and j. + * Returns the number of cards. */ - public void swapCards(int i, int j) { - Card temp = cards.get(i); - cards.set(i, cards.get(j)); - cards.set(j, temp); + public int size() { + return cards.size(); + } + + /** + * True if the collection is empty, false otherwise. + */ + public boolean empty() { + return cards.size() == 0; } /** @@ -76,13 +78,6 @@ public void dealAll(CardCollection that) { deal(that, n); } - /** - * Adds the given card to the collection. - */ - public void addCard(Card card) { - cards.add(card); - } - /** * Returns the card with the given index. */ @@ -99,18 +94,23 @@ public Card last() { } /** - * Removes and returns the card with the given index. + * Swaps the cards at indexes i and j. */ - public Card popCard(int i) { - return cards.remove(i); + public void swapCards(int i, int j) { + Card temp = cards.get(i); + cards.set(i, cards.get(j)); + cards.set(j, temp); } /** - * Removes and returns the last card. + * Randomly permute the cards. */ - public Card popCard() { - int i = size() - 1; - return popCard(i); + public void shuffle() { + Random random = new Random(); + for (int i = size() - 1; i > 0; i--) { + int j = random.nextInt(i); + swapCards(i, j); + } } /** @@ -120,10 +120,4 @@ public String toString() { return label + ": " + cards.toString(); } - /** - * Gets the internal cards array (should only be used for testing). - */ - public Card[] getCards() { - return (Card[]) cards.toArray(); - } } diff --git a/ch14/Deck.java b/ch14/Deck.java index 10ba798..8db1814 100644 --- a/ch14/Deck.java +++ b/ch14/Deck.java @@ -15,4 +15,5 @@ public Deck(String label) { } } } + } diff --git a/ch14/Eights.java b/ch14/Eights.java index 78a767b..13b7961 100644 --- a/ch14/Eights.java +++ b/ch14/Eights.java @@ -40,17 +40,6 @@ public Eights() { in = new Scanner(System.in); } - /** - * Displays the state of the game. - */ - public void displayState() { - one.display(); - two.display(); - discardPile.display(); - System.out.print("Draw pile: "); - System.out.println(drawPile.size() + " cards"); - } - /** * Returns true if either hand is empty. */ @@ -75,19 +64,6 @@ public void reshuffle() { drawPile.shuffle(); } - /** - * One player takes a turn. - */ - public void takeTurn(Player player) { - Card prev = discardPile.last(); - Card next = player.play(this, prev); - - System.out.println(player.getName() + " plays " + next); - System.out.println(); - - discardPile.addCard(next); - } - /** * Returns a card from the draw pile. */ @@ -98,13 +74,6 @@ public Card draw() { return drawPile.popCard(); } - /** - * Waits for the user to press enter. - */ - public void waitForUser() { - in.nextLine(); - } - /** * Switches players. */ @@ -116,6 +85,36 @@ public Player nextPlayer(Player current) { } } + /** + * Displays the state of the game. + */ + public void displayState() { + one.display(); + two.display(); + discardPile.display(); + System.out.print("Draw pile: "); + System.out.println(drawPile.size() + " cards"); + } + + /** + * Waits for the user to press enter. + */ + public void waitForUser() { + in.nextLine(); + } + + /** + * One player takes a turn. + */ + public void takeTurn(Player player) { + Card prev = discardPile.last(); + Card next = player.play(this, prev); + discardPile.addCard(next); + + System.out.println(player.getName() + " plays " + next); + System.out.println(); + } + /** * Plays the game. */ @@ -142,4 +141,5 @@ public static void main(String[] args) { Eights game = new Eights(); game.playGame(); } + } diff --git a/ch14/Hand.java b/ch14/Hand.java index a65b7f5..8089a49 100644 --- a/ch14/Hand.java +++ b/ch14/Hand.java @@ -20,4 +20,5 @@ public void display() { } System.out.println(); } + } diff --git a/ch14/Player.java b/ch14/Player.java index c9c9695..433ff28 100644 --- a/ch14/Player.java +++ b/ch14/Player.java @@ -101,13 +101,6 @@ public int score() { return sum; } - /** - * Returns a string representation of the player. - */ - public String toString() { - return name + ": " + hand; - } - /** * Displays the player's hand. */ @@ -121,4 +114,5 @@ public void display() { public void displayScore() { System.out.println(name + " has " + score() + " points"); } + } diff --git a/ch14/Test.java b/ch14/Test.java index 0e5c243..274710c 100644 --- a/ch14/Test.java +++ b/ch14/Test.java @@ -3,13 +3,9 @@ */ public class Test { - /** - * Test code. - */ public static void main(String[] args) { Deck deck = new Deck("Deck"); deck.shuffle(); - System.out.println(deck); Hand hand = new Hand("Hand"); deck.deal(hand, 5); @@ -17,6 +13,8 @@ public static void main(String[] args) { Hand drawPile = new Hand("Draw Pile"); deck.dealAll(drawPile); - System.out.println(drawPile.size()); + System.out.printf("Draw Pile has %d cards.\n", + drawPile.size()); } + }