diff --git a/src/.DS_Store b/src/.DS_Store new file mode 100644 index 0000000..6e3d302 Binary files /dev/null and b/src/.DS_Store differ diff --git a/src/12cards0setsMilestone.dat b/src/12cards0setsMilestone.dat new file mode 100755 index 0000000..dc7c2c6 --- /dev/null +++ b/src/12cards0setsMilestone.dat @@ -0,0 +1,13 @@ +# 12 cards with 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 diff --git a/src/12cards14setsMilestone.dat b/src/12cards14setsMilestone.dat new file mode 100755 index 0000000..1b3ea71 --- /dev/null +++ b/src/12cards14setsMilestone.dat @@ -0,0 +1,12 @@ +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 diff --git a/src/3cards.txt b/src/3cards.txt new file mode 100755 index 0000000..3c2b949 --- /dev/null +++ b/src/3cards.txt @@ -0,0 +1,4 @@ +#3cards +1 1 1 1 +1 1 1 2 +1 1 1 3 \ No newline at end of file diff --git a/src/3cards0setsMilestone.dat b/src/3cards0setsMilestone.dat new file mode 100644 index 0000000..b0a41dc --- /dev/null +++ b/src/3cards0setsMilestone.dat @@ -0,0 +1,12 @@ +# Lines starting with a # character are comments, and ignored +# Blank lines are also ignored, even if they contain whitespace + +# All other lines are expected to have 4 integers, separated by spaces +1 1 1 1 +1 1 1 2 +2 2 2 3 + +# A file does not need to contain all 81 cards + + + diff --git a/src/3cards1setMilestone.dat b/src/3cards1setMilestone.dat new file mode 100755 index 0000000..3fdd6d1 --- /dev/null +++ b/src/3cards1setMilestone.dat @@ -0,0 +1,3 @@ +1 1 1 1 +2 2 2 2 +3 3 3 3 diff --git a/src/81cards.txt b/src/81cards.txt new file mode 100644 index 0000000..9e0b11b --- /dev/null +++ b/src/81cards.txt @@ -0,0 +1,81 @@ +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 2 1 2 +1 2 1 3 +1 2 2 1 +1 2 2 2 +1 2 2 3 +1 2 3 1 +1 2 3 2 +1 2 3 3 +1 3 1 1 +1 3 1 2 +1 3 1 3 +1 3 2 1 +1 3 2 2 +1 3 2 3 +1 3 3 1 +1 3 3 2 +1 3 3 3 +2 1 1 1 +2 1 1 2 +2 1 1 3 +2 1 2 1 +2 1 2 2 +2 1 2 3 +2 1 3 1 +2 1 3 2 +2 1 3 3 +2 2 1 1 +2 2 1 2 +2 2 1 3 +2 2 2 1 +2 2 2 2 +2 2 2 3 +2 2 3 1 +2 2 3 2 +2 2 3 3 +2 3 1 1 +2 3 1 2 +2 3 1 3 +2 3 2 1 +2 3 2 2 +2 3 2 3 +2 3 3 1 +2 3 3 2 +2 3 3 3 +3 1 1 1 +3 1 1 2 +3 1 1 3 +3 1 2 1 +3 1 2 2 +3 1 2 3 +3 1 3 1 +3 1 3 2 +3 1 3 3 +3 2 1 1 +3 2 1 2 +3 2 1 3 +3 2 2 1 +3 2 2 2 +3 2 2 3 +3 2 3 1 +3 2 3 2 +3 2 3 3 +3 3 1 1 +3 3 1 2 +3 3 1 3 +3 3 2 1 +3 3 2 2 +3 3 2 3 +3 3 3 1 +3 3 3 2 +3 3 3 3 \ No newline at end of file diff --git a/src/Card.class b/src/Card.class new file mode 100644 index 0000000..8f20a97 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..c2ac76c --- a/src/Card.java +++ b/src/Card.java @@ -1,5 +1,114 @@ -public class Card { - // Create the rest of this class yourself +public class Card{ + + private int quantity; + private int color; + private int shading; + private int shape; + + private String quan; + private String col; + private String shad; + private String shap; + + public Card(int cardQuantity, int cardColor, int cardShading, int cardShape) { + if (cardQuantity < 1 || cardQuantity > 3) { + quantity = (((cardQuantity % 3) + 3) % 3) + 1; + } + else { + quantity = cardQuantity; + } + if (cardColor < 1 || cardColor > 3) { + color = (((cardColor % 3) + 3) % 3) + 1; + } + else { + color = cardColor; + } + if (cardShading < 1 || cardShading > 3) { + shading = (((cardShading % 3) + 3) % 3) + 1; + } + else { + shading = cardShading; + } + if (cardShape < 1 || cardShape > 3) { + shape = (((cardShape % 3) + 3) % 3) + 1; + } + else { + shape = cardShape; + } + } + + public int getQuantity() { + return quantity; + } + + public int getColor() { + return color; + } + + public int getShading() { + return shading; + } + + public int getShape() { + return shape; + } + + public boolean isSet(Card two, Card three) { + + int totalQuantity = getQuantity() + two.getQuantity() + three.getQuantity(); + int totalColor = getColor() + two.getColor() + three.getColor(); + int totalShading = getShading() + two.getShading() + three.getShading(); + int totalShape = getShape() + two.getShape() + three.getShape(); + + if (totalQuantity % 3 == 0 && totalColor % 3 == 0 && totalShading % 3 == 0 && totalShape % 3 == 0) { + return true; + } + else { + return false; + } + } + + public String toString() { + + if (quantity == 1) { + quan = ("1"); + } + else if (quantity == 2) { + quan = ("2"); + } + else { // (quantity == 3) + quan = ("3"); + } + if (color == 1) { + col = ("R"); //red + } + else if (color == 2) { + col = ("G"); //green + } + else{ //(color == 3) + col = ("P"); //purple + } + if (shading == 1) { + shad = ("O"); //empty + } + else if (shading == 2) { + shad = ("T"); //striped/shaded + } + else{ // (shading == 3) + shad = ("S"); //solid/filled + } + if (shape == 1) { + shap = ("O"); //oval + } + else if (shape == 2) { + shap = ("D"); //diamond + } + else{ // (shading == 3) + shap = ("S"); //squiggle + } + String sequence = quan + col + shad + shap; + return sequence; + } public boolean equals(Object obj) { Card that = (Card)obj; @@ -9,4 +118,5 @@ public boolean equals(Object obj) { shading == that.getShading() && shape == that.getShape(); } + } diff --git a/src/CardMain.class b/src/CardMain.class new file mode 100644 index 0000000..c6450c2 Binary files /dev/null and b/src/CardMain.class differ diff --git a/src/CardMain.java b/src/CardMain.java new file mode 100644 index 0000000..c64622d --- /dev/null +++ b/src/CardMain.java @@ -0,0 +1,14 @@ +public class CardMain{ + public static void main(String[] args){ + + Game g = new Game("81cards.txt"); + System.out.println(g.numCards()); + System.out.println(g.numSets()); + g.playRound(); + System.out.println(g.numSets()); + System.out.println(g.numCards()); + } +} + + + diff --git a/src/CardTest.class b/src/CardTest.class new file mode 100644 index 0000000..0f33998 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..6716a36 --- /dev/null +++ b/src/CardTest.java @@ -0,0 +1,193 @@ +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 test123() + { + //testing when everything is different + Card card1 = new Card(1, 1, 1, 1); + assertEquals(card1.getQuantity(), 1); + assertEquals(card1.getColor(), 1); + assertEquals(card1.getShading(), 1); + assertEquals(card1.getShape(), 1); + assertEquals(card1.toString(), "1ROO"); + Card card2 = new Card(2, 2, 2, 2); + assertEquals(card2.getQuantity(), 2); + assertEquals(card2.getColor(), 2); + assertEquals(card2.getShading(), 2); + assertEquals(card2.getShape(), 2); + assertEquals(card2.toString(), "2GTD"); + Card card3 = new Card(3, 3, 3, 3); + assertEquals(card3.getQuantity(), 3); + assertEquals(card3.getColor(), 3); + assertEquals(card3.getShading(), 3); + assertEquals(card3.getShape(), 3); + assertEquals(card3.toString(), "3PSS"); + assertEquals(card1.isSet(card2, card3), true); + } + public void testStupidNumbers() + { + //testing when all shading is different + Card negCard = new Card(-3, -33, -333, -3333); + assertEquals(negCard.getQuantity(), 1); + assertEquals(negCard.getColor(), 1); + assertEquals(negCard.getShading(), 1); + assertEquals(negCard.getShape(), 1); + assertEquals(negCard.toString(), "1ROO"); + Card lrgCard = new Card(2147483647, 172831, 7910, 457); + assertEquals(lrgCard.getQuantity(), 2); + assertEquals(lrgCard.getColor(), 2); + assertEquals(lrgCard.getShading(), 3); + assertEquals(lrgCard.getShape(), 2); + assertEquals(lrgCard.toString(), "2GSD"); + Card ranCard = new Card(24, 0, -2147483648, 2); + assertEquals(ranCard.getQuantity(), 1); + assertEquals(ranCard.getColor(), 1); + assertEquals(ranCard.getShading(), 2); + assertEquals(ranCard.getShape(), 2); + assertEquals(ranCard.toString(), "1RTD"); + assertEquals(negCard.isSet(lrgCard, ranCard), false); + } + + public void test3() + { + //testing when all the same + Card card4 = new Card(3, 3, 3, 3); + assertEquals(card4.getQuantity(), 3); + assertEquals(card4.getColor(), 3); + assertEquals(card4.getShading(), 3); + assertEquals(card4.getShape(), 3); + assertEquals(card4.toString(), "3PSS"); + Card card5 = new Card(3,3,3,3); + assertEquals(card5.getQuantity(), 3); + assertEquals(card5.getColor(), 3); + assertEquals(card5.getShading(), 3); + assertEquals(card5.getShape(), 3); + assertEquals(card5.toString(), "3PSS"); + Card card6 = new Card(3,3,3,3); + assertEquals(card6.getQuantity(), 3); + assertEquals(card6.getColor(), 3); + assertEquals(card6.getShading(), 3); + assertEquals(card6.getShape(), 3); + assertEquals(card6.toString(), "3PSS"); + assertEquals(card4.isSet(card5, card6), true); + } + + public void test2() + { + Card card1 = new Card(2, 2, 2, 2); + assertEquals(card1.getQuantity(), 2); + assertEquals(card1.getColor(), 2); + assertEquals(card1.getShading(), 2); + assertEquals(card1.getShape(), 2); + assertEquals(card1.toString(), "2GTD"); + Card card2 = new Card(2, 2, 2, 2); + assertEquals(card2.getQuantity(), 2); + assertEquals(card2.getColor(), 2); + assertEquals(card2.getShading(), 2); + assertEquals(card2.getShape(), 2); + assertEquals(card2.toString(), "2GTD"); + Card card3 = new Card(2, 2, 2, 2); + assertEquals(card3.getQuantity(), 2); + assertEquals(card3.getColor(), 2); + assertEquals(card3.getShading(), 2); + assertEquals(card3.getShape(), 2); + assertEquals(card3.toString(), "2GTD"); + assertEquals(card1.isSet(card2, card3), true); + } + + public void test1() + { + Card card1 = new Card(1, 1, 1, 1); + assertEquals(card1.getQuantity(), 1); + assertEquals(card1.getColor(), 1); + assertEquals(card1.getShading(), 1); + assertEquals(card1.getShape(), 1); + assertEquals(card1.toString(), "1ROO"); + Card card2 = new Card(1, 1, 1, 1); + assertEquals(card2.getQuantity(), 1); + assertEquals(card2.getColor(), 1); + assertEquals(card2.getShading(), 1); + assertEquals(card2.getShape(), 1); + assertEquals(card2.toString(), "1ROO"); + Card card3 = new Card(1, 1, 1, 1); + assertEquals(card3.getQuantity(), 1); + assertEquals(card3.getColor(), 1); + assertEquals(card3.getShading(), 1); + assertEquals(card3.getShape(), 1); + assertEquals(card3.toString(), "1ROO"); + assertEquals(card1.isSet(card2, card3), true); + } + + public void testAgainWheeee() + { + Card card1 = new Card(3, 3, 1, 2); + assertEquals(card1.getQuantity(), 3); + assertEquals(card1.getColor(), 3); + assertEquals(card1.getShading(), 1); + assertEquals(card1.getShape(), 2); + assertEquals(card1.toString(), "3POD"); + Card card2 = new Card(1, 2, 3, 3); + assertEquals(card2.getQuantity(), 1); + assertEquals(card2.getColor(), 2); + assertEquals(card2.getShading(), 3); + assertEquals(card2.getShape(), 3); + assertEquals(card2.toString(), "1GSS"); + Card card3 = new Card(2, 1, 2, 1); + assertEquals(card3.getQuantity(), 2); + assertEquals(card3.getColor(), 1); + assertEquals(card3.getShading(), 2); + assertEquals(card3.getShape(), 1); + assertEquals(card3.toString(), "2RTO"); + assertEquals(card1.isSet(card2, card3), true); + } + + public void testAgainAgain() + { + Card card1 = new Card(3, 3, 1, 23); + assertEquals(card1.getQuantity(), 3); + assertEquals(card1.getColor(), 3); + assertEquals(card1.getShading(), 1); + assertEquals(card1.getShape(), 3); + assertEquals(card1.toString(), "3POS"); + Card card2 = new Card(1, 2, 3, 3); + assertEquals(card2.getQuantity(), 1); + assertEquals(card2.getColor(), 2); + assertEquals(card2.getShading(), 3); + assertEquals(card2.getShape(), 3); + assertEquals(card2.toString(), "1GSS"); + Card card3 = new Card(2, 1, 2, 1); + assertEquals(card3.getQuantity(), 2); + assertEquals(card3.getColor(), 1); + assertEquals(card3.getShading(), 2); + assertEquals(card3.getShape(), 1); + assertEquals(card3.toString(), "2RTO"); + assertEquals(card1.isSet(card2, card3), false); + } + + public void testEqualsMethod() + { + Card card1 = new Card(1, 1, 1, 1); + Card card2 = new Card(1, 1, 1, 1); + Card card3 = new Card(2, 2, 2, 2); + + assertTrue(card1.equals(card2)); + assertTrue(card2.equals(card1)); + assertFalse(card1.equals(card3)); + assertFalse(card3.equals(card1)); + assertFalse(card2.equals(card3)); + assertFalse(card3.equals(card2)); + } + +} \ No newline at end of file diff --git a/src/Deck.class b/src/Deck.class new file mode 100644 index 0000000..cc8e77d 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..1fb35be --- a/src/Deck.java +++ b/src/Deck.java @@ -1,13 +1,55 @@ +import java.util.Collections; import java.io.BufferedReader; import java.io.FileReader; import java.util.StringTokenizer; import java.util.ArrayList; + public class Deck { - // Implement the rest of this class yourself + private int nextCardIndex = 0; + private ArrayList cards = new ArrayList(81); + + public Deck(){ + for (int i=1; i<=3; i++) { + for (int j=1; j<=3; j++) { + for (int k=1; k<=3; k++) { + for (int m=1; m<=3; m++) { + Card card = new Card(i, j, k, m); + cards.add(card); + } + } + } + } + Collections.shuffle(cards); + } + + public int getSize(){ + return cards.size(); + } + + public boolean hasNext() { + if (nextCardIndex < cards.size()) { + return true; + } + else { + return false; + } + } + + public Card getNext() { + //returns card + + if (hasNext() == true) { + nextCardIndex +=1; + return cards.get(nextCardIndex-1); + } + + else{ + return null; + } + } public Deck(String filename) { - cards = new ArrayList(81); try { String line; @@ -42,4 +84,6 @@ public Deck(String filename) { throw new RuntimeException("Error while reading file: " + e.toString()); } } + + } diff --git a/src/DeckTest.class b/src/DeckTest.class new file mode 100644 index 0000000..e5498fc 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..5c12011 --- /dev/null +++ b/src/DeckTest.java @@ -0,0 +1,42 @@ +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 testEmptyDeck() { + Deck d = new Deck("EmptyDeck.txt"); + assertEquals(d.hasNext(), false); + } + + public void testAllCards() { + Deck d = new Deck("81cards.txt"); + for (int i=0; i<81; i++) { + assertEquals(d.hasNext(), true); + d.getNext(); + } + d.getNext(); + assertEquals(d.hasNext(), false); + } + + public void test3Cards() { + Deck d = new Deck("3cards.txt"); + for (int i=0; i<3; i++) { + assertEquals(d.hasNext(), true); + d.getNext(); + } + d.getNext(); + assertEquals(d.hasNext(), false); + } + + +} diff --git a/src/EmptyDeck.text b/src/EmptyDeck.text new file mode 100644 index 0000000..e69de29 diff --git a/src/Game.class b/src/Game.class new file mode 100644 index 0000000..4068448 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..591d2fa --- /dev/null +++ b/src/Game.java @@ -0,0 +1,181 @@ +public class Game{ + private Deck d; + private Table t; + + public Game(){ + //begins with randomly generated deck of 12 cards + + d = new Deck(); + t = new Table(); + + for (int i = 0; i<12; i++){ + t.add(d.getNext()); + } + } + + public Game(String filename){ + //loads specific deck + + d = new Deck(filename); + t = new Table(); + + while (d.hasNext()){ + t.add(d.getNext()); + + if (t.numCards() == 12){ + return; + } + } + } + + public int numSets(){ + //returns number of sets on the table + return t.numSets(); + } + + public int numCards(){ + + //returns number of cards on table which is 12 + //or less if deck has lest than 12 cards + return t.numCards(); + } + + public void playRound(){ + //used to advance game from one round to the next + // + //a round deals with removing one set + //and adding additional cards + // + //1.)when no sets on table but still cards in deck: + // this means just add 3 cards + //2.)most of the time: + // cards in deck and 12 on table containing 1+ sets + // in this situation we move from one round + // to the next by removing one set and adding 3 cards + // to the table from the deck + //3.)if a round starts with 12+ cards on table: + // (due to no sets) + // and theres is at least one set, + // then after removing a set we do not add cards + //4.)if there is at least one set but no cards left: + // we move to next round by just removing set + //5.)if no cards in deck or sets on table: + // game over + // + //6.)sometimes when you use custom decks: + // there may be only 1-2 cards remaining + // when you have to add cards to table + // in this case cards are added like normal + // and the table remains short a few cards + + int sets = t.numSets(); + + //1.) + if (sets == 0 && d.hasNext() == true){ + for (int i = 0; i < 3; i++){ + if (d.hasNext() == false){ + return; + } + t.add(d.getNext()); + } + return; + } + + + //2.) + if (sets > 0 && d.hasNext() == true){ + //call the number of cards in the begining to make it quicker + int numCards = t.numCards(); + //go through the table looking for the set + for (int i=0; i 12 && sets > 0){ + //call the number of cards in the begining to make it quicker + int numCards = t.numCards(); + //go through the table looking for the set + for (int i=0; i 0 && d.hasNext() == false){ + //call the number of cards in the begining to make it quicker + int numCards = t.numCards(); + //go through the table looking for the set + for (int i=0; i