diff --git a/src/3cardNew.dat b/src/3cardNew.dat new file mode 100644 index 0000000..aecb4b1 --- /dev/null +++ b/src/3cardNew.dat @@ -0,0 +1,3 @@ +1 2 3 1 +2 1 2 3 +3 1 2 1 \ No newline at end of file diff --git a/src/Card.java b/src/Card.java old mode 100755 new mode 100644 index 9eed9a5..ef5987a --- a/src/Card.java +++ b/src/Card.java @@ -1,12 +1,112 @@ public class Card { - // Create the rest of this class yourself + private int quantity; + private int color; + private int shade; + private int shape; + - public boolean equals(Object obj) { + public Card(int number, int colorObject, int shadetype, int shapeKind ){ + quantity = changeValue(number); + color = changeValue(colorObject); + shade = changeValue(shadetype); + shape = changeValue(shapeKind); + } + + private int changeValue( int changeinValue) { + if (changeinValue < 1 || changeinValue > 3) + return (((changeinValue % 3) + 3) % 3) + 1; + else + return changeinValue; + } + + + public int getQuantity(){ + + return quantity; + } + + public int getColor(){ + + return color; + } + + public int getShading(){ + + return shade; + } + + public int getShape(){ + + return shape; + } + + public boolean isSet (Card cardM,Card cardMM){ + if (((((color + cardM.getColor() + cardMM.getColor()) % 3) == 0)) && + (((quantity + cardM.getQuantity() + cardMM.getQuantity()) % 3) == 0) && + (((shade + cardM.getShading() + cardMM.getShading()) % 3) == 0) && + (((shape + cardM.getShape() + cardMM.getShape()) % 3) == 0)) + { + return true; + } + else { + return false; + } + } +public String toString() { + String str = ""; + str += quantity; + if(color == 1) { + str += "R"; + } + if(color == 2) { + str += "G"; + } + if(color == 3) { + str += "P"; + } + if(shade == 1) { + str += "O"; + } + if(shade == 2) { + str += "T"; + } + if(shade == 3) { + str += "S"; + } + if(shape == 1) { + str += "O"; + } + if(shape == 2) { + str += "D"; + } + if(shape == 3) { + str += "S"; + } + return str; + } + +private int fixValue(int valueToFix) { + if (valueToFix < 1 || valueToFix > 3) + return (((valueToFix % 3) + 3) % 3) + 1; + else + return valueToFix; + } +public boolean equals(Object obj) { Card that = (Card)obj; return quantity == that.getQuantity() && color == that.getColor() && - shading == that.getShading() && + shade == that.getShading() && shape == that.getShape(); - } } +} + + + + + + + + + + diff --git a/src/CardTest.java b/src/CardTest.java new file mode 100644 index 0000000..faf3763 --- /dev/null +++ b/src/CardTest.java @@ -0,0 +1,29 @@ +import junit.framework.TestCase; + +public class CardTest extends TestCase { + + public void testCard() { + Card c1 = new Card(3,2,3,2); + assertEquals("3GSD",c1.toString()); + + Card c2 = new Card(1,3,1,3); + assertEquals("1POS",c2.toString()); + + Card c3 = new Card(2,1,2,1); + assertEquals("2RTO",c3.toString()); + } + + public void testTrueSet() { + Card c1 = new Card(1,2,3,2); + Card c2 = new Card(2,3,1,3); + Card c3 = new Card(3,1,2,1); + assertEquals(true, c1.isSet(c2, c3)); + } + + public void testFalseSet() { + Card c1 = new Card(1,3,3,1); + Card c2 = new Card(3,3,3,3); + Card c3 = new Card(3,2,1,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..8f593e1 --- a/src/Deck.java +++ b/src/Deck.java @@ -2,10 +2,47 @@ 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 int nextCardIndex = 0; + private ArrayList cards; + + public Deck(){ + cards = new ArrayList(81); + + for(int mar1 = 1; mar1 <= 3; mar1++){ + for (int mar2 = 1; mar2<= 3; mar2++){ + for (int mar3 = 1; mar3 <= 3; mar3++){ + for(int mar4 = 1; mar4 <= 3; mar4++){ + cards.add(new Card(mar1, mar2, mar3, mar4)); + nextCardIndex=0; + } + } + } + } + Collections.shuffle(cards); + } + public boolean hasNext(){ + 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); + } + } + + public Deck(String filename) { cards = new ArrayList(81); @@ -15,26 +52,23 @@ public Deck(String filename) { int position = 0; while((line = infile.readLine()) != null) { - // Blank lines might contain white space, so trim it off + line = line.trim(); - // ignore blank lines if(line.length() == 0) continue; - // ignore comments if(line.startsWith("#")) continue; - - // a valid line contains 4 integers + StringTokenizer tokenizer = new StringTokenizer(line); int quantity = Integer.parseInt(tokenizer.nextToken()); int color = Integer.parseInt(tokenizer.nextToken()); - int shading = Integer.parseInt(tokenizer.nextToken()); + int shade = Integer.parseInt(tokenizer.nextToken()); int shape = Integer.parseInt(tokenizer.nextToken()); - cards.add(new Card(quantity, color, shading, shape)); + cards.add(new Card(quantity, color, shade, shape)); nextCardIndex = 0; } } diff --git a/src/Decktest.java b/src/Decktest.java new file mode 100644 index 0000000..ac11536 --- /dev/null +++ b/src/Decktest.java @@ -0,0 +1,43 @@ +import junit.framework.TestCase; + +public class Decktest extends TestCase { + + public void testHasNextGetNext0Par() { + // Tests Deck() with hasNext() and getNext + Deck d = new Deck(); + Card c; + int sumQuan = 0; + int sumCo = 0; + int sumShade = 0; + int sumShape = 0; + int sumWhole = 0; + int total = 0; + while(d.hasNext()){ + c = d.getNext(); + sumQuan += c.getQuantity(); + sumCo += c.getColor(); + sumShade += c.getShade(); + sumShape += c.getShape(); + total++; + } + sumWhole = sumQuan + sumCo + sumShade + sumShape; + assertEquals(162, sumQuan); + assertEquals(162, sumCo); + assertEquals(162, sumShade); + assertEquals(162, sumShape); + assertEquals(648, sumWhole); + assertEquals(81, total); + } + public void testDeckHasGetNextDat(){ + // Tests hasNext() and getNext() methods with Deck(file.dat) + Deck d = new Deck("3cardNew.dat"); + + assertEquals(true, d.hasNext()); + assertEquals("1GSO", d.getNext().toString()); + assertEquals(true, d.hasNext()); + assertEquals("2RTS", d.getNext().toString()); + assertEquals(true, d.hasNext()); + assertEquals("3RTO", d.getNext().toString()); + assertEquals(false, d.hasNext()); + } +} \ No newline at end of file diff --git a/src/Game.java b/src/Game.java new file mode 100644 index 0000000..a85c888 --- /dev/null +++ b/src/Game.java @@ -0,0 +1,89 @@ +public class Game { + private Deck d; + private Table t; + + public Game (){ + d = new Deck(); + t = new Table(); + + for(int i = 0; i<12; i++) + t.add(d.getNext()); + } + + public Game(String str){ + d = new Deck(str); + 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 (t.numSets() == 0 && d.hasNext() == true) { + for (int i = 0; i < 3; i++) { + if(d.hasNext() == true) { + t.add(d.getNext()); + } + } + return; + } + if (d.hasNext() == true && t.numSets() != 0) { + for (int mar1 = 0; mar1 < t.numCards() - 2; mar1++){ + for (int mar2 = mar1 + 1; mar2 < t.numCards() - 1; mar2++){ + for (int mar3 = mar2 + 1; mar3 < t.numCards(); mar3++){ + + if (t.getCard(mar1).isSet(t.getCard(mar2), t.getCard(mar3))){ + t.removeSet(t.getCard(mar1), t.getCard(mar2), t.getCard(mar3)); + + while (t.numCards() < 12) + { + if (d.hasNext() == false) + return; + + t.add(d.getNext()); + } + return; + } + } + } + } + } + + if (t.numSets() != 0 && d.hasNext() == false){ + for (int mar1 = 0; mar1 < t.numCards() - 2; mar1++){ + for (int mar2 = mar1 + 1; mar2 < t.numCards() - 1; mar2++){ + for (int mar3 = mar2 + 1; mar3 < t.numCards(); mar3++){ + + if (t.getCard(mar1).isSet(t.getCard(mar2), t.getCard(mar3))){ + t.removeSet(t.getCard(mar1), t.getCard(mar2), t.getCard(mar3)); + return; + } + } + } + } + return; + } + } + + + + + public boolean isGameOver() + { + if (d.hasNext() == false && t.numSets() == 0) + return true; + + return false; + } +} + + + + diff --git a/src/MonteCarloSim.java b/src/MonteCarloSim.java new file mode 100644 index 0000000..5e1d4c1 --- /dev/null +++ b/src/MonteCarloSim.java @@ -0,0 +1,27 @@ +public class MonteCarloSim +{ + public static void main(String[] args) + { + double nStartSets = 0; + double nEndingSets = 0; + double nCards = 0; + + for (int i = 0; i < 1000000; i++) + { + Game game = new Game(); + nCards += game.numCards(); + nStartSets += game.numSets(); + + while (!game.isGameOver()) + { + game.playRound(); + } + + nEndingSets += game.numCards(); + } + + System.out.println(nStartSets/1000000); + System.out.println(nEndingSets/1000000); + System.out.println(nCards/1000000); + } +} \ No newline at end of file diff --git a/src/Table.java b/src/Table.java new file mode 100644 index 0000000..f8fcdca --- /dev/null +++ b/src/Table.java @@ -0,0 +1,113 @@ +public class Table { + private TableNode head; + private int amount; + + public Table() { + head = null; + } + + public void add(Card marlon) { + TableNode newNode = new TableNode(marlon); + newNode.setNext(head); + head = newNode; + } + + private boolean onTable(Card m){ + TableNode curr = head; + + while(curr != null){ + if(m.equals(curr.getCard())){ + return true; + } + else{ + curr = curr.getNext(); + } + } + return false; + } + private void removeCard(Card m){ + if (head == null) + return; + TableNode prev = null; + TableNode curr = head; + if (m.equals(head.getCard())) + head = head.getNext(); + else{ + while(!m.equals(curr.getCard())){ + prev = curr; + curr = curr.getNext(); + } + prev.setNext(curr.getNext()); + } + } + + + + + + public void removeSet(Card m1, Card m2, Card m3) { + if(m1.isSet (m2, m3) == false) + return; + if(!onTable(m1)) + return; + if(!onTable(m2)) + return; + if(!onTable(m3)) + return; + removeCard(m1); + removeCard(m2); + removeCard(m3); + } + + + public int numCards(){ + TableNode temp = head; + amount = 0; + if(head == null) + return 0; + while(temp != null) + { + amount++; + temp = temp.getNext(); + } + return amount; + } + + public Card getCard(int index) { + TableNode curr = head; + if(head == null) + return null; + if(index < numCards()) + { + for(int i = 0; i