From 0e2b5c673a1a1976829b98220ad713628848c3ee Mon Sep 17 00:00:00 2001 From: Marisa Pacitti Date: Wed, 1 Apr 2015 10:11:14 -0400 Subject: [PATCH 1/6] Card Card Test --- src/Card.java | 68 +++++++++++++++++++++++++++++++++++++++++++++-- src/CardTest.java | 18 +++++++++++++ 2 files changed, 84 insertions(+), 2 deletions(-) mode change 100755 => 100644 src/Card.java create mode 100644 src/CardTest.java diff --git a/src/Card.java b/src/Card.java old mode 100755 new mode 100644 index 9eed9a5..332695c --- a/src/Card.java +++ b/src/Card.java @@ -1,7 +1,71 @@ public class Card { - // Create the rest of this class yourself + public int quantity; + public int color; + public int shading; + public int shape; + + public Card (int cardQuantity, int cardColor, int cardShading, int cardShape){ + quantity = ((((cardQuantity % 3)+3)%3)+1); + color = ((((cardColor % 3)+3)%3)+1); + shading = ((((cardShading % 3)+3)%3)+1); + shape = ((((cardShape % 3)+3)%3)+1); + } + + public int getQuantity() { + return quantity; + } + + public int getColor() { + return color; + } + + public int getShading() { + return shading; + } + + public int getShape() { + return shape; + } + + public boolean isSet (Card cardA, Card cardB) { + if ((((quantity + cardA.getQuantity() + cardA.getQuantity())%3)==0) && + (((color + cardA.getColor() + cardA.getColor())%3)==0) && + (((shading + cardA.getShading() + cardA.getShading())%3)==0) && + (((shape + cardA.getShape() + cardA.getShape())%3)==0)) + return true; + else + return false; + } + + public String toString() { + String str = ""; + String col = ""; + String shade = ""; + String sha = ""; + + if (color == 1) + col = "R"; + else if (color == 2) + col = "G"; + else + col = "P"; + + if (shading == 1) + shade = "O"; + else if (color == 2) + shade = "T"; + else + shade = "S"; + + if (shape == 1) + sha = "O"; + else if (color == 2) + sha = "D"; + else + sha = "S"; + + return str = quantity + col + shade + sha; - public boolean equals(Object obj) { Card that = (Card)obj; return quantity == that.getQuantity() && diff --git a/src/CardTest.java b/src/CardTest.java new file mode 100644 index 0000000..addd2a4 --- /dev/null +++ b/src/CardTest.java @@ -0,0 +1,18 @@ +import junit.framework.TestCase; + +pubic class CardTest extends TestCase{ + + public void testIsSet() { + Card c1 = new Card(1, 2, 1, 3); + Card c2 = new Card(2, 1, 3, 2); + Card c3 = new Card(3, 3, 2, 1); + assertEquals(true, c1.isSet(c2, c3)); + } + + public void testNotSet() { + Card c1 = new Card(1, 1, 1, 3); + Card c2 = new Card(2, 2, 3, 2); + Card c3 = new Card(3, 2, 2, 3); + assertEquals(false, c1.isSet(c2, c3)); + } +} \ No newline at end of file From 5051e4a7a9d102d8dabcaef7c5ebe5e36ca5057c Mon Sep 17 00:00:00 2001 From: Marisa Pacitti Date: Wed, 8 Apr 2015 08:55:25 -0400 Subject: [PATCH 2/6] Deck and Deck Test --- src/3card.dat | 3 +++ src/Deck.java | 39 +++++++++++++++++++++++++++++++++++++++ src/DeckTest.java | 28 ++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 src/3card.dat mode change 100755 => 100644 src/Deck.java create mode 100644 src/DeckTest.java diff --git a/src/3card.dat b/src/3card.dat new file mode 100644 index 0000000..aaf05fe --- /dev/null +++ b/src/3card.dat @@ -0,0 +1,3 @@ +2 0 3 1 +3 1 2 1 +1 3 2 0 \ No newline at end of file diff --git a/src/Deck.java b/src/Deck.java old mode 100755 new mode 100644 index ab3a2a3..7dc2ed9 --- a/src/Deck.java +++ b/src/Deck.java @@ -2,8 +2,47 @@ import java.io.FileReader; import java.util.StringTokenizer; import java.util.ArrayList; +import java.util.Collections; + public class Deck { + public ArrayList cards; + public int nextCardIndex; + + public Deck() { + cards = new ArrayList(81); + for(int var1 = 1; var1 <= 3; var1++) { + for(int var2 = 1; var2 <= 3; var2++) { + for(int var3 = 1; var3 <= 3; var3++) { + for(int var4 = 1; var4 <= 3; var4++) { + cards.add(new Card(var1, var2, var3, var4)); + } + } + } + } + int nextCardIndex = 0; + Collections.shuffle(cards); + } + + public boolean hasNext() { + if (nextCardIndex >= cards.size()) { + return false; + } + else { + return true; + } + } + + public Card getNext() { + if (hasNext() == false) { + return null; + } + else { + nextCardIndex += 1; + return cards.get(nextCardIndex - 1); + } + } + // Implement the rest of this class yourself public Deck(String filename) { diff --git a/src/DeckTest.java b/src/DeckTest.java new file mode 100644 index 0000000..562d3a8 --- /dev/null +++ b/src/DeckTest.java @@ -0,0 +1,28 @@ +import junit.framework.TestCase; +public class DeckTest extends TestCase { + + public void testWorking() { + Deck d = new Deck(); + assertEquals(81, d.cards.size()); + } + + public void testhasNext() { + Deck d = new Deck(); + assertTrue(d.hasNext()); + } + + public void testDeckCards() { + Deck d = new Deck(); + Card c1 = new Card(2, 3, 0, 1); + assertTrue(d.hasNext()); + assertEquals(false, c1.equals(d.getNext())); + Card c2 = new Card(3, 1, 2, 1); + assertTrue(d.hasNext()); + assertEquals(false, c2.equals(d.getNext())); + Card c3 = new Card(1, 3, 2, 1); + assertTrue(d.hasNext()); + assertEquals(false, c3.equals(d.getNext())); + } + +} + \ No newline at end of file From 7d5d73004ac5cbd4acd1210e80ae8a57cb598cb3 Mon Sep 17 00:00:00 2001 From: Marisa Pacitti Date: Wed, 15 Apr 2015 08:54:02 -0400 Subject: [PATCH 3/6] Table and TableNode --- src/Table.java | 113 +++++++++++++++++++++++++++++++++++++++++ src/TableNode.java | 21 ++++++++ src/TableNodeTest.java | 21 ++++++++ src/TableTest.java | 21 ++++++++ 4 files changed, 176 insertions(+) create mode 100644 src/Table.java create mode 100644 src/TableNode.java create mode 100644 src/TableNodeTest.java create mode 100644 src/TableTest.java diff --git a/src/Table.java b/src/Table.java new file mode 100644 index 0000000..5ce60e9 --- /dev/null +++ b/src/Table.java @@ -0,0 +1,113 @@ +public class Table { + private TableNode head; + private int length; + + public Table() { + head = null; + length = 0; + } + + public void add(Card card) { + TableNode newNode = new TableNode(card); + if (head == null) { + head = newNode; + } + else { + newNode.setNext(head); + head = newNode; + } + } + + public void removeSet(Card card1, Card card2, Card card3) { + if (card1.isSet(card2, card3) == false) { + return; + } + TableNode temp = head; + Card cardnum1 = null; + Card cardnum2 = null; + Card cardnum3 = null; + + while(temp !=null) { + if(temp.getCard() == card1) { + temp = temp.getNext(); + card1 = null; + } + } + } + + //while (curr != null) { + //if (curr.getCard() == card1) { + //cardnum1 = card1; + // } + //if (curr.getCard() == card2) { + //cardnum2 = card2; + //} + //if (curr.getCard() == card3) { + //cardnum3 = card3; + //} + //curr = curr.getNext(); + //} + + // while (curr == null) { + //if (curr.getCard() != card1) { + //cardnum1 = card1; + //} + //if (curr.getCard() != card2) { + //cardnum2 = card2; + //} + //if (curr.getCard() != card3) { + //cardnum3 = card3; + //} + //} + //} + + public int numCards() { + TableNode temp = head; + int card = 0; + if (temp == null) { + return 0; + } + else { + while (temp != null) { + card += 1; + temp = temp.getNext(); + } + return card; + } + } + + public Card getCard(int index){ + TableNode temp = head; + if(temp == null) { + return null; + } + for(int t = 0; t < index; t++) { + temp = temp.getNext(); + if(temp==null) { + return null; + } + } + return temp.getCard(); + } + + public int numSets() { + TableNode card1 = head; + TableNode card2 = head; + TableNode card3 = head; + int setnum = 0; + for (int a = 0; a < length - 2; a++) { + for (int b = 1; b < length - 1; b++) { + for (int c = 2; c < length; c++){ + + Card carda = getCard(a); + Card cardb = getCard(b); + Card cardc = getCard(c); + if(carda.isSet(cardb, cardc)) { + setnum += 1; + } + } + } + } + return setnum; + } +} diff --git a/src/TableNode.java b/src/TableNode.java new file mode 100644 index 0000000..67fefd5 --- /dev/null +++ b/src/TableNode.java @@ -0,0 +1,21 @@ +public class TableNode { + private Card card; + private TableNode next; + + public TableNode(Card card1) { + card = card1; + next = null; + } + + public void setNext(TableNode nextcard) { + next = nextcard; + } + + public TableNode getNext() { + return next; + } + + public Card getCard() { + return card; + } +} \ No newline at end of file diff --git a/src/TableNodeTest.java b/src/TableNodeTest.java new file mode 100644 index 0000000..0ce3e1a --- /dev/null +++ b/src/TableNodeTest.java @@ -0,0 +1,21 @@ +import junit.framework.TestCase; + +public class TableNodeTest extends TestCase { + + public void testTableNodeCards() { + Card card1 = new Card(1, 3, 3, 2); + TableNode node1 = new TableNode(card1); + assertEquals(null, node1.getNext()); + assertEquals(card1, node1.getCard()); + + Card card2 = new Card(1, 3, 1, 2); + TableNode node2 = new TableNode(card2); + assertEquals(null, node2.getNext()); + assertEquals(card2, node2.getCard()); + + Card card3 = new Card(1, 3, 2, 2); + TableNode node3 = new TableNode(card3); + assertEquals(null, node3.getNext()); + assertEquals(card3, node3.getCard()); + } +} \ No newline at end of file diff --git a/src/TableTest.java b/src/TableTest.java new file mode 100644 index 0000000..fd994f0 --- /dev/null +++ b/src/TableTest.java @@ -0,0 +1,21 @@ +import junit.framework.TestCase; + +public class TableTest extends TestCase{ + + public void testTable() { + Table tt = new Table(); + Card card1 = new Card(1, 2, 3, 2); + tt.add(card1); + + Card card2 = new Card(3, 2, 3, 1); + tt.add(card2); + + Card card3 = new Card(1, 2, 2, 3); + tt.add(card3); + + assertEquals(3, tt.numCards()); + assertTrue(tt.getCard(0).equals(card3)); + assertTrue(tt.getCard(1).equals(card2)); + assertTrue(tt.getCard(2).equals(card1)); + } +} \ No newline at end of file From 830ecb2adcb959ebda2c4c342391b0f3e136fd33 Mon Sep 17 00:00:00 2001 From: Marisa Pacitti Date: Thu, 16 Apr 2015 16:47:43 -0400 Subject: [PATCH 4/6] New Table --- src/Table.java | 124 ++++++++++++++++++++++----------------------- src/TableTest.java | 13 +++-- 2 files changed, 71 insertions(+), 66 deletions(-) diff --git a/src/Table.java b/src/Table.java index 5ce60e9..b15235a 100644 --- a/src/Table.java +++ b/src/Table.java @@ -18,62 +18,62 @@ public void add(Card card) { } } - public void removeSet(Card card1, Card card2, Card card3) { - if (card1.isSet(card2, card3) == false) { - return; + public boolean onTable(Card card){ + TableNode node = head; + while (node != null) { + if (node.getCard().equals(card)) { + return true; + } + node = node.getNext(); } - TableNode temp = head; - Card cardnum1 = null; - Card cardnum2 = null; - Card cardnum3 = null; - - while(temp !=null) { - if(temp.getCard() == card1) { - temp = temp.getNext(); - card1 = null; + return false; + } + + + public void removeCard(Card card){ + TableNode newnode = null; + TableNode node = head; + while (node != null) { + if (node.getCard().equals(card)) { + if (newnode == null) { + head = node.getNext(); + } + else { + newnode.setNext(node.getNext()); + } + return; } + newnode = node; + node = node.getNext(); } } + + public void removeSet(Card card1, Card card2, Card card3) { + TableNode node = head; + TableNode curr = null; - //while (curr != null) { - //if (curr.getCard() == card1) { - //cardnum1 = card1; - // } - //if (curr.getCard() == card2) { - //cardnum2 = card2; - //} - //if (curr.getCard() == card3) { - //cardnum3 = card3; - //} - //curr = curr.getNext(); - //} - - // while (curr == null) { - //if (curr.getCard() != card1) { - //cardnum1 = card1; - //} - //if (curr.getCard() != card2) { - //cardnum2 = card2; - //} - //if (curr.getCard() != card3) { - //cardnum3 = card3; - //} - //} - //} + if(!card1.isSet(card2, card3)){ + return; + } + if(!onTable(card1) || !onTable(card2) || !onTable(card3)){ + return; + } + removeCard(card1); + removeCard(card2); + removeCard(card3); + } public int numCards() { TableNode temp = head; - int card = 0; + int numcount = 0; if (temp == null) { return 0; } - else { - while (temp != null) { - card += 1; - temp = temp.getNext(); - } - return card; + while (temp != null) { + numcount += 1; + temp = temp.getNext(); } + return numcount; } public Card getCard(int index){ @@ -90,24 +90,22 @@ public Card getCard(int index){ return temp.getCard(); } - public int numSets() { - TableNode card1 = head; - TableNode card2 = head; - TableNode card3 = head; - int setnum = 0; - for (int a = 0; a < length - 2; a++) { - for (int b = 1; b < length - 1; b++) { - for (int c = 2; c < length; c++){ - - Card carda = getCard(a); - Card cardb = getCard(b); - Card cardc = getCard(c); - if(carda.isSet(cardb, cardc)) { - setnum += 1; - } - } - } - } - return setnum; + + public int numSets(){ + int length = numCards(); + int count = 0; + for(int i= 0; i < length; i++){ + Card icard = getCard(i); + for(int j = i + 1; j < length; j++){ + Card jcard = getCard(j); + for(int k = j + 1; k < length; k++){ + Card kcard = getCard(k); + if(icard.isSet(jcard, kcard)){ + count++; + } + } + } + } + return count; } } diff --git a/src/TableTest.java b/src/TableTest.java index fd994f0..eaed0bd 100644 --- a/src/TableTest.java +++ b/src/TableTest.java @@ -2,9 +2,16 @@ public class TableTest extends TestCase{ - public void testTable() { + public void testEmpty() { Table tt = new Table(); - Card card1 = new Card(1, 2, 3, 2); + assertEquals(0, tt.numCards()); + assertEquals(null, tt.getCard(0)); + assertEquals(0, tt.numSets()); + } + + public void testCards() { + Table tt = new Table(); + Card card1 = new Card(2, 1, 3, 2); tt.add(card1); Card card2 = new Card(3, 2, 3, 1); @@ -16,6 +23,6 @@ public void testTable() { assertEquals(3, tt.numCards()); assertTrue(tt.getCard(0).equals(card3)); assertTrue(tt.getCard(1).equals(card2)); - assertTrue(tt.getCard(2).equals(card1)); + assertTrue(tt.getCard(2).equals(card1)); } } \ No newline at end of file From 705ddc8bcb6d2665928e287d983ddff47a4ce6e9 Mon Sep 17 00:00:00 2001 From: Marisa Pacitti Date: Fri, 24 Apr 2015 10:08:21 -0400 Subject: [PATCH 5/6] Game and GameTest --- src/Game.java | 89 +++++++++++++++++++++++++++++++++++++++++++++++ src/GameTest.java | 7 ++++ 2 files changed, 96 insertions(+) create mode 100644 src/Game.java create mode 100644 src/GameTest.java diff --git a/src/Game.java b/src/Game.java new file mode 100644 index 0000000..1edee49 --- /dev/null +++ b/src/Game.java @@ -0,0 +1,89 @@ +public class Game { + private Table t = new Table(); + private Deck d = new Deck(); + + public Game() { + Deck d = new Deck(); + for (int i = 0; i < 12; i++) { + t.add(d.getNext()); + } + } + + public Game(String filename) { + Deck d = new Deck(filename); + for (int i=0; i < 12; i++) { + d.getNext(); + } + } + + public int numSets() { + return t.numSets(); + } + + public int numCards() { + return t.numCards(); + } + + public void playRound() { + if (d.hasNext() == true && t.numSets() == 0) { + for (int r = 0; r < 3; r++) { + t.add(d.getNext()); + } + return; + } + + if (d.hasNext() == true && 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(i).isSet(t.getCard(j), t.getCard(k))) { + t.removeSet(t.getCard(i), t.getCard(j), t.getCard(k)); + for (int r = 0; r < 3; r++) { + t.add(d.getNext()); + } + } + } + } + } + } + + if (t.numCards() > 12 && 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(i).isSet(t.getCard(j), t.getCard(k))) { + t.removeSet(t.getCard(i), t.getCard(j), t.getCard(k)); + return; + } + } + } + } + } + + if (d.hasNext() == false && 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(i).isSet(t.getCard(j), t.getCard(k))) { + t.removeSet(t.getCard(i), t.getCard(j), t.getCard(k)); + return; + } + } + } + } + } + + if (d.hasNext() == false && t.numSets() == 0) { + isGameOver(); + } + } + + public boolean isGameOver() { + if (d.hasNext() == false && t.numSets() == 0) { + return true; + } + else { + return false; + } + } +} \ No newline at end of file diff --git a/src/GameTest.java b/src/GameTest.java new file mode 100644 index 0000000..ebfc76b --- /dev/null +++ b/src/GameTest.java @@ -0,0 +1,7 @@ +import junit.framework.TestCase; +public class GameTest extends TestCase { + public void testThisGame() { + Game g = new Game(); + assertEquals(g.numCards(), 12); + } +} From 5aa426396c3935ec4533c41a62ea5a02441c3ba1 Mon Sep 17 00:00:00 2001 From: Marisa Pacitti Date: Fri, 1 May 2015 09:21:09 -0400 Subject: [PATCH 6/6] Monte Carlo Simulation and Updated Card and Game --- src/Card.java | 105 +++++++++++++++++++++++++++++------------ src/Game.java | 50 +++++++++++--------- src/MonteCarloSim.java | 21 +++++++++ 3 files changed, 124 insertions(+), 52 deletions(-) create mode 100644 src/MonteCarloSim.java diff --git a/src/Card.java b/src/Card.java index 332695c..cc73f30 100644 --- a/src/Card.java +++ b/src/Card.java @@ -3,14 +3,26 @@ public class Card { public int color; public int shading; public int shape; - + public Card (int cardQuantity, int cardColor, int cardShading, int cardShape){ - quantity = ((((cardQuantity % 3)+3)%3)+1); - color = ((((cardColor % 3)+3)%3)+1); - shading = ((((cardShading % 3)+3)%3)+1); - shape = ((((cardShape % 3)+3)%3)+1); + quantity = formValue(cardQuantity); + color = formValue(cardColor);; + shading = formValue(cardShading);; + shape = formValue(cardShape);; } + private int formValue(int val) { + if (val < 4 && val > 0) { + return val; + } + else if (val >= 0) { + return val % 3 + 1; + } + else { + return (((val % 3) + 3) % 3) +1; + } + } + public int getQuantity() { return quantity; } @@ -28,49 +40,82 @@ public int getShape() { } public boolean isSet (Card cardA, Card cardB) { - if ((((quantity + cardA.getQuantity() + cardA.getQuantity())%3)==0) && - (((color + cardA.getColor() + cardA.getColor())%3)==0) && - (((shading + cardA.getShading() + cardA.getShading())%3)==0) && - (((shape + cardA.getShape() + cardA.getShape())%3)==0)) + if ((((quantity + cardA.getQuantity() + cardB.getQuantity())%3)==0) && + (((color + cardA.getColor() + cardB.getColor())%3)==0) && + (((shading + cardA.getShading() + cardB.getShading())%3)==0) && + (((shape + cardA.getShape() + cardB.getShape())%3)==0)) { return true; - else + } + else { return false; + } + } + + public boolean isRightForSet(int val1, int val2, int val3) { + if ((val1 + val2 + val3) % 3 == 0) { + return true; + } + else { + return false; + } } public String toString() { String str = ""; + String quant = ""; String col = ""; String shade = ""; String sha = ""; - if (color == 1) - col = "R"; - else if (color == 2) - col = "G"; - else - col = "P"; + if (quantity == 1) { + str += "1"; + } + else if (quantity == 2) { + str += "2"; + } + else { + str += "3"; + } + + if (color == 1) { + str += "R"; + } + else if (color == 2){ + str += "G"; + } + else { + str += "P"; + } - if (shading == 1) - shade = "O"; - else if (color == 2) - shade = "T"; - else - shade = "S"; + if (shading == 1) { + str += "O"; + } + else if (shading == 2) { + str += "T"; + } + else { + str += "S"; + } - if (shape == 1) - sha = "O"; - else if (color == 2) - sha = "D"; - else - sha = "S"; + if (shape == 1) { + str += "O"; + } + else if (shape == 2) { + str += "D"; + } + else { + str += "S"; + } - return str = quantity + col + shade + sha; + return str = quant+ col + shade + sha; + } + 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/Game.java b/src/Game.java index 1edee49..af5d7e6 100644 --- a/src/Game.java +++ b/src/Game.java @@ -1,18 +1,20 @@ public class Game { - private Table t = new Table(); - private Deck d = new Deck(); + private Table t; + private Deck d; public Game() { - Deck d = new Deck(); + d = new Deck(); + t = new Table(); for (int i = 0; i < 12; i++) { t.add(d.getNext()); } } public Game(String filename) { - Deck d = new Deck(filename); - for (int i=0; i < 12; i++) { - d.getNext(); + d = new Deck(filename); + t = new Table(); + for (int i=0; i < 12 && d.hasNext(); i++) { + t.add(d.getNext()); } } @@ -27,20 +29,26 @@ public int numCards() { public void playRound() { if (d.hasNext() == true && t.numSets() == 0) { for (int r = 0; r < 3; r++) { - t.add(d.getNext()); - } - return; + if (d.hasNext() == false) + return; + t.add(d.getNext()); } + return; + } - if (d.hasNext() == true && t.numSets() > 0) { + if (d.hasNext() == true && 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(i).isSet(t.getCard(j), t.getCard(k))) { + if (t.getCard(i).isSet(t.getCard(j), t.getCard(k)) == true) { t.removeSet(t.getCard(i), t.getCard(j), t.getCard(k)); - for (int r = 0; r < 3; r++) { + for (int r = 0; t.numCards() < 12; r++) { + if (d.hasNext() == false) { + return; + } t.add(d.getNext()); - } + } + return; } } } @@ -51,16 +59,17 @@ public void playRound() { 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(i).isSet(t.getCard(j), t.getCard(k))) { + if (t.getCard(i).isSet(t.getCard(j), t.getCard(k)) == true) { t.removeSet(t.getCard(i), t.getCard(j), t.getCard(k)); - return; + return; } } } } } + - if (d.hasNext() == false && t.numSets() > 0) { + if (d.hasNext() == false && 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++) { @@ -72,14 +81,11 @@ public void playRound() { } } } - - if (d.hasNext() == false && t.numSets() == 0) { - isGameOver(); - } + return; } - + public boolean isGameOver() { - if (d.hasNext() == false && t.numSets() == 0) { + if (!d.hasNext() && t.numSets() == 0) { return true; } else { diff --git a/src/MonteCarloSim.java b/src/MonteCarloSim.java new file mode 100644 index 0000000..29e1367 --- /dev/null +++ b/src/MonteCarloSim.java @@ -0,0 +1,21 @@ +public class MonteCarloSim { + public static void main(String[] args) { + double countSet = 0; + double countCard = 0; + int simulation = 10000; + + for (int i = 0; i < simulation; i++) { + Game g = new Game(); + countSet += g.numSets(); + + while (g.isGameOver() == false) { + g.playRound(); + } + countCard += g.numCards(); + } + System.out.println("The average number of sets is:" + countSet/simulation); + System.out.println("The average number of cards is:" + countCard/simulation); + } +} + + \ No newline at end of file