From 9fb71349996db8ea0f92b355f860526eab7dda2e Mon Sep 17 00:00:00 2001 From: Dan Howard Date: Mon, 30 Mar 2015 14:43:01 -0400 Subject: [PATCH 01/15] Card Class --- src/Card.java | 80 ++++++++++++++++++++++++++++++++++++++++------- src/CardTest.java | 66 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+), 12 deletions(-) mode change 100755 => 100644 src/Card.java create mode 100644 src/CardTest.java diff --git a/src/Card.java b/src/Card.java old mode 100755 new mode 100644 index 9eed9a5..b46166f --- a/src/Card.java +++ b/src/Card.java @@ -1,12 +1,68 @@ -public class Card { - // Create the rest of this class yourself - - public boolean equals(Object obj) { - Card that = (Card)obj; - - return quantity == that.getQuantity() && - color == that.getColor() && - shading == that.getShading() && - shape == that.getShape(); - } -} +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 cardB, Card cardC){ + return + (((quantity + cardB.getQuantity() + cardC.getQuantity()) % 3) == 0) && + + (((color + cardB.getColor() + cardC.getColor()) % 3) == 0) && + + (((shading + cardB.getShading() + cardC.getShading()) % 3) == 0) && + + (((shape + cardB.getShape() + cardC.getShape()) % 3) == 0); + } + + public String toString(){ + String[] Scolor = new String[3]; + Scolor[0] = "R"; + Scolor[1] = "G"; + Scolor[2] = "P"; + + String[] Sshading = new String[3]; + Sshading[0] = "O"; + Sshading[1] = "T"; + Sshading[2] = "S"; + + String[] Sshape = new String[3]; + Sshape[0] = "O"; + Sshape[1] = "D"; + Sshape[2] = "S"; + + return quantity + Scolor[color-1] + Sshading[shading-1] + Sshape[shape-1]; + } + + 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/CardTest.java b/src/CardTest.java new file mode 100644 index 0000000..f1ed433 --- /dev/null +++ b/src/CardTest.java @@ -0,0 +1,66 @@ +import junit.framework.TestCase; + +public class CardTest extends TestCase{ + + public void testCardInt(){ + + Card c1 = new Card(7,1,1,1); + + assertEquals(2, c1.getQuantity()); + assertEquals(2, c1.getColor()); + assertEquals(2, c1.getShading()); + assertEquals(2, c1.getShape()); + + Card card3 = new Card(0, -1, -2, -3); + assertEquals(1, card3.getQuantity()); + assertEquals(3, card3.getColor()); + assertEquals(2, card3.getShading()); + assertEquals(1, card3.getShape()); + } + + public void testSet(){ + + Card cardA = new Card(1,1,1,1); + Card cardB = new Card(2,1,1,1); + Card cardC = new Card(3,1,1,1); + Card cardD = new Card(1,1,1,3); + + assertEquals(true, cardA.isSet(cardB, cardC)); + assertEquals(false, cardA.isSet(cardC, cardD)); + } + + public void testCardString(){ + + Card card1 = new Card(0, 1, 1, 1); + Card card2 = new Card(-2, -4, 0, -5); + Card card3 = new Card(-4, -3, -8, 8); + + assertEquals("1GTD", card1.toString()); + assertEquals("2POD", card2.toString()); + assertEquals("3RTS", card3.toString()); + } + + public void testCardEquals() + { + Card card1 = new Card(1, 2, 3, 1); + Card card2 = new Card(1, 2, 3, 1); + Card card3 = new Card(4, 5, 6, 7); + Card card4 = new Card(-2, -1, 0, -5); + Card card5 = new Card(1, 1, 1, 1); + + assertEquals(true, card1.equals(card2)); + assertEquals(true, card1.equals(card3)); + assertEquals(true, card1.equals(card4)); + assertEquals(true, card2.equals(card3)); + assertEquals(true, card2.equals(card4)); + assertEquals(true, card3.equals(card4)); + + assertEquals(false, card5.equals(card1)); + assertEquals(false, card5.equals(card2)); + assertEquals(false, card5.equals(card3)); + assertEquals(false, card5.equals(card4)); + + } +} + + \ No newline at end of file From 7e899d7abc2468db78d35fd1053f44290a2ca1ac Mon Sep 17 00:00:00 2001 From: Dan Howard Date: Mon, 30 Mar 2015 14:49:18 -0400 Subject: [PATCH 02/15] Updated --- src/CardTest.java | 110 +++++++++++++++++++--------------------------- 1 file changed, 45 insertions(+), 65 deletions(-) diff --git a/src/CardTest.java b/src/CardTest.java index f1ed433..1c62e6e 100644 --- a/src/CardTest.java +++ b/src/CardTest.java @@ -1,66 +1,46 @@ -import junit.framework.TestCase; - -public class CardTest extends TestCase{ - - public void testCardInt(){ - - Card c1 = new Card(7,1,1,1); - - assertEquals(2, c1.getQuantity()); - assertEquals(2, c1.getColor()); - assertEquals(2, c1.getShading()); - assertEquals(2, c1.getShape()); - - Card card3 = new Card(0, -1, -2, -3); - assertEquals(1, card3.getQuantity()); - assertEquals(3, card3.getColor()); - assertEquals(2, card3.getShading()); - assertEquals(1, card3.getShape()); - } - - public void testSet(){ - - Card cardA = new Card(1,1,1,1); - Card cardB = new Card(2,1,1,1); - Card cardC = new Card(3,1,1,1); - Card cardD = new Card(1,1,1,3); - - assertEquals(true, cardA.isSet(cardB, cardC)); - assertEquals(false, cardA.isSet(cardC, cardD)); - } - - public void testCardString(){ - - Card card1 = new Card(0, 1, 1, 1); - Card card2 = new Card(-2, -4, 0, -5); - Card card3 = new Card(-4, -3, -8, 8); - - assertEquals("1GTD", card1.toString()); - assertEquals("2POD", card2.toString()); - assertEquals("3RTS", card3.toString()); - } - - public void testCardEquals() - { - Card card1 = new Card(1, 2, 3, 1); - Card card2 = new Card(1, 2, 3, 1); - Card card3 = new Card(4, 5, 6, 7); - Card card4 = new Card(-2, -1, 0, -5); - Card card5 = new Card(1, 1, 1, 1); - - assertEquals(true, card1.equals(card2)); - assertEquals(true, card1.equals(card3)); - assertEquals(true, card1.equals(card4)); - assertEquals(true, card2.equals(card3)); - assertEquals(true, card2.equals(card4)); - assertEquals(true, card3.equals(card4)); - - assertEquals(false, card5.equals(card1)); - assertEquals(false, card5.equals(card2)); - assertEquals(false, card5.equals(card3)); - assertEquals(false, card5.equals(card4)); - - } -} - +import junit.framework.TestCase; + +public class CardTest extends TestCase{ + + public void testCardInt(){ + + Card c1 = new Card(7,1,1,1); + + assertEquals(2, c1.getQuantity()); + assertEquals(2, c1.getColor()); + assertEquals(2, c1.getShading()); + assertEquals(2, c1.getShape()); + + Card card3 = new Card(0, -1, -2, -3); + assertEquals(1, card3.getQuantity()); + assertEquals(3, card3.getColor()); + assertEquals(2, card3.getShading()); + assertEquals(1, card3.getShape()); + } + + public void testSet(){ + + Card cardA = new Card(1,1,1,1); + Card cardB = new Card(2,1,1,1); + Card cardC = new Card(3,1,1,1); + Card cardD = new Card(1,1,1,3); + + assertEquals(true, cardA.isSet(cardB, cardC)); + assertEquals(false, cardA.isSet(cardC, cardD)); + } + + public void testCardString(){ + + Card card1 = new Card(0, 1, 1, 1); + Card card2 = new Card(-2, -4, 0, -5); + Card card3 = new Card(-4, -3, -8, 8); + + assertEquals("1GTD", card1.toString()); + assertEquals("2POD", card2.toString()); + assertEquals("3RTS", card3.toString()); + } + +} + + \ No newline at end of file From 4178177023ab65e5a2e43fd738bcbd540c8d9858 Mon Sep 17 00:00:00 2001 From: Dan Howard Date: Mon, 6 Apr 2015 15:18:34 -0400 Subject: [PATCH 03/15] Deck Class --- src/Deck.java | 50 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) mode change 100755 => 100644 src/Deck.java diff --git a/src/Deck.java b/src/Deck.java old mode 100755 new mode 100644 index ab3a2a3..25f7b1e --- a/src/Deck.java +++ b/src/Deck.java @@ -2,14 +2,16 @@ 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 ArrayList cards = new ArrayList(); + private int nextCardIndex; public Deck(String filename) { cards = new ArrayList(81); - - try { + + try { String line; BufferedReader infile = new BufferedReader(new FileReader(filename)); int position = 0; @@ -42,4 +44,46 @@ public Deck(String filename) { throw new RuntimeException("Error while reading file: " + e.toString()); } } + + public Deck(){ + for (int quantity = 1; quantity < 4; quantity++) { + for (int color = 1; color < 4; color++) { + for (int shading = 1; shading < 4; shading++) { + for (int shape = 1; shape < 4; shape++) { + cards.add(new Card(quantity, color, shading, shape)); + } + } + } + } + Collections.shuffle(cards); + } + + public boolean hasNext(){ + //for (nextCardIndex = 0; nextCardIndex <= cards.size(); nextCardIndex++){ + if (nextCardIndex < cards.size()){ + return true; + } + else{ + return false; + } + } + //return hasNext(); + + + public Card getNext(){ + + if (hasNext() == false){ + return null; + } + + else{ + nextCardIndex += 1; + return cards.get(nextCardIndex-1); + } + } } + + + + + \ No newline at end of file From 54a10af2f4918b98d02d980b9fbfebfd25fd6b44 Mon Sep 17 00:00:00 2001 From: Dan Howard Date: Mon, 13 Apr 2015 15:19:55 -0400 Subject: [PATCH 04/15] Table and TableNode --- src/Table.java | 74 ++++++++++++++++++++++++++++++++++++++++++++++ src/TableNode.java | 21 +++++++++++++ 2 files changed, 95 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..3060bee --- /dev/null +++ b/src/Table.java @@ -0,0 +1,74 @@ +public class Table{ + private TableNode head; + private int length; + + public Table(){ + head = null; + length = 0; + } + + public void add(Card c1){ + TableNode tempNode = new TableNode(c1); + tempNode.setNext(head); + head = tempNode; + } + + public void removeSet(Card c1, Card c2, Card c3){ + TableNode curr = head; + if (c1.isSet(c2, c3)){ + + c1 = null; + c2 = null; + c2 = null; + } + else{ + return; + } + } + + public int numCards(){ + TableNode curr = head; + int cardCount = 0; + + if (curr == null){ + return 0; + } + else{ + + while (curr != null){ + curr = curr.getNext(); + cardCount += 1; + } + return cardCount; + } + } + + public Card getCard(int index){ + TableNode curr = head; + + if (curr == null){ + return null; + } + else{ + for (int i = 0; i < index; i++){ + curr = curr.getNext(); + } + } + + return curr.getCard(); + } + + + public int numSets(){ + TableNode curr = head; + TableNode curr2 = curr.getNext(); + TableNode curr3 = curr2.getNext(); + int setCount = 0; + + if (curr.getCard().isSet(curr2.getCard(), curr3.getCard())){ + setCount += 1; + } + while (curr.getNext() + return setCount; + } + } \ No newline at end of file diff --git a/src/TableNode.java b/src/TableNode.java new file mode 100644 index 0000000..fa32e4a --- /dev/null +++ b/src/TableNode.java @@ -0,0 +1,21 @@ +public class TableNode{ + private Card card; + private TableNode next; + + public TableNode(Card c1){ + card = c1; + 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 0b3067962efbacc20e033c4e71844d045e28eee5 Mon Sep 17 00:00:00 2001 From: Dan Howard Date: Mon, 13 Apr 2015 15:43:51 -0400 Subject: [PATCH 05/15] Table Updated --- src/Table.java | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/Table.java b/src/Table.java index 3060bee..4f28615 100644 --- a/src/Table.java +++ b/src/Table.java @@ -15,7 +15,10 @@ public void add(Card c1){ public void removeSet(Card c1, Card c2, Card c3){ TableNode curr = head; - if (c1.isSet(c2, c3)){ + if (c1.isSet(c2, c3) == false){ + return; + } + else if (c1.isSet(c2, c3)){ c1 = null; c2 = null; @@ -64,11 +67,10 @@ public int numSets(){ TableNode curr2 = curr.getNext(); TableNode curr3 = curr2.getNext(); int setCount = 0; - - if (curr.getCard().isSet(curr2.getCard(), curr3.getCard())){ - setCount += 1; - } - while (curr.getNext() - return setCount; - } - } \ No newline at end of file + if (curr.getCard().isSet(curr2.getCard(), curr3.getCard()) == true){ + setCount +=1; + } + return setCount; + } +} + \ No newline at end of file From 49af06894fa89638a3f8d9c3fb4de53ed9819068 Mon Sep 17 00:00:00 2001 From: 13howardD Date: Tue, 21 Apr 2015 21:13:46 -0700 Subject: [PATCH 06/15] First Game commit --- src/Game.java | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/Game.java diff --git a/src/Game.java b/src/Game.java new file mode 100644 index 0000000..888b0aa --- /dev/null +++ b/src/Game.java @@ -0,0 +1,29 @@ +public class Game{ + private Table t; + private Deck d; + + public Game(){ + Deck d = new Deck(); + Table t = new Table(); + } + + public Game(String name){ + + } + + public int numSets(){ + return 0; + } + + public int numCards(){ + return 0; + } + + public void playRound(){ + + } + + public boolean isGameOver(){ + return true; + } +} \ No newline at end of file From b27c5e3a7668e29890e0d9da605d8f8535b79d22 Mon Sep 17 00:00:00 2001 From: 13howardD Date: Thu, 23 Apr 2015 17:50:21 -0700 Subject: [PATCH 07/15] Game Updated --- src/Game.java | 59 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 51 insertions(+), 8 deletions(-) diff --git a/src/Game.java b/src/Game.java index 888b0aa..e8e60e6 100644 --- a/src/Game.java +++ b/src/Game.java @@ -3,27 +3,70 @@ public class Game{ private Deck d; public Game(){ - Deck d = new Deck(); - Table t = new Table(); + d = new Deck(); + t = new Table(); + for (int i = 0; i < 12; i++){ + t.add(d.getNext()); + } } - public Game(String name){ + public Game(String stringName){ + t = new Table(); + d = new Deck(stringName); + while (d.hasNext() == true){ + t.add(d.getNext()); + } + + if (t.numCards() == 12){ + return; + } } public int numSets(){ - return 0; + return t.numSets(); } public int numCards(){ - return 0; + return t.numCards(); } public void playRound(){ - - } + if (d.hasNext() == true && t.numSets() == 0){ + for (int i = 0; i < 3; i++){ + if (d.hasNext() == false){ + t.add(d.getNext()); + } + return; + } + + if (d.hasNext() == true && t.numSets() != 0){ + for (int x = 0; x < t.numCards() - 2; x++){ + for (int y = x + 1; y < t.numCards() - 1; y++){ + for (int z = y + 1; z < t.numCards(); z++){ + if (t.getCard(x).isSet(t.getCard(y), t.getCard(z))){ + t.removeSet(t.getCard(x), t.getCard(y), t.getCard(z)); + + while(t.numCards() < 12){ + if (d.hasNext() == true){ + t.add(d.getNext()); + } + return; + } + } + } + } + } + } + } + } public boolean isGameOver(){ - return true; + if (d.hasNext() == false && t.numSets() == 0){ + return true; + } + else{ + return false; + } } } \ No newline at end of file From 0728be4d73e9efde98e04251a871dece1b548199 Mon Sep 17 00:00:00 2001 From: 13howardD Date: Sun, 26 Apr 2015 00:54:22 -0700 Subject: [PATCH 08/15] Game Updated --- src/Game.java | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/src/Game.java b/src/Game.java index e8e60e6..4e2db48 100644 --- a/src/Game.java +++ b/src/Game.java @@ -10,16 +10,16 @@ public Game(){ } } - public Game(String stringName){ + public Game(String filename){ t = new Table(); - d = new Deck(stringName); + d = new Deck(filename); while (d.hasNext() == true){ t.add(d.getNext()); - } - - if (t.numCards() == 12){ + + if (t.numCards() > 11){ return; + } } } @@ -34,32 +34,26 @@ public int numCards(){ public void playRound(){ if (d.hasNext() == true && t.numSets() == 0){ for (int i = 0; i < 3; i++){ - if (d.hasNext() == false){ + if (d.hasNext() == false) t.add(d.getNext()); } return; } - if (d.hasNext() == true && t.numSets() != 0){ + else if (d.hasNext() == true && t.numSets() != 0){ for (int x = 0; x < t.numCards() - 2; x++){ for (int y = x + 1; y < t.numCards() - 1; y++){ for (int z = y + 1; z < t.numCards(); z++){ if (t.getCard(x).isSet(t.getCard(y), t.getCard(z))){ t.removeSet(t.getCard(x), t.getCard(y), t.getCard(z)); - - while(t.numCards() < 12){ - if (d.hasNext() == true){ - t.add(d.getNext()); - } - return; + return; } } } } + return; } - } - } - } + } public boolean isGameOver(){ if (d.hasNext() == false && t.numSets() == 0){ From 716c014b6813c9944fd4eb5bbac5e9c4db7039cf Mon Sep 17 00:00:00 2001 From: 13howardD Date: Sun, 26 Apr 2015 17:38:42 -0700 Subject: [PATCH 09/15] Changes to Game --- src/Game.java | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/Game.java b/src/Game.java index 4e2db48..c8c837e 100644 --- a/src/Game.java +++ b/src/Game.java @@ -34,26 +34,39 @@ public int numCards(){ public void playRound(){ if (d.hasNext() == true && t.numSets() == 0){ for (int i = 0; i < 3; i++){ - if (d.hasNext() == false) + if (d.hasNext()){ t.add(d.getNext()); } + else{ return; + } } + } - else if (d.hasNext() == true && t.numSets() != 0){ + if (d.hasNext() && t.numSets() != 0){ for (int x = 0; x < t.numCards() - 2; x++){ for (int y = x + 1; y < t.numCards() - 1; y++){ for (int z = y + 1; z < t.numCards(); z++){ if (t.getCard(x).isSet(t.getCard(y), t.getCard(z))){ t.removeSet(t.getCard(x), t.getCard(y), t.getCard(z)); - return; + + if (t.numCards() < 12 && d.hasNext()) { + for (int o = 0; o < 3; o++) { + if (d.hasNext()) { + t.add(d.getNext()); + } + else { + return; + } + } + } + return; } - } } - } - return; } - } + } + } + } public boolean isGameOver(){ if (d.hasNext() == false && t.numSets() == 0){ From ea43d508da102b1f588fd3fb0f2f520a7d069c3e Mon Sep 17 00:00:00 2001 From: 13howardD Date: Mon, 27 Apr 2015 18:33:15 -0700 Subject: [PATCH 10/15] Card Class Final Commit --- src/Card.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Card.java b/src/Card.java index b46166f..0fc95fe 100644 --- a/src/Card.java +++ b/src/Card.java @@ -65,4 +65,5 @@ public boolean equals(Object obj){ shading == that.getShading() && shape == that.getShape(); } - } \ No newline at end of file + } +// d \ No newline at end of file From fe3f20efb6176cb553c90b25e9d7fde977620223 Mon Sep 17 00:00:00 2001 From: 13howardD Date: Mon, 27 Apr 2015 18:34:59 -0700 Subject: [PATCH 11/15] Deck Class Final Commit --- src/Deck.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Deck.java b/src/Deck.java index 25f7b1e..2b0af64 100644 --- a/src/Deck.java +++ b/src/Deck.java @@ -67,7 +67,6 @@ public boolean hasNext(){ return false; } } - //return hasNext(); public Card getNext(){ From f469f53aef05810bedd494c47263e7882876f01c Mon Sep 17 00:00:00 2001 From: 13howardD Date: Mon, 27 Apr 2015 18:35:56 -0700 Subject: [PATCH 12/15] Game Class Final Commit --- src/Game.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Game.java b/src/Game.java index c8c837e..4ac608c 100644 --- a/src/Game.java +++ b/src/Game.java @@ -67,7 +67,7 @@ public void playRound(){ } } } - + //f public boolean isGameOver(){ if (d.hasNext() == false && t.numSets() == 0){ return true; From 8a830f67b1624d6542be1f943173a3e4e81dae0c Mon Sep 17 00:00:00 2001 From: 13howardD Date: Mon, 27 Apr 2015 18:37:40 -0700 Subject: [PATCH 13/15] TableNode Class Final Commit --- src/TableNode.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/TableNode.java b/src/TableNode.java index fa32e4a..3cf88d6 100644 --- a/src/TableNode.java +++ b/src/TableNode.java @@ -18,4 +18,5 @@ public TableNode getNext(){ public Card getCard(){ return card; } -} \ No newline at end of file +} +// d \ No newline at end of file From b3a6c38043f595fd7fad21bd713ac0473bcd7b05 Mon Sep 17 00:00:00 2001 From: 13howardD Date: Tue, 28 Apr 2015 14:17:36 -0700 Subject: [PATCH 14/15] Monte Carlo Simulation - Set Game --- src/MCSim.java | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/MCSim.java diff --git a/src/MCSim.java b/src/MCSim.java new file mode 100644 index 0000000..ab6fa57 --- /dev/null +++ b/src/MCSim.java @@ -0,0 +1,32 @@ +public class MCSim{ + public static void main (String[] args){ + + double setCount = 0; + double cardCount = 0; + int sim = 100001; + + for (int i = 0; i <= sim; i++){ + + Game game1 = new Game(); + setCount += game1.numSets(); + + while (game1.isGameOver()){ + game1.playRound(); + } + } + + System.out.println("The average number of sets from 100,000 random sets of cards is " + setCount / sim); + + for (int i = 0; i <= sim; i++){ + + Game game2 = new Game(); + cardCount += game2.numCards(); + + while (game2.isGameOver()){ + game2.playRound(); + } + } + + System.out.println("The average number of cards on the table when the game ends is " + cardCount / sim); + } +} \ No newline at end of file From 1ad620990ea146c5f7028b7d8a37f770237a7c96 Mon Sep 17 00:00:00 2001 From: 13howardD Date: Wed, 29 Apr 2015 14:00:24 -0700 Subject: [PATCH 15/15] Table Class Final Commit --- src/Table.java | 147 ++++++++++++++++++++++++++++++------------------- 1 file changed, 91 insertions(+), 56 deletions(-) diff --git a/src/Table.java b/src/Table.java index 4f28615..b28860a 100644 --- a/src/Table.java +++ b/src/Table.java @@ -1,76 +1,111 @@ -public class Table{ +public class Table { private TableNode head; - private int length; - public Table(){ + public Table() { head = null; - length = 0; } - public void add(Card c1){ - TableNode tempNode = new TableNode(c1); - tempNode.setNext(head); - head = tempNode; + public void add(Card card) { + TableNode Node1 = new TableNode(card); + Node1.setNext(head); + head = Node1; } - public void removeSet(Card c1, Card c2, Card c3){ - TableNode curr = head; - if (c1.isSet(c2, c3) == false){ + public void removeSet(Card c1, Card c2, Card c3) { + if (c1.isSet(c2, c3) == false) { return; } - else if (c1.isSet(c2, c3)){ - - c1 = null; - c2 = null; - c2 = null; - } - else{ - return; + + else { + Card remove1 = null; + Card remove2 = null; + Card remove3 = null; + + TableNode temp = head; + + while (temp != null) { + if (c1.equals(temp.getCard())) + remove1 = c1; + + if (c2.equals(temp.getCard())) + remove2 = c2; + + if (c3.equals(temp.getCard())) + remove3 = c3; + + temp = temp.getNext(); + } + + if (remove1 != null && remove2 != null && remove3 != null) { + TableNode curr = head; + TableNode prev = null; + + while (curr != null) { + + if (c1.equals(curr.getCard()) || c2.equals(curr.getCard()) || c3.equals(curr.getCard())) { + if (curr == head) { + curr = curr.getNext(); + head = head.getNext(); + } + else { + prev.setNext(curr.getNext()); + curr = curr.getNext(); + } + } + else { + prev = curr; + curr = curr.getNext(); + } + } + } } } - public int numCards(){ - TableNode curr = head; - int cardCount = 0; - - if (curr == null){ - return 0; - } - else{ - - while (curr != null){ - curr = curr.getNext(); - cardCount += 1; + public int numCards() { + TableNode currNode = head; + int count = 0; + while (currNode != null) { + count += 1; + currNode = currNode.getNext(); } - return cardCount; + return count; } - } - public Card getCard(int index){ - TableNode curr = head; - - if (curr == null){ + public Card getCard(int index) { + TableNode temp = head; + if (temp == null) { return null; - } - else{ - for (int i = 0; i < index; i++){ - curr = curr.getNext(); + } + else { + for (int i = 0; i < index; i++) { + temp = temp.getNext(); + } + return temp.getCard(); } - } - - return curr.getCard(); } - - public int numSets(){ - TableNode curr = head; - TableNode curr2 = curr.getNext(); - TableNode curr3 = curr2.getNext(); - int setCount = 0; - if (curr.getCard().isSet(curr2.getCard(), curr3.getCard()) == true){ - setCount +=1; - } - return setCount; - } + public int numSets() { + int count = 0; + TableNode n1 = head; + + while (n1 != null && n1.getNext().getNext() != null) { + TableNode n2 = n1.getNext(); + + while (n2 != null && n2.getNext() != null) { + TableNode n3 = n2.getNext(); + + while (n3 != null) { + if (n3.getCard().isSet(n2.getCard(), n1.getCard()) == true) { + count += 1; + } + n3 = n3.getNext(); + } + n2 = n2.getNext(); + } + n1 = n1.getNext(); + + } + return count; + } } - \ No newline at end of file + // d \ No newline at end of file