diff --git a/src/Card.java b/src/Card.java old mode 100755 new mode 100644 index 9eed9a5..0fc95fe --- a/src/Card.java +++ b/src/Card.java @@ -1,12 +1,69 @@ -public class Card { - // Create the rest of this class yourself - - public boolean equals(Object obj) { - Card that = (Card)obj; - - return quantity == that.getQuantity() && - color == that.getColor() && - shading == that.getShading() && - shape == that.getShape(); - } -} +public class Card{ + private int quantity; + private int color; + private int shading; + private 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 cardB, Card cardC){ + return + (((quantity + cardB.getQuantity() + cardC.getQuantity()) % 3) == 0) && + + (((color + cardB.getColor() + cardC.getColor()) % 3) == 0) && + + (((shading + cardB.getShading() + cardC.getShading()) % 3) == 0) && + + (((shape + cardB.getShape() + cardC.getShape()) % 3) == 0); + } + + public String toString(){ + String[] Scolor = new String[3]; + Scolor[0] = "R"; + Scolor[1] = "G"; + Scolor[2] = "P"; + + String[] Sshading = new String[3]; + Sshading[0] = "O"; + Sshading[1] = "T"; + Sshading[2] = "S"; + + String[] Sshape = new String[3]; + Sshape[0] = "O"; + Sshape[1] = "D"; + Sshape[2] = "S"; + + return quantity + Scolor[color-1] + Sshading[shading-1] + Sshape[shape-1]; + } + + public boolean equals(Object obj){ + Card that = (Card)obj; + + return quantity == that.getQuantity() && + color == that.getColor() && + shading == that.getShading() && + shape == that.getShape(); + } + } +// d \ No newline at end of file diff --git a/src/CardTest.java b/src/CardTest.java new file mode 100644 index 0000000..1c62e6e --- /dev/null +++ b/src/CardTest.java @@ -0,0 +1,46 @@ +import junit.framework.TestCase; + +public class CardTest extends TestCase{ + + public void testCardInt(){ + + Card c1 = new Card(7,1,1,1); + + assertEquals(2, c1.getQuantity()); + assertEquals(2, c1.getColor()); + assertEquals(2, c1.getShading()); + assertEquals(2, c1.getShape()); + + Card card3 = new Card(0, -1, -2, -3); + assertEquals(1, card3.getQuantity()); + assertEquals(3, card3.getColor()); + assertEquals(2, card3.getShading()); + assertEquals(1, card3.getShape()); + } + + public void testSet(){ + + Card cardA = new Card(1,1,1,1); + Card cardB = new Card(2,1,1,1); + Card cardC = new Card(3,1,1,1); + Card cardD = new Card(1,1,1,3); + + assertEquals(true, cardA.isSet(cardB, cardC)); + assertEquals(false, cardA.isSet(cardC, cardD)); + } + + public void testCardString(){ + + Card card1 = new Card(0, 1, 1, 1); + Card card2 = new Card(-2, -4, 0, -5); + Card card3 = new Card(-4, -3, -8, 8); + + assertEquals("1GTD", card1.toString()); + assertEquals("2POD", card2.toString()); + assertEquals("3RTS", card3.toString()); + } + +} + + + \ No newline at end of file diff --git a/src/Deck.java b/src/Deck.java old mode 100755 new mode 100644 index ab3a2a3..2b0af64 --- a/src/Deck.java +++ b/src/Deck.java @@ -2,14 +2,16 @@ 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 + private ArrayList cards = new ArrayList(); + private int nextCardIndex; public Deck(String filename) { cards = new ArrayList(81); - - try { + + try { String line; BufferedReader infile = new BufferedReader(new FileReader(filename)); int position = 0; @@ -42,4 +44,45 @@ public Deck(String filename) { throw new RuntimeException("Error while reading file: " + e.toString()); } } + + public Deck(){ + for (int quantity = 1; quantity < 4; quantity++) { + for (int color = 1; color < 4; color++) { + for (int shading = 1; shading < 4; shading++) { + for (int shape = 1; shape < 4; shape++) { + cards.add(new Card(quantity, color, shading, shape)); + } + } + } + } + Collections.shuffle(cards); + } + + public boolean hasNext(){ + //for (nextCardIndex = 0; nextCardIndex <= cards.size(); nextCardIndex++){ + if (nextCardIndex < cards.size()){ + return true; + } + else{ + return false; + } + } + + + public Card getNext(){ + + if (hasNext() == false){ + return null; + } + + else{ + nextCardIndex += 1; + return cards.get(nextCardIndex-1); + } + } } + + + + + \ No newline at end of file diff --git a/src/Game.java b/src/Game.java new file mode 100644 index 0000000..4ac608c --- /dev/null +++ b/src/Game.java @@ -0,0 +1,79 @@ +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){ + t = new Table(); + d = new Deck(filename); + + while (d.hasNext() == true){ + 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 (d.hasNext() == true && t.numSets() == 0){ + for (int i = 0; i < 3; i++){ + if (d.hasNext()){ + t.add(d.getNext()); + } + else{ + return; + } + } + } + + if (d.hasNext() && t.numSets() != 0){ + for (int x = 0; x < t.numCards() - 2; x++){ + for (int y = x + 1; y < t.numCards() - 1; y++){ + for (int z = y + 1; z < t.numCards(); z++){ + if (t.getCard(x).isSet(t.getCard(y), t.getCard(z))){ + t.removeSet(t.getCard(x), t.getCard(y), t.getCard(z)); + + if (t.numCards() < 12 && d.hasNext()) { + for (int o = 0; o < 3; o++) { + if (d.hasNext()) { + t.add(d.getNext()); + } + else { + return; + } + } + } + return; + } + } + } + } + } + } + //f + 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/MCSim.java b/src/MCSim.java new file mode 100644 index 0000000..ab6fa57 --- /dev/null +++ b/src/MCSim.java @@ -0,0 +1,32 @@ +public class MCSim{ + public static void main (String[] args){ + + double setCount = 0; + double cardCount = 0; + int sim = 100001; + + for (int i = 0; i <= sim; i++){ + + Game game1 = new Game(); + setCount += game1.numSets(); + + while (game1.isGameOver()){ + game1.playRound(); + } + } + + System.out.println("The average number of sets from 100,000 random sets of cards is " + setCount / sim); + + for (int i = 0; i <= sim; i++){ + + Game game2 = new Game(); + cardCount += game2.numCards(); + + while (game2.isGameOver()){ + game2.playRound(); + } + } + + System.out.println("The average number of cards on the table when the game ends is " + cardCount / sim); + } +} \ No newline at end of file diff --git a/src/Table.java b/src/Table.java new file mode 100644 index 0000000..b28860a --- /dev/null +++ b/src/Table.java @@ -0,0 +1,111 @@ +public class Table { + private TableNode head; + + public Table() { + head = null; + } + + public void add(Card card) { + TableNode Node1 = new TableNode(card); + Node1.setNext(head); + head = Node1; + } + + public void removeSet(Card c1, Card c2, Card c3) { + if (c1.isSet(c2, c3) == false) { + return; + } + + else { + Card remove1 = null; + Card remove2 = null; + Card remove3 = null; + + TableNode temp = head; + + while (temp != null) { + if (c1.equals(temp.getCard())) + remove1 = c1; + + if (c2.equals(temp.getCard())) + remove2 = c2; + + if (c3.equals(temp.getCard())) + remove3 = c3; + + temp = temp.getNext(); + } + + if (remove1 != null && remove2 != null && remove3 != null) { + TableNode curr = head; + TableNode prev = null; + + while (curr != null) { + + if (c1.equals(curr.getCard()) || c2.equals(curr.getCard()) || c3.equals(curr.getCard())) { + if (curr == head) { + curr = curr.getNext(); + head = head.getNext(); + } + else { + prev.setNext(curr.getNext()); + curr = curr.getNext(); + } + } + else { + prev = curr; + curr = curr.getNext(); + } + } + } + } + } + + public int numCards() { + TableNode currNode = head; + int count = 0; + while (currNode != null) { + count += 1; + currNode = currNode.getNext(); + } + return count; + } + + 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() { + int count = 0; + 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) { + count += 1; + } + n3 = n3.getNext(); + } + n2 = n2.getNext(); + } + n1 = n1.getNext(); + + } + return count; + } +} + // d \ No newline at end of file diff --git a/src/TableNode.java b/src/TableNode.java new file mode 100644 index 0000000..3cf88d6 --- /dev/null +++ b/src/TableNode.java @@ -0,0 +1,22 @@ +public class TableNode{ + private Card card; + private TableNode next; + + public TableNode(Card c1){ + card = c1; + next = null; + } + + public void setNext(TableNode nextNode){ + next = nextNode; + } + + public TableNode getNext(){ + return next; + } + + public Card getCard(){ + return card; + } +} +// d \ No newline at end of file