diff --git a/src/Card.java b/src/Card.java old mode 100755 new mode 100644 index 9eed9a5..bb7899f --- a/src/Card.java +++ b/src/Card.java @@ -1,7 +1,66 @@ public class Card { // Create the rest of this class yourself - public boolean equals(Object obj) { + 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; + if (quantity == 0) + quantity += 3; + + color = (cardColor % 3 + 3) % 3; + if (color == 0) + color += 3; + + shading = (cardShading % 3 + 3) % 3; + if (shading == 0) + shading += 3; + + shape = (cardShape % 3 + 3) % 3; + if (shape == 0) + shape += 3; + } + + public int getQuantity() + { + return quantity; + } + + public int getColor() + { + return color; + } + + public int getShading() + { + return shading; + } + + public int getShape() + { + return shape; + } + + public String toString() + { + char[] letters = new char[] {'R', 'G', 'P', 'O', 'T', 'S', 'O', 'D', 'S'}; + return String.valueOf(quantity) + letters[color - 1] + letters[shading + 2] + letters[shape + 5]; + } + + public boolean isSet(Card card2, Card card3) + { + return (quantity + card2.getQuantity() + card3.getQuantity()) % 3 == 0 && + (color + card2.getColor() + card3.getColor()) % 3 == 0 && + (shading + card2.getShading() + card3.getShading()) % 3 == 0 && + (shape + card2.getShape() + card3.getShape()) % 3 == 0; + } + + public boolean equals(Object obj) + { Card that = (Card)obj; return quantity == that.getQuantity() && diff --git a/src/CardTest.java b/src/CardTest.java new file mode 100644 index 0000000..4996e6f --- /dev/null +++ b/src/CardTest.java @@ -0,0 +1,93 @@ +import junit.framework.TestCase; + +/** + * A JUnit test case class. + * Every method starting with the word "test" will be called when running + * the test with JUnit. + */ +public class CardTest extends TestCase { + + /** + * A test method. + * (Replace "X" with a name describing the test. You may write as + * many "testSomething" methods in this class as you wish, and each + * one will be called when running JUnit over this class.) + */ + public void testCardQueeries() + { + Card card1 = new Card(3, 2, 2, 1); + //a card with numbers between 1 and 3 + assertEquals(3, card1.getQuantity()); + assertEquals(2, card1.getColor()); + assertEquals(2, card1.getShading()); + assertEquals(1, card1.getShape()); + + Card card2 = new Card(4, 5, 6, 7); + //a card with numbers greater than 3 + assertEquals(1, card2.getQuantity()); + assertEquals(2, card2.getColor()); + assertEquals(3, card2.getShading()); + assertEquals(1, card2.getShape()); + + Card card3 = new Card(0, -1, -2, -3); + //a card with numbers lower than 1 + assertEquals(3, card3.getQuantity()); + assertEquals(2, card3.getColor()); + assertEquals(1, card3.getShading()); + assertEquals(3, card3.getShape()); + } + + 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)); + + } + + public void testCardString() + { + Card card1 = new Card(1, 1, 1, 2); + Card card2 = new Card(3, 2, 3, 2); + Card card3 = new Card(4, 5, 6, 7); + Card card4 = new Card(0, -1, -2, -3); + + assertEquals("1ROD", card1.toString()); + assertEquals("3GSD", card2.toString()); + assertEquals("1GSO", card3.toString()); + assertEquals("3GOS", card4.toString()); + } + + public void testCardSet() + { + Card card1 = new Card(1, 1, 1, 1); + Card card2 = new Card(2, 1, 1, 1); + Card card3 = new Card(3, 1, 1, 1); + Card card4 = new Card(2, 2, 2, 2); + Card card5 = new Card(3, 3, 3, 3); + + assertEquals(true, card1.isSet(card2, card3)); + assertEquals(true, card1.isSet(card4, card5)); + assertEquals(false, card1.isSet(card2, card4)); + assertEquals(true, card3.isSet(card2, card1)); + assertEquals(true, card2.isSet(card1, card3)); + assertEquals(false, card2.isSet(card1, card4)); + assertEquals(false, card4.isSet(card1, card2)); + assertEquals(false, card4.isSet(card3, card2)); + + } +} \ No newline at end of file diff --git a/src/Deck.java b/src/Deck.java index ab3a2a3..93b9ca4 100755 --- a/src/Deck.java +++ b/src/Deck.java @@ -2,14 +2,55 @@ 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; + private int nextCardIndex; - public Deck(String filename) { + public Deck() + { cards = new ArrayList(81); + nextCardIndex = 0; - try { + for (int i = 1; i < 4; i++) + { + for (int j = 1; j < 4; j++) + { + for (int k = 1; k < 4; k++) + { + for (int l = 1; l < 4; l++) + { + cards.add(new Card(i, j, k, l)); + } + } + } + } + + Collections.shuffle(cards); + } + + + public boolean hasNext() + { + return nextCardIndex < cards.size(); + } + + public Card getNext() + { + if (hasNext() == false) + return null; + + 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; @@ -38,7 +79,8 @@ public Deck(String filename) { nextCardIndex = 0; } } - catch(Exception e) { + catch(Exception e) + { throw new RuntimeException("Error while reading file: " + e.toString()); } } diff --git a/src/DeckNoSetsFirstTable.txt b/src/DeckNoSetsFirstTable.txt new file mode 100644 index 0000000..704c694 --- /dev/null +++ b/src/DeckNoSetsFirstTable.txt @@ -0,0 +1,12 @@ +1 1 1 1 +1 1 1 2 +1 1 2 1 +1 1 2 3 +1 2 1 1 +1 2 3 1 +2 1 1 1 +2 3 1 3 +3 3 2 1 +3 1 2 3 +3 3 3 3 +3 2 1 2 \ No newline at end of file diff --git a/src/DeckTest.java b/src/DeckTest.java new file mode 100644 index 0000000..4a18ae5 --- /dev/null +++ b/src/DeckTest.java @@ -0,0 +1,75 @@ +import junit.framework.TestCase; + +/** + * A JUnit test case class. + * Every method starting with the word "test" will be called when running + * the test with JUnit. + */ +public class DeckTest extends TestCase { + + + /** + * A test method. + * (Replace "X" with a name describing the test. You may write as + * many "testSomething" methods in this class as you wish, and each + * one will be called when running JUnit over this class.) + */ + public void testDeckCreation() + { + Deck deck = new Deck(); + int i = 0; + + while (deck.hasNext()) + { + deck.getNext(); + i += 1; + } + + assertEquals(81, i); + } + + public void testCostumDeckCreation() + { + Deck deck = new Deck("DeckTester.txt"); + Card card1 = new Card(1, 1, 1, 1); + Card card2 = new Card(1, 1, 1, 2); + Card card3 = new Card(1, 1, 1, 3); + Card card4 = new Card(1, 1, 2, 1); + + assertEquals(true, deck.hasNext()); + assertEquals(true, deck.getNext().equals(card1)); + + assertEquals(true, deck.hasNext()); + assertEquals(true, deck.getNext().equals(card2)); + + assertEquals(true, deck.hasNext()); + assertEquals(true, deck.getNext().equals(card3)); + + assertEquals(true, deck.hasNext()); + assertEquals(true, deck.getNext().equals(card4)); + + assertEquals(false, deck.hasNext()); + assertEquals(null, deck.getNext()); + } + + public void testEmptyDeck() + { + Deck deck = new Deck("emptydeck.txt"); + + assertEquals(false, deck.hasNext()); + assertEquals(null, deck.getNext()); + } + + public void testOneDeck() + { + Deck deck = new Deck("onedeck.txt"); + Card card = new Card(1, 1, 1, 1); + + assertEquals(true, deck.hasNext()); + assertEquals(true, deck.getNext().equals(card)); + + assertEquals(false, deck.hasNext()); + assertEquals(null, deck.getNext()); + } +} + diff --git a/src/DeckTester.dat b/src/DeckTester.dat new file mode 100644 index 0000000..22d7a3d --- /dev/null +++ b/src/DeckTester.dat @@ -0,0 +1,4 @@ +1 1 1 1 +1 1 1 2 +1 1 1 3 +1 1 2 1 \ No newline at end of file diff --git a/src/DeckTester.txt b/src/DeckTester.txt new file mode 100644 index 0000000..22d7a3d --- /dev/null +++ b/src/DeckTester.txt @@ -0,0 +1,4 @@ +1 1 1 1 +1 1 1 2 +1 1 1 3 +1 1 2 1 \ No newline at end of file diff --git a/src/FullTableAndSets.txt b/src/FullTableAndSets.txt new file mode 100644 index 0000000..114614b --- /dev/null +++ b/src/FullTableAndSets.txt @@ -0,0 +1,15 @@ +1 1 1 1 +1 1 1 2 +1 1 1 3 +2 2 2 2 +3 3 3 3 +2 2 2 3 +2 2 2 1 +3 3 3 2 +3 3 3 1 +2 1 2 2 +3 2 2 1 +3 1 1 3 +1 2 2 3 +3 2 1 1 +3 1 2 3 \ No newline at end of file diff --git a/src/Game.java b/src/Game.java new file mode 100644 index 0000000..f9836c1 --- /dev/null +++ b/src/Game.java @@ -0,0 +1,109 @@ +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(); + + while (d.hasNext() == true) + { + t.add(d.getNext()); + + if (t.numCards() > 11) + return; + } + } + + public int numSets() + { + return t.numSets(); + } + + public int numCards() + { + return t.numCards(); + } + + public void playRound() + { + if (t.numSets() == 0 && d.hasNext() == true) + { + for (int i = 0; i < 3; i++) + { + if (d.hasNext() == false) + return; + + t.add(d.getNext()); + } + + return; + } + + else if (d.hasNext() == true && t.numSets() != 0) + { + for (int j = 0; j < t.numCards() - 2; j++) + { + for (int k = j + 1; k < t.numCards() - 1; k++) + { + for (int l = k + 1; l < t.numCards(); l++) + { + if (t.getCard(j).isSet(t.getCard(k), t.getCard(l)) == true) + { + t.removeSet(t.getCard(j), t.getCard(k), t.getCard(l)); + + while (t.numCards() < 12) + { + if (d.hasNext() == false) + return; + + t.add(d.getNext()); + } + + return; + } + } + } + } + } + + else if (t.numSets() != 0 && d.hasNext() == false) + { + for (int j = 0; j < t.numCards() - 2; j++) + { + for (int k = j + 1; k < t.numCards() - 1; k++) + { + for (int l = k + 1; l < t.numCards(); l++) + { + if (t.getCard(j).isSet(t.getCard(k), t.getCard(l))) + { + t.removeSet(t.getCard(j), t.getCard(k), t.getCard(l)); + return; + } + } + } + } + } + + return; + } + + public boolean isGameOver() + { + if (d.hasNext() == false && t.numSets() == 0) + return true; + + return false; + } +} \ No newline at end of file diff --git a/src/GameAverage.java b/src/GameAverage.java new file mode 100644 index 0000000..af7387e --- /dev/null +++ b/src/GameAverage.java @@ -0,0 +1,27 @@ +public class GameAverage +{ + public static void main(String[] args) + { + double beginningSets = 0; + double endingCards = 0; + double beginningCards = 0; + + for (int i = 0; i < 1000000; i++) + { + Game game = new Game(); + beginningCards += game.numCards(); + beginningSets += game.numSets(); + + while (!game.isGameOver()) + { + game.playRound(); + } + + endingCards += game.numCards(); + } + + System.out.println(beginningCards/1000000); + System.out.println(beginningSets/1000000); + System.out.println(endingCards/1000000); + } +} \ No newline at end of file diff --git a/src/GameTest.java b/src/GameTest.java new file mode 100644 index 0000000..1ce87b0 --- /dev/null +++ b/src/GameTest.java @@ -0,0 +1,170 @@ +import junit.framework.TestCase; + +/** + * A JUnit test case class. + * Every method starting with the word "test" will be called when running + * the test with JUnit. + */ +public class GameTest extends TestCase { + + /** + * A test method. + * (Replace "X" with a name describing the test. You may write as + * many "testSomething" methods in this class as you wish, and each + * one will be called when running JUnit over this class.) + */ + public void testInitialGame() + { + Game game = new Game(); + + assertEquals(12, game.numCards()); + } + + public void testEmptyGame() + { + Game game = new Game("emptydeck.txt"); + + assertEquals(0, game.numCards()); + assertEquals(0, game.numSets()); + assertEquals(true, game.isGameOver()); + } + + public void testOneCardGame() + { + Game game = new Game("onedeck.txt"); + + assertEquals(1, game.numCards()); + assertEquals(0, game.numSets()); + assertEquals(true, game.isGameOver()); + } + + public void testGameWithSetInitial() + { + Game game = new Game("DeckTester.txt"); + + assertEquals(4, game.numCards()); + assertEquals(1, game.numSets()); + assertEquals(false, game.isGameOver()); + } + + public void testRoundOneSetEmptyDeck() + { + Game game = new Game("DeckTester.txt"); + + assertEquals(4, game.numCards()); + assertEquals(1, game.numSets()); + + game.playRound(); + + assertEquals(1, game.numCards()); + assertEquals(0, game.numSets()); + assertEquals(true, game.isGameOver()); + } + + public void testRoundTwoSetsEmptyDeck() + { + Game game = new Game("twoSetsSixCards.txt"); + + assertEquals(6, game.numCards()); + assertEquals(2, game.numSets()); + assertEquals(false, game.isGameOver()); + + game.playRound(); + + assertEquals(3, game.numCards()); + assertEquals(1, game.numSets()); + assertFalse(game.isGameOver()); + + game.playRound(); + + assertEquals(0, game.numCards()); + assertEquals(0, game.numSets()); + assertTrue(game.isGameOver()); + } + + public void testNoSetsOnTable() + { + Game game = new Game("FullTableAndSets.txt"); + + assertEquals(12, game.numCards()); + assertEquals(false, game.isGameOver()); + + game.playRound(); + + assertEquals(12, game.numCards()); + assertEquals(false, game.isGameOver()); + + game.playRound(); + + assertEquals(9, game.numCards()); + } + + public void testFullTableAndDeck() + { + Game game = new Game("fullTableAllOnes.txt"); + + assertEquals(12, game.numCards()); + assertFalse(game.isGameOver()); + + game.playRound(); + assertEquals(12, game.numCards()); + assertFalse(game.isGameOver()); + + game.playRound(); + assertEquals(12, game.numCards()); + assertFalse(game.isGameOver()); + + game.playRound(); + assertEquals(9, game.numCards()); + assertFalse(game.isGameOver()); + + game.playRound(); + assertEquals(6, game.numCards()); + assertFalse(game.isGameOver()); + + game.playRound(); + assertEquals(3, game.numCards()); + assertFalse(game.isGameOver()); + + game.playRound(); + assertEquals(0, game.numCards()); + assertTrue(game.isGameOver()); + } + + public void testTestTable() + { + Game game = new Game("TestTable.txt"); + + assertEquals(12, game.numCards()); + assertEquals(3, game.numSets()); + assertFalse(game.isGameOver()); + + game.playRound(); + + assertEquals(12, game.numCards()); + assertFalse(game.isGameOver()); + + game.playRound(); + + assertEquals(9, game.numCards()); + assertEquals(true, game.isGameOver()); + + game.playRound(); + + assertEquals(9, game.numCards()); + } + + public void testNoSet() + { + Game game = new Game("HopefulNoSet.txt"); + + assertEquals(12, game.numCards()); + assertEquals(0, game.numSets()); + assertFalse(game.isGameOver()); + + game.playRound(); + assertEquals(15, game.numCards()); + assertEquals(0, game.numSets()); + assertTrue(game.isGameOver()); + } +} diff --git a/src/HopefulNoSet.txt b/src/HopefulNoSet.txt new file mode 100644 index 0000000..3f2f8fc --- /dev/null +++ b/src/HopefulNoSet.txt @@ -0,0 +1,15 @@ +2 3 2 1 +3 3 2 2 +2 1 2 1 +3 2 1 1 +3 1 3 2 +2 1 1 1 +2 1 1 3 +1 3 3 3 +3 1 1 3 +2 2 1 2 +2 3 3 2 +1 2 1 2 +2 3 1 2 +2 3 3 1 +3 2 1 3 \ No newline at end of file diff --git a/src/Table.java b/src/Table.java new file mode 100644 index 0000000..4a376ee --- /dev/null +++ b/src/Table.java @@ -0,0 +1,162 @@ +public class Table +{ + TableNode head; + + public Table() + { + head = null; + } + + public void add(Card card) + { + TableNode newNode = new TableNode(card); + + newNode.setNext(head); + head = newNode; + } + + public void removeSet(Card card1, Card card2, Card card3) + { + if (card1.isSet(card2, card3) == false) + return; + + TableNode ref = head; + + Card removedCard1 = null; + Card removedCard2 = null; + Card removedCard3 = null; + + int removedCards = 0; + + while (ref != null) + { + if (ref.getCard().equals(card1)) + removedCard1 = card1; + if (ref.getCard().equals(card2)) + removedCard2 = card2; + if (ref.getCard().equals(card3)) + removedCard3 = card3; + + ref = ref.getNext(); + } + + if (removedCard1 != null && removedCard2 != null && removedCard3 != null) + { + if (removedCard1.equals(card1) && removedCard2.equals(card2) && removedCard3.equals(card3)) + { + TableNode curr = head; + TableNode prev = null; + int prevNumCards = numCards(); + + while (curr != null) + { + if (curr.getCard().equals(card1) || curr.getCard().equals(card2) || curr.getCard().equals(card3)) + { + if (removedCards == 3) + return; + + if (curr == head) + { + prev = head; + head = head.getNext(); + prev.setNext(null); + curr = head; + prev = null; + + removedCards += 1; + } + + else + { + prev.setNext(curr.getNext()); + curr.setNext(null); + + removedCards += 1; + } + } + + if (numCards() == 0) + return; + + if (curr.getNext() == null && prev != null) + curr = prev.getNext(); + + + if (prevNumCards == numCards()) + { + prev = curr; + curr = curr.getNext(); + } + + prevNumCards = numCards(); + } + } + } + + return; + } + + public int numCards() + { + TableNode ref = head; + int numCards = 1; + + if (ref == null) + return 0; + + while (ref.getNext() != null) + { + ref = ref.getNext(); + numCards += 1; + } + + return numCards; + } + + public Card getCard(int cardIndex) + { + TableNode ref = head; + if (ref == null) + return null; + + for (int n = 0; n < cardIndex; n++) + { + ref = ref.getNext(); + + if (ref == null) + return null; + } + + return ref.getCard(); + } + + public int numSets() + { + int numSets = 0; + TableNode n1 = head; + + while (n1 != null && n1.getNext() != null && n1.getNext().getNext() != null) + { + TableNode n2 = n1.getNext(); + + while (n2 != null && n2.getNext() != null) + { + TableNode n3 = n2.getNext(); + + while (n3 != null) + { + if (n1.getCard().isSet(n2.getCard(), n3.getCard()) == true) + numSets += 1; + + n3 = n3.getNext(); + } + + n2 = n2.getNext(); + } + + n1 = n1.getNext(); + } + + return numSets; + } +} \ No newline at end of file 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..622690f --- /dev/null +++ b/src/TableNodeTest.java @@ -0,0 +1,40 @@ +import junit.framework.TestCase; + +/** + * A JUnit test case class. + * Every method starting with the word "test" will be called when running + * the test with JUnit. + */ +public class TableNodeTest extends TestCase { + + /** + * A test method. + * (Replace "X" with a name describing the test. You may write as + * many "testSomething" methods in this class as you wish, and each + * one will be called when running JUnit over this class.) + */ + 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)); + + } +} diff --git a/src/TableTest.java b/src/TableTest.java new file mode 100644 index 0000000..80284bb --- /dev/null +++ b/src/TableTest.java @@ -0,0 +1,312 @@ +import junit.framework.TestCase; + +/** + * A JUnit test case class. + * Every method starting with the word "test" will be called when running + * the test with JUnit. + */ +public class TableTest extends TestCase { + + /** + * A test method. + * (Replace "X" with a name describing the test. You may write as + * many "testSomething" methods in this class as you wish, and each + * one will be called when running JUnit over this class.) + */ + public void testEmptyTable() + { + Table table = new Table(); + + assertEquals(null, table.getCard(0)); + assertEquals(0, table.numCards()); + } + + public void testTableOne() + { + Table table = new Table(); + Card card = new Card(1, 1, 1, 1); + table.add(card); + + assertEquals(true, table.getCard(0).equals(card)); + assertEquals(1, table.numCards()); + assertEquals(null, table.getCard(1)); + assertEquals(null, table.getCard(4)); + } + + public void testTableTwoCards() + { + Table table = new Table(); + Card card1 = new Card(1, 1, 1, 1); + Card card2 = new Card(1, 1, 1, 2); + table.add(card1); + table.add(card2); + + assertEquals(true, table.getCard(0).equals(card2)); + assertEquals(true, table.getCard(1).equals(card1)); + assertEquals(null, table.getCard(2)); + assertEquals(null, table.getCard(33)); + assertEquals(2, table.numCards()); + } + + public void testTableManyCards() + { + Table table = new Table(); + Card card1 = new Card(1, 1, 1, 1); + Card card2 = new Card(1, 1, 1, 2); + Card card3 = new Card(1, 1, 1, 3); + Card card4 = new Card(1, 1, 2, 1); + Card card5 = new Card(1, 1, 2, 2); + Card card6 = new Card(1, 1, 2, 3); + + table.add(card1); + table.add(card2); + table.add(card3); + table.add(card4); + table.add(card5); + table.add(card6); + + assertEquals(6, table.numCards()); + assertEquals(true, table.getCard(0).equals(card6)); + assertEquals(true, table.getCard(1).equals(card5)); + assertEquals(true, table.getCard(2).equals(card4)); + assertEquals(true, table.getCard(3).equals(card3)); + assertEquals(true, table.getCard(4).equals(card2)); + assertEquals(true, table.getCard(5).equals(card1)); + } + + public void testNumSetsNone() + { + Table table = new Table(); + + assertEquals(0, table.numSets()); + table.add(new Card(1, 1, 1, 1)); + + assertEquals(0, table.numSets()); + table.add(new Card(1, 1, 1, 2)); + + assertEquals(0, table.numSets()); + table.add(new Card(2, 1, 1, 2)); + + assertEquals(0, table.numSets()); + table.add(new Card(3, 2, 2, 1)); + assertEquals(0, table.numSets()); + table.add(new Card(3, 2, 3, 2)); + assertEquals(0, table.numSets()); + } + + public void testNumSetsOne() + { + Table table = new Table(); + table.add(new Card(1, 1, 1, 1)); + table.add(new Card(1, 1, 1, 2)); + table.add(new Card(1, 1, 1, 3)); + assertEquals(1, table.numSets()); + + table.add(new Card(3, 1, 1, 1)); + assertEquals(1, table.numSets()); + table.add(new Card(3, 1, 1, 2)); + assertEquals(1, table.numSets()); + } + + public void testNumSetsTwo() + { + Table table = new Table(); + table.add(new Card(1, 1, 1, 1)); + table.add(new Card(1, 1, 1, 2)); + table.add(new Card(1, 1, 1, 3)); + table.add(new Card(3, 1, 1, 1)); + table.add(new Card(2, 1, 1, 1)); + + assertEquals(2, table.numSets()); + table.add(new Card(1, 1, 2, 3)); + table.add(new Card(1, 1, 3, 3)); + assertEquals(3, table.numSets()); + } + + public void testRemoveSetNotSet() + { + Table table = new Table(); + Card card1 = new Card(1, 1, 1, 1); + Card card2 = new Card(1, 1, 1, 2); + Card card3 = new Card(2, 1, 1, 1); + + table.add(card1); + table.add(card2); + table.add(card3); + + assertEquals(3, table.numCards()); + + table.removeSet(card1, card2, card3); + + assertEquals(3, table.numCards()); + } + + public void testRemoveSetAtTail() + { + Table table = new Table(); + + Card card1 = new Card(1, 1, 1, 1); + Card card2 = new Card(1, 1, 1, 2); + Card card3 = new Card(1, 1, 1, 3); + Card card4 = new Card(2, 2, 2, 2); + + table.add(card1); + table.add(card2); + table.add(card3); + table.add(card4); + + assertEquals(4, table.numCards()); + + table.removeSet(card1, card2, card3); + + assertEquals(1, table.numCards()); + assertEquals(true, table.getCard(0).equals(card4)); + } + + public void testRemoveSetAtHead() + { + Table table = new Table(); + Card card1 = new Card(2, 2, 2, 2); + Card card2 = new Card(1, 1, 1, 1); + Card card3 = new Card(1, 1, 1, 2); + Card card4 = new Card(1, 1, 1, 3); + + table.add(card1); + table.add(card2); + table.add(card3); + table.add(card4); + + assertEquals(4, table.numCards()); + + table.removeSet(card2, card3, card4); + + assertEquals(1, table.numCards()); + assertTrue(table.getCard(0).equals(card1)); + } + + public void testRemoveSetNotInTable() + { + Table table = new Table(); + Card card1 = new Card(1, 1, 1, 1); + Card card2 = new Card(1, 1, 1, 2); + Card card3 = new Card(1, 1, 1, 3); + Card card4 = new Card(2, 2, 2, 2); + + assertEquals(0, table.numCards()); + + table.removeSet(card1, card2, card3); + + assertEquals(0, table.numCards()); + + table.add(card1); + assertEquals(1, table.numCards()); + table.removeSet(card1, card2, card3); + + assertEquals(1, table.numCards()); + + table.add(card2); + assertEquals(2, table.numCards()); + table.removeSet(card1, card2, card3); + + table.add(card4); + assertEquals(3, table.numCards()); + table.removeSet(card1, card2, card3); + + assertEquals(3, table.numCards()); + } + + public void testRemoveSetTailHeadAndMiddle() + { + Table table = new Table(); + Card card1 = new Card(1, 1, 1, 1); + Card card2 = new Card(1, 1, 1, 2); + Card card3 = new Card(1, 1, 1, 3); + Card card4 = new Card(2, 2, 2, 2); + Card card5 = new Card(2, 1, 2, 2); + + table.add(card1); + table.add(card4); + table.add(card2); + table.add(card5); + table.add(card3); + + assertEquals(5, table.numCards()); + + table.removeSet(card1, card2, card3); + + assertEquals(2, table.numCards()); + assertEquals(true, table.getCard(0).equals(card5)); + assertEquals(true, table.getCard(1).equals(card4)); + } + + public void testRemoveSetThreeCardTable() + { + Table table = new Table(); + Card card1 = new Card(1, 1, 1, 1); + Card card2 = new Card(1, 1, 1, 2); + Card card3 = new Card(1, 1, 1, 3); + + table.add(card1); + table.add(card2); + table.add(card3); + + assertEquals(3, table.numCards()); + + table.removeSet(card1, card2, card3); + assertEquals(0, table.numCards()); + } + + public void testRemoveSetAllCardsMiddle() + { + Table table = new Table(); + Card card1 = new Card(1, 1, 1, 1); + Card card2 = new Card(1, 1, 1, 2); + Card card3 = new Card(1, 1, 1, 3); + Card card4 = new Card(2, 2, 2, 2); + Card card5 = new Card(2, 1, 2, 2); + + table.add(card5); + table.add(card1); + table.add(card2); + table.add(card3); + table.add(card4); + + assertEquals(5, table.numCards()); + + table.removeSet(card1, card2, card3); + + assertEquals(2, table.numCards()); + assertEquals(true, table.getCard(0).equals(card4)); + assertEquals(true, table.getCard(1).equals(card5)); + } + + public void testRemoveSetAllSeperatedMiddle() + { + Table table = new Table(); + Card card1 = new Card(1, 1, 1, 1); + Card card2 = new Card(1, 1, 1, 2); + Card card3 = new Card(1, 1, 1, 3); + Card card4 = new Card(2, 2, 2, 2); + Card card5 = new Card(2, 1, 2, 2); + Card card6 = new Card(2, 3, 2, 3); + Card card7 = new Card(3, 1, 3, 2); + + table.add(card7); + table.add(card1); + table.add(card6); + table.add(card2); + table.add(card5); + table.add(card3); + table.add(card4); + + assertEquals(7, table.numCards()); + + table.removeSet(card1, card2, card3); + + assertEquals(4, table.numCards()); + assertTrue(table.getCard(0).equals(card4)); + assertTrue(table.getCard(1).equals(card5)); + assertTrue(table.getCard(2).equals(card6)); + assertTrue(table.getCard(3).equals(card7)); + } +} diff --git a/src/TestTable.txt b/src/TestTable.txt new file mode 100644 index 0000000..50ce243 --- /dev/null +++ b/src/TestTable.txt @@ -0,0 +1,15 @@ +2 1 2 1 +2 3 1 3 +2 3 1 2 +2 1 3 2 +1 3 2 2 +3 2 3 3 +2 2 2 1 +1 2 3 1 +1 1 2 2 +1 2 1 1 +3 2 3 2 +1 2 1 2 +2 3 3 1 +2 1 2 1 +3 3 3 2 \ No newline at end of file diff --git a/src/emptydeck.txt b/src/emptydeck.txt new file mode 100644 index 0000000..e69de29 diff --git a/src/fullTableAllOnes.txt b/src/fullTableAllOnes.txt new file mode 100644 index 0000000..1420555 --- /dev/null +++ b/src/fullTableAllOnes.txt @@ -0,0 +1,18 @@ +1 1 1 1 +1 1 1 1 +1 1 1 1 +1 1 1 1 +1 1 1 1 +1 1 1 1 +1 1 1 1 +1 1 1 1 +1 1 1 1 +1 1 1 1 +1 1 1 1 +1 1 1 1 +1 1 1 1 +1 1 1 1 +1 1 1 1 +1 1 1 1 +1 1 1 1 +1 1 1 1 \ No newline at end of file diff --git a/src/onedeck.txt b/src/onedeck.txt new file mode 100644 index 0000000..6540ed2 --- /dev/null +++ b/src/onedeck.txt @@ -0,0 +1 @@ +1 1 1 1 \ No newline at end of file diff --git a/src/twoSetsSixCards.txt b/src/twoSetsSixCards.txt new file mode 100644 index 0000000..3c70c84 --- /dev/null +++ b/src/twoSetsSixCards.txt @@ -0,0 +1,6 @@ +1 1 1 1 +1 1 1 2 +1 1 1 3 +3 1 1 1 +3 1 1 2 +3 1 1 3 \ No newline at end of file