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/Card.java b/src/Card.java old mode 100755 new mode 100644 index 9eed9a5..cc73f30 --- a/src/Card.java +++ b/src/Card.java @@ -1,12 +1,121 @@ 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 = 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; + } + + 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() + 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 { + 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 (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) { + str += "O"; + } + else if (shading == 2) { + str += "T"; + } + else { + str += "S"; + } + + if (shape == 1) { + str += "O"; + } + else if (shape == 2) { + str += "D"; + } + else { + str += "S"; + } + + 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/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 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 diff --git a/src/Game.java b/src/Game.java new file mode 100644 index 0000000..af5d7e6 --- /dev/null +++ b/src/Game.java @@ -0,0 +1,95 @@ +public class Game { + private Table t; + private Deck d; + + public Game() { + d = new Deck(); + t = new Table(); + for (int i = 0; i < 12; i++) { + t.add(d.getNext()); + } + } + + public Game(String filename) { + d = new Deck(filename); + t = new Table(); + for (int i=0; i < 12 && d.hasNext(); i++) { + t.add(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++) { + if (d.hasNext() == false) + return; + 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)) == true) { + t.removeSet(t.getCard(i), t.getCard(j), t.getCard(k)); + for (int r = 0; t.numCards() < 12; r++) { + if (d.hasNext() == false) { + return; + } + t.add(d.getNext()); + } + return; + } + } + } + } + } + + 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)) == true) { + 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; + } + } + } + } + } + return; + } + + public boolean isGameOver() { + if (!d.hasNext() && 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); + } +} 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 diff --git a/src/Table.java b/src/Table.java new file mode 100644 index 0000000..b15235a --- /dev/null +++ b/src/Table.java @@ -0,0 +1,111 @@ +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 boolean onTable(Card card){ + TableNode node = head; + while (node != null) { + if (node.getCard().equals(card)) { + return true; + } + node = node.getNext(); + } + 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; + + 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 numcount = 0; + if (temp == null) { + return 0; + } + while (temp != null) { + numcount += 1; + temp = temp.getNext(); + } + return numcount; + } + + 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(){ + 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/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..eaed0bd --- /dev/null +++ b/src/TableTest.java @@ -0,0 +1,28 @@ +import junit.framework.TestCase; + +public class TableTest extends TestCase{ + + public void testEmpty() { + Table tt = new Table(); + 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); + 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