diff --git a/src/12cards14setsMilestone.dat b/src/12cards14setsMilestone.dat new file mode 100644 index 0000000..eb8d6c1 --- /dev/null +++ b/src/12cards14setsMilestone.dat @@ -0,0 +1,15 @@ +1 1 1 1 +1 1 1 2 +1 1 1 3 +1 1 2 1 +1 1 2 2 +1 1 2 3 +1 1 3 1 +1 1 3 2 +1 1 3 3 +1 2 1 1 +1 3 1 1 +1 2 1 2 +1 1 1 1 +2 2 2 2 +2 3 2 1 diff --git a/src/12cards4setsMilestone.dat b/src/12cards4setsMilestone.dat new file mode 100644 index 0000000..ff9563b --- /dev/null +++ b/src/12cards4setsMilestone.dat @@ -0,0 +1,12 @@ +1 1 1 1 +1 1 1 2 +1 1 1 3 +1 2 1 1 +1 2 1 2 +1 2 1 3 +2 1 1 1 +2 1 1 2 +2 1 1 3 +2 2 1 1 +2 2 1 2 +2 2 1 3 diff --git a/src/14cardsMilestone.dat b/src/14cardsMilestone.dat new file mode 100644 index 0000000..af0f4fa --- /dev/null +++ b/src/14cardsMilestone.dat @@ -0,0 +1,14 @@ +1 1 1 1 +1 1 1 2 +1 1 1 3 +2 1 1 1 +2 1 1 2 +2 1 1 3 +3 1 1 1 +3 1 1 2 +3 1 1 3 +1 2 1 1 +1 2 1 2 +1 2 1 3 +2 2 2 2 +3 3 3 3 diff --git a/src/14in12Milestone.dat b/src/14in12Milestone.dat new file mode 100644 index 0000000..8126933 --- /dev/null +++ b/src/14in12Milestone.dat @@ -0,0 +1,13 @@ +# this collection has 14 sets +1 1 1 1 +1 1 1 2 +1 1 1 3 +2 2 2 1 +2 2 2 2 +2 2 2 3 +3 3 3 1 +3 3 3 2 +3 3 3 3 +1 2 2 2 +1 3 3 3 +1 2 2 1 diff --git a/src/15cards0setsin12Milestone.dat b/src/15cards0setsin12Milestone.dat new file mode 100644 index 0000000..d0f889d --- /dev/null +++ b/src/15cards0setsin12Milestone.dat @@ -0,0 +1,17 @@ +# The first 12 cards have 0 sets +1 3 3 2 +3 3 2 2 +1 1 2 1 +1 2 3 3 +3 2 3 3 +1 3 1 3 +3 1 2 2 +2 2 3 2 +1 1 1 1 +3 2 1 1 +2 1 1 3 +2 2 1 3 +# These cards add some sets. +2 2 2 2 +3 3 3 3 +1 2 1 2 diff --git a/src/15cards19setsMilestone.dat b/src/15cards19setsMilestone.dat new file mode 100644 index 0000000..eb8d6c1 --- /dev/null +++ b/src/15cards19setsMilestone.dat @@ -0,0 +1,15 @@ +1 1 1 1 +1 1 1 2 +1 1 1 3 +1 1 2 1 +1 1 2 2 +1 1 2 3 +1 1 3 1 +1 1 3 2 +1 1 3 3 +1 2 1 1 +1 3 1 1 +1 2 1 2 +1 1 1 1 +2 2 2 2 +2 3 2 1 diff --git a/src/15cardsMilestone.dat b/src/15cardsMilestone.dat new file mode 100644 index 0000000..d9ba327 --- /dev/null +++ b/src/15cardsMilestone.dat @@ -0,0 +1,18 @@ +# a deck where the first 12 cards contain a set (3 actually) +# 3 purple solid diamond, 2 green open diamond, 1 red striped diamond +3 1 3 2 +3 3 3 2 +2 3 3 2 +2 1 1 2 +2 3 2 1 +2 1 2 1 +2 1 2 3 +1 2 2 2 +2 3 2 2 +1 2 2 3 +3 3 2 1 +3 1 2 1 +# extra cards for when the set is removed +1 3 2 1 +1 1 2 1 +1 1 3 1 \ No newline at end of file diff --git a/src/18cards0setsin15Milestone.dat b/src/18cards0setsin15Milestone.dat new file mode 100644 index 0000000..089767f --- /dev/null +++ b/src/18cards0setsin15Milestone.dat @@ -0,0 +1,18 @@ +1 1 1 1 +1 1 1 2 +1 1 2 1 +1 1 2 2 +1 2 1 1 +1 2 1 2 +1 2 2 1 +1 2 2 2 +2 1 1 1 +2 1 1 2 +2 1 2 1 +2 1 2 2 +2 2 1 1 +2 2 1 2 +2 2 2 1 +2 2 2 2 +3 3 3 3 +3 1 1 1 diff --git a/src/3cards.txt b/src/3cards.txt new file mode 100644 index 0000000..7910d18 --- /dev/null +++ b/src/3cards.txt @@ -0,0 +1,3 @@ +1 1 1 1 +2 2 2 2 +3 3 3 3 \ No newline at end of file diff --git a/src/7cardsMilestone.dat b/src/7cardsMilestone.dat new file mode 100644 index 0000000..5c0121b --- /dev/null +++ b/src/7cardsMilestone.dat @@ -0,0 +1,7 @@ +1 1 1 1 +2 2 2 2 +3 3 3 3 +1 2 1 2 +2 1 2 1 +3 3 1 1 +1 2 3 1 diff --git a/src/Card.class b/src/Card.class new file mode 100644 index 0000000..050623d Binary files /dev/null and b/src/Card.class differ diff --git a/src/Card.java b/src/Card.java old mode 100755 new mode 100644 index 9eed9a5..7527f03 --- a/src/Card.java +++ b/src/Card.java @@ -1,7 +1,47 @@ +import junit.framework.TestCase; + public class Card { - // Create the rest of this class yourself + private int[] c = new int[4]; + private int quantity; + private int color; + private int shading; + private int shape; + + public Card(int cardQuan, int cardColor, int cardShading, int cardShape) { + + quantity = cardQuan; + color = cardColor; + shading = cardShading; + shape = cardShape; + + c[0] = quantity; + c[1] = color; + c[2] = shading; + c[3] = shape; + + for (int i = 0; i < 4; i++) { + if (c[i] >= 1 && c[i] <= 3) { + c[i] = c[i]; + } + else { + if (c[i] < 0) { + c[i] = (((c[i] % 3) + 3) % 3) + 1; + } + else { + c[i] = c[i] % 3 + 1; + } + } + } + + quantity = c[0]; + color = c[1]; + shading = c[2]; + shape = c[3]; + + } public boolean equals(Object obj) { + Card that = (Card)obj; return quantity == that.getQuantity() && @@ -9,4 +49,79 @@ public boolean equals(Object obj) { shading == that.getShading() && shape == that.getShape(); } + public boolean isSet(Card c2, Card c3) { + + int quanSum = quantity + c2.getQuantity() + c3.getQuantity(); + int colorSum = color + c2.getColor() + c3.getColor(); + int shadingSum = shading + c2.getShading() + c3.getShading(); + int shapeSum = shape + c2.getShape() + c3.getShape(); + + if ((quanSum % 3 == 0) && (colorSum % 3 == 0) && (shadingSum % 3 == 0) && (shapeSum % 3 == 0)) { + return true; + } + else { + return false; + } + } + + public String toString() { + String strQuantity = ""; + String strColor = ""; + String strShad = ""; + String strShape = ""; + + if (quantity == 1) { + strQuantity += "1"; + } + else if (quantity == 2) { + strQuantity += "2"; + } + else if (quantity == 3) { + strQuantity += "3"; + } + if (color == 1) { + strColor += "R"; + } + else if (color == 2) { + strColor += "G"; + } + else if(color == 3) { + strColor += "P"; + } + if (shading == 1) { + strShad += "O"; + } + else if (shading == 2) { + strShad += "T"; + } + else if (shading == 3) { + strShad += "S"; + } + if (shape == 1) { + strShape += "O"; + } + else if (shape == 2) { + strShape += "D"; + } + else if (shape == 3) { + strShape += "S"; + } + return strQuantity + strColor + strShad + strShape; + } + + public int getQuantity() { + return quantity; + } + + public int getColor() { + return color; + } + + public int getShading() { + return shading; + } + + public int getShape() { + return shape; + } } diff --git a/src/Card.java~ b/src/Card.java~ new file mode 100644 index 0000000..2cbf664 --- /dev/null +++ b/src/Card.java~ @@ -0,0 +1,139 @@ +import junit.framework.TestCase; + +public class Card { + + private int[] c = new int[4]; + private int quantity; + private int color; + private int shading; + private int shape; + + public Card(int cardQuan, int cardColor, int cardShading, int cardShape) { + + quantity = cardQuan; + color = cardColor; + shading = cardShading; + shape = cardShape; + + c[0] = quantity; + c[1] = color; + c[2] = shading; + c[3] = shape; + + for (int i = 0; i < 4; i++) { + if (c[i] >= 1 && c[i] <= 3) { + c[i] = c[i]; + } + else { + if (c[i] < 0) { + c[i] = (((c[i] % 3) + 3) % 3) + 1; + } + else { + c[i] = c[i] % 3 + 1; + } + } + } + + quantity = c[0]; + color = c[1]; + shading = c[2]; + shape = c[3]; + + } + 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 c2, Card c3) { + + int quanSum = quantity + c2.getQuantity() + c3.getQuantity(); + int colorSum = color + c2.getColor() + c3.getColor(); + int shadingSum = shading + c2.getShading() + c3.getShading(); + int shapeSum = shape + c2.getShape() + c3.getShape(); + + if ((quanSum % 3 == 0) && (colorSum % 3 != 0) && (shadingSum % 3 != 0) && (shapeSum % 3 != 0)) { + return true; + } + else if ((quanSum % 3 != 0) && (colorSum % 3 == 0) && (shadingSum % 3 != 0) && (shapeSum % 3 != 0)) { + return true; + } + else if ((quanSum % 3 != 0) && (colorSum % 3 != 0) && (shadingSum % 3 == 0) && (shapeSum % 3 != 0)) { + return true; + } + else if ((quanSum % 3 != 0) && (colorSum % 3 != 0) && (shadingSum % 3 != 0) && (shapeSum % 3 == 0)) { + return true; + } + else if ((quanSum % 3 == 0) && (colorSum % 3 == 0) && (shadingSum % 3 == 0) && (shapeSum % 3 == 0)) { + return true; + } + else { + return false; + } + } + + public String toString() { + String strQuantity = ""; + String strColor = ""; + String strShad = ""; + String strShape = ""; + + if (quantity == 1) { + strQuantity += "1"; + } + else if (quantity == 2) { + strQuantity += "2"; + } + else if (quantity == 3) { + strQuantity += "3"; + } + if (color == 1) { + strColor += "R"; + } + else if (color == 2) { + strColor += "G"; + } + else if(color == 3) { + strColor += "P"; + } + if (shading == 1) { + strShad += "O"; + } + else if (shading == 2) { + strShad += "T"; + } + else if (shading == 3) { + strShad += "S"; + } + if (shape == 1) { + strShape += "O"; + } + else if (shape == 2) { + strShape += "D"; + } + else if (shape == 3) { + strShape += "S"; + } + return strQuantity + strColor + strShad + strShape; + } + + public int getQuantity() { + return quantity; + } + + public int getColor() { + return color; + } + + public int getShading() { + return shading; + } + + public int getShape() { + return shape; + } +} diff --git a/src/CardTest.class b/src/CardTest.class new file mode 100644 index 0000000..2f62acd Binary files /dev/null and b/src/CardTest.class differ diff --git a/src/CardTest.java b/src/CardTest.java new file mode 100644 index 0000000..c2c2dba --- /dev/null +++ b/src/CardTest.java @@ -0,0 +1,128 @@ +import junit.framework.TestCase; +public class CardTest extends TestCase { + public void testGetters() { + Card c1 = new Card(6, 5, 4, -3); + + assertEquals(1, c1.getQuantity()); + assertEquals(3, c1.getColor()); + assertEquals(2, c1.getShading()); + assertEquals(1, c1.getShape()); + } + + public void testEquals() { + Card c1 = new Card(1, 1, 1, 1); + Card c2 = new Card(1, 1, 1, 1); + + assertEquals(c1, c2); + } + + public void testSameQuantities() { + Card c1 = new Card(15, 32, 105, 21); + Card c2 = new Card(30, 16, 27, 31); + Card c3 = new Card(45, 20, 23, 34); + + assertEquals(true, c1.isSet(c2, c3)); + } + + public void testSameColors() { + Card c1 = new Card(1, 3, 1, 2); + Card c2 = new Card(2, 3, 2, 3); + Card c3 = new Card(2, 3, 2, 3); + + assertEquals(true, c1.isSet(c2, c3)); + } + + public void testSameShading() { + Card c1 = new Card(1, 3, 1, 2); + Card c2 = new Card(2, 2, 1, 3); + Card c3 = new Card(2, 2, 1, 3); + + assertEquals(true, c1.isSet(c2, c3)); + } + + public void testSameShape() { + Card c1 = new Card(1, 3, 1, 2); + Card c2 = new Card(2, 2, 3, 2); + Card c3 = new Card(2, 2, 3, 2); + + assertEquals(true, c1.isSet(c2, c3)); + } + + public void testAllDiffQuantities() { + Card c1 = new Card(1, 3, 1, 2); + Card c2 = new Card(2, 2, 2, 3); + Card c3 = new Card(3, 2, 2, 3); + + assertEquals(true, c1.isSet(c2, c3)); + } + + public void testAllDiffColors() { + Card c1 = new Card(1, 3, 1, 2); + Card c2 = new Card(2, 2, 2, 3); + Card c3 = new Card(2, 1, 2, 3); + + assertEquals(true, c1.isSet(c2, c3)); + } + + public void testAllDiffShading() { + Card c1 = new Card(1, 3, 1, 2); + Card c2 = new Card(2, 2, 2, 3); + Card c3 = new Card(2, 2, 3, 3); + + assertEquals(true, c1.isSet(c2, c3)); + } + + public void testAllDiffShape() { + Card c1 = new Card(1, 3, 1, 2); + Card c2 = new Card(2, 2, 3, 1); + Card c3 = new Card(2, 2, 3, 3); + + assertEquals(true, c1.isSet(c2, c3)); + } + + public void testNotASet() { + Card c1 = new Card(15, 3, 1, 2); + Card c2 = new Card(15, 2, 3, 2); + Card c3 = new Card(23, 2, 3, 3); + + assertEquals(false, c1.isSet(c2, c3)); + } + + public void testTwoSameAtts() { + Card c1 = new Card(2, 3, 1, 2); + Card c2 = new Card(2, 3, 3, 2); + Card c3 = new Card(2, 3, 3, 3); + + assertEquals(false, c1.isSet(c2, c3)); + } + + public void testThreeSameAtts() { + Card c1 = new Card(12, 90, 21, 25); + Card c2 = new Card(24, 60, 15, 22); + Card c3 = new Card(30, 30, 18, 31); + + assertEquals(false, c1.isSet(c2, c3)); + } + + public void testAnotherNonSet() { + Card c1 = new Card(6, 1, 1, 2); + Card c2 = new Card(9, 2, 3, 2); + Card c3 = new Card(22, 2, 3, 3); + + assertEquals(false, c1.isSet(c2, c3)); + } + + public void testToString() { + Card c1 = new Card(6, 1, 1, 2); + + assertEquals("1ROD", c1.toString()); + } + + public void testAnotherToString() { + Card c1 = new Card(18, 14, 90, 26); + + assertEquals("1POS", c1.toString()); + } +} + + \ No newline at end of file diff --git a/src/CardTest.java~ b/src/CardTest.java~ new file mode 100644 index 0000000..3c60828 --- /dev/null +++ b/src/CardTest.java~ @@ -0,0 +1,9 @@ +import junit.framework.TestCase; +public class CardTest extends TestCase { + public void testGetQuantity() { + Card card1 = new Card(6, 2, 5, 4); + + assertEquals(1, card1.getQuantity()); + } +} + \ No newline at end of file diff --git a/src/Deck.class b/src/Deck.class new file mode 100644 index 0000000..39f5327 Binary files /dev/null and b/src/Deck.class differ diff --git a/src/Deck.java b/src/Deck.java old mode 100755 new mode 100644 index ab3a2a3..f031c2d --- a/src/Deck.java +++ b/src/Deck.java @@ -2,11 +2,37 @@ 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 + //Instance variables must be cards and nextCardIndex for the provided constructor to work. + ArrayList cards; + int nextCardIndex; + public Deck() { + //Creates a deck with all standard 81 set cards, shuffled. + //Usage (in test code, and in later code): + //Deck d = new Deck(); + nextCardIndex = 0; + cards = new ArrayList(81); + for (int var1 = 1; var1 <= 3; var1++) { + for (int var2 = 1; var2 <= 3; var2++) { + for (int var3 = 1; var3 <= 3; var3++) { + for (int var4 = 1; var4 <= 3; var4++) { + cards.add(new Card(var1, var2, var3, var4)); + } + } + } + } + Collections.shuffle(cards); + } + public Deck(String filename) { + //Creates a pre-defined deck from a file named filename, + //and does not shuffle it. + //Usage (in test code, and in later code): + // Deck d = new Deck("example.dat"); cards = new ArrayList(81); try { @@ -38,8 +64,29 @@ public Deck(String filename) { nextCardIndex = 0; } } - catch(Exception e) { + catch(Exception e) { throw new RuntimeException("Error while reading file: " + e.toString()); } } -} + public boolean hasNext() { + //returns true if any cards are left, false otherwise + if (nextCardIndex == cards.size()) { + return false; + } + else { + return true; + } + } + + public Card getNext() { + //returns the next card in the deck + if (hasNext()) { + nextCardIndex++; + return cards.get(nextCardIndex-1); + } + else { + return null; + + } + } +} \ No newline at end of file diff --git a/src/Deck.java~ b/src/Deck.java~ new file mode 100644 index 0000000..87375da --- /dev/null +++ b/src/Deck.java~ @@ -0,0 +1,93 @@ +import java.io.BufferedReader; +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 + //Instance variables must be cards and nextCardIndex for the provided constructor to work. + ArrayList cards; + int nextCardIndex; + + public Deck() { + //Creates a deck with all standard 81 set cards, shuffled. + //Usage (in test code, and in later code): + //Deck d = new Deck(); + nextCardIndex = 0; + cards = new ArrayList(81); + for (int var1 = 1; var1 <= 3; var1++) { + for (int var2 = 1; var2 <= 3; var2++) { + for (int var3 = 1; var3 <= 3; var3++) { + for (int var4 = 1; var4 <= 3; var4++) { + cards.add(new Card(var1, var2, var3, var4)); + } + } + } + } + Collections.shuffle(cards); + + } + + public Deck(String filename) { + //Creates a pre-defined deck from a file named filename, + //and does not shuffle it. + //Usage (in test code, and in later code): + // Deck d = new Deck("example.dat"); + 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 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()); + } + } + public boolean hasNext() { + //returns true if any cards are left, false otherwise + if (nextCardIndex == cards.size()) { + return false; + } + else { + return true; + } + } + + public Card getNext() { + //returns the next card in the deck + if (hasNext()) { + nextCardIndex++; + return cards.get(nextCardIndex-1); + } + else { + return null; + + } + } +} \ No newline at end of file diff --git a/src/DeckTest.class b/src/DeckTest.class new file mode 100644 index 0000000..aa04947 Binary files /dev/null and b/src/DeckTest.class differ diff --git a/src/DeckTest.java b/src/DeckTest.java new file mode 100644 index 0000000..55ca62e --- /dev/null +++ b/src/DeckTest.java @@ -0,0 +1,28 @@ +import junit.framework.TestCase; + +public class DeckTest extends TestCase { + public void testDeck() { + Deck d = new Deck(); + + assertEquals(81, d.cards.size()); + } + + public void testHasNext() { + Deck d = new Deck(); + + assertEquals(true, d.hasNext()); + } + + public void testOtherConstructor() { + Deck d = new Deck("3cards.txt"); + + assertEquals("1ROO", d.getNext().toString()); + assertEquals(true, d.hasNext()); + + assertEquals("2GTD", d.getNext().toString()); + assertEquals(true, d.hasNext()); + + assertEquals("3PSS", d.getNext().toString()); + assertEquals(false, d.hasNext()); + } +} \ No newline at end of file diff --git a/src/DeckTest.java~ b/src/DeckTest.java~ new file mode 100644 index 0000000..f1b01e2 --- /dev/null +++ b/src/DeckTest.java~ @@ -0,0 +1,24 @@ +import junit.framework.TestCase; +public class DeckTest extends TestCase { + public void testHasNext() { + Deck d = new Deck(); + + assertEquals(false, d.hasNext()); + } + + public void testGetNext() { + Deck d = new Deck(); + + assertEquals(null, d.getNext()); + } + + public void testOtherConstructor() { + Deck d = new Deck("4cards.rtf"); + + assertEquals("1 1 1 1", d.getNext()); + } + + + + +} \ No newline at end of file diff --git a/src/Game.class b/src/Game.class new file mode 100644 index 0000000..8e3ddb0 Binary files /dev/null and b/src/Game.class differ diff --git a/src/Game.java b/src/Game.java new file mode 100644 index 0000000..32d9adc --- /dev/null +++ b/src/Game.java @@ -0,0 +1,87 @@ +public class Game { + Table t; + Deck d; + + public Game() { + d = new Deck(); + t = new Table(); + for (int i = 0; i < 12; i++) { + t.add(d.getNext()); + } + } + + public Game(String filename) { + d = new Deck(filename); + t = new Table(); + while(d.hasNext() && t.numCards() != 12) { + t.add(d.getNext()); + } + } + + public int numSets() { + return t.numSets(); + } + + public int numCards() { + return t.numCards(); + } + + public void playRound() { + boolean setFound = false; + + for (int i = 0; i < t.numCards() - 2; i++) { + if (setFound) { + break; + } + for (int j = i + 1; j < t.numCards() - 1; j++) { + if (setFound) { + break; + } + for (int k = j + 1; k < t.numCards(); k ++) { + if (setFound) { + break; + } + + if (t.getCard(i).isSet(t.getCard(j), t.getCard(k))){ + t.removeSet(t.getCard(i), t.getCard(j), t.getCard(k)); + setFound = true; + if(t.numCards() <= 9) { + if (d.hasNext()) { + t.add(d.getNext()); + } + if (d.hasNext()) { + t.add(d.getNext()); + } + if (d.hasNext()) { + t.add(d.getNext()); + } + } + } + } + } + } + if (t.numSets() == 0) { + if (d.hasNext()) { + t.add(d.getNext()); + } + if (d.hasNext()) { + t.add(d.getNext()); + } + if (d.hasNext()) { + t.add(d.getNext()); + } + } + } + + + + public boolean isGameOver() { + if (t.numSets() == 0 && d.hasNext() == false) { + return true; + } + + else { + return false; + } + } +} \ No newline at end of file diff --git a/src/Game.java~ b/src/Game.java~ new file mode 100644 index 0000000..8dff4e2 --- /dev/null +++ b/src/Game.java~ @@ -0,0 +1,67 @@ +public class Game { + Table t = new Table(); + Deck d = new Deck(); + + public Game() { + for (int i = 0; i < 12; i++) { + t.add(d.getNext()); + } + } + + public Game(String filename) { + d = new Deck(filename); + while(d.hasNext() && t.numCards() != 12) { + t.add(d.getNext()); + } + } + + public int numSets() { + return t.numSets(); + } + + public int numCards() { + return t.numCards(); + } + + public void playRound() { + + + + for (int i = 0; i < t.numCards(); i++) { + + for (int j = i + 1; j < t.numCards(); j++) { + + for (int k = j + 1; k < t.numCards(); k ++) { + while (t.numSets() = 1 ){ + + if (t.getCard(i).isSet(t.getCard(j), t.getCard(k))) { + + t.removeSet(t.getCard(i), t.getCard(j), t.getCard(k)); + if(t.numCards() <= 9) { + t.add(d.getNext()); + t.add(d.getNext()); + t.add(d.getNext()); + } + } + } + } + } + } + + if (t.numSets() == 0) { + t.add(d.getNext()); + t.add(d.getNext()); + t.add(d.getNext()); + } + } + + public boolean isGameOver() { + if (t.numSets() == 0 && d.hasNext() == false) { + return true; + } + + else { + return false; + } + } +} \ No newline at end of file diff --git a/src/GameTest.class b/src/GameTest.class new file mode 100644 index 0000000..62be544 Binary files /dev/null and b/src/GameTest.class differ diff --git a/src/GameTest.java b/src/GameTest.java new file mode 100644 index 0000000..05e117c --- /dev/null +++ b/src/GameTest.java @@ -0,0 +1,29 @@ +import junit.framework.TestCase; +public class GameTest extends TestCase { + public void testConstructorRuns() { + Game g = new Game(); + assertEquals(12, g.numCards()); + } + + public void testOtherConstructor() { + Game g = new Game("15cards19setsMilestone.dat"); + + assertEquals(12, g.numCards()); + assertEquals(14, g.numSets()); + } + + public void testPlayRound() { + Game g = new Game("15cards19setsMilestone.dat"); + g.playRound(); + + assertEquals(12, g.numCards()); + } + + public void testIsGameOver() { + Game g = new Game("empty game.txt"); + + assertEquals(true, g.isGameOver()); + } +} + + \ No newline at end of file diff --git a/src/GameTest.java~ b/src/GameTest.java~ new file mode 100644 index 0000000..df000a2 --- /dev/null +++ b/src/GameTest.java~ @@ -0,0 +1,29 @@ +import junit.framework.TestCase; +public class GameTest extends TestCase { + public void testConstructorRuns() { + Game g = new Game(); + assertEquals(12, g.numCards()); + } + + public void testOtherConstructor() { + Game g = new Game("15cards19setsMilestone.dat"); + + assertEquals(12, g.numCards()); + assertEquals(14, g.numSets()); + } + + public void testPlayRound() { + Game g = new Game("15cards19setsMilestone.dat"); + g.playRound(); + + assertEquals(1, g.numSets()); + } + + public void testIsGameOver() { + Game g = new Game("empty game.txt"); + + assertEquals(true, g.isGameOver()); + } +} + + \ No newline at end of file diff --git a/src/GameTestMilestone.class b/src/GameTestMilestone.class new file mode 100644 index 0000000..41c835b Binary files /dev/null and b/src/GameTestMilestone.class differ diff --git a/src/GameTestMilestone.java b/src/GameTestMilestone.java new file mode 100644 index 0000000..959de1b --- /dev/null +++ b/src/GameTestMilestone.java @@ -0,0 +1,163 @@ +import junit.framework.TestCase; + +public class GameTestMilestone extends TestCase +{ + public void testNewGame() + { + // When a game is created from a random deck, + // there are 12 cards on the table and the game + // is not over. Can't say much more... + Game g = new Game(); + + assertEquals(12, g.numCards()); + assertFalse(g.isGameOver()); + } + + public void testNoSetsFirstTwelve() + { + Game g = new Game("15cards0setsin12Milestone.dat"); + + assertEquals(0, g.numSets()); + assertEquals(12, g.numCards()); + + g.playRound(); + + assertEquals(15, g.numCards()); + } + + public void testNoSetsFirstFifteen() + { + Game g = new Game("18cards0setsin15Milestone.dat"); + + assertEquals(0, g.numSets()); + assertEquals(12, g.numCards()); + + g.playRound(); + + assertEquals(0, g.numSets()); + assertEquals(15, g.numCards()); + + g.playRound(); + + assertEquals(9, g.numSets()); + assertEquals(18, g.numCards()); + } + + public void testBackToTwelveAfterFifteen() + { + Game g = new Game("15cards0setsin12Milestone.dat"); + // this takes us to 15 + g.playRound(); + + assertEquals(15, g.numCards()); + + // When we play another round, we wont' add more cards. + g.playRound(); + assertEquals(12, g.numCards()); + } + + public void testBackToFifteenAfterEighteen() + { + Game g = new Game("18cards0setsin15Milestone.dat"); + g.playRound(); + g.playRound(); + + // now we have 18 cards on the table. + assertEquals(18, g.numCards()); + + // When we play another round, we won't add more cards. + g.playRound(); + assertEquals(15, g.numCards()); + } + + public void testDeckNotMultipleOfThree() + { + Game g = new Game("14cardsMilestone.dat"); + assertEquals(12, g.numCards()); + // when we play a round, there is a set, but there are + // only 2 more cards available. + g.playRound(); + + assertEquals(11, g.numCards()); + } + + public void testDeckLessThanTwelve() + { + Game g = new Game("7cardsMilestone.dat"); + + assertEquals(7, g.numCards()); + g.playRound(); + + assertEquals(4, g.numCards()); + } + + public void testGameOverEmptyTable() + { + Game g = new Game("12cards4setsMilestone.dat"); + + assertEquals(12, g.numCards()); + assertEquals(4, g.numSets()); + + g.playRound(); + assertEquals(9, g.numCards()); + assertEquals(3, g.numSets()); + + g.playRound(); + assertEquals(6, g.numCards()); + assertEquals(2, g.numSets()); + + g.playRound(); + assertEquals(3, g.numCards()); + assertEquals(1, g.numSets()); + + g.playRound(); + assertEquals(0, g.numCards()); + assertTrue(g.isGameOver()); + } + + public void test14Sets() + { + Game g = new Game("14in12Milestone.dat"); + + assertEquals(12, g.numCards()); + assertEquals(14, g.numSets()); + assertFalse(g.isGameOver()); + + g.playRound(); + assertEquals(9, g.numCards()); + assertFalse(g.isGameOver()); + + g.playRound(); + assertEquals(6, g.numCards()); + assertFalse(g.isGameOver()); + + g.playRound(); + assertEquals(3, g.numCards()); + assertTrue(g.isGameOver()); + } + + public void testRemoveAdd() + { + Game g = new Game("15cardsMilestone.dat"); + + assertEquals(12, g.numCards()); + assertEquals(3, g.numSets()); + assertFalse(g.isGameOver()); + + g.playRound(); + + assertEquals(12, g.numCards()); + assertEquals(3, g.numSets()); + assertFalse(g.isGameOver()); + + g.playRound(); + assertEquals(9, g.numCards()); + assertEquals(1, g.numSets()); + assertFalse(g.isGameOver()); + + g.playRound(); + assertEquals(6, g.numCards()); + assertEquals(0, g.numSets()); + assertTrue(g.isGameOver()); + } +} diff --git a/src/GameTestMilestone.java~ b/src/GameTestMilestone.java~ new file mode 100644 index 0000000..959de1b --- /dev/null +++ b/src/GameTestMilestone.java~ @@ -0,0 +1,163 @@ +import junit.framework.TestCase; + +public class GameTestMilestone extends TestCase +{ + public void testNewGame() + { + // When a game is created from a random deck, + // there are 12 cards on the table and the game + // is not over. Can't say much more... + Game g = new Game(); + + assertEquals(12, g.numCards()); + assertFalse(g.isGameOver()); + } + + public void testNoSetsFirstTwelve() + { + Game g = new Game("15cards0setsin12Milestone.dat"); + + assertEquals(0, g.numSets()); + assertEquals(12, g.numCards()); + + g.playRound(); + + assertEquals(15, g.numCards()); + } + + public void testNoSetsFirstFifteen() + { + Game g = new Game("18cards0setsin15Milestone.dat"); + + assertEquals(0, g.numSets()); + assertEquals(12, g.numCards()); + + g.playRound(); + + assertEquals(0, g.numSets()); + assertEquals(15, g.numCards()); + + g.playRound(); + + assertEquals(9, g.numSets()); + assertEquals(18, g.numCards()); + } + + public void testBackToTwelveAfterFifteen() + { + Game g = new Game("15cards0setsin12Milestone.dat"); + // this takes us to 15 + g.playRound(); + + assertEquals(15, g.numCards()); + + // When we play another round, we wont' add more cards. + g.playRound(); + assertEquals(12, g.numCards()); + } + + public void testBackToFifteenAfterEighteen() + { + Game g = new Game("18cards0setsin15Milestone.dat"); + g.playRound(); + g.playRound(); + + // now we have 18 cards on the table. + assertEquals(18, g.numCards()); + + // When we play another round, we won't add more cards. + g.playRound(); + assertEquals(15, g.numCards()); + } + + public void testDeckNotMultipleOfThree() + { + Game g = new Game("14cardsMilestone.dat"); + assertEquals(12, g.numCards()); + // when we play a round, there is a set, but there are + // only 2 more cards available. + g.playRound(); + + assertEquals(11, g.numCards()); + } + + public void testDeckLessThanTwelve() + { + Game g = new Game("7cardsMilestone.dat"); + + assertEquals(7, g.numCards()); + g.playRound(); + + assertEquals(4, g.numCards()); + } + + public void testGameOverEmptyTable() + { + Game g = new Game("12cards4setsMilestone.dat"); + + assertEquals(12, g.numCards()); + assertEquals(4, g.numSets()); + + g.playRound(); + assertEquals(9, g.numCards()); + assertEquals(3, g.numSets()); + + g.playRound(); + assertEquals(6, g.numCards()); + assertEquals(2, g.numSets()); + + g.playRound(); + assertEquals(3, g.numCards()); + assertEquals(1, g.numSets()); + + g.playRound(); + assertEquals(0, g.numCards()); + assertTrue(g.isGameOver()); + } + + public void test14Sets() + { + Game g = new Game("14in12Milestone.dat"); + + assertEquals(12, g.numCards()); + assertEquals(14, g.numSets()); + assertFalse(g.isGameOver()); + + g.playRound(); + assertEquals(9, g.numCards()); + assertFalse(g.isGameOver()); + + g.playRound(); + assertEquals(6, g.numCards()); + assertFalse(g.isGameOver()); + + g.playRound(); + assertEquals(3, g.numCards()); + assertTrue(g.isGameOver()); + } + + public void testRemoveAdd() + { + Game g = new Game("15cardsMilestone.dat"); + + assertEquals(12, g.numCards()); + assertEquals(3, g.numSets()); + assertFalse(g.isGameOver()); + + g.playRound(); + + assertEquals(12, g.numCards()); + assertEquals(3, g.numSets()); + assertFalse(g.isGameOver()); + + g.playRound(); + assertEquals(9, g.numCards()); + assertEquals(1, g.numSets()); + assertFalse(g.isGameOver()); + + g.playRound(); + assertEquals(6, g.numCards()); + assertEquals(0, g.numSets()); + assertTrue(g.isGameOver()); + } +} diff --git a/src/SetExperiment.java b/src/SetExperiment.java new file mode 100644 index 0000000..0bc1bb5 --- /dev/null +++ b/src/SetExperiment.java @@ -0,0 +1,31 @@ +public class SetExperiment { + public static void main(String[] args) { + double cardSum = 0; + double setSum = 0; + + for(int i = 0; i < 1000; i++) { + Game g = new Game(); + + while(g.isGameOver() == false) { + g.playRound(); + } + + setSum += g.numCards(); + } + System.out.println("Average number of cards at the end: " + setSum / 1000); + + for(int j = 0; j < 1000; j++) { + + Table t = new Table(); + Deck d = new Deck(); + + for(int k = 0; k < 12; k++) { + t.add(d.getNext()); + } + + cardSum += t.numSets(); + } +System.out.println("Average number of sets in 12 random cards: " + cardSum / 1000); + } +} + \ No newline at end of file diff --git a/src/Table.class b/src/Table.class new file mode 100644 index 0000000..08da8f2 Binary files /dev/null and b/src/Table.class differ diff --git a/src/Table.java b/src/Table.java new file mode 100644 index 0000000..93565f3 --- /dev/null +++ b/src/Table.java @@ -0,0 +1,108 @@ +public class Table { + TableNode head; + + public Table() { + head = null; + } + + public void add(Card newCard) { + TableNode temp = new TableNode(newCard); + temp.setNext(head); + head = temp; + } + + public void removeSet(Card c1, Card c2, Card c3) { + if (! c1.isSet(c2, c3)) { + return; + } + + if (! onTable(c1)) { + return; + } + + if (! onTable(c2)) { + return; + } + + if (! onTable(c3)) { + return; + } + + removeCard(c1); + removeCard(c2); + removeCard(c3); + } + + public int numCards() { + TableNode curr = head; + int num = 0; + while (curr != null) { + num ++ ; + curr = curr.getNext(); + } + return num; + } + + public Card getCard(int cardIndex) { + TableNode curr = head; + int c = 0; + while (c != cardIndex) { + curr = curr.getNext(); + c++; + } + return curr.getCard(); + } + + public int numSets() { + int sumSets = 0; + for (int i = 0; i < numCards(); i++) { + for (int j = i + 1; j < numCards(); j++) { + for (int k = j + 1; k < numCards(); k ++) { + if (getCard(i).isSet(getCard(j), getCard(k))) { + sumSets++; + } + } + } + } + return sumSets; +} + + private boolean onTable(Card c) { + TableNode curr = head; + while (curr != null) { + if (curr.getCard().equals(c)) { + return true; + } + curr = curr.getNext(); + } + return false; + } + + private void removeCard(Card c) { + TableNode prev = findPrev(c); + if (prev == null) { + head = head.getNext(); + } + else { + TableNode curr = prev.getNext(); + prev.setNext(curr.getNext()); + } + } + + private TableNode findPrev(Card c) { + TableNode curr = head; + TableNode prev = null; + while (curr != null) { + if (curr.getCard().equals(c)) { + return prev; + } + else { + prev = curr; + curr = curr.getNext(); + } + } + return null; + } +} + + \ No newline at end of file diff --git a/src/Table.java~ b/src/Table.java~ new file mode 100644 index 0000000..285c47a --- /dev/null +++ b/src/Table.java~ @@ -0,0 +1,106 @@ +public class Table { + TableNode head; + + public Table() { + head = null; + } + + public void add(Card newCard) { + TableNode temp = new TableNode(newCard); + temp.setNext(head); + head = temp; + } + + public void removeSet(Card c1, Card c2, Card c3) { + if (! c1.isSet(c2, c3)) { + return; + } + + if (! onTable(c1)) { + return; + } + + if (! onTable(c2)) { + return; + } + + if (! onTable(c3)) { + return; + } + + removeCard(c1); + removeCard(c2); + removeCard(c3); + } + + public int numCards() { + TableNode curr = head; + int num = 0; + while (curr != null) { + num ++ ; + curr = curr.getNext(); + } + return num; + } + + public Card getCard(int cardIndex) { + TableNode curr = head; + int c = 0; + while (c != cardIndex) { + curr = curr.getNext(); + c++; + } + return curr.getCard(); + } + + public int numSets() { + int sumSets = 0; + for (int i = 0; i < numCards(); i++) { + for (int j = i + 1; j < numCards(); j++) { + for (int k = j + 1; k < numCards(); k ++) { + if (getCard(i).isSet(getCard(j), getCard(k))) { + sumSets++; + } + } + } + } + return sumSets; +} + + private boolean onTable(Card c) { + TableNode curr = head; + while (curr != null) { + if (curr.getCard() == c) { + return true; + } + curr = curr.getNext(); + } + return false; + } + + private void removeCard(Card c) { + TableNode prev = findPrev(c); + if (prev == null) { + head = head.getNext(); + } + else { + TableNode curr = prev.getNext(); + prev.setNext(curr.getNext()); + } + } + + private TableNode findPrev(Card c) { + TableNode curr = head; + while (curr != null) { + if (curr.getNext().getCard() == c) { + return curr; + } + else { + curr = curr.getNext(); + } + } + return null; + } +} + + \ No newline at end of file diff --git a/src/TableNode.class b/src/TableNode.class new file mode 100644 index 0000000..85db8f7 Binary files /dev/null and b/src/TableNode.class differ diff --git a/src/TableNode.java b/src/TableNode.java new file mode 100644 index 0000000..5def365 --- /dev/null +++ b/src/TableNode.java @@ -0,0 +1,22 @@ +public class TableNode { + Card card; + 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; + } +} + \ No newline at end of file diff --git a/src/TableNode.java~ b/src/TableNode.java~ new file mode 100644 index 0000000..d0b309c --- /dev/null +++ b/src/TableNode.java~ @@ -0,0 +1,22 @@ +public class TableNode { + Card card; + 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; + } +} + \ No newline at end of file diff --git a/src/TableNodeTest.class b/src/TableNodeTest.class new file mode 100644 index 0000000..3667b06 Binary files /dev/null and b/src/TableNodeTest.class differ diff --git a/src/TableNodeTest.java b/src/TableNodeTest.java new file mode 100644 index 0000000..b65f7d4 --- /dev/null +++ b/src/TableNodeTest.java @@ -0,0 +1,25 @@ +import junit.framework.TestCase; +public class TableNodeTest extends TestCase { + public void testGetters() { + Card c1 = new Card(1, 1, 1, 1); + Card c2 = new Card(1, 1, 2, 2); + Card c3 = new Card(1, 2, 3, 1); + + TableNode t1 = new TableNode(c1); + TableNode t2 = new TableNode(c2); + TableNode t3 = new TableNode(c3); + + t1.setNext(t2); + t2.setNext(t3); + + assertEquals("1ROO", t1.getCard().toString()); + assertEquals("1RTD", t2.getCard().toString()); + assertEquals("1GSO", t3.getCard().toString()); + + assertEquals("1RTD", t1.getNext().getCard().toString()); + assertEquals("1GSO", t2.getNext().getCard().toString()); + } +} + + + \ No newline at end of file diff --git a/src/TableNodeTest.java~ b/src/TableNodeTest.java~ new file mode 100644 index 0000000..4088e67 --- /dev/null +++ b/src/TableNodeTest.java~ @@ -0,0 +1,25 @@ +import junit.framework.TestCase; +public class TableNodeTest extends TestCase { + public void testMethods() { + Card c1 = new Card(1, 1, 1, 1); + Card c2 = new Card(1, 1, 2, 2); + Card c3 = new Card(1, 2, 3, 1); + + TableNode t1 = new TableNode(c1); + TableNode t2 = new TableNode(c2); + TableNode t3 = new TableNode(c3); + + t1.setNext(t2); + t2.setNext(t3); + + assertEquals("1ROO", t1.getCard().toString()); + assertEquals("1RTD", t2.getCard().toString()); + assertEquals("1GSO", t3.getCard().toString()); + + assertEquals("1RTD", t1.getNext().getCard().toString()); + assertEquals("1GSO", t2.getNext().getCard().toString()); + } +} + + + \ No newline at end of file diff --git a/src/TableTest.class b/src/TableTest.class new file mode 100644 index 0000000..9d65436 Binary files /dev/null and b/src/TableTest.class differ diff --git a/src/TableTest.java b/src/TableTest.java new file mode 100644 index 0000000..f66a3d2 --- /dev/null +++ b/src/TableTest.java @@ -0,0 +1,67 @@ +import junit.framework.TestCase; +public class TableTest extends TestCase { + + public void testNumCards() { + Card c1 = new Card(1, 1, 1, 1); + Card c2 = new Card(1, 1, 2, 2); + Card c3 = new Card(1, 2, 3, 1); + + Table t = new Table(); + + t.add(c1); + t.add(c2); + t.add(c3); + + assertEquals(3, t.numCards()); + } + + public void testGetCard() { + Card c1 = new Card(1, 1, 1, 1); + Card c2 = new Card(1, 1, 2, 2); + Card c3 = new Card(1, 2, 3, 1); + + Table t = new Table(); + + t.add(c1); + t.add(c2); + t.add(c3); + + assertEquals("1GSO", t.getCard(0).toString()); + assertEquals("1RTD", t.getCard(1).toString()); + assertEquals("1ROO", t.getCard(2).toString()); + } + + public void testRemoveSet() { + Card c1 = new Card(1, 1, 1, 1); + Card c2 = new Card(2, 2, 2, 2); + Card c3 = new Card(3, 3, 3, 3); + Card c4 = new Card(3, 2, 1, 1); + + Table t = new Table(); + + t.add(c1); + t.add(c2); + t.add(c3); + t.add(c4); + + t.removeSet(c1, c2, c3); + + assertEquals(1, t.numCards()); + } + + public void testNumSets() { + Card c1 = new Card(1, 1, 1, 1); + Card c2 = new Card(2, 2, 2, 2); + Card c3 = new Card(3, 3, 3, 3); + Card c4 = new Card(3, 2, 1, 1); + + Table t = new Table(); + + t.add(c1); + t.add(c2); + t.add(c3); + t.add(c4); + + assertEquals(3, t.numSets()); + } + } \ No newline at end of file diff --git a/src/TableTest.java~ b/src/TableTest.java~ new file mode 100644 index 0000000..4c76142 --- /dev/null +++ b/src/TableTest.java~ @@ -0,0 +1,31 @@ +import junit.framework.TestCase; +public class TableTest extends TestCase { + public void testNumCards() { + Card c1 = new Card(1, 1, 1, 1); + Card c2 = new Card(1, 1, 2, 2); + Card c3 = new Card(1, 2, 3, 1); + + Table t = new Table(); + + t.add(c1); + t.add(c2); + t.add(c3); + + assertEquals(3, t.numCards()); + + + } +public void testgetCard() { + Card c1 = new Card(1, 1, 1, 1); + Card c2 = new Card(1, 1, 2, 2); + Card c3 = new Card(1, 2, 3, 1); + + Table t = new Table(); + + t.add(c1); + t.add(c2); + t.add(c3); + + assertEquals("1ROO", t.getCard(2).toString()); + } +} \ No newline at end of file diff --git a/src/TableTestMilestone.class b/src/TableTestMilestone.class new file mode 100644 index 0000000..c531291 Binary files /dev/null and b/src/TableTestMilestone.class differ diff --git a/src/TableTestMilestone.java b/src/TableTestMilestone.java new file mode 100644 index 0000000..cfc0d99 --- /dev/null +++ b/src/TableTestMilestone.java @@ -0,0 +1,296 @@ +import junit.framework.TestCase; + +public class TableTestMilestone extends TestCase +{ + + public void testConstructorRuns() + { + Table t = new Table(); + } + + + public void testAddRuns() + { + Table t = new Table(); + + t.add(new Card(1, 2, 3, 1)); + } + + + public void testNumCardsNoCards() + { + Table t = new Table(); + + assertEquals(0, t.numCards()); + } + + + public void testNumCardsOneCard() + { + Table t = new Table(); + + t.add(new Card(2, 3, 1, 2)); + + assertEquals(1, t.numCards()); + } + + + public void testNumCardsTwoCards() + { + Table t = new Table(); + + t.add(new Card(3, 1, 2, 3)); + t.add(new Card(1, 2, 3, 1)); + + assertEquals(2, t.numCards()); + } + + + public void testGetCardOneCard() + { + Table t = new Table(); + + Card c = new Card(1, 1, 1, 1); + + t.add(c); + + assertEquals(c, t.getCard(0)); + } + + + public void testGetCardTwoCards() + { + Table t = new Table(); + + Card c1 = new Card(1, 1, 1, 1); + Card c2 = new Card(2, 2, 2, 2); + + t.add(c1); + t.add(c2); + + assertEquals(c2, t.getCard(0)); + assertEquals(c1, t.getCard(1)); + } + + + public void testGetCardThreeCards() + { + Table t = new Table(); + + Card c1 = new Card(1, 1, 1, 1); + Card c2 = new Card(2, 2, 2, 2); + Card c3 = new Card(3, 3, 3, 3); + + t.add(c1); + t.add(c2); + t.add(c3); + + assertEquals(c3, t.getCard(0)); + assertEquals(c2, t.getCard(1)); + assertEquals(c1, t.getCard(2)); + } + + + public void testNumSetsEmptyTable() + { + Table t = new Table(); + + assertEquals(0, t.numSets()); + } + + + public void testNumSets3Cards1Set() + { + Table t = makeTable("3cards1setMilestone.dat"); + + assertEquals(1, t.numSets()); + } + + + public void testNumSets3cards0Sets() + { + Table t = makeTable("3cards0setsMilestone.dat"); + + assertEquals(0, t.numSets()); + } + + + public void testNumSets12Cards14Sets() + { + Table t = makeTable("12cards14setsMilestone.dat"); + + assertEquals(14, t.numSets()); + } + + + public void testRemoveSetEmptyTable() + { + Table t = new Table(); + + // Removing a valid set from an empty table shouldn't + // do anything. + t.removeSet(new Card(1, 1, 1, 1), + new Card(2, 2, 2, 2), + new Card(3, 3, 3, 3)); + + assertEquals(0, t.numCards()); + assertEquals(0, t.numSets()); + } + + + public void testSmallTable() + { + Table t = makeTable("3cards0setsMilestone.dat"); + + Card c1 = new Card(1, 1, 1, 1); + Card c2 = new Card(1, 1, 1, 2); + Card c3 = new Card(2, 2, 2, 3); + + assertEquals(3, t.numCards()); + assertEquals(0, t.numSets()); + + // because we're doing head insertion + // the Cards are indexed in the reverse + // order from which they are added + assertEquals(c3, t.getCard(0)); + assertEquals(c2, t.getCard(1)); + assertEquals(c1, t.getCard(2)); + } + + + public void testSmallTableNoSets() + { + Table t = makeTable("3cards0setsMilestone.dat"); + + Card c1 = new Card(1, 1, 1, 1); + Card c2 = new Card(1, 1, 1, 2); + Card c3 = new Card(2, 2, 2, 3); + + // trying to remove the cards as a set + // should do nothing + t.removeSet(c1, c2, c3); + + assertEquals(3, t.numCards()); + assertEquals(0, t.numSets()); + + assertEquals(c3, t.getCard(0)); + assertEquals(c2, t.getCard(1)); + assertEquals(c1, t.getCard(2)); + } + + + public void testSmallTable1Set() + { + Table t = makeTable("3cards1setMilestone.dat"); + + Card c1 = new Card(1, 1, 1, 1); + Card c2 = new Card(2, 2, 2, 2); + Card c3 = new Card(3, 3, 3, 3); + + // This will result in a middle remove, + // a tail remove and a head remove + t.removeSet(c2, c1, c3); + + + assertEquals(0, t.numCards()); + assertEquals(0, t.numSets()); + } + + + public void testSetNotOnTable() + { + Table t = makeTable("3cards0setsMilestone.dat"); + + Card c1 = new Card(1, 2, 2, 2); + Card c2 = new Card(2, 2, 1, 3); + Card c3 = new Card(3, 2, 3, 1); + + // These cards form a set, but are not on the table + t.removeSet(c1, c2, c3); + + assertEquals(3, t.numCards()); + } + + + public void testNotAllOnTable() + { + Table t = makeTable("3cards0setsMilestone.dat"); + Card c1 = new Card(1, 1, 1, 1); + Card c2 = new Card(1, 1, 1, 2); + Card c3 = new Card(1, 1, 1, 3); + + // c1 and c2 are on the table, but c3 is not. The cards from a set + t.removeSet(c1, c2, c3); + + assertEquals(3, t.numCards()); + } + + + public void testFourteenInTwelve() + { + Table t = makeTable("12cards14setsMilestone.dat"); + + assertEquals(14, t.numSets()); + assertEquals(12, t.numCards()); + } + + + public void testRemoveMiddleMiddleHead() + { + Table t = makeTable("12cards14setsMilestone.dat"); + // 1 1 1 3 (3rd to last card) + // 1 3 1 1 (second card) + // 1 2 1 2 (first card) + t.removeSet(new Card(1, 1, 1, 3), + new Card(1, 3, 1, 1), + new Card(1, 2, 1, 2)); + + assertEquals(9, t.numCards()); + // that removed 6 of the 14 sets. + assertEquals(8, t.numSets()); + } + + + public void testRemoveTailMiddleMiddle() + { + Table t = makeTable("12cards14setsMilestone.dat"); + + // 1 1 1 1 (last card) + // 1 1 2 1 (middle) + // 1 1 3 1 (middle) + t.removeSet(new Card(1, 1, 1, 1), + new Card(1, 1, 2, 1), + new Card(1, 1, 3, 1)); + + assertEquals(9, t.numCards()); + } + + + public void testNoSetsinTwelve() + { + Table t = makeTable("12cards0setsMilestone.dat"); + + assertEquals(12, t.numCards()); + assertEquals(0, t.numSets()); + + // try to remove first, middle, and last cards + // which do not form a set so should do nothing + t.removeSet(new Card(1, 3, 3, 2), + new Card(1, 3, 1, 3), + new Card(2, 2, 1, 3)); + + assertEquals(12, t.numCards()); + assertEquals(0, t.numSets()); + } + + private Table makeTable(String filename) + { + Deck d = new Deck(filename); + Table t = new Table(); + + while(d.hasNext()) + t.add(d.getNext()); + + return t; + } +} diff --git a/src/TableTestMilestone.java~ b/src/TableTestMilestone.java~ new file mode 100644 index 0000000..6c0bc7c --- /dev/null +++ b/src/TableTestMilestone.java~ @@ -0,0 +1,295 @@ +import junit.framework.TestCase; + +public class TableTestMilestone extends TestCase +{ + + public void testConstructorRuns() + { + Table t = new Table(); + } + + + public void testAddRuns() + { + Table t = new Table(); + + t.add(new Card(1, 2, 3, 1)); + } + + + public void testNumCardsNoCards() + { + Table t = new Table(); + + assertEquals(0, t.numCards()); + } + + + public void testNumCardsOneCard() + { + Table t = new Table(); + + t.add(new Card(2, 3, 1, 2)); + + assertEquals(1, t.numCards()); + } + + + public void testNumCardsTwoCards() + { + Table t = new Table(); + + t.add(new Card(3, 1, 2, 3)); + t.add(new Card(1, 2, 3, 1)); + + assertEquals(2, t.numCards()); + } + + + public void testGetCardOneCard() + { + Table t = new Table(); + + Card c = new Card(1, 1, 1, 1); + + t.add(c); + + assertEquals(c, t.getCard(0)); + } + + + public void testGetCardTwoCards() + { + Table t = new Table(); + + Card c1 = new Card(1, 1, 1, 1); + Card c2 = new Card(2, 2, 2, 2); + + t.add(c1); + t.add(c2); + + assertEquals(c2, t.getCard(0)); + assertEquals(c1, t.getCard(1)); + } + + + public void testGetCardThreeCards() + { + Table t = new Table(); + + Card c1 = new Card(1, 1, 1, 1); + Card c2 = new Card(2, 2, 2, 2); + Card c3 = new Card(3, 3, 3, 3); + + t.add(c1); + t.add(c2); + t.add(c3); + + assertEquals(c3, t.getCard(0)); + assertEquals(c2, t.getCard(1)); + assertEquals(c1, t.getCard(2)); + } + + + public void testNumSetsEmptyTable() + { + Table t = new Table(); + + assertEquals(0, t.numSets()); + } + + + public void testNumSets3Cards1Set() + { + Table t = makeTable("3cards1setMilestone.dat"); + + assertEquals(1, t.numSets()); + } + + + public void testNumSets3cards0Sets() + { + Table t = makeTable("3cards0setsMilestone.dat"); + + assertEquals(0, t.numSets()); + } + + + public void testNumSets12Cards14Sets() + { + Table t = makeTable("12cards14setsMilestone.dat"); + + assertEquals(14, t.numSets()); + } + + + public void testRemoveSetEmptyTable() + { + Table t = new Table(); + + // Removing a valid set from an empty table shouldn't + // do anything. + t.removeSet(new Card(1, 1, 1, 1), + new Card(2, 2, 2, 2), + new Card(3, 3, 3, 3)); + + assertEquals(0, t.numCards()); + assertEquals(0, t.numSets()); + } + + + public void testSmallTable() + { + Table t = makeTable("3cards0setsMilestone.dat"); + + Card c1 = new Card(1, 1, 1, 1); + Card c2 = new Card(1, 1, 1, 2); + Card c3 = new Card(2, 2, 2, 3); + + assertEquals(3, t.numCards()); + assertEquals(0, t.numSets()); + + // because we're doing head insertion + // the Cards are indexed in the reverse + // order from which they are added + assertEquals(c3, t.getCard(0)); + assertEquals(c2, t.getCard(1)); + assertEquals(c1, t.getCard(2)); + } + + + public void testSmallTableNoSets() + { + Table t = makeTable("3cards0setsMilestone.dat"); + + Card c1 = new Card(1, 1, 1, 1); + Card c2 = new Card(1, 1, 1, 2); + Card c3 = new Card(2, 2, 2, 3); + + // trying to remove the cards as a set + // should do nothing + t.removeSet(c1, c2, c3); + + assertEquals(3, t.numCards()); + assertEquals(0, t.numSets()); + + assertEquals(c3, t.getCard(0)); + assertEquals(c2, t.getCard(1)); + assertEquals(c1, t.getCard(2)); + } + + + public void testSmallTable1Set() + { + Table t = makeTable("3cards1setMilestone.dat"); + + Card c1 = new Card(1, 1, 1, 1); + Card c2 = new Card(2, 2, 2, 2); + Card c3 = new Card(3, 3, 3, 3); + + // This will result in a middle remove, + // a tail remove and a head remove + t.removeSet(c2, c1, c3); + + assertEquals(0, t.numCards()); + assertEquals(0, t.numSets()); + } + + + public void testSetNotOnTable() + { + Table t = makeTable("3cards0setsMilestone.dat"); + + Card c1 = new Card(1, 2, 2, 2); + Card c2 = new Card(2, 2, 1, 3); + Card c3 = new Card(3, 2, 3, 1); + + // These cards form a set, but are not on the table + t.removeSet(c1, c2, c3); + + assertEquals(3, t.numCards()); + } + + + public void testNotAllOnTable() + { + Table t = makeTable("3cards0setsMilestone.dat"); + Card c1 = new Card(1, 1, 1, 1); + Card c2 = new Card(1, 1, 1, 2); + Card c3 = new Card(1, 1, 1, 3); + + // c1 and c2 are on the table, but c3 is not. The cards from a set + t.removeSet(c1, c2, c3); + + assertEquals(3, t.numCards()); + } + + + public void testFourteenInTwelve() + { + Table t = makeTable("12cards14setsMilestone.dat"); + + assertEquals(14, t.numSets()); + assertEquals(12, t.numCards()); + } + + + public void testRemoveMiddleMiddleHead() + { + Table t = makeTable("12cards14setsMilestone.dat"); + // 1 1 1 3 (3rd to last card) + // 1 3 1 1 (second card) + // 1 2 1 2 (first card) + t.removeSet(new Card(1, 1, 1, 3), + new Card(1, 3, 1, 1), + new Card(1, 2, 1, 2)); + + assertEquals(9, t.numCards()); + // that removed 6 of the 14 sets. + assertEquals(8, t.numSets()); + } + + + public void testRemoveTailMiddleMiddle() + { + Table t = makeTable("12cards14setsMilestone.dat"); + + // 1 1 1 1 (last card) + // 1 1 2 1 (middle) + // 1 1 3 1 (middle) + t.removeSet(new Card(1, 1, 1, 1), + new Card(1, 1, 2, 1), + new Card(1, 1, 3, 1)); + + assertEquals(9, t.numCards()); + } + + + public void testNoSetsinTwelve() + { + Table t = makeTable("12cards0setsMilestone.dat"); + + assertEquals(12, t.numCards()); + assertEquals(0, t.numSets()); + + // try to remove first, middle, and last cards + // which do not form a set so should do nothing + t.removeSet(new Card(1, 3, 3, 2), + new Card(1, 3, 1, 3), + new Card(2, 2, 1, 3)); + + assertEquals(12, t.numCards()); + assertEquals(0, t.numSets()); + } + + private Table makeTable(String filename) + { + Deck d = new Deck(filename); + Table t = new Table(); + + while(d.hasNext()) + t.add(d.getNext()); + + return t; + } +} diff --git a/src/empty game.txt b/src/empty game.txt new file mode 100644 index 0000000..e69de29