diff --git a/src/Card.java b/src/Card.java old mode 100755 new mode 100644 index 9eed9a5..950dd07 --- a/src/Card.java +++ b/src/Card.java @@ -1,7 +1,115 @@ 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 quant, int clr, int shd, int shp) { + quantity = fixValue(quant); + color = fixValue(clr); + shading = fixValue(shd); + shape = fixValue(shp); + } + + public int getQuantity() { + return quantity; + } + + public int getColor() { + return color; + } + + public int getShading() { + return shading; + } + + public int getShape() { + return shape; + } + + public boolean isSet(Card B, Card C) { + + if((B.getQuantity() + C.getQuantity() + quantity) % 3 == 0 && + (B.getColor() + C.getColor() +color) % 3 == 0 && + (B.getShading() + C.getShading() + shading) % 3 == 0 && + (B.getShape() + C.getShape() + shape) % 3 == 0) { + + return true; + } + + else { + + return false; + } + + } + + //Tried to do my own toString method but I couldn't figure it out. + //So I did this code even though it's longer than I wanted it too. + + public String toString() { + + String str = ""; + + str += quantity; + + if (color == 1) { + str += "R"; + } + + if (color == 2) { + str += "G"; + } + + if (color == 3) { + str += "P"; + } + + if (shading == 1) { + str += "O"; + } + + if (shading == 2) { + str += "T"; + } + + if (shading == 3) { + str += "S"; + } + + if (shape == 1) { + str += "O"; + } + + if (shape == 2) { + str += "D"; + } + + if (shape == 3) { + str += "S"; + } + + return str; + + } + //From class. Thanks to me! + 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() && @@ -9,4 +117,4 @@ public boolean equals(Object obj) { shading == that.getShading() && shape == that.getShape(); } -} +} \ No newline at end of file diff --git a/src/CardClassTesting.java b/src/CardClassTesting.java new file mode 100644 index 0000000..6c480b6 --- /dev/null +++ b/src/CardClassTesting.java @@ -0,0 +1,38 @@ +import junit.framework.TestCase; + +public class CardClassTesting extends TestCase { + + public void testIsSet() { + Card A = new Card(2,1,1,1); + Card B = new Card(2,1,1,1); + Card C = new Card(4,1,1,1); + Card D = new Card(1,1,4,1); + Card E = new Card(5,4,4,4); + + assertEquals(true, A.isSet(A,B)); + assertEquals(false, A.isSet(C,D)); + assertEquals(false, B.isSet(A,C)); + assertEquals(false, E.isSet(D,B)); + } + + public void testValues() { + Card A = new Card(2,1,1,1); + Card B = new Card(-2,-3,7,1); + + assertEquals(3, A.getQuantity()); + assertEquals(2, A.getColor()); + assertEquals(2, A.getShading()); + assertEquals(2, A.getShape()); + + assertEquals(2, B.getQuantity()); + assertEquals(1, B.getColor()); + assertEquals(2, B.getShading()); + assertEquals(2, B.getShape()); + } + + public void testToString() { + Card A = new Card(9,-3,8,2); + + assertEquals("1RSS", A.toString()); + } +} diff --git a/src/Deck.java b/src/Deck.java old mode 100755 new mode 100644 index ab3a2a3..ed39b3f --- a/src/Deck.java +++ b/src/Deck.java @@ -1,45 +1,92 @@ -import java.io.BufferedReader; -import java.io.FileReader; -import java.util.StringTokenizer; -import java.util.ArrayList; - -public class Deck { - // Implement the rest of this class yourself - - public Deck(String filename) { - cards = new ArrayList(81); +import java.io.BufferedReader; +import java.io.FileReader; +import java.util.StringTokenizer; +import java.util.ArrayList; +import java.util.Collections; + +public class Deck { + + private ArrayList cards = new ArrayList(81); + private int nextCardIndex = 0; + + public Deck() { - try { - String line; - BufferedReader infile = new BufferedReader(new FileReader(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; + for ( int quant = 1; quant <= 3; quant ++) { + + for (int clr = 1; clr <= 3; clr ++) { - // ignore comments - if(line.startsWith("#")) - continue; + for (int shd = 1; shd <= 3; shd ++) { + + for (int shp = 1; shp <=3; shp ++) { - // 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 shape = Integer.parseInt(tokenizer.nextToken()); + Card card = new Card(quant, clr, shd, shp); + cards.add(card); + } + } + } + } + + Collections.shuffle(cards); + + } + + public boolean hasNext(){ + + if (nextCardIndex < cards.size()){ + return true; + } + + else { - cards.add(new Card(quantity, color, shading, shape)); - nextCardIndex = 0; - } + return false; + } + } - catch(Exception e) { - throw new RuntimeException("Error while reading file: " + e.toString()); + + public Card getNext(){ + + if (hasNext() == false){ + + return null; + } + + else { + + nextCardIndex += 1; + return cards.get(nextCardIndex-1); } - } + } + public Deck(String filename) { + cards = new ArrayList(81); + + try { + String line; + BufferedReader infile = new BufferedReader(new FileReader(filename)); + int position = 0; + + while((line = infile.readLine()) != null) { + line = line.trim(); + + if(line.length() == 0) + continue; + + if(line.startsWith("#")) + continue; + + StringTokenizer tokenizer = new StringTokenizer(line); + + int quantity = Integer.parseInt(tokenizer.nextToken()); + int color = Integer.parseInt(tokenizer.nextToken()); + int shading = Integer.parseInt(tokenizer.nextToken()); + int shape = Integer.parseInt(tokenizer.nextToken()); + + cards.add(new Card(quantity, color, shading, shape)); + nextCardIndex = 0; + } + } + catch(Exception e) { + throw new RuntimeException("Error while reading file: " + e.toString()); + } + } +} \ No newline at end of file diff --git a/src/Game.java b/src/Game.java new file mode 100644 index 0000000..c3425a4 --- /dev/null +++ b/src/Game.java @@ -0,0 +1,95 @@ +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() == false) + return; + + t.add(d.getNext()); + } + return; + } + + else if (d.hasNext() == true && t.numSets() != 0) { + for (int j = 0; j < t.numCards() - 2; j++) { + for (int k = j + 1; k < t.numCards() - 1; k++) { + for (int l = k + 1; l < t.numCards(); l++) { + + if (t.getCard(j).isSet(t.getCard(k), t.getCard(l)) == true) { + t.removeSet(t.getCard(j), t.getCard(k), t.getCard(l)); + + while (t.numCards() < 12) { + + if (d.hasNext() == false) + return; + + t.add(d.getNext()); + } + + return; + } + } + } + } + } + + else if (t.numSets() != 0 && d.hasNext() == false) { + for (int j = 0; j < t.numCards() - 2; j++) { + for (int k = j + 1; k < t.numCards() - 1; k++) { + for (int l = k + 1; l < t.numCards(); l++) { + + if (t.getCard(j).isSet(t.getCard(k), t.getCard(l))) { + t.removeSet(t.getCard(j), t.getCard(k), t.getCard(l)); + + 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/SetSim.java b/src/SetSim.java new file mode 100644 index 0000000..87827ca --- /dev/null +++ b/src/SetSim.java @@ -0,0 +1,36 @@ +public class SetSim{ + public static void main (String[] args) { + double setCount = 0; + double cardCount = 0; + int SimGames = 1000; + + //Average sets loop. Took good notes in class! + + for (int i = 0; i <= SimGames; i++){ + + Game G = new Game(); + setCount += G.numSets(); + + while (G.isGameOver()){ + G.playRound(); + } + } + + System.out.println("Average # of sets :"+ setCount / SimGames); + + //Average cards loops. Had to run separate games. Thanks for discussing it in class! + + for (int i = 0; i <= SimGames; i++){ + + Game G = new Game(); + + while (G.isGameOver()==true){ + G.playRound(); + } + cardCount += G.numCards(); + i+= 1; + } + + System.out.println("Average # of cards:" + cardCount / SimGames); + } +} \ No newline at end of file diff --git a/src/Table.java b/src/Table.java new file mode 100644 index 0000000..171cc9c --- /dev/null +++ b/src/Table.java @@ -0,0 +1,104 @@ +public class Table{ + private TableNode head; + private int length; + + public Table(){ + head = null; + length = 0; + } + + public void add(Card A) { + TableNode tempNode = new TableNode(A); + tempNode.setNext(head); + head = tempNode; + } + + public void removeSet(Card A, Card B, Card C){ + + TableNode cp = head; + + if (A.isSet(B, C) == false) { + + return; + } + + else if (A.isSet(B, C)) { + + A = null; + B = null; + C = null; + + } + + else { + + return; + } + + } + + public int numCards(){ + TableNode curr = head; + int numCardCount = 0; + + if (curr == null){ + return 0; + } + else { + + while (curr != null){ + curr = curr.getNext(); + numCardCount += 1; + } + return numCardCount; + } + } + + public Card getCard (int index) { + TableNode cp = head; + + if (cp == null){ + return null; + } + + else { + + for (int i = 0; i < index; i++){ + cp = cp.getNext(); + } + + } + + return cp.getCard(); + } + + + public int numSets() + { + int numSetCount = 0; + TableNode cp = head; + + while (cp != null && cp.getNext() != null && cp.getNext().getNext() != null) + { + TableNode cp2 = cp.getNext(); + + while (cp2 != null && cp2.getNext() != null) + { + TableNode cp3 = cp2.getNext(); + + while (cp3 != null) + { + if (cp.getCard().isSet(cp2.getCard(), cp3.getCard()) == true) + numSetCount += 1; + + cp3 =cp3.getNext(); + } + + cp2 = cp2.getNext(); + } + + cp = cp.getNext(); + } + return numSetCount; + } +} \ No newline at end of file diff --git a/src/TableNode.java b/src/TableNode.java new file mode 100644 index 0000000..5914ff4 --- /dev/null +++ b/src/TableNode.java @@ -0,0 +1,24 @@ +public class TableNode{ + private Card card; + private TableNode next; + + public TableNode(Card A){ + card = A; + next = null; + } + + public void setNext(TableNode nextNode){ + + next = nextNode; + } + + public TableNode getNext(){ + + return next; + } + + public Card getCard(){ + + return card; + } +} \ No newline at end of file