From 4230f7c7b353788c940bca6cb7b90475614a2d4e Mon Sep 17 00:00:00 2001 From: Anna Lamoureux Date: Mon, 30 Mar 2015 23:00:26 -0400 Subject: [PATCH 01/14] Some progress. Test for commit and push. --- src/Card.java | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) mode change 100755 => 100644 src/Card.java diff --git a/src/Card.java b/src/Card.java old mode 100755 new mode 100644 index 9eed9a5..f5ae705 --- a/src/Card.java +++ b/src/Card.java @@ -1,5 +1,16 @@ public class Card { - // Create the rest of this class yourself + private int quantity; + private int color; + private int shading; + private int shape; + + public Card(int q, int c, int shd, int shp) { + quantity = q; + color = c; + shading = shd; + shape = shp; + + } public boolean equals(Object obj) { Card that = (Card)obj; @@ -9,4 +20,29 @@ public boolean equals(Object obj) { shading == that.getShading() && shape == that.getShape(); } + + public int getQuantity() { + return quantity; + } + + public int getColor() { + return color; + } + + public int getShading() { + return shading; + } + + public int getShape() { + return shape; + } + + public boolean isSet(Card c1, Card c2) { + return false; + } + + public String toString() { + return ""; + } + } From c749b417cbbd13f6e01fb630872f2c633fda76e9 Mon Sep 17 00:00:00 2001 From: Anna Lamoureux Date: Tue, 31 Mar 2015 21:00:15 -0400 Subject: [PATCH 02/14] Final versions of a fully functional card class with respective test file. --- src/Card.java | 88 +++++++++++++++++++++++++++++++++++++++-------- src/CardTest.java | 86 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 159 insertions(+), 15 deletions(-) create mode 100644 src/CardTest.java diff --git a/src/Card.java b/src/Card.java index f5ae705..1fc970e 100644 --- a/src/Card.java +++ b/src/Card.java @@ -1,3 +1,5 @@ +import java.util.ArrayList; + public class Card { private int quantity; private int color; @@ -5,20 +7,30 @@ public class Card { private int shape; public Card(int q, int c, int shd, int shp) { - quantity = q; - color = c; - shading = shd; - shape = shp; - - } - - public boolean equals(Object obj) { - Card that = (Card)obj; - - return quantity == that.getQuantity() && - color == that.getColor() && - shading == that.getShading() && - shape == that.getShape(); + if (q < 1 || q > 3) { + quantity = (((q % 3) + 3) % 3) + 1; + } + else { + quantity = q; + } + if (c < 1 || c > 3) { + color = (((c % 3) + 3) % 3) + 1; + } + else { + color = c; + } + if (shd < 1 || shd > 3) { + shading = (((shd % 3) + 3) % 3) + 1; + } + else { + shading = shd; + } + if (shp < 1 || shp > 3) { + shape = (((shp % 3) + 3) % 3) + 1; + } + else { + shape = shp; + } } public int getQuantity() { @@ -38,11 +50,57 @@ public int getShape() { } public boolean isSet(Card c1, Card c2) { + int sum = quantity + color + shading + shape; + int card1 = c1.getQuantity() + c1.getColor() + c1.getShading() + c1.getShape(); + int card2 = c2.getQuantity() + c2.getColor() + c2.getShading() + c2.getShape(); + if ((card1 + card2 + sum) % 3 == 0) { + return true; + } + else { return false; + } } public String toString() { - return ""; + String string = ""; + ArrayList quant = new ArrayList(); + quant.add(""); + quant.add("1"); + quant.add("2"); + quant.add("3"); + + ArrayList col = new ArrayList(); + col.add(""); + col.add("R"); + col.add("G"); + col.add("P"); + + ArrayList shad = new ArrayList(); + shad.add(""); + shad.add("O"); + shad.add("T"); + shad.add("S"); + + ArrayList shap = new ArrayList(); + shap.add(""); + shap.add("O"); + shap.add("D"); + shap.add("S"); + + string += quant.get(getQuantity()); + string += col.get(getColor()); + string += shad.get(getShading()); + string += shap.get(getShape()); + + return string; } + public boolean equals(Object obj) { + Card that = (Card)obj; + + return quantity == that.getQuantity() && + color == that.getColor() && + shading == that.getShading() && + shape == that.getShape(); + } } diff --git a/src/CardTest.java b/src/CardTest.java new file mode 100644 index 0000000..a6907e2 --- /dev/null +++ b/src/CardTest.java @@ -0,0 +1,86 @@ +import junit.framework.TestCase; + +public class CardTest extends TestCase { + + public void testOneCard() { + Card c = new Card(1, 1, 1, 1); + + assertEquals(1, c.getQuantity()); + assertEquals(1, c.getColor()); + assertEquals(1, c.getShading()); + assertEquals(1, c.getShape()); + assertEquals("1ROO", c.toString()); + } + + public void testInvalidValues() { + Card c = new Card(-2, 4, 8, 0); + + assertEquals(2, c.getQuantity()); + assertEquals(2, c.getColor()); + assertEquals(3, c.getShading()); + assertEquals(1, c.getShape()); + assertEquals("2GSO", c.toString()); + } + + public void testMixedValidityValues() { + Card c = new Card(2, 1, 5, 0); + + assertEquals(2, c.getQuantity()); + assertEquals(1, c.getColor()); + assertEquals(3, c.getShading()); + assertEquals(1, c.getShape()); + assertEquals("2RSO", c.toString()); + } + + public void testNotSet() { + Card c1 = new Card(2, 1, 5, 0); + Card c2 = new Card(1, 1, 3, 2); + Card c3 = new Card(2, 1, 1, 1); + + assertEquals(2, c1.getQuantity()); + assertEquals(1, c1.getColor()); + assertEquals(3, c1.getShading()); + assertEquals(1, c1.getShape()); + assertEquals("2RSO", c1.toString()); + + assertEquals(1, c2.getQuantity()); + assertEquals(1, c2.getColor()); + assertEquals(3, c2.getShading()); + assertEquals(2, c2.getShape()); + assertEquals("1RSD", c2.toString()); + + assertEquals(2, c3.getQuantity()); + assertEquals(1, c3.getColor()); + assertEquals(1, c3.getShading()); + assertEquals(1, c3.getShape()); + assertEquals("2ROO", c3.toString()); + + assertFalse(c1.isSet(c2, c3)); + } + + public void testSet() { + Card c1 = new Card(2, 1, 2, 3); + Card c2 = new Card(2, 1, 3, 2); + Card c3 = new Card(2, 1, 1, 1); + + assertEquals(2, c1.getQuantity()); + assertEquals(1, c1.getColor()); + assertEquals(2, c1.getShading()); + assertEquals(3, c1.getShape()); + assertEquals("2RTS", c1.toString()); + + assertEquals(2, c2.getQuantity()); + assertEquals(1, c2.getColor()); + assertEquals(3, c2.getShading()); + assertEquals(2, c2.getShape()); + assertEquals("2RSD", c2.toString()); + + assertEquals(2, c3.getQuantity()); + assertEquals(1, c3.getColor()); + assertEquals(1, c3.getShading()); + assertEquals(1, c3.getShape()); + assertEquals("2ROO", c3.toString()); + + assertTrue(c1.isSet(c2, c3)); + } +} From 3934710168b042009c8106e8c10437728964547b Mon Sep 17 00:00:00 2001 From: Anna Lamoureux Date: Tue, 7 Apr 2015 23:52:53 -0400 Subject: [PATCH 03/14] Deck Class, DeckTest Class, and data files, one with card data and one empty. --- src/Deck.java | 50 ++++++++++++++++++++++++++++++++++++++++++++++- src/DeckTest.java | 41 ++++++++++++++++++++++++++++++++++++++ src/deck.dat | 11 +++++++++++ src/empty.dat | 1 + 4 files changed, 102 insertions(+), 1 deletion(-) mode change 100755 => 100644 src/Deck.java create mode 100644 src/DeckTest.java create mode 100644 src/deck.dat create mode 100644 src/empty.dat diff --git a/src/Deck.java b/src/Deck.java old mode 100755 new mode 100644 index ab3a2a3..200fe97 --- a/src/Deck.java +++ b/src/Deck.java @@ -2,11 +2,35 @@ import java.io.FileReader; import java.util.StringTokenizer; import java.util.ArrayList; +import java.util.Collections; public class Deck { - // Implement the rest of this class yourself + //Instrance variables must be cards and nextCardIndex for the provided constructor + //to work + public ArrayList cards; + public int nextCardIndex; + + public Deck() { + // Creates a deck with all standard 81 set cards, shuffled + //Usage (in test code, and in the later code); Deck d = new Deck() + cards = new ArrayList(81); + + for (int q = 1; q <= 3; q++) { + for (int c = 1; c <= 3; c++) { + for (int shd = 1; shd <= 3; shd++) { + for (int shp = 1; shp <= 3; shp++) { + cards.add(new Card (q, c, shd, shp)); + } + } + } + } + Collections.shuffle(cards); + nextCardIndex = 0; + } public Deck(String filename) { + //Creates a pre-defined deck from a file named filename and does not shuffle + //Usage (in test code, and in the later code); Deck d = new Deck("example.dat") cards = new ArrayList(81); try { @@ -42,4 +66,28 @@ public Deck(String filename) { throw new RuntimeException("Error while reading file: " + e.toString()); } } + + public boolean hasNext() { + //Returns true if any cards are left, false otherwise + if (nextCardIndex < cards.size()) { + return true; + } + else { + return false; + } + } + + public Card getNext() { + //Returns the next card in the deck if there is one, null otherwise + //You can call hasNext within this method to see if you should return + //a card or return null + int cardIndex = nextCardIndex; + nextCardIndex += 1; + if (hasNext() == false) { + return null; + } + else { + return cards.get(cardIndex); + } + } } diff --git a/src/DeckTest.java b/src/DeckTest.java new file mode 100644 index 0000000..7046695 --- /dev/null +++ b/src/DeckTest.java @@ -0,0 +1,41 @@ +import junit.framework.TestCase; + +public class DeckTest extends TestCase { + + public void testDeck() { + Deck d = new Deck(); + + int num = 0; + while (d.hasNext() == true) { + d.getNext(); + num += 1; + } + + assertEquals(81, num); + } + + public void testFile() { + Deck d = new Deck("deck.dat"); + + int num = 0; + while (d.hasNext() == true) { + num += 1; + d.getNext(); + } + + assertEquals(9, num); + } + + public void testEmpty() { + Deck d = new Deck("empty.dat"); + + int num = 0; + while (d.hasNext() == true) { + num += 1; + d.getNext(); + } + + assertFalse(d.hasNext()); + assertEquals(null, d.getNext()); + } +} diff --git a/src/deck.dat b/src/deck.dat new file mode 100644 index 0000000..219af50 --- /dev/null +++ b/src/deck.dat @@ -0,0 +1,11 @@ +#Deck of Set Cards + +1 1 1 1 +1 1 1 2 +1 1 1 3 +1 3 2 1 +2 1 3 1 +2 3 2 3 +3 2 1 1 +3 3 2 1 +3 3 3 3 \ No newline at end of file diff --git a/src/empty.dat b/src/empty.dat new file mode 100644 index 0000000..907d8a4 --- /dev/null +++ b/src/empty.dat @@ -0,0 +1 @@ +#Empty Deck \ No newline at end of file From 611d72b6d13fbcf2bf986efa0d830ae4c4218538 Mon Sep 17 00:00:00 2001 From: Anna Lamoureux Date: Thu, 9 Apr 2015 14:07:26 -0400 Subject: [PATCH 04/14] Commit progress with fixed Deck class, a complete TableNode class and it's test, and a start on the Table class --- src/Deck.java | 4 ++-- src/Table.java | 36 ++++++++++++++++++++++++++++++++++++ src/TableNode.java | 21 +++++++++++++++++++++ src/TestTableNode.java | 27 +++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 src/Table.java create mode 100644 src/TableNode.java create mode 100644 src/TestTableNode.java diff --git a/src/Deck.java b/src/Deck.java index 200fe97..05c7919 100644 --- a/src/Deck.java +++ b/src/Deck.java @@ -81,12 +81,12 @@ public Card getNext() { //Returns the next card in the deck if there is one, null otherwise //You can call hasNext within this method to see if you should return //a card or return null - int cardIndex = nextCardIndex; - nextCardIndex += 1; if (hasNext() == false) { return null; } else { + int cardIndex = nextCardIndex; + nextCardIndex += 1; return cards.get(cardIndex); } } diff --git a/src/Table.java b/src/Table.java new file mode 100644 index 0000000..1c43e09 --- /dev/null +++ b/src/Table.java @@ -0,0 +1,36 @@ +public class Table{ + private TableNode head; + + public Table() { + head = null; + } + + public void add(Card c) { + + } + + public void removeSet(Card c1, Card c2, Card c3) { + + } + + public int numCards() { + return 0; + } + + public Card getCard(int index) { + TableNode temp = head; + if (temp == null) { + return null; + } + else { + for (int i = 0; i < index; i++) { + temp = temp.getNext(); + } + return temp.getCard(); + } + } + + public int numSets() { + return 0; + } +} \ No newline at end of file diff --git a/src/TableNode.java b/src/TableNode.java new file mode 100644 index 0000000..318dea8 --- /dev/null +++ b/src/TableNode.java @@ -0,0 +1,21 @@ +public class TableNode { + public Card card; + public TableNode next; + + public TableNode(Card c) { + card = c; + next = null; + } + + public void setNext(TableNode n) { + next = n; + } + + public TableNode getNext() { + return next; + } + + public Card getCard() { + return card; + } +} \ No newline at end of file diff --git a/src/TestTableNode.java b/src/TestTableNode.java new file mode 100644 index 0000000..ef32759 --- /dev/null +++ b/src/TestTableNode.java @@ -0,0 +1,27 @@ +import junit.framework.TestCase; + +public class TestTableNode extends TestCase { + + public void testSingleTableNode() { + Card c = new Card(1, 1, 1, 1); + TableNode node = new TableNode(c); + + assertEquals(null, node.getNext()); + assertEquals(c, node.getCard()); + + } + + public void testTwoTableNodes() { + Card c1 = new Card(1, 1, 1, 1); + Card c2 = new Card(2, 2, 2, 2); + TableNode n1 = new TableNode(c1); + TableNode n2 = new TableNode(c2); + + assertEquals(null, n1.getNext()); + assertEquals(c1, n1.getCard()); + assertEquals(null, n2.getNext()); + assertEquals(c2, n2.getCard()); + + } + +} From 67509a54e73d536d5279ae09462887ef8259627b Mon Sep 17 00:00:00 2001 From: Anna Lamoureux Date: Thu, 9 Apr 2015 14:44:07 -0400 Subject: [PATCH 05/14] Updated Table with all but numSets and removeSet done and working. Also improved TableTest --- src/Table.java | 14 ++++++++--- src/TableTest.java | 59 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 src/TableTest.java diff --git a/src/Table.java b/src/Table.java index 1c43e09..6f2bd18 100644 --- a/src/Table.java +++ b/src/Table.java @@ -1,4 +1,4 @@ -public class Table{ +public class Table { private TableNode head; public Table() { @@ -6,7 +6,9 @@ public Table() { } public void add(Card c) { - + TableNode n = new TableNode(c); + n.setNext(head); + head = n; } public void removeSet(Card c1, Card c2, Card c3) { @@ -14,7 +16,13 @@ public void removeSet(Card c1, Card c2, Card c3) { } public int numCards() { - return 0; + TableNode temp = head; + int num = 0; + while (temp != null) { + num += 1; + temp = temp.getNext(); + } + return num; } public Card getCard(int index) { diff --git a/src/TableTest.java b/src/TableTest.java new file mode 100644 index 0000000..847356d --- /dev/null +++ b/src/TableTest.java @@ -0,0 +1,59 @@ +import junit.framework.TestCase; + +public class TableTest extends TestCase { + + public void testEmptyTable() { + Table t = new Table(); + + assertEquals(0, t.numCards()); + assertEquals(null, t.getCard(0)); + assertEquals(0, t.numSets()); + + } + + public void testOneCardTable() { + Table t = new Table(); + Card c1 = new Card(1, 1, 1, 1); + + t.add(c1); + + assertEquals(1, t.numCards()); + assertEquals(c1, t.getCard(0)); + assertEquals(0, t.numSets()); + + } + + public void testTwoCardTable() { + Table t = new Table(); + Card c1 = new Card(1, 1, 1, 1); + Card c2 = new Card(2, 2, 2, 2); + + t.add(c1); + t.add(c2); + + assertEquals(2, t.numCards()); + assertEquals(c1, t.getCard(1)); + assertEquals(c2, t.getCard(0)); + assertEquals(0, t.numSets()); + + } + + public void testOneSetCardTable() { + Table t = new Table(); + Card c1 = new Card(1, 1, 1, 1); + Card c2 = new Card(2, 2, 2, 2); + Card c3 = new Card(3, 3, 3, 3); + + t.add(c2); + t.add(c1); + t.add(c3); + + assertEquals(3, t.numCards()); + assertEquals(c1, t.getCard(1)); + assertEquals(c2, t.getCard(2)); + assertEquals(c3, t.getCard(0)); + assertEquals(1, t.numSets()); + + } + +} From 20e351a286d0192594092b72cc89e969770cf3a6 Mon Sep 17 00:00:00 2001 From: Anna Lamoureux Date: Tue, 14 Apr 2015 18:35:44 -0400 Subject: [PATCH 06/14] Some progress --- src/Table.java | 45 +++++++++++++++++++- src/TestTable.java | 100 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+), 2 deletions(-) create mode 100644 src/TestTable.java diff --git a/src/Table.java b/src/Table.java index 6f2bd18..3215e76 100644 --- a/src/Table.java +++ b/src/Table.java @@ -12,7 +12,25 @@ public void add(Card c) { } public void removeSet(Card c1, Card c2, Card c3) { - + //if the 3 cards don't form a set or if any of the cards are not on the table, return + //otherwise, remove c1, c2, c3, preserving relative order of the rest of the cards + /*if (c1.isSet(c2, c3)) { + Card remove1 = null; + Card remove2 = null; + Card remove3 = null; + + TableNode temp = head; + TableNode prev = head; + while (temp != null) { + if (temp.getCard() == c1) { + temp = temp.getNext(); + c1 = null; + } + } + } + else { + return; + }*/ } public int numCards() { @@ -26,6 +44,7 @@ public int numCards() { } public Card getCard(int index) { + //if index is out of bounds, return null TableNode temp = head; if (temp == null) { return null; @@ -39,6 +58,28 @@ public Card getCard(int index) { } public int numSets() { - return 0; + int num = 0; + if (numCards() < 3) { + return 0; + } + else { + TableNode n1 = head; + while (n1 != null && n1.getNext().getNext() != null) { + TableNode n2 = n1.getNext(); + while (n2 != null && n2.getNext() != null) { + TableNode n3 = n2.getNext(); + while (n3 != null) { + if (n3.getCard().isSet(n2.getCard(), n1.getCard()) == true) { + num += 1; + } + n3 = n3.getNext(); + } + n2 = n2.getNext(); + } + n1 = n1.getNext(); + + } + return num; + } } } \ No newline at end of file diff --git a/src/TestTable.java b/src/TestTable.java new file mode 100644 index 0000000..25dae37 --- /dev/null +++ b/src/TestTable.java @@ -0,0 +1,100 @@ +import junit.framework.TestCase; + +public class TestTable extends TestCase { + + public void testEmptyTable() { + Table t = new Table(); + + assertEquals(0, t.numCards()); + assertEquals(null, t.getCard(0)); + assertEquals(0, t.numSets()); + + } + + public void testOneCardTable() { + Table t = new Table(); + Card c1 = new Card(1, 1, 1, 1); + + t.add(c1); + + assertEquals(1, t.numCards()); + assertEquals(c1, t.getCard(0)); + assertEquals(0, t.numSets()); + + } + + public void testTwoCardTable() { + Table t = new Table(); + Card c1 = new Card(1, 1, 1, 1); + Card c2 = new Card(2, 2, 2, 2); + + t.add(c1); + t.add(c2); + + assertEquals(2, t.numCards()); + assertEquals(c1, t.getCard(1)); + assertEquals(c2, t.getCard(0)); + assertEquals(0, t.numSets()); + + } + + public void testOneSetCardTable() { + Table t = new Table(); + Card c1 = new Card(1, 1, 1, 2); + Card c2 = new Card(2, 1, 1, 1); + Card c3 = new Card(3, 1, 1, 3); + + t.add(c2); + t.add(c1); + t.add(c3); + + assertEquals(3, t.numCards()); + assertEquals(c1, t.getCard(1)); + assertEquals(c2, t.getCard(2)); + assertEquals(c3, t.getCard(0)); + assertEquals(1, t.numSets()); + + } + + public void testNoSetCardTable() { + Table t = new Table(); + Card c1 = new Card(1, 2, 1, 2); + Card c2 = new Card(2, 1, 1, 1); + Card c3 = new Card(3, 1, 1, 3); + + t.add(c2); + t.add(c1); + t.add(c3); + + assertEquals(3, t.numCards()); + assertEquals(c1, t.getCard(1)); + assertEquals(c2, t.getCard(2)); + assertEquals(c3, t.getCard(0)); + assertEquals(0, t.numSets()); + + } + + public void testOneSetFiveCardsCardTable() { + Table t = new Table(); + Card c1 = new Card(1, 2, 1, 2); + Card c2 = new Card(2, 1, 1, 1); + Card c3 = new Card(3, 3, 1, 3); + Card c4 = new Card(3, 2, 1, 1); + Card c5 = new Card(2, 2, 2, 2); + + t.add(c1); + t.add(c2); + t.add(c3); + t.add(c4); + t.add(c5); + + assertEquals(5, t.numCards()); + assertEquals(c1, t.getCard(4)); + assertEquals(c2, t.getCard(3)); + assertEquals(c3, t.getCard(2)); + assertEquals(c4, t.getCard(1)); + assertEquals(c5, t.getCard(0)); + assertEquals(1, t.numSets()); + + } +} From 49808080411a35dd6356aae13d84250e634a8f23 Mon Sep 17 00:00:00 2001 From: Anna Lamoureux Date: Wed, 15 Apr 2015 01:23:33 -0400 Subject: [PATCH 07/14] Final submission for Test and TestTable. Does not pass last test. --- src/Card.java | 9 +++--- src/Table.java | 74 ++++++++++++++++++++++++++++++++++++---------- src/TestTable.java | 61 +++++++++++++++++++++++++++++++++++++- 3 files changed, 124 insertions(+), 20 deletions(-) diff --git a/src/Card.java b/src/Card.java index 1fc970e..3fc295b 100644 --- a/src/Card.java +++ b/src/Card.java @@ -50,10 +50,11 @@ public int getShape() { } public boolean isSet(Card c1, Card c2) { - int sum = quantity + color + shading + shape; - int card1 = c1.getQuantity() + c1.getColor() + c1.getShading() + c1.getShape(); - int card2 = c2.getQuantity() + c2.getColor() + c2.getShading() + c2.getShape(); - if ((card1 + card2 + sum) % 3 == 0) { + int quantityset = quantity + c1.getQuantity() + c2.getQuantity(); + int colorset = color + c1.getColor() + c2.getColor(); + int shadeset = shading + c1.getShading() + c2.getShading(); + int shapeset = shape + c1.getShape() + c2.getShape(); + if (quantityset % 3 == 0 && colorset % 3 == 0 && shadeset % 3 == 0 && shapeset % 3 == 0) { return true; } else { diff --git a/src/Table.java b/src/Table.java index 3215e76..75c8546 100644 --- a/src/Table.java +++ b/src/Table.java @@ -12,43 +12,87 @@ public void add(Card c) { } public void removeSet(Card c1, Card c2, Card c3) { - //if the 3 cards don't form a set or if any of the cards are not on the table, return - //otherwise, remove c1, c2, c3, preserving relative order of the rest of the cards - /*if (c1.isSet(c2, c3)) { + int n = 0; + if (c1.isSet(c2, c3) == false) { + return; + } + + else { Card remove1 = null; Card remove2 = null; Card remove3 = null; TableNode temp = head; - TableNode prev = head; + while (temp != null) { - if (temp.getCard() == c1) { - temp = temp.getNext(); + if (c1 == temp.getCard()) { + remove1 = c1; c1 = null; + temp = temp.getNext(); + } + if (c2 == temp.getCard()) { + remove2 = c2; + c2 = null; + temp = temp.getNext(); + } + if (c3 == temp.getCard()) { + remove3 = c3; + c3 = null; + temp = temp.getNext(); + } + return; + } + + if (c1 == remove1 && c2 == remove2 && c3 == remove3) { + temp = head; + TableNode prev = null; + + while (temp != null && n < 3) { + if (c1 == temp.getCard() || c2 == temp.getCard() || c3 == temp.getCard()) { + if (temp == head) { + prev = temp; + temp = temp.getNext(); + prev.setNext(null); + temp = null; + n += 1; + } + else { + prev.setNext(temp.getNext()); + temp.setNext(null); + n += 1; + } + } + return; + } + + if (temp.getNext() == null && prev != null) { + temp = prev.getNext(); } - } + } } - else { - return; - }*/ + return; } public int numCards() { TableNode temp = head; int num = 0; - while (temp != null) { - num += 1; - temp = temp.getNext(); - } + while (temp != null) { + num += 1; + temp = temp.getNext(); + } return num; } public Card getCard(int index) { - //if index is out of bounds, return null + if (index > numCards() - 1) { + return null; + } + TableNode temp = head; if (temp == null) { return null; } + else { for (int i = 0; i < index; i++) { temp = temp.getNext(); diff --git a/src/TestTable.java b/src/TestTable.java index 25dae37..b2230b2 100644 --- a/src/TestTable.java +++ b/src/TestTable.java @@ -80,7 +80,7 @@ public void testOneSetFiveCardsCardTable() { Card c2 = new Card(2, 1, 1, 1); Card c3 = new Card(3, 3, 1, 3); Card c4 = new Card(3, 2, 1, 1); - Card c5 = new Card(2, 2, 2, 2); + Card c5 = new Card(2, 2, 3, 2); t.add(c1); t.add(c2); @@ -94,7 +94,66 @@ public void testOneSetFiveCardsCardTable() { assertEquals(c3, t.getCard(2)); assertEquals(c4, t.getCard(1)); assertEquals(c5, t.getCard(0)); + assertEquals(null, t.getCard(5)); assertEquals(1, t.numSets()); } + + public void testNoSetRemove() { + Table t = new Table(); + Card c1 = new Card(1, 1, 1, 1); + Card c2 = new Card(2, 1, 1, 1); + Card c3 = new Card(3, 1, 1, 1); + Card c4 = new Card(3, 2, 1, 1); + Card c5 = new Card(2, 2, 3, 2); + + t.add(c1); + t.add(c2); + t.add(c3); + t.add(c4); + t.add(c5); + + assertEquals(5, t.numCards()); + assertEquals(c1, t.getCard(4)); + assertEquals(c2, t.getCard(3)); + assertEquals(c3, t.getCard(2)); + assertEquals(c4, t.getCard(1)); + assertEquals(c5, t.getCard(0)); + assertEquals(null, t.getCard(5)); + assertEquals(1, t.numSets()); + + t.removeSet(c1, c2, c4); + + assertEquals(5, t.numCards()); + assertEquals(1, t.numSets()); + } + + public void testOneSetRemove() { + Table t = new Table(); + Card c1 = new Card(1, 1, 1, 1); + Card c2 = new Card(2, 1, 1, 1); + Card c3 = new Card(3, 1, 1, 1); + Card c4 = new Card(3, 2, 1, 1); + Card c5 = new Card(2, 2, 3, 2); + + t.add(c1); + t.add(c2); + t.add(c3); + t.add(c4); + t.add(c5); + + assertEquals(5, t.numCards()); + assertEquals(c1, t.getCard(4)); + assertEquals(c2, t.getCard(3)); + assertEquals(c3, t.getCard(2)); + assertEquals(c4, t.getCard(1)); + assertEquals(c5, t.getCard(0)); + assertEquals(null, t.getCard(5)); + assertEquals(1, t.numSets()); + + t.removeSet(c1, c2, c3); + + assertEquals(2, t.numCards()); + assertEquals(0, t.numSets()); + } } From 54ad99f3b8218a42cd8223bfa48fd24eddb776cc Mon Sep 17 00:00:00 2001 From: Anna Lamoureux Date: Thu, 16 Apr 2015 13:46:52 -0400 Subject: [PATCH 08/14] Working Table.java with all tests passing. --- src/Table.java | 43 +++++++++++++------------------------- src/TestTable.java | 52 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 28 deletions(-) diff --git a/src/Table.java b/src/Table.java index 75c8546..60e9376 100644 --- a/src/Table.java +++ b/src/Table.java @@ -12,65 +12,52 @@ public void add(Card c) { } public void removeSet(Card c1, Card c2, Card c3) { - int n = 0; if (c1.isSet(c2, c3) == false) { return; } else { - Card remove1 = null; - Card remove2 = null; - Card remove3 = null; + boolean isRemove1 = false; + boolean isRemove2 = false; + boolean isRemove3 = false; TableNode temp = head; while (temp != null) { if (c1 == temp.getCard()) { - remove1 = c1; - c1 = null; - temp = temp.getNext(); + isRemove1 = true; } if (c2 == temp.getCard()) { - remove2 = c2; - c2 = null; - temp = temp.getNext(); + isRemove2 = true; } if (c3 == temp.getCard()) { - remove3 = c3; - c3 = null; - temp = temp.getNext(); + isRemove3 = true; } - return; + temp = temp.getNext(); } - if (c1 == remove1 && c2 == remove2 && c3 == remove3) { + if (isRemove1 && isRemove2 && isRemove3) { temp = head; TableNode prev = null; - while (temp != null && n < 3) { + while (temp != null) { if (c1 == temp.getCard() || c2 == temp.getCard() || c3 == temp.getCard()) { if (temp == head) { - prev = temp; temp = temp.getNext(); - prev.setNext(null); - temp = null; - n += 1; + head = head.getNext(); } else { prev.setNext(temp.getNext()); - temp.setNext(null); - n += 1; + temp = temp.getNext(); } } - return; - } - - if (temp.getNext() == null && prev != null) { - temp = prev.getNext(); + else { + prev = temp; + temp = temp.getNext(); + } } } } - return; } public int numCards() { diff --git a/src/TestTable.java b/src/TestTable.java index b2230b2..e9bc3ec 100644 --- a/src/TestTable.java +++ b/src/TestTable.java @@ -156,4 +156,56 @@ public void testOneSetRemove() { assertEquals(2, t.numCards()); assertEquals(0, t.numSets()); } + + public void testAddOneAtATime() { + Table t = new Table(); + Card c1 = new Card(1, 1, 1, 1); + Card c2 = new Card(2, 1, 1, 1); + Card c3 = new Card(3, 1, 1, 1); + Card c4 = new Card(3, 2, 1, 1); + Card c5 = new Card(2, 2, 3, 2); + + t.add(c1); + assertEquals(1, t.numCards()); + assertEquals(c1, t.getCard(0)); + assertEquals(0, t.numSets()); + + t.add(c2); + assertEquals(2, t.numCards()); + assertEquals(c2, t.getCard(0)); + assertEquals(c1, t.getCard(1)); + assertEquals(0, t.numSets()); + + t.add(c3); + assertEquals(3, t.numCards()); + assertEquals(c1, t.getCard(2)); + assertEquals(c2, t.getCard(1)); + assertEquals(c3, t.getCard(0)); + assertEquals(1, t.numSets()); + + t.add(c4); + assertEquals(4, t.numCards()); + assertEquals(c1, t.getCard(3)); + assertEquals(c2, t.getCard(2)); + assertEquals(c3, t.getCard(1)); + assertEquals(c4, t.getCard(0)); + assertEquals(1, t.numSets()); + + t.add(c5); + assertEquals(5, t.numCards()); + assertEquals(c1, t.getCard(4)); + assertEquals(c2, t.getCard(3)); + assertEquals(c3, t.getCard(2)); + assertEquals(c4, t.getCard(1)); + assertEquals(c5, t.getCard(0)); + assertEquals(1, t.numSets()); + + t.removeSet(c1, c3, c4); + assertEquals(5, t.numCards()); + assertEquals(1, t.numSets()); + + t.removeSet(c1, c2, c3); + assertEquals(2, t.numCards()); + assertEquals(0, t.numSets()); + } } From 1f0ab8c04c73b5f62be0cd4f7e73fdcd87f135ce Mon Sep 17 00:00:00 2001 From: Anna Lamoureux Date: Thu, 16 Apr 2015 18:32:35 -0400 Subject: [PATCH 09/14] Changed Table.java from == to .equals --- src/Table.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Table.java b/src/Table.java index 60e9376..5530e5a 100644 --- a/src/Table.java +++ b/src/Table.java @@ -24,13 +24,13 @@ public void removeSet(Card c1, Card c2, Card c3) { TableNode temp = head; while (temp != null) { - if (c1 == temp.getCard()) { + if (c1.equals(temp.getCard())) { isRemove1 = true; } - if (c2 == temp.getCard()) { + if (c2.equals(temp.getCard())) { isRemove2 = true; } - if (c3 == temp.getCard()) { + if (c3.equals(temp.getCard())) { isRemove3 = true; } temp = temp.getNext(); @@ -41,7 +41,7 @@ public void removeSet(Card c1, Card c2, Card c3) { TableNode prev = null; while (temp != null) { - if (c1 == temp.getCard() || c2 == temp.getCard() || c3 == temp.getCard()) { + if (c1.equals(temp.getCard()) || c2.equals(temp.getCard()) || c3.equals(temp.getCard())) { if (temp == head) { temp = temp.getNext(); head = head.getNext(); From 0638c72119b338b335b228e2698c9f31ea2cc461 Mon Sep 17 00:00:00 2001 From: Anna Lamoureux Date: Thu, 23 Apr 2015 19:10:27 -0400 Subject: [PATCH 10/14] Need more tests and and some tests are failing. --- src/Game.java | 74 +++++++++++++++++++++++++++++++++++++++++++++++ src/TestGame.java | 49 +++++++++++++++++++++++++++++++ src/datDeck.dat | 13 +++++++++ src/deck1.dat | 12 ++++++++ src/newDeck.dat | 13 +++++++++ 5 files changed, 161 insertions(+) create mode 100644 src/Game.java create mode 100644 src/TestGame.java create mode 100644 src/datDeck.dat create mode 100644 src/deck1.dat create mode 100644 src/newDeck.dat diff --git a/src/Game.java b/src/Game.java new file mode 100644 index 0000000..ae2014e --- /dev/null +++ b/src/Game.java @@ -0,0 +1,74 @@ +public class Game { + Table t; + Deck d; + + public Game() { + t = new Table(); + d = new Deck(); + + for (int a = 0; a < 12; a++) { + t.add(d.getNext()); + } + } + + public Game(String filename) { + t = new Table(); + d = new Deck(filename); + + while (d.hasNext()) { + t.add(d.getNext()); + if (t.numCards() > 11) { + return; + } + } + } + + public int numSets() { + return t.numSets(); + } + + public int numCards() { + return t.numCards(); + + } + + public void playRound() { + if (t.numSets() != 0) { + for (int i = 0; i < (t.numCards() - 2); i++) { + for (int j = i + 1; j < (t.numCards() - 1); j++) { + for (int k = j + 1; k < t.numCards(); k++) { + if (t.getCard(k).isSet(t.getCard(i), t.getCard(j))) { + t.removeSet(t.getCard(k), t.getCard(i), t.getCard(j)); + return; + } + } + } + } + } + if (d.hasNext()) { + if (t.numCards() < 12) { + for (int b = 0; b < (12 - t.numCards()); b++) { + t.add(d.getNext()); + } + } + else { + if (t.numSets() == 0) { + for (int c = 0; c <= 3; c++) { + t.add(d.getNext()); + } + } + } + } + } + + public boolean isGameOver() { + if (!d.hasNext() && t.numSets() == 0) { + return true; + } + + else { + return false; + } + } +} + \ No newline at end of file diff --git a/src/TestGame.java b/src/TestGame.java new file mode 100644 index 0000000..c832ca3 --- /dev/null +++ b/src/TestGame.java @@ -0,0 +1,49 @@ +import junit.framework.TestCase; + +public class TestGame extends TestCase { + + public void testAutoTable() { + Game g = new Game(); + + assertEquals(12, g.numCards()); + } + + public void testFileTableUnderTwelve() { + Game g = new Game("newDeck.dat"); + + assertEquals(5, g.numCards()); + assertEquals(1, g.numSets()); + + g.playRound(); + + assertEquals(2, g.numCards()); + assertEquals(0, g.numSets()); + assertTrue(g.isGameOver()); + } + + public void testFileTableTwelve() { + Game g = new Game("deck1.dat"); + + assertEquals(12, g.numCards()); + assertEquals(4, g.numSets()); + + g.playRound(); + + assertEquals(9, g.numCards()); + assertEquals(3, g.numSets()); + assertTrue(g.isGameOver()); + } + + public void testFileTableOverTwelve() { + Game g = new Game("datDeck.dat"); + + assertEquals(12, g.numCards()); + assertEquals(4, g.numSets()); + + g.playRound(); + + assertEquals(10, g.numCards()); + assertEquals(3, g.numSets()); + assertFalse(g.isGameOver()); + } +} diff --git a/src/datDeck.dat b/src/datDeck.dat new file mode 100644 index 0000000..275961d --- /dev/null +++ b/src/datDeck.dat @@ -0,0 +1,13 @@ +1 2 3 3 +2 2 2 2 +1 1 2 2 +1 1 1 1 +3 2 1 1 +2 3 3 3 +1 2 2 1 +3 3 1 2 +2 3 3 1 +3 3 3 3 +1 2 3 3 +3 2 1 2 +3 2 1 3 \ No newline at end of file diff --git a/src/deck1.dat b/src/deck1.dat new file mode 100644 index 0000000..b16883a --- /dev/null +++ b/src/deck1.dat @@ -0,0 +1,12 @@ +1 2 3 3 +2 2 2 2 +1 1 2 2 +1 1 1 1 +3 2 1 1 +2 3 3 3 +1 2 2 1 +3 3 1 2 +2 3 3 1 +3 3 3 3 +1 2 3 3 +3 2 1 2 \ No newline at end of file diff --git a/src/newDeck.dat b/src/newDeck.dat new file mode 100644 index 0000000..afe6f87 --- /dev/null +++ b/src/newDeck.dat @@ -0,0 +1,13 @@ +1 2 3 3 +2 2 2 2 +1 1 2 2 +1 1 1 1 +3 2 1 1 +# 2 3 3 3 +# 1 2 2 1 +# 3 3 1 2 +# 2 3 3 1 +# 3 3 3 3 +# 1 2 3 3 +# 3 2 1 1 +# 3 2 1 3 \ No newline at end of file From bfa14a2c58855730bd4b051ce38862abb30e67f8 Mon Sep 17 00:00:00 2001 From: Anna Lamoureux Date: Fri, 24 Apr 2015 00:36:08 -0400 Subject: [PATCH 11/14] Finished test and class with used .dat files --- src/Game.java | 37 +++++++++++++++++++------------- src/TestGame.java | 54 +++++++++++++++++++++++++++++++++++++++++------ src/datDeck.dat | 22 +++++++++---------- src/deck1.dat | 20 +++++++++--------- src/dick.dat | 12 +++++++++++ src/dick2.dat | 13 ++++++++++++ src/overlap.dat | 5 +++++ 7 files changed, 120 insertions(+), 43 deletions(-) create mode 100644 src/dick.dat create mode 100644 src/dick2.dat create mode 100644 src/overlap.dat diff --git a/src/Game.java b/src/Game.java index ae2014e..63e2683 100644 --- a/src/Game.java +++ b/src/Game.java @@ -33,34 +33,41 @@ public int numCards() { } public void playRound() { + if (t.numSets() == 0 && d.hasNext()) { + for (int c = 0; c < 3; c++) { + if (d.hasNext()) { + t.add(d.getNext()); + } + else { + return; + } + } + } + if (t.numSets() != 0) { for (int i = 0; i < (t.numCards() - 2); i++) { for (int j = i + 1; j < (t.numCards() - 1); j++) { for (int k = j + 1; k < t.numCards(); k++) { if (t.getCard(k).isSet(t.getCard(i), t.getCard(j))) { t.removeSet(t.getCard(k), t.getCard(i), t.getCard(j)); + if (t.numCards() < 12 && d.hasNext()) { + for (int c = 0; c < 3; c++) { + if (d.hasNext()) { + t.add(d.getNext()); + } + else { + return; + } + } + } return; } } } } } - if (d.hasNext()) { - if (t.numCards() < 12) { - for (int b = 0; b < (12 - t.numCards()); b++) { - t.add(d.getNext()); - } - } - else { - if (t.numSets() == 0) { - for (int c = 0; c <= 3; c++) { - t.add(d.getNext()); - } - } - } - } } - + public boolean isGameOver() { if (!d.hasNext() && t.numSets() == 0) { return true; diff --git a/src/TestGame.java b/src/TestGame.java index c832ca3..65dfea9 100644 --- a/src/TestGame.java +++ b/src/TestGame.java @@ -8,7 +8,7 @@ public void testAutoTable() { assertEquals(12, g.numCards()); } - public void testFileTableUnderTwelve() { + public void testFileTableUnderTwelveOneSet() { Game g = new Game("newDeck.dat"); assertEquals(5, g.numCards()); @@ -21,29 +21,69 @@ public void testFileTableUnderTwelve() { assertTrue(g.isGameOver()); } - public void testFileTableTwelve() { + public void testFileTableTwelveOneSet() { Game g = new Game("deck1.dat"); assertEquals(12, g.numCards()); - assertEquals(4, g.numSets()); + assertEquals(1, g.numSets()); g.playRound(); assertEquals(9, g.numCards()); - assertEquals(3, g.numSets()); + assertEquals(0, g.numSets()); assertTrue(g.isGameOver()); } - public void testFileTableOverTwelve() { + public void testFileTableOverTwelveOneSet() { Game g = new Game("datDeck.dat"); assertEquals(12, g.numCards()); - assertEquals(4, g.numSets()); + assertEquals(1, g.numSets()); g.playRound(); assertEquals(10, g.numCards()); - assertEquals(3, g.numSets()); + assertEquals(0, g.numSets()); + assertTrue(g.isGameOver()); + } + + public void testFileTableTwelveTwoSet() { + Game g = new Game("dick.dat"); + + assertEquals(12, g.numCards()); + assertEquals(2, g.numSets()); + + g.playRound(); + + assertEquals(9, g.numCards()); + assertEquals(1, g.numSets()); assertFalse(g.isGameOver()); } + + public void testFileTableOverTwelveTwoSet() { + Game g = new Game("dick2.dat"); + + assertEquals(12, g.numCards()); + assertEquals(2, g.numSets()); + + g.playRound(); + + assertEquals(10, g.numCards()); + assertEquals(1, g.numSets()); + assertFalse(g.isGameOver()); + } + + public void testFileTwoOverlappingSets() { + Game g = new Game("overlap.dat"); + + assertEquals(5, g.numCards()); + assertEquals(2, g.numSets()); + + g.playRound(); + + assertEquals(2, g.numCards()); + assertEquals(0, g.numSets()); + assertTrue(g.isGameOver()); + } + } diff --git a/src/datDeck.dat b/src/datDeck.dat index 275961d..8da63d9 100644 --- a/src/datDeck.dat +++ b/src/datDeck.dat @@ -1,13 +1,13 @@ -1 2 3 3 -2 2 2 2 -1 1 2 2 1 1 1 1 -3 2 1 1 -2 3 3 3 -1 2 2 1 -3 3 1 2 -2 3 3 1 +1 1 1 2 +1 1 1 3 +3 1 1 1 +3 1 1 2 +3 1 2 3 +3 1 3 3 3 3 3 3 -1 2 3 3 -3 2 1 2 -3 2 1 3 \ No newline at end of file +3 3 2 2 +3 3 1 3 +3 3 3 2 +1 2 2 2 +1 2 1 2 \ No newline at end of file diff --git a/src/deck1.dat b/src/deck1.dat index b16883a..0215970 100644 --- a/src/deck1.dat +++ b/src/deck1.dat @@ -1,12 +1,12 @@ -1 2 3 3 -2 2 2 2 -1 1 2 2 1 1 1 1 -3 2 1 1 -2 3 3 3 -1 2 2 1 -3 3 1 2 -2 3 3 1 +1 1 1 2 +1 1 1 3 +3 1 1 1 +3 1 1 2 +3 1 2 3 3 3 3 3 -1 2 3 3 -3 2 1 2 \ No newline at end of file +3 3 2 2 +3 3 1 3 +3 3 3 2 +1 2 2 2 +1 2 1 2 \ No newline at end of file diff --git a/src/dick.dat b/src/dick.dat new file mode 100644 index 0000000..693ed0f --- /dev/null +++ b/src/dick.dat @@ -0,0 +1,12 @@ +1 1 1 1 +1 1 1 2 +1 1 1 3 +3 1 1 1 +3 1 1 2 +3 1 2 3 +3 3 3 1 +3 3 2 2 +3 3 1 3 +3 3 3 2 +1 2 2 2 +1 2 1 2 \ No newline at end of file diff --git a/src/dick2.dat b/src/dick2.dat new file mode 100644 index 0000000..a71984a --- /dev/null +++ b/src/dick2.dat @@ -0,0 +1,13 @@ +1 1 1 1 +1 1 1 2 +1 1 1 3 +1 2 2 2 +1 2 1 2 +1 2 2 3 +3 1 1 1 +3 1 1 2 +3 1 2 3 +3 3 3 1 +3 3 2 2 +3 3 1 3 +3 3 3 2 \ No newline at end of file diff --git a/src/overlap.dat b/src/overlap.dat new file mode 100644 index 0000000..ed8ed30 --- /dev/null +++ b/src/overlap.dat @@ -0,0 +1,5 @@ +1 1 1 1 +1 2 2 1 +1 3 3 1 +2 1 3 3 +3 2 3 2 \ No newline at end of file From d0c5a58543470c24c9f1407ab8c2e9d374a8e585 Mon Sep 17 00:00:00 2001 From: Anna Lamoureux Date: Mon, 27 Apr 2015 12:24:27 -0400 Subject: [PATCH 12/14] Fixed Game.java to pass all of tests from GameTestMilestone.java --- src/Game.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/Game.java b/src/Game.java index 63e2683..0e88b4d 100644 --- a/src/Game.java +++ b/src/Game.java @@ -33,17 +33,24 @@ public int numCards() { } public void playRound() { - if (t.numSets() == 0 && d.hasNext()) { + if (t.numSets() == 0 && d.hasNext() && t.numCards() > 11) { for (int c = 0; c < 3; c++) { if (d.hasNext()) { t.add(d.getNext()); - } - else { - return; - } + } } + return; } - + + if (t.numCards() == 0 && d.hasNext()) { + for (int c = 0; c < 12; c++) { + if (d.hasNext()) { + t.add(d.getNext()); + } + } + return; + } + if (t.numSets() != 0) { for (int i = 0; i < (t.numCards() - 2); i++) { for (int j = i + 1; j < (t.numCards() - 1); j++) { From 7b47acf95927ccffed1659eeb35aabb956866c5b Mon Sep 17 00:00:00 2001 From: Anna Lamoureux Date: Mon, 27 Apr 2015 12:42:39 -0400 Subject: [PATCH 13/14] Monte Carlo Simulation that calculates average number of rounds per game --- src/MonteCarloSimulation.java | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/MonteCarloSimulation.java diff --git a/src/MonteCarloSimulation.java b/src/MonteCarloSimulation.java new file mode 100644 index 0000000..2d79b1f --- /dev/null +++ b/src/MonteCarloSimulation.java @@ -0,0 +1,20 @@ +public class MonteCarloSimulation { + public static void main(String[] args) { + int numGames = 0; + float averageRounds = 0; + int numRounds = 0; + int gameRuns = 100000; + + for (int i = 0; i < gameRuns; i++) { + Game g = new Game(); + + while (!g.isGameOver()) { + g.playRound(); + numRounds += 1; + } + numGames += 1; + } + averageRounds = numRounds / numGames; + System.out.println("The average number of rounds per Set game: " + averageRounds); + } +} \ No newline at end of file From 5e67fc93380b7b4005d7bb741af4a74af2cb9eb1 Mon Sep 17 00:00:00 2001 From: Anna Lamoureux Date: Wed, 29 Apr 2015 14:20:09 -0400 Subject: [PATCH 14/14] Updated Monte Carlo Simulation with three test results --- src/MonteCarloSimulation.java | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/MonteCarloSimulation.java b/src/MonteCarloSimulation.java index 2d79b1f..6ea3292 100644 --- a/src/MonteCarloSimulation.java +++ b/src/MonteCarloSimulation.java @@ -1,10 +1,13 @@ public class MonteCarloSimulation { public static void main(String[] args) { - int numGames = 0; - float averageRounds = 0; int numRounds = 0; + float avgNumRounds; + float avgCardsRemain; + float avgNumSets; int gameRuns = 100000; - + long numSets = 0; + int numCardsRemain = 0; + for (int i = 0; i < gameRuns; i++) { Game g = new Game(); @@ -12,9 +15,22 @@ public static void main(String[] args) { g.playRound(); numRounds += 1; } - numGames += 1; + numCardsRemain += g.numCards(); + } + + for (int j = 0; j < gameRuns; j++) { + Game g = new Game(); + + g.playRound(); + + numSets += g.numSets(); + } - averageRounds = numRounds / numGames; - System.out.println("The average number of rounds per Set game: " + averageRounds); + avgCardsRemain = numCardsRemain / gameRuns; + avgNumSets = (float) numSets / gameRuns; + avgNumRounds = numRounds / gameRuns; + System.out.println("The average number of rounds per Set game: " + avgNumRounds); + System.out.println("The average number of cards remaining per Set game: " + avgCardsRemain); + System.out.println("The average number of sets per table dealt: " + avgNumSets); } } \ No newline at end of file