From a146ded391798447e7b88298de20104817de0ef1 Mon Sep 17 00:00:00 2001 From: wsilverio11 Date: Tue, 31 Mar 2015 21:06:21 -0400 Subject: [PATCH 1/7] C O O L PROJECT --- Card.java | 66 +++++++++++++++++++++++++++++++++++++++++++ CardClassTesting.java | 38 +++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 Card.java create mode 100644 CardClassTesting.java diff --git a/Card.java b/Card.java new file mode 100644 index 0000000..3f6877a --- /dev/null +++ b/Card.java @@ -0,0 +1,66 @@ +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 A, Card B) { + + return + + (((quantity + A.getQuantity() + B.getQuantity()) % 3) == 0) && + (((color + A.getColor() + B.getColor()) % 3) == 0) && + (((shading + A.getShading() + B.getShading()) % 3) == 0) && + (((shape + A.getShape() + B.getShape()) % 3) == 0); + } + + public String toString() { + String [] Colour = {"R","G","P"}; + String [] Filling = {"O","T","S"}; + String [] Form = {"O","D","S"}; + + // Figured out a way to do it with Arrays! + // I still want to meet on Thursday though! + return quantity + Colour[color-1] + Filling[shading-1] + Form[shape-1]; + } + + 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/CardClassTesting.java b/CardClassTesting.java new file mode 100644 index 0000000..6c480b6 --- /dev/null +++ b/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()); + } +} From 9176acede7c8639ae87b1d255af491d20142597b Mon Sep 17 00:00:00 2001 From: wsilverio11 Date: Tue, 31 Mar 2015 21:09:30 -0400 Subject: [PATCH 2/7] Update Card.java --- Card.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Card.java b/Card.java index 3f6877a..06c777c 100644 --- a/Card.java +++ b/Card.java @@ -5,12 +5,12 @@ public class Card { private int shading; private int shape; - public Card(int cardQuantity, int cardColor, int cardShading, int cardShape){ + public Card(int Quantity, int Color, int Shading, int Shape){ - 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 = ((((Quantity % 3)+3)%3)+1); + color = ((((Color % 3)+3)%3)+1); + shading = ((((Shading % 3)+3)%3)+1); + shape = ((((Shape % 3)+3)%3)+1); } public int getQuantity() { From edf38fcf6a69aba1eb5172868037d03f5e43e736 Mon Sep 17 00:00:00 2001 From: wsilverio11 Date: Tue, 31 Mar 2015 23:34:12 -0400 Subject: [PATCH 3/7] =?UTF-8?q?=E2=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Card.java | 66 ------------------- src/Card.java | 64 ++++++++++++++++-- .../CardClassTesting.java | 0 3 files changed, 59 insertions(+), 71 deletions(-) delete mode 100644 Card.java mode change 100755 => 100644 src/Card.java rename CardClassTesting.java => src/CardClassTesting.java (100%) diff --git a/Card.java b/Card.java deleted file mode 100644 index 06c777c..0000000 --- a/Card.java +++ /dev/null @@ -1,66 +0,0 @@ -public class Card { - - private int quantity; - private int color; - private int shading; - private int shape; - - public Card(int Quantity, int Color, int Shading, int Shape){ - - quantity = ((((Quantity % 3)+3)%3)+1); - color = ((((Color % 3)+3)%3)+1); - shading = ((((Shading % 3)+3)%3)+1); - shape = ((((Shape % 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 A, Card B) { - - return - - (((quantity + A.getQuantity() + B.getQuantity()) % 3) == 0) && - (((color + A.getColor() + B.getColor()) % 3) == 0) && - (((shading + A.getShading() + B.getShading()) % 3) == 0) && - (((shape + A.getShape() + B.getShape()) % 3) == 0); - } - - public String toString() { - String [] Colour = {"R","G","P"}; - String [] Filling = {"O","T","S"}; - String [] Form = {"O","D","S"}; - - // Figured out a way to do it with Arrays! - // I still want to meet on Thursday though! - return quantity + Colour[color-1] + Filling[shading-1] + Form[shape-1]; - } - - 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/Card.java b/src/Card.java old mode 100755 new mode 100644 index 9eed9a5..06c777c --- a/src/Card.java +++ b/src/Card.java @@ -1,12 +1,66 @@ public class Card { - // Create the rest of this class yourself - - public boolean equals(Object obj) { - Card that = (Card)obj; + + private int quantity; + private int color; + private int shading; + private int shape; + + public Card(int Quantity, int Color, int Shading, int Shape){ + + quantity = ((((Quantity % 3)+3)%3)+1); + color = ((((Color % 3)+3)%3)+1); + shading = ((((Shading % 3)+3)%3)+1); + shape = ((((Shape % 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 A, Card B) { + + return + + (((quantity + A.getQuantity() + B.getQuantity()) % 3) == 0) && + (((color + A.getColor() + B.getColor()) % 3) == 0) && + (((shading + A.getShading() + B.getShading()) % 3) == 0) && + (((shape + A.getShape() + B.getShape()) % 3) == 0); + } + + public String toString() { + String [] Colour = {"R","G","P"}; + String [] Filling = {"O","T","S"}; + String [] Form = {"O","D","S"}; + // Figured out a way to do it with Arrays! + // I still want to meet on Thursday though! + return quantity + Colour[color-1] + Filling[shading-1] + Form[shape-1]; + } + + 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/CardClassTesting.java b/src/CardClassTesting.java similarity index 100% rename from CardClassTesting.java rename to src/CardClassTesting.java From 0f698bba25fcc786ec849fc78834f31eb2bf8f9f Mon Sep 17 00:00:00 2001 From: wsilverio11 Date: Tue, 7 Apr 2015 20:55:29 -0400 Subject: [PATCH 4/7] =?UTF-8?q?=E2=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Card.java | 37 +++++++++++++--- src/Deck.java | 118 +++++++++++++++++++++++++++++++++----------------- 2 files changed, 109 insertions(+), 46 deletions(-) mode change 100755 => 100644 src/Deck.java diff --git a/src/Card.java b/src/Card.java index 06c777c..67c2645 100644 --- a/src/Card.java +++ b/src/Card.java @@ -44,13 +44,36 @@ public boolean isSet(Card A, Card B) { } public String toString() { - String [] Colour = {"R","G","P"}; - String [] Filling = {"O","T","S"}; - String [] Form = {"O","D","S"}; - - // Figured out a way to do it with Arrays! - // I still want to meet on Thursday though! - return quantity + Colour[color-1] + Filling[shading-1] + Form[shape-1]; + 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; } public boolean equals(Object obj){ diff --git a/src/Deck.java b/src/Deck.java old mode 100755 new mode 100644 index ab3a2a3..8a09440 --- a/src/Deck.java +++ b/src/Deck.java @@ -1,45 +1,85 @@ -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 qty = 1; qty <= 3; qty ++) { + + 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()); - - cards.add(new Card(quantity, color, shading, shape)); - nextCardIndex = 0; - } + Card card = new Card(qty,clr,shd,shp); + } + } + } + } + Collections.shuffle(cards); + + } + + public boolean hasNext(){ + + if (nextCardIndex < cards.size()){ + return true; + } + else { + 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 From a9b389c3d2b73a8645a888f362d2b5a1744fda1d Mon Sep 17 00:00:00 2001 From: wsilverio11 Date: Tue, 14 Apr 2015 22:31:37 -0400 Subject: [PATCH 5/7] T A B L E AND T A B L E N O D E --- src/Table.java | 104 +++++++++++++++++++++++++++++++++++++++++++++ src/TableNode.java | 24 +++++++++++ 2 files changed, 128 insertions(+) create mode 100644 src/Table.java create mode 100644 src/TableNode.java 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 From 700ed6768af9d8397e2b81b2f35557f150861186 Mon Sep 17 00:00:00 2001 From: wsilverio11 Date: Thu, 23 Apr 2015 20:33:03 -0400 Subject: [PATCH 6/7] G A M E --- src/Game.java | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 src/Game.java 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 From d809951984756cc219ff068951bb1207bf1c027f Mon Sep 17 00:00:00 2001 From: wsilverio11 Date: Thu, 30 Apr 2015 22:35:10 -0400 Subject: [PATCH 7/7] Modified Card and Deck. Final Monte Carlo Sim for Set. --- src/Card.java | 123 ++++++++++++++++++++++++++++++------------------ src/Deck.java | 15 ++++-- src/SetSim.java | 36 ++++++++++++++ 3 files changed, 124 insertions(+), 50 deletions(-) create mode 100644 src/SetSim.java diff --git a/src/Card.java b/src/Card.java index 67c2645..950dd07 100644 --- a/src/Card.java +++ b/src/Card.java @@ -1,89 +1,120 @@ public class Card { - + private int quantity; private int color; private int shading; - private int shape; - - public Card(int Quantity, int Color, int Shading, int Shape){ - - quantity = ((((Quantity % 3)+3)%3)+1); - color = ((((Color % 3)+3)%3)+1); - shading = ((((Shading % 3)+3)%3)+1); - shape = ((((Shape % 3)+3)%3)+1); + 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(){ - + + public int getColor() { return color; } - - public int getShading(){ - + + public int getShading() { return shading; } - - public int getShape(){ - + + public int getShape() { return shape; } - - public boolean isSet(Card A, Card B) { - - return - - (((quantity + A.getQuantity() + B.getQuantity()) % 3) == 0) && - (((color + A.getColor() + B.getColor()) % 3) == 0) && - (((shading + A.getShading() + B.getShading()) % 3) == 0) && - (((shape + A.getShape() + B.getShape()) % 3) == 0); + + 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; + } + } - - public String toString() { + + //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) { + + if (color == 1) { str += "R"; } - if(color == 2) { + + if (color == 2) { str += "G"; } - if(color == 3) { + + if (color == 3) { str += "P"; } - if(shading == 1) { + + if (shading == 1) { str += "O"; } - if(shading == 2) { + + if (shading == 2) { str += "T"; } - if(shading == 3) { + + if (shading == 3) { str += "S"; } - if(shape == 1) { + + if (shape == 1) { str += "O"; } - if(shape == 2) { + + if (shape == 2) { str += "D"; } - if(shape == 3) { + + if (shape == 3) { str += "S"; } + return str; - } - - public boolean equals(Object obj){ - Card that = (Card)obj; + } + //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() && color == that.getColor() && shading == that.getShading() && shape == that.getShape(); } - - } +} \ No newline at end of file diff --git a/src/Deck.java b/src/Deck.java index 8a09440..ed39b3f 100644 --- a/src/Deck.java +++ b/src/Deck.java @@ -11,7 +11,7 @@ public class Deck { public Deck() { - for ( int qty = 1; qty <= 3; qty ++) { + for ( int quant = 1; quant <= 3; quant ++) { for (int clr = 1; clr <= 3; clr ++) { @@ -19,11 +19,13 @@ public Deck() { for (int shp = 1; shp <=3; shp ++) { - Card card = new Card(qty,clr,shd,shp); + Card card = new Card(quant, clr, shd, shp); + cards.add(card); } - } + } } } + Collections.shuffle(cards); } @@ -33,18 +35,23 @@ public boolean hasNext(){ if (nextCardIndex < cards.size()){ return true; } + else { + return false; } + } public Card getNext(){ if (hasNext() == false){ + return null; } - else{ + else { + nextCardIndex += 1; return cards.get(nextCardIndex-1); } 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