From 67438579e63decb8c2d014c66dca7ad28e65a864 Mon Sep 17 00:00:00 2001 From: avachali Date: Sat, 23 Apr 2022 18:00:24 -0400 Subject: [PATCH 1/4] fixed errors --- appa/.project | 11 ++++ appb/.project | 11 ++++ appc/.project | 11 ++++ appd/.project | 11 ++++ ch01/.project | 11 ++++ ch02/.project | 11 ++++ ch03/.project | 11 ++++ ch04/.project | 11 ++++ ch05/.project | 11 ++++ ch06/.project | 11 ++++ ch07/.project | 11 ++++ ch08/.project | 11 ++++ ch09/.project | 11 ++++ ch10/.project | 11 ++++ ch11/.project | 11 ++++ ch12/.project | 11 ++++ ch13/.project | 11 ++++ ch13/Card.java | 84 +++++++++++++++++++++++++++- ch14/.project | 11 ++++ ch14/Card.java | 84 +++++++++++++++++++++++++++- ch15/.project | 11 ++++ ch16/.project | 11 ++++ ch16/Cell.java | 78 +++++++++++++++++++++++++- ch16/GridCanvas.java | 130 ++++++++++++++++++++++++++++++++++++++++++- ch17/.project | 11 ++++ 25 files changed, 603 insertions(+), 4 deletions(-) diff --git a/appa/.project b/appa/.project index 89577ab..5aebeb6 100644 --- a/appa/.project +++ b/appa/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1650750308634 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/appb/.project b/appb/.project index 7658200..c8d2242 100644 --- a/appb/.project +++ b/appb/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1650750308682 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/appc/.project b/appc/.project index 0e3c2da..cdb6157 100644 --- a/appc/.project +++ b/appc/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1650750308699 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/appd/.project b/appd/.project index 7b8a1e5..3b59329 100644 --- a/appd/.project +++ b/appd/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1650750308714 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/ch01/.project b/ch01/.project index d4e68d5..b665469 100644 --- a/ch01/.project +++ b/ch01/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1650750308730 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/ch02/.project b/ch02/.project index c7628eb..90a9ebf 100644 --- a/ch02/.project +++ b/ch02/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1650750308742 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/ch03/.project b/ch03/.project index 13fab09..4de4066 100644 --- a/ch03/.project +++ b/ch03/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1650750308756 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/ch04/.project b/ch04/.project index 9834b0d..c2db8ce 100644 --- a/ch04/.project +++ b/ch04/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1650750308767 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/ch05/.project b/ch05/.project index 933b313..3db44ba 100644 --- a/ch05/.project +++ b/ch05/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1650750308782 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/ch06/.project b/ch06/.project index 50d0c5e..34354f9 100644 --- a/ch06/.project +++ b/ch06/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1650750308796 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/ch07/.project b/ch07/.project index 6887963..22973bd 100644 --- a/ch07/.project +++ b/ch07/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1650750308810 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/ch08/.project b/ch08/.project index a78d2dd..637bf12 100644 --- a/ch08/.project +++ b/ch08/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1650750308822 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/ch09/.project b/ch09/.project index 3dbf792..b3e41a2 100644 --- a/ch09/.project +++ b/ch09/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1650750308834 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/ch10/.project b/ch10/.project index 5b1937d..1aed399 100644 --- a/ch10/.project +++ b/ch10/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1650750308843 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/ch11/.project b/ch11/.project index dd4d0e0..7272cf6 100644 --- a/ch11/.project +++ b/ch11/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1650750308858 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/ch12/.project b/ch12/.project index 4d575af..1e2bfad 100644 --- a/ch12/.project +++ b/ch12/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1650750308874 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/ch13/.project b/ch13/.project index 5ae6360..3465f4f 100644 --- a/ch13/.project +++ b/ch13/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1650750308894 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/ch13/Card.java b/ch13/Card.java index 1c6483e..e7a0661 120000 --- a/ch13/Card.java +++ b/ch13/Card.java @@ -1 +1,83 @@ -../ch12/Card.java \ No newline at end of file +/** + * A standard playing card. + */ +public class Card { + + 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 Card(int rank, int suit) { + this.rank = rank; + this.suit = suit; + } + + /** + * Returns a negative integer if this card comes before + * the given card, zero if the two cards are equal, or + * a positive integer if this card comes after the card. + */ + public int compareTo(Card that) { + if (this.suit < that.suit) { + return -1; + } + if (this.suit > that.suit) { + return 1; + } + if (this.rank < that.rank) { + return -1; + } + if (this.rank > that.rank) { + return 1; + } + return 0; + } + + /** + * Returns true if the given card has the same + * rank AND same suit; otherwise returns false. + */ + public boolean equals(Card that) { + return this.rank == that.rank + && this.suit == that.suit; + } + + /** + * Gets the card's rank. + */ + public int getRank() { + return this.rank; + } + + /** + * Gets the card's suit. + */ + public int getSuit() { + return this.suit; + } + + /** + * Returns the card's index in a sorted deck of 52 cards. + */ + public int position() { + return this.suit * 13 + this.rank - 1; + } + + /** + * Returns a string representation of the card. + */ + public String toString() { + return RANKS[this.rank] + " of " + SUITS[this.suit]; + } + +} diff --git a/ch14/.project b/ch14/.project index 4294f38..1fa9dc3 100644 --- a/ch14/.project +++ b/ch14/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1650750308909 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/ch14/Card.java b/ch14/Card.java index 1c6483e..e7a0661 120000 --- a/ch14/Card.java +++ b/ch14/Card.java @@ -1 +1,83 @@ -../ch12/Card.java \ No newline at end of file +/** + * A standard playing card. + */ +public class Card { + + 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 Card(int rank, int suit) { + this.rank = rank; + this.suit = suit; + } + + /** + * Returns a negative integer if this card comes before + * the given card, zero if the two cards are equal, or + * a positive integer if this card comes after the card. + */ + public int compareTo(Card that) { + if (this.suit < that.suit) { + return -1; + } + if (this.suit > that.suit) { + return 1; + } + if (this.rank < that.rank) { + return -1; + } + if (this.rank > that.rank) { + return 1; + } + return 0; + } + + /** + * Returns true if the given card has the same + * rank AND same suit; otherwise returns false. + */ + public boolean equals(Card that) { + return this.rank == that.rank + && this.suit == that.suit; + } + + /** + * Gets the card's rank. + */ + public int getRank() { + return this.rank; + } + + /** + * Gets the card's suit. + */ + public int getSuit() { + return this.suit; + } + + /** + * Returns the card's index in a sorted deck of 52 cards. + */ + public int position() { + return this.suit * 13 + this.rank - 1; + } + + /** + * Returns a string representation of the card. + */ + public String toString() { + return RANKS[this.rank] + " of " + SUITS[this.suit]; + } + +} diff --git a/ch15/.project b/ch15/.project index 77395cb..6a13820 100644 --- a/ch15/.project +++ b/ch15/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1650750308924 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/ch16/.project b/ch16/.project index ae6ca3f..d07029c 100644 --- a/ch16/.project +++ b/ch16/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1650750308937 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/ch16/Cell.java b/ch16/Cell.java index 1e0bc1b..5b6a0f9 120000 --- a/ch16/Cell.java +++ b/ch16/Cell.java @@ -1 +1,77 @@ -../ch15/Cell.java \ No newline at end of file +import java.awt.Color; +import java.awt.Graphics; + +/** + * A square at a fixed location that changes color. + * + * @author Chris Mayfield + * @version 7.1.0 + */ +public class Cell { + + public static final Color[] COLORS = {Color.WHITE, Color.BLACK}; + + private final int x; + private final int y; + private final int size; + private int state; + + /** + * Constructs a new cell, initially turned off. + * + * @param x the X coordinate + * @param y the Y coordinate + * @param size number of pixels + */ + public Cell(int x, int y, int size) { + this.x = x; + this.y = y; + this.size = size; + this.state = 0; + } + + /** + * Draws the cell on the screen. + * + * @param g graphics context + */ + public void draw(Graphics g) { + g.setColor(COLORS[state]); + g.fillRect(x + 1, y + 1, size - 1, size - 1); + g.setColor(Color.LIGHT_GRAY); + g.drawRect(x, y, size, size); + } + + /** + * Tests whether the cell is off. + * + * @return true if the cell is off + */ + public boolean isOff() { + return state == 0; + } + + /** + * Tests whether the cell is on. + * + * @return true if the cell is on + */ + public boolean isOn() { + return state == 1; + } + + /** + * Sets the cell's state to off. + */ + public void turnOff() { + state = 0; + } + + /** + * Sets the cell's state to on. + */ + public void turnOn() { + state = 1; + } + +} diff --git a/ch16/GridCanvas.java b/ch16/GridCanvas.java index 6711096..3856a57 120000 --- a/ch16/GridCanvas.java +++ b/ch16/GridCanvas.java @@ -1 +1,129 @@ -../ch15/GridCanvas.java \ No newline at end of file +import java.awt.Canvas; +import java.awt.Graphics; + +/** + * 2D array of cells representing a rectangular grid. + * + * @author Chris Mayfield + * @version 7.1.0 + */ +public class GridCanvas extends Canvas { + + /** Cells stored in row-major order. */ + private Cell[][] array; + + /** + * Constructs a grid of given size. + * + * @param rows number of rows + * @param cols number of columns + * @param size pixels per cell + */ + public GridCanvas(int rows, int cols, int size) { + + // build 2D array of cells + array = new Cell[rows][cols]; + for (int r = 0; r < rows; r++) { + int y = r * size; + for (int c = 0; c < cols; c++) { + int x = c * size; + array[r][c] = new Cell(x, y, size); + } + } + + // set the canvas size + setSize(cols * size, rows * size); + } + + /** + * Gets the number of rows. + * + * @return number of rows + */ + public int numRows() { + return array.length; + } + + /** + * Gets the number of columns. + * + * @return number of columns + */ + public int numCols() { + return array[0].length; + } + + /** + * Gets the cell at index (r, c). + * + * @param r row index + * @param c column index + * @return the cell + */ + public Cell getCell(int r, int c) { + return array[r][c]; + } + + /** + * Convenience method that turns on the cell at (r, c). + * + * @param r row index + * @param c column index + */ + public void turnOn(int r, int c) { + array[r][c].turnOn(); + } + + /** + * Returns 1 if the cell at (r, c) exists and is on. Returns 0 if the cell + * doesn't exist or is off. + * + * @param r row index + * @param c column index + * @return 1 or 0 + */ + public int test(int r, int c) { + try { + if (array[r][c].isOn()) { + return 1; + } + } catch (ArrayIndexOutOfBoundsException e) { + // cell doesn't exist + } + return 0; + } + + /** + * Draws the grid, cell by cell. + * + * @param g graphics context + */ + public void draw(Graphics g) { + for (Cell[] row : array) { + for (Cell cell : row) { + cell.draw(g); + } + } + } + + /** + * Paints the grid on the screen. + * + * @param g graphics context + */ + public void paint(Graphics g) { + draw(g); + } + + /** + * Overriding this method helps the simulation run more smoothly. Normally + * the Canvas is cleared before painting, but there is no need to clear it + * since the paint method draws the entire grid. + * + * @param g graphics context + */ + public void update(Graphics g) { + draw(g); + } + +} diff --git a/ch17/.project b/ch17/.project index 7010276..ec9b3a2 100644 --- a/ch17/.project +++ b/ch17/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1650750308952 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + From 922e35a6a514c9b1b34b7330e09f66bf0693191c Mon Sep 17 00:00:00 2001 From: avachali Date: Sat, 23 Apr 2022 21:51:10 -0400 Subject: [PATCH 2/4] fixed war logic --- appb/Convert.java | 25 +++++++++++++------------ ch13/Deck.java | 34 +++++++++++++++++++++++++++++++++- ch13/Pile.java | 4 ++++ ch13/War.java | 44 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 13 deletions(-) diff --git a/appb/Convert.java b/appb/Convert.java index 232abd6..24d6fd8 100644 --- a/appb/Convert.java +++ b/appb/Convert.java @@ -65,20 +65,21 @@ public static double toMetric(int feet, int inches) { public static void main(String[] args) { double cm, result; int feet, inches; - Scanner in = new Scanner(System.in); + try (Scanner in = new Scanner(System.in)) { + // test the Imperial conversion + System.out.print("Exactly how many cm? "); + cm = in.nextDouble(); + result = toImperial(cm); + System.out.printf("That's %.2f inches\n", result); + System.out.println(); - // test the Imperial conversion - System.out.print("Exactly how many cm? "); - cm = in.nextDouble(); - result = toImperial(cm); - System.out.printf("That's %.2f inches\n", result); - System.out.println(); + // test the Metric conversion + System.out.print("Now how many feet? "); + feet = in.nextInt(); + System.out.print("And how many inches? "); + inches = in.nextInt(); + } - // test the Metric conversion - System.out.print("Now how many feet? "); - feet = in.nextInt(); - System.out.print("And how many inches? "); - inches = in.nextInt(); result = toMetric(feet, inches); System.out.printf("That's %.2f cm\n", result); } diff --git a/ch13/Deck.java b/ch13/Deck.java index 25b2912..08df364 100644 --- a/ch13/Deck.java +++ b/ch13/Deck.java @@ -1,3 +1,7 @@ +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; import java.util.Random; /** @@ -52,13 +56,41 @@ public void print() { * Returns a string representation of the deck. */ public String toString() { - return "TODO"; + return Arrays.toString(cards).toString(); } /** * Randomly permutes the array of cards. */ + // public void shuffle() { + // //topCard = 0; + + // ArrayList temp = new ArrayList(); + // Random random = new Random(); + // // random.nextInt(max - min) + min + // List list = (List) Arrays.asList(cards); + + // while (!list.isEmpty()) { + // int loc = random.nextInt(list.size()); + // temp.add(list.get(loc)); + // list.remove(loc); + // } + // //deck = temp; + // //printDeck(temp); + // } + public void shuffle() { + //topCard = 0; + + Collections.shuffle(Arrays.asList(this.cards)); + + // for (int i = 0; i < cards.length; i++) { + // int loc = random.nextInt(cards.length); + // Card temp = cards[loc]; + // cards[loc] = cards[i]; + // cards[i] = temp; + // } + } /** diff --git a/ch13/Pile.java b/ch13/Pile.java index 1df5cdc..96fe381 100644 --- a/ch13/Pile.java +++ b/ch13/Pile.java @@ -44,4 +44,8 @@ public Card popCard() { return this.cards.remove(0); } + public int size() { + return this.cards.size(); + } + } diff --git a/ch13/War.java b/ch13/War.java index d9e9c04..0f2679f 100644 --- a/ch13/War.java +++ b/ch13/War.java @@ -1,3 +1,5 @@ +import java.util.ArrayList; + /** * Simulates a simple card game. */ @@ -7,30 +9,72 @@ public static void main(String[] args) { // create and shuffle the deck Deck deck = new Deck(); + System.out.println(deck.toString()); + deck.shuffle(); + System.out.println(deck.toString()); + // divide the deck into piles Pile p1 = new Pile(); p1.addDeck(deck.subdeck(0, 25)); Pile p2 = new Pile(); p2.addDeck(deck.subdeck(26, 51)); + ArrayList warList = new ArrayList<>(); + // while both piles are not empty while (!p1.isEmpty() && !p2.isEmpty()) { Card c1 = p1.popCard(); Card c2 = p2.popCard(); + System.out.println("C1: "+ c1.toString()); + System.out.println("C2: "+ c2.toString()); + + // compare the cards int diff = c1.getRank() - c2.getRank(); if (diff > 0) { p1.addCard(c1); p1.addCard(c2); + if (warList.size() > 0) { + for (Card card : warList) { + p1.addCard(card); + + + } + warList.clear(); + } } else if (diff < 0) { p2.addCard(c1); p2.addCard(c2); + if (warList.size() > 0) { + for (Card card : warList) { + p1.addCard(card); + + + } + warList.clear(); + } } else { // it's a tie...draw four more cards + System.out.println("WAR"); + if(p1.size() < 4 || p2.size() <4) { + System.out.println("Game ended! No winner"); + break; + } + warList.add(c1); + warList.add(c2); + for (int i = 0; i < 3; i++) { + warList.add(p1.popCard()); + + warList.add(p2.popCard()); + + } + } + System.out.println("size of p1: "+p1.size()); + System.out.println("size of p2: "+p2.size()); } // display the winner From 953016ec1dc3d85d71929f73c65145a84d621061 Mon Sep 17 00:00:00 2001 From: avachali Date: Sat, 23 Apr 2022 22:02:48 -0400 Subject: [PATCH 3/4] fixed err with adding to correct list --- ch13/War.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ch13/War.java b/ch13/War.java index 0f2679f..9fdcc66 100644 --- a/ch13/War.java +++ b/ch13/War.java @@ -31,10 +31,9 @@ public static void main(String[] args) { System.out.println("C1: "+ c1.toString()); System.out.println("C2: "+ c2.toString()); - // compare the cards int diff = c1.getRank() - c2.getRank(); - if (diff > 0) { + if (diff > 0) { //player 1 won - gets all cards incl war list p1.addCard(c1); p1.addCard(c2); if (warList.size() > 0) { @@ -43,15 +42,14 @@ public static void main(String[] args) { } - warList.clear(); + warList.clear();//remove all from warlist } } else if (diff < 0) { p2.addCard(c1); p2.addCard(c2); if (warList.size() > 0) { for (Card card : warList) { - p1.addCard(card); - + p2.addCard(card); } warList.clear(); @@ -75,6 +73,8 @@ public static void main(String[] args) { } System.out.println("size of p1: "+p1.size()); System.out.println("size of p2: "+p2.size()); + System.out.println("size of warList: "+warList.size()); + } // display the winner From 176efa367c706fd3f7c07a39e5bed12a01f9e542 Mon Sep 17 00:00:00 2001 From: avachali Date: Sat, 23 Apr 2022 22:09:28 -0400 Subject: [PATCH 4/4] fixed final winner message --- ch13/War.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ch13/War.java b/ch13/War.java index 9fdcc66..4ac13b4 100644 --- a/ch13/War.java +++ b/ch13/War.java @@ -71,6 +71,7 @@ public static void main(String[] args) { } } + System.out.println("size of p1: "+p1.size()); System.out.println("size of p2: "+p2.size()); System.out.println("size of warList: "+warList.size()); @@ -80,8 +81,10 @@ public static void main(String[] args) { // display the winner if (p2.isEmpty()) { System.out.println("Player 1 wins!"); - } else { + } else if (p1.isEmpty()) { System.out.println("Player 2 wins!"); + } else { + System.out.println("TIE!"); } }