From 2b531c4a3b639a805b7df7c5541f4d6a8d6dd0b7 Mon Sep 17 00:00:00 2001 From: fritz88 Date: Fri, 8 May 2015 01:30:56 -0400 Subject: [PATCH 01/10] Create Card --- src/Card | 125 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 src/Card diff --git a/src/Card b/src/Card new file mode 100644 index 0000000..cc029da --- /dev/null +++ b/src/Card @@ -0,0 +1,125 @@ +public class Card +{ + public int quantity = 0; + public int color = 0; + public int shading = 0; + public int shape = 0; + + public Card(int quan, int col, int shade, int shap) + { + if(quan < 1 || quan > 3) + { + if(quan <0) + quantity = (((quan%3) +3) %3)+1; + else + quantity = (quan % 3)+1; + } + else + quantity = quan; + + if(col < 1 || col > 3) + { + if(col <0) + color = (((col % 3) + 3) % 3)+1; + else + color = (col % 3)+1; + } + else + color = col; + + if(shade < 1 || shade > 3) + { + if(shade <0) + shading = (((shade % 3) + 3) % 3)+1; + else + shading = (shade % 3)+1; + } + else + shading = shade; + + if(shap < 1 || shap > 3) + { + if(shap <0) + shape = (((shap % 3) + 3) % 3)+1; + else + shape = (shap % 3)+1; + } + else + shape = shap; + } + public int getQuantity() + { + return quantity; + } + public int getColor() + { + return color; + } + public int getShading() + { + return shading; + } + public int getShape() + { + return shape; + } + public boolean equals(Object obj) + { + Card that = (Card)obj; + + return (quantity == that.getQuantity() && + color == that.getColor() && + shading == that.getShading() && + shape == that.getShape() ); + } + public boolean isSet(Card card2, Card card3) + { + boolean quanCheck = false; + boolean colCheck = false; + boolean shadeCheck = false; + boolean shapeCheck = false; + + if(((quantity + card2.getQuantity() + card3.getQuantity() )%3) == 0) + quanCheck = true; + if(((color + card2.getColor() + card3.getColor() )%3) == 0) + colCheck = true; + if(((shading + card2.getShading() + card3.getShading() )%3) == 0) + shadeCheck = true; + if(((shape + card2.getShape() + card3.getShape() )%3) == 0) + shapeCheck = true; + + return (quanCheck == true && colCheck == true && shadeCheck == true && shapeCheck == true); + } + + public String toString() + { + //Quantity + String info = "" + quantity; + + //Color + if(color == 1) + info += "R"; + else if(color == 2) + info += "G"; + else if(color == 3) + info += "P"; + + //Shading + if(shading == 1) + info += "O"; + else if(shading == 2) + info += "T"; + else if(shading == 3) + info += "S"; + + //Shape + if(shape == 1) + info += "O"; + else if(shape == 2) + info += "D"; + else if(shape == 3) + info += "S"; + + return info; + } +} From 6f5141238eda8760490065d33f54878fb2c4081d Mon Sep 17 00:00:00 2001 From: fritz88 Date: Fri, 8 May 2015 01:33:15 -0400 Subject: [PATCH 02/10] Card.java Use this Card --- src/{Card => Card2.java} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/{Card => Card2.java} (100%) diff --git a/src/Card b/src/Card2.java similarity index 100% rename from src/Card rename to src/Card2.java From b0c4fc639fae6645af422c0ddb2e8eb107098bda Mon Sep 17 00:00:00 2001 From: fritz88 Date: Fri, 8 May 2015 01:34:06 -0400 Subject: [PATCH 03/10] CardTest.java --- src/CardTest.java | 64 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/CardTest.java diff --git a/src/CardTest.java b/src/CardTest.java new file mode 100644 index 0000000..6445982 --- /dev/null +++ b/src/CardTest.java @@ -0,0 +1,64 @@ +import junit.framework.TestCase; + +public class CardTest extends TestCase +{ + Card a = new Card(1,1,1,1); + Card b = new Card(2,2,3,3); + Card c = new Card(1,2,3,1); + Card d = new Card(-4,-5,6,7); + + public void testGetQuantity() + { + assertEquals(1,a.getQuantity()); + assertEquals(2,b.getQuantity()); + assertEquals(1,c.getQuantity()); + assertEquals(3,d.getQuantity()); + } + public void testGetColor() + { + assertEquals(1,a.getColor()); + assertEquals(2,b.getColor()); + assertEquals(2,c.getColor()); + assertEquals(2,d.getColor()); + } + public void testGetShading() + { + assertEquals(1,a.getShading()); + assertEquals(3,b.getShading()); + assertEquals(3,c.getShading()); + assertEquals(1,d.getShading()); + } + public void testGetShape() + { + assertEquals(1,a.getShape()); + assertEquals(3,b.getShape()); + assertEquals(1,c.getShape()); + assertEquals(2,d.getShape()); + } + public void testEquals() + { + a = b; + assertEquals(true,a.equals(b)); + assertEquals(false,c.equals(d)); + } + public void testIsString() + { + a = new Card(1,1,1,1); + b = new Card(2,2,2,2); + c = new Card(3,3,3,3); + + assertEquals("1ROO",a.toString()); + assertEquals("2GTD",b.toString()); + assertEquals("3PSS",c.toString()); + } + public void testIsSet() + { + a = new Card(1,1,1,1); + b = new Card(2,2,2,2); + c = new Card(3,3,3,3); + d = new Card(1,2,3,1); + + assertEquals(true,a.isSet(b,c)); + assertEquals(false,a.isSet(c,d)); + } +} From ebc33046dc3dbad35cf8373d1205f98be1096d34 Mon Sep 17 00:00:00 2001 From: fritz88 Date: Fri, 8 May 2015 01:34:51 -0400 Subject: [PATCH 04/10] Deck2.java Use this Deck --- src/Deck2.java | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 src/Deck2.java diff --git a/src/Deck2.java b/src/Deck2.java new file mode 100644 index 0000000..fd0ffcd --- /dev/null +++ b/src/Deck2.java @@ -0,0 +1,84 @@ +import java.io.BufferedReader; +import java.io.FileReader; +import java.util.StringTokenizer; +import java.util.ArrayList; +import java.util.Collections; + +public class Deck +{ + private int nextCardIndex = 0; + private ArrayList cards = new ArrayList(81); + + public Deck() + { + for(int quan = 1; quan < 4; quan++) + { + for(int col = 1; col < 4; col++) + { + for(int shade = 1; shade < 4; shade++) + { + for(int shap = 1; shap < 4; shap++) + { + Card card = new Card(quan,col,shade,shap); + cards.add(card); + } + } + } + } + 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; + nextCardIndex ++; + return cards.get(nextCardIndex-1); + } + + public Deck(String filename) + { + 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; + + // 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 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()); + } + } +} From 8cde0364ec50daec88b9bf05e7d15b5e0d1007dc Mon Sep 17 00:00:00 2001 From: fritz88 Date: Fri, 8 May 2015 01:35:54 -0400 Subject: [PATCH 05/10] DeckTest.java --- src/DeckTest.java | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/DeckTest.java diff --git a/src/DeckTest.java b/src/DeckTest.java new file mode 100644 index 0000000..2bffc4b --- /dev/null +++ b/src/DeckTest.java @@ -0,0 +1,25 @@ +import junit.framework.TestCase; + +public class DeckTest extends TestCase +{ + Deck d1 = new Deck(); + Card c1 = new Card(1,1,1,1); + + public void testHasNext() + { + assertEquals(true,d1.hasNext()); + while(d1.getNext() != null) + { + c1 = d1.getNext(); + } + assertEquals(false,d1.hasNext()); + } + public void testGetNext() + { + while(d1.hasNext() == true) + { + c1 = d1.getNext(); + } + assertEquals(null,d1.getNext()); + } +} From 8fdcaf4e753e53ef6a340cad2164e8b15a3f48c7 Mon Sep 17 00:00:00 2001 From: fritz88 Date: Fri, 8 May 2015 01:36:20 -0400 Subject: [PATCH 06/10] Game.java --- src/Game.java | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 src/Game.java diff --git a/src/Game.java b/src/Game.java new file mode 100644 index 0000000..158df94 --- /dev/null +++ b/src/Game.java @@ -0,0 +1,100 @@ +public class Game +{ + private int removedCards = 0; + private int sets = 0; + private int numRounds = 0; + Table t = new Table(); + Deck d; + + public Game() + { + d = new Deck(); + for(int i=0; i<12; i++) + { + if(d.hasNext()) + t.add(d.getNext()); + } + } + + public Game(String filename) + { + d = new Deck(filename); + for(int i=0; i<12; i++) + { + if(d.hasNext()) + t.add(d.getNext()); + } + } + + public int numSets() + { + return t.numSets(); + } + + public int numCards() + { + return t.numCards(); + } + + public void playRound() + { + if(isGameOver() == false) + { + if(t.numSets() > 0) + { + Card a = null; + Card b = null; + Card c = null; + boolean findSet = false; + + for(int i=0; i Date: Fri, 8 May 2015 01:36:54 -0400 Subject: [PATCH 07/10] MonteCarloSim.java --- src/MonteCarloSim.java | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/MonteCarloSim.java diff --git a/src/MonteCarloSim.java b/src/MonteCarloSim.java new file mode 100644 index 0000000..5365f70 --- /dev/null +++ b/src/MonteCarloSim.java @@ -0,0 +1,23 @@ +public class MonteCarloSim +{ + public static void main(String[] args) + { + int numGames = 10000; + double setsAtStart = 0.0; + double cardsLeft = 0; + + for(int i=0; i Date: Fri, 8 May 2015 01:37:37 -0400 Subject: [PATCH 08/10] Table.java --- src/Table.java | 108 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 src/Table.java diff --git a/src/Table.java b/src/Table.java new file mode 100644 index 0000000..8fa38b3 --- /dev/null +++ b/src/Table.java @@ -0,0 +1,108 @@ +public class Table +{ + private TableNode head; + private int cardsOnTable = 0; + + public Table() + { + head = null; + } + + public void add(Card card) + { + TableNode newCard = new TableNode(card); + newCard.setNext(head); + head = newCard; + cardsOnTable ++; + } + public void removeSet(Card one, Card two, Card three) + { + if(one.isSet(two,three) == true && onTable(one,two,three) == true) + { + removeCard(one); + removeCard(two); + removeCard(three); + } + } + private boolean onTable(Card one) + { + for(int i=0; i= 0 && index < cardsOnTable) + for(int i=0; i < index; i++) + current = current.getNext(); + else + return null; + return current.getCard(); + } + public void removeCard(Card remove) + { + TableNode current = head; + for(int i=0; i < numCards(); i++) + { + if((current.getCard()).equals(remove)) + { + head = current.getNext(); + cardsOnTable--; + return; + } + if(((current.getNext()).getCard()).equals(remove)) + { + current.setNext((current.getNext()).getNext()); + cardsOnTable--; + return; + } + current = current.getNext(); + } + } +} From e17b2148aa6a771bba87f3ef300dac2d516487e5 Mon Sep 17 00:00:00 2001 From: fritz88 Date: Fri, 8 May 2015 01:38:10 -0400 Subject: [PATCH 09/10] TableNode.java --- src/TableNode.java | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/TableNode.java diff --git a/src/TableNode.java b/src/TableNode.java new file mode 100644 index 0000000..e2835ed --- /dev/null +++ b/src/TableNode.java @@ -0,0 +1,24 @@ +public class TableNode +{ + private Card card; + private TableNode next; + + public TableNode(Card c) + { + card = c; + next = null; + } + + public void setNext(TableNode n) + { + next = n; + } + public TableNode getNext() + { + return next; + } + public Card getCard() + { + return card; + } +} From b12ed2145f5215f29057d8568a2f449461bd8290 Mon Sep 17 00:00:00 2001 From: fritz88 Date: Fri, 8 May 2015 01:38:44 -0400 Subject: [PATCH 10/10] TableNodeTest.java --- src/TableNodeTest.java | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/TableNodeTest.java diff --git a/src/TableNodeTest.java b/src/TableNodeTest.java new file mode 100644 index 0000000..8e075f8 --- /dev/null +++ b/src/TableNodeTest.java @@ -0,0 +1,30 @@ +import junit.framework.TestCase; + +public class TableNodeTest extends TestCase +{ + Card card1 = new Card(1,1,1,1); + TableNode node1 = new TableNode(card1); + Card card2 = new Card(1,2,3,4); + TableNode node2 = new TableNode(card2); + + public void testGetCard() + { + assertEquals(card1,node1.getCard()); + assertEquals(card2,node2.getCard()); + } + + public void testGetNext() + { + assertEquals(null,node1.getNext()); + assertEquals(null,node2.getNext()); + } + + public void testSetNext() + { + node1.setNext(node2); + node2.setNext(null); + + assertEquals(node2,node1.getNext()); + assertEquals(null,node2.getNext()); + } +}