From ed04c559847adfc2ff328e2134af1f0d799da379 Mon Sep 17 00:00:00 2001 From: Marlon Moraga Date: Wed, 8 Apr 2015 10:01:29 -0400 Subject: [PATCH 01/11] My setproject is finished --- src/SetProject/3cardNew.dat | 3 + src/SetProject/Card.java | 104 +++++++++++++++++++++++++++++++++++ src/SetProject/Deck.java | 79 ++++++++++++++++++++++++++ src/SetProject/Decktest.java | 43 +++++++++++++++ 4 files changed, 229 insertions(+) create mode 100644 src/SetProject/3cardNew.dat create mode 100644 src/SetProject/Card.java create mode 100644 src/SetProject/Deck.java create mode 100644 src/SetProject/Decktest.java diff --git a/src/SetProject/3cardNew.dat b/src/SetProject/3cardNew.dat new file mode 100644 index 0000000..aecb4b1 --- /dev/null +++ b/src/SetProject/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/SetProject/Card.java b/src/SetProject/Card.java new file mode 100644 index 0000000..0e289bc --- /dev/null +++ b/src/SetProject/Card.java @@ -0,0 +1,104 @@ +public class Card { + private int quantity; + private int color; + private int shade; + private int shape; + + + 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 getShade(){ + + 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.getShade() + cardMM.getShade()) % 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; + } +} + + + + + + + + + + diff --git a/src/SetProject/Deck.java b/src/SetProject/Deck.java new file mode 100644 index 0000000..e46dd63 --- /dev/null +++ b/src/SetProject/Deck.java @@ -0,0 +1,79 @@ +import java.io.BufferedReader; +import java.io.FileReader; +import java.util.StringTokenizer; +import java.util.ArrayList; + +public class Deck { + 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; + } + 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); + + 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 shade = Integer.parseInt(tokenizer.nextToken()); + int shape = Integer.parseInt(tokenizer.nextToken()); + + cards.add(new Card(quantity, color, shade, shape)); + nextCardIndex = 0; + } + } + catch(Exception e) { + throw new RuntimeException("Error while reading file: " + e.toString()); + } + } +} diff --git a/src/SetProject/Decktest.java b/src/SetProject/Decktest.java new file mode 100644 index 0000000..ac11536 --- /dev/null +++ b/src/SetProject/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 From db3b812c66c2da52d333feb49c8206a94adadee5 Mon Sep 17 00:00:00 2001 From: Marlon Moraga Date: Wed, 8 Apr 2015 10:10:06 -0400 Subject: [PATCH 02/11] I finished --- src/3cardNew.dat | 3 ++ src/Card.java | 106 +++++++++++++++++++++++++++++++++++++++++++--- src/CardTest.java | 29 +++++++++++++ src/Deck.java | 40 +++++++++++++++-- src/Decktest.java | 43 +++++++++++++++++++ 5 files changed, 211 insertions(+), 10 deletions(-) create mode 100644 src/3cardNew.dat mode change 100755 => 100644 src/Card.java create mode 100644 src/CardTest.java mode change 100755 => 100644 src/Deck.java create mode 100644 src/Decktest.java 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..0e289bc --- a/src/Card.java +++ b/src/Card.java @@ -1,12 +1,104 @@ 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) { - Card that = (Card)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; + } + - return quantity == that.getQuantity() && - color == that.getColor() && - shading == that.getShading() && - shape == that.getShape(); + public int getQuantity(){ + + return quantity; + } + + public int getColor(){ + + return color; + } + + public int getShade(){ + + 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.getShade() + cardMM.getShade()) % 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; + } } + + + + + + + + + + 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..e46dd63 --- a/src/Deck.java +++ b/src/Deck.java @@ -4,8 +4,42 @@ import java.util.ArrayList; 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; + } + 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); @@ -31,10 +65,10 @@ public Deck(String filename) { 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 From dcf49619269d0012fb7962de9ca23921bb5177fe Mon Sep 17 00:00:00 2001 From: Marlon Moraga Date: Wed, 8 Apr 2015 10:10:32 -0400 Subject: [PATCH 03/11] Cleaned up files --- src/SetProject/3cardNew.dat | 3 - src/SetProject/Card.java | 104 ----------------------------------- src/SetProject/Deck.java | 79 -------------------------- src/SetProject/Decktest.java | 43 --------------- 4 files changed, 229 deletions(-) delete mode 100644 src/SetProject/3cardNew.dat delete mode 100644 src/SetProject/Card.java delete mode 100644 src/SetProject/Deck.java delete mode 100644 src/SetProject/Decktest.java diff --git a/src/SetProject/3cardNew.dat b/src/SetProject/3cardNew.dat deleted file mode 100644 index aecb4b1..0000000 --- a/src/SetProject/3cardNew.dat +++ /dev/null @@ -1,3 +0,0 @@ -1 2 3 1 -2 1 2 3 -3 1 2 1 \ No newline at end of file diff --git a/src/SetProject/Card.java b/src/SetProject/Card.java deleted file mode 100644 index 0e289bc..0000000 --- a/src/SetProject/Card.java +++ /dev/null @@ -1,104 +0,0 @@ -public class Card { - private int quantity; - private int color; - private int shade; - private int shape; - - - 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 getShade(){ - - 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.getShade() + cardMM.getShade()) % 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; - } -} - - - - - - - - - - diff --git a/src/SetProject/Deck.java b/src/SetProject/Deck.java deleted file mode 100644 index e46dd63..0000000 --- a/src/SetProject/Deck.java +++ /dev/null @@ -1,79 +0,0 @@ -import java.io.BufferedReader; -import java.io.FileReader; -import java.util.StringTokenizer; -import java.util.ArrayList; - -public class Deck { - 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; - } - 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); - - 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 shade = Integer.parseInt(tokenizer.nextToken()); - int shape = Integer.parseInt(tokenizer.nextToken()); - - cards.add(new Card(quantity, color, shade, shape)); - nextCardIndex = 0; - } - } - catch(Exception e) { - throw new RuntimeException("Error while reading file: " + e.toString()); - } - } -} diff --git a/src/SetProject/Decktest.java b/src/SetProject/Decktest.java deleted file mode 100644 index ac11536..0000000 --- a/src/SetProject/Decktest.java +++ /dev/null @@ -1,43 +0,0 @@ -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 From 5327a853b2a957796eac9e1d38b6807fff65e2b6 Mon Sep 17 00:00:00 2001 From: Marlon Moraga Date: Wed, 15 Apr 2015 09:23:29 -0400 Subject: [PATCH 04/11] TableSet has been added --- src/TableNode.java | 26 +++++++++ src/TableNodeTest.java | 29 +++++++++ src/TableSet.java | 129 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 184 insertions(+) create mode 100644 src/TableNode.java create mode 100644 src/TableNodeTest.java create mode 100644 src/TableSet.java diff --git a/src/TableNode.java b/src/TableNode.java new file mode 100644 index 0000000..5523ced --- /dev/null +++ b/src/TableNode.java @@ -0,0 +1,26 @@ +public class TableNode +{ + Card card; + TableNode next; + + public TableNode(Card card1) + { + card = card1; + next = null; + } + + public void setNext(TableNode node) + { + next = node; + } + + public TableNode getNext() + { + return next; + } + + public Card getCard() + { + return card; + } +} \ No newline at end of file diff --git a/src/TableNodeTest.java b/src/TableNodeTest.java new file mode 100644 index 0000000..ef39265 --- /dev/null +++ b/src/TableNodeTest.java @@ -0,0 +1,29 @@ +import junit.framework.TestCase; + +public class TableNodeTest extends TestCase { + + public void testTableNode() + { + Card card = new Card(1, 1, 1, 1); + TableNode node = new TableNode(card); + + assertEquals(null, node.getNext()); + assertEquals(true, node.getCard().equals(card)); + + Card card2 = new Card(1, 1, 1, 2); + TableNode node2 = new TableNode(card2); + node.setNext(node2); + + assertEquals(true, node.getCard().equals(card)); + assertEquals(true, node.getNext().getCard().equals(card2)); + assertEquals(null, node.getNext().getNext()); + + Card card3 = new Card(1, 1, 1, 3); + node2 = new TableNode(card3); + node.setNext(node2); + + assertEquals(true, node.getCard().equals(card)); + assertEquals(true, node.getNext().getCard().equals(card3)); + + } +} \ No newline at end of file diff --git a/src/TableSet.java b/src/TableSet.java new file mode 100644 index 0000000..ed8e5a2 --- /dev/null +++ b/src/TableSet.java @@ -0,0 +1,129 @@ +public class TableSet { + private TableNode head; + private int length; + + public TableSet() { + // Creating head + head = null; + } + + public void add(Card card) { + TableNode newNode = new TableNode(card); + newNode.setNext(head); + head = newNode; + } + + private boolean onTable(Card c){ + TableNode curr = head; + + while(curr != null){ + if(equals(c)){ + return true; + } + else{ + curr.getNext(); + } + } + return false; + } + private void removeCard(Card c){ + TableNode curr = head; + TableNode prev = null; + while(curr != null){ + if(!curr.equals(c)) + curr.getNext(); + else{ + if(curr == head) + head = head.getNext(); + else + prev.setNext(curr.getNext()); + } + prev = curr; + curr = curr.getNext(); + } + } + + + + public void removeSet(Card c1, Card c2, Card c3) { + //if 3 cards dont form a set or if any of the cards are not on the table, + // return. + //otherwise: + // remove c1, c2, and c3 preserving the relative order of the rest of the cards. + + if(c1.isSet(c2, c3) == false) + return; + if(!onTable(c1)) + return; + if(!onTable(c2)) + return; + if(!onTable(c3)) + return; + removeCard(c1); + removeCard(c2); + removeCard(c3); + + } + + + public int numCards(){ + //if we havent stored the length of the list, we have to iterate through the + //list and count the cards. + TableNode temp = head; + if(head == null) + return 0; + while(temp != null) + { + length++; + temp = temp.getNext(); + } + return length; + } + + public Card getCard(int index) { + //if index is out of bounds, return null. + //otherwise: + // iterate through the list index number of times, + // return the node there + TableNode curr = head; + if(head == null) + return null; + if(index < numCards()) + { + for(int i = 0; i Date: Thu, 16 Apr 2015 15:05:14 -0400 Subject: [PATCH 05/11] Updated Table (New version) --- src/Table.java | 111 ++++++++++++++++++++++++++++++++++++++++++ src/testTable.java | 117 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 228 insertions(+) create mode 100644 src/Table.java create mode 100644 src/testTable.java diff --git a/src/Table.java b/src/Table.java new file mode 100644 index 0000000..9c20ef4 --- /dev/null +++ b/src/Table.java @@ -0,0 +1,111 @@ +public class Table { + private TableNode head; + private int length; + + public Table() { + //initialize head + 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){ + 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; + if(head == null) + return 0; + while(temp != null) + { + length++; + temp = temp.getNext(); + } + return length; + } + + public Card getCard(int index) { + TableNode curr = head; + if(head == null) + return null; + if(index < numCards()) + { + for(int i = 0; i Date: Fri, 17 Apr 2015 10:24:44 -0400 Subject: [PATCH 06/11] Updated --- src/Table.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Table.java b/src/Table.java index 9c20ef4..1e00e78 100644 --- a/src/Table.java +++ b/src/Table.java @@ -27,6 +27,8 @@ private boolean onTable(Card m){ return false; } private void removeCard(Card m){ + if (head == null) + return; TableNode prev = null; TableNode curr = head; if (m.equals(head.getCard())) From 5e5bbd74d9baad3900427a03ff83ca8a5426ca59 Mon Sep 17 00:00:00 2001 From: Marlon Moraga Date: Thu, 23 Apr 2015 14:55:20 -0400 Subject: [PATCH 07/11] Updated --- src/Table.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Table.java b/src/Table.java index 1e00e78..f8fcdca 100644 --- a/src/Table.java +++ b/src/Table.java @@ -1,9 +1,8 @@ public class Table { private TableNode head; - private int length; + private int amount; public Table() { - //initialize head head = null; } @@ -47,7 +46,7 @@ private void removeCard(Card m){ public void removeSet(Card m1, Card m2, Card m3) { - if(m1.isSet(m2, m3) == false) + if(m1.isSet (m2, m3) == false) return; if(!onTable(m1)) return; @@ -63,14 +62,15 @@ public void removeSet(Card m1, Card m2, Card m3) { public int numCards(){ TableNode temp = head; + amount = 0; if(head == null) return 0; while(temp != null) { - length++; + amount++; temp = temp.getNext(); } - return length; + return amount; } public Card getCard(int index) { @@ -79,9 +79,9 @@ public Card getCard(int index) { return null; if(index < numCards()) { - for(int i = 0; i Date: Fri, 24 Apr 2015 10:15:36 -0400 Subject: [PATCH 08/11] =?UTF-8?q?Updated=20Files=20and=20Game=20has=20been?= =?UTF-8?q?=20added=E2=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Card.java | 12 +++++-- src/Deck.java | 14 ++++---- src/Game.java | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 9 deletions(-) create mode 100644 src/Game.java diff --git a/src/Card.java b/src/Card.java index 0e289bc..ef5987a 100644 --- a/src/Card.java +++ b/src/Card.java @@ -30,7 +30,7 @@ public int getColor(){ return color; } - public int getShade(){ + public int getShading(){ return shade; } @@ -43,7 +43,7 @@ public int getShape(){ public boolean isSet (Card cardM,Card cardMM){ if (((((color + cardM.getColor() + cardMM.getColor()) % 3) == 0)) && (((quantity + cardM.getQuantity() + cardMM.getQuantity()) % 3) == 0) && - (((shade + cardM.getShade() + cardMM.getShade()) % 3) == 0) && + (((shade + cardM.getShading() + cardMM.getShading()) % 3) == 0) && (((shape + cardM.getShape() + cardMM.getShape()) % 3) == 0)) { return true; @@ -91,6 +91,14 @@ private int fixValue(int valueToFix) { else return valueToFix; } +public boolean equals(Object obj) { + Card that = (Card)obj; + + return quantity == that.getQuantity() && + color == that.getColor() && + shade == that.getShading() && + shape == that.getShape(); +} } diff --git a/src/Deck.java b/src/Deck.java index e46dd63..8f593e1 100644 --- a/src/Deck.java +++ b/src/Deck.java @@ -2,23 +2,26 @@ 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; - + 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; } } } } - nextCardIndex = 0; + Collections.shuffle(cards); } public boolean hasNext(){ if (nextCardIndex < cards.size()) @@ -49,18 +52,15 @@ 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()); diff --git a/src/Game.java b/src/Game.java new file mode 100644 index 0000000..ed9593d --- /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(){ + // Working with Conch on playround, very difficult + 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() == true) + return; + + t.add(d.getNext()); + } + } + } + } + } + } + + 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; + } +} + + + + From 5b2a99636c3529d10bccb478cae5ae2a128521de Mon Sep 17 00:00:00 2001 From: Marlon Moraga Date: Wed, 29 Apr 2015 10:21:41 -0400 Subject: [PATCH 09/11] Game Average --- src/MonteCarloSim.java | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/MonteCarloSim.java diff --git a/src/MonteCarloSim.java b/src/MonteCarloSim.java new file mode 100644 index 0000000..30256a3 --- /dev/null +++ b/src/MonteCarloSim.java @@ -0,0 +1,27 @@ +public class MonteCarloSim +{ + public static void main(String[] args) + { + double nBeginingSets = 0; + double nEndSets = 0; + double nBeginCards = 0; + + for (int i = 0; i < 1000000; i++) + { + Game game = new Game(); + nBeginCards += game.numCards(); + nBeginingSets += game.numSets(); + + while (!game.isGameOver()) + { + game.playRound(); + } + + nEndSets += game.numCards(); + } + + System.out.println(nBeginingSets/1000000); + System.out.println(nEndSets/1000000); + System.out.println(nBeginCards/1000000); + } +} \ No newline at end of file From c394ca86e47902e5593a254a197122e2ed60d0f5 Mon Sep 17 00:00:00 2001 From: MarlonMoraga Date: Wed, 29 Apr 2015 10:24:47 -0400 Subject: [PATCH 10/11] Delete MonteCarloSim.java --- src/MonteCarloSim.java | 27 --------------------------- 1 file changed, 27 deletions(-) delete mode 100644 src/MonteCarloSim.java diff --git a/src/MonteCarloSim.java b/src/MonteCarloSim.java deleted file mode 100644 index 30256a3..0000000 --- a/src/MonteCarloSim.java +++ /dev/null @@ -1,27 +0,0 @@ -public class MonteCarloSim -{ - public static void main(String[] args) - { - double nBeginingSets = 0; - double nEndSets = 0; - double nBeginCards = 0; - - for (int i = 0; i < 1000000; i++) - { - Game game = new Game(); - nBeginCards += game.numCards(); - nBeginingSets += game.numSets(); - - while (!game.isGameOver()) - { - game.playRound(); - } - - nEndSets += game.numCards(); - } - - System.out.println(nBeginingSets/1000000); - System.out.println(nEndSets/1000000); - System.out.println(nBeginCards/1000000); - } -} \ No newline at end of file From 4d0862ced843d0ab849d577eade662c85738710e Mon Sep 17 00:00:00 2001 From: Marlon Moraga Date: Fri, 1 May 2015 10:01:01 -0400 Subject: [PATCH 11/11] Finally finished the Entire project!!!!!!!! --- src/Game.java | 4 ++-- src/MonteCarloSim.java | 18 +++++++++--------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Game.java b/src/Game.java index ed9593d..a85c888 100644 --- a/src/Game.java +++ b/src/Game.java @@ -26,7 +26,6 @@ public int numCards(){ } public void playRound(){ - // Working with Conch on playround, very difficult if (t.numSets() == 0 && d.hasNext() == true) { for (int i = 0; i < 3; i++) { if(d.hasNext() == true) { @@ -45,11 +44,12 @@ public void playRound(){ while (t.numCards() < 12) { - if (d.hasNext() == true) + if (d.hasNext() == false) return; t.add(d.getNext()); } + return; } } } diff --git a/src/MonteCarloSim.java b/src/MonteCarloSim.java index 30256a3..5e1d4c1 100644 --- a/src/MonteCarloSim.java +++ b/src/MonteCarloSim.java @@ -2,26 +2,26 @@ public class MonteCarloSim { public static void main(String[] args) { - double nBeginingSets = 0; - double nEndSets = 0; - double nBeginCards = 0; + double nStartSets = 0; + double nEndingSets = 0; + double nCards = 0; for (int i = 0; i < 1000000; i++) { Game game = new Game(); - nBeginCards += game.numCards(); - nBeginingSets += game.numSets(); + nCards += game.numCards(); + nStartSets += game.numSets(); while (!game.isGameOver()) { game.playRound(); } - nEndSets += game.numCards(); + nEndingSets += game.numCards(); } - System.out.println(nBeginingSets/1000000); - System.out.println(nEndSets/1000000); - System.out.println(nBeginCards/1000000); + System.out.println(nStartSets/1000000); + System.out.println(nEndingSets/1000000); + System.out.println(nCards/1000000); } } \ No newline at end of file