diff --git a/src/Card.java b/src/Card.java old mode 100755 new mode 100644 index 9eed9a5..bb8d36e --- a/src/Card.java +++ b/src/Card.java @@ -1,12 +1,97 @@ public class Card { - // Create the rest of this class yourself - public boolean equals(Object obj) { - Card that = (Card)obj; + private int quantity; + private int color; + private int shading; + private int shape; + private String zing = ""; - return quantity == that.getQuantity() && - color == that.getColor() && - shading == that.getShading() && - shape == that.getShape(); - } + public Card( int Quantity, int Color, int Shading, int Shape) { + quantity = repairValue(Quantity); + color = repairValue(Color); + shading = repairValue(Shading); + shape = repairValue(Shape); + } + private int repairValue( int valueToRepair) { + if (valueToRepair < 1 || valueToRepair > 3) + return (((valueToRepair % 3) + 3) % 3) + 1; + else + return valueToRepair; + } + public int getQuantity() { + return quantity; + } + public int getColor() { + return color; + } + public int getShading() { + return shading; + } + public int getShape () { + return shape; + } + public boolean isSet( Card card2, Card card3) { + if(((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)) + { + return true; + } + + else + { + return false; + } + } + public String toString() { + + if (quantity == 1) { + zing += 1; + } + else if (quantity == 2) { + zing += 2; + } + else if (quantity == 3) { + zing += 3; + } + if (color == 1) { + zing += "R"; + } + else if (color == 2) { + zing += "G"; + } + else if (color == 3) { + zing += "P"; + } + if (shading == 1) { + zing += "O"; + } + else if (shading == 2) { + zing += "T"; + } + else if (shading == 3) { + zing += "S"; + } + if (shape == 1) { + zing += "O"; + } + else if (shape == 2) { + zing += "D"; + } + else if (shape == 3) { + zing += "S"; + } + return zing; + } + public boolean equals(Object obj) { + Card that = (Card)obj; + return quantity == that.getQuantity() && + color == that.getColor() && + shading == that.getShading() && + shape == that.getShape(); + } } + + + \ No newline at end of file diff --git a/src/CardTest.java b/src/CardTest.java new file mode 100644 index 0000000..3ced431 --- /dev/null +++ b/src/CardTest.java @@ -0,0 +1,54 @@ +import junit.framework.TestCase; + + +public class CardTest extends TestCase { + + public void testNewCard() { + Card z1 = new Card( 1, 2, 3, 3); + + assertEquals(1, z1.getQuantity()); + assertEquals(2, z1.getColor()); + assertEquals(3, z1.getShading()); + assertEquals(3, z1.getShape()); + } + public void testTwoCards() { + Card z1 = new Card(1,1,1,1); + Card z2 = new Card(1,1,1,1); + + assertEquals(z1, z2); + } + public void testSameColor() { + Card z1 = new Card(6, 2, 3, 3); + Card z2 = new Card(5, 2, 4, 2); + Card z3 = new Card(4, 2, 3, 1); + + assertEquals(false, z1.isSet(z2,z3)); + } + public void testSameShape() { + Card z1 = new Card(1, 2, 3, 3); + Card z2 = new Card(2, 2, 3, 3); + Card z3 = new Card(3, 2, 3, 3); + + assertEquals(true, z1.isSet(z2,z3)); + } + public void testSameShading() { + Card z1 = new Card(12, 3, 3, 3); + Card z2 = new Card(12, 3, 3, 2); + Card z3 = new Card(12, 3, 3, 1); + + assertEquals(true, z1.isSet(z2,z3)); + } + public void testNegativeValue() { + Card z1 = new Card(6, 2, 3, -34); + + assertEquals(1, z1.getQuantity()); + assertEquals(2, z1.getColor()); + assertEquals(3, z1.getShading()); + assertEquals(3, z1.getShape()); + } + public void testtoString() { + Card z1 = new Card(2, 2, 2, 2); + + assertEquals("2GTD", z1.toString()); + } +} diff --git a/src/Deck.java b/src/Deck.java old mode 100755 new mode 100644 index ab3a2a3..e6b8af3 --- a/src/Deck.java +++ b/src/Deck.java @@ -2,9 +2,26 @@ 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 int nextCardIndex; + private ArrayList cards; + + public Deck() { + cards = new ArrayList(81); + for (int quantity = 1; quantity <= 3; quantity++) { + for(int color = 1; color <= 3; color++) { + for(int shading = 1; shading <= 3; shading++) { + for(int shape = 1; shape <= 3; shape++) { + Card r = new Card(quantity, color, shading, shape); + cards.add(r); + } + } + } + } + Collections.shuffle(cards); + } public Deck(String filename) { cards = new ArrayList(81); @@ -13,7 +30,7 @@ public Deck(String filename) { 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(); @@ -25,7 +42,7 @@ public Deck(String filename) { // ignore comments if(line.startsWith("#")) continue; - + // a valid line contains 4 integers StringTokenizer tokenizer = new StringTokenizer(line); @@ -42,4 +59,22 @@ public Deck(String filename) { throw new RuntimeException("Error while reading file: " + e.toString()); } } + + public boolean hasNext() { + if(nextCardIndex < cards.size()) { + return true; + } + else { + return false; + } + } + public Card getNext() { + if(hasNext() != false) { + nextCardIndex += 1; + return cards.get(nextCardIndex -1); + } + else { + return null; + } + } } diff --git a/src/DeckTest.java b/src/DeckTest.java new file mode 100644 index 0000000..7a5e8ae --- /dev/null +++ b/src/DeckTest.java @@ -0,0 +1,25 @@ +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 testtwoCard() { + Deck z = new Deck("twoCard.dat"); + + assertEquals("1ROO", z.getNext().toString()); + assertEquals("1ROD", z.getNext().toString()); + assertTrue(z.hasNext()); + assertFalse(z.hasNext()); + } + +} diff --git a/src/Game.java b/src/Game.java new file mode 100644 index 0000000..bd4d67a --- /dev/null +++ b/src/Game.java @@ -0,0 +1,81 @@ +public class Game { + private Table t; + private Deck d; + + public Game() { + t = new Table(); + d = new Deck(); + + for(int i = 0; i < 12; i++) + if(d.hasNext() != false) { + t.add(d.getNext()); + } + } + + public Game(String filename) { + + d = new Deck(filename); + t = new Table(); + for( int i = 0; i<12; i++) { + if(d.hasNext() == true) + t.add(d.getNext()); + } + } + + public int numSets() { + + return t.numSets(); + } + + public int numCards() { + + return t.numCards(); + } + + public void playRound() { + if(t.numSets() == 0 && d.hasNext() != false){ + for( int i = 0; i < 3; i++) + t.add(d.getNext()); + return; + } + boolean foundSet = false; + for( int i = 0; i < numCards() -2; i++) { + if(foundSet) + break; + for(int j = i +1; j < numCards() -1; j++) { + if( foundSet) + break; + for(int k = j +1; k < numCards(); k++) { + Card wolverine = t.getCard(i); + Card magneto = t.getCard(j); + Card professorx = t.getCard(k); + + if (wolverine.isSet(magneto, professorx)) { + t.removeSet(wolverine, magneto, professorx); + foundSet = true; + break; + } + } + } + } + if (d.hasNext() == true && numCards() < 12) { + for( int w = 0; w < 3; w++) { + if(d.hasNext() == true) { + + 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.java b/src/GameTest.java new file mode 100644 index 0000000..d7718c3 --- /dev/null +++ b/src/GameTest.java @@ -0,0 +1,11 @@ +import junit.framework.TestCase; + +public class GameTest extends TestCase { + + public void testAutoTable() { + Game samsung = new Game(); + + assertEquals(12, samsung.numCards()); + } + +} diff --git a/src/Set Game Write up .docx b/src/Set Game Write up .docx new file mode 100644 index 0000000..af1dd8b Binary files /dev/null and b/src/Set Game Write up .docx differ diff --git a/src/Table.java b/src/Table.java new file mode 100644 index 0000000..0069e6e --- /dev/null +++ b/src/Table.java @@ -0,0 +1,113 @@ +public class Table { + private TableNode head; + + public Table() { + head = null; + } + + public void add(Card wifi) { + TableNode temp = new TableNode(wifi); + temp.setNext(head); + head = temp; + } + + + private boolean onTable(Card z) { + TableNode curr = head; + + while(curr != null) { + if(curr.getCard().equals(z)) { + return true; + } + curr = curr.getNext(); + } + return false; + } + + public void removeSet(Card z1, Card z2, Card z3) { + if(! z1.isSet(z2,z3)) + return; + if(! onTable(z1)) + return; + if(! onTable(z2)) + return; + if( ! onTable(z3)) + return; + removeCard(z1); + removeCard(z2); + removeCard(z3); + } + public TableNode findPrev(Card z) { + TableNode jeans = head; + TableNode yoga = null; + + while( jeans!= null) { + if(z.equals(jeans.getCard())) { + return yoga; + } + else { + yoga = jeans; + jeans = jeans.getNext(); + } + } + return null; + } + + private void removeCard(Card z) { + TableNode prev = findPrev(z); + + if(prev == null) + head = head.getNext(); + else { + TableNode curr = prev.getNext(); + prev.setNext(curr.getNext()); + } + } + + public int numCards() { + TableNode curr = head; + int plum = 0; + while( curr != null) { + plum +=1; + curr = curr.getNext(); + } + return plum; + } + + public Card getCard( int index) { + TableNode z = head; + + if(z == null) { + return null; + } + + for(int i = 0; i < index; i++) { + z = z.getNext(); + + if(z == null) + return null; + } + return z.getCard(); + } + + public int numSets() { + int length = numCards(); + int yeet = 0; + + for( int i = 0; i < length -2; i++) { + for(int j = i +1; j < length-1; j++) { + for(int k = j +1; k < length; k++) { + Card z1 = getCard(i); + Card z2 = getCard(j); + Card z3 = getCard(k); + + if (z1.isSet(z2,z3)) { + yeet +=1; + + } + } + } + } + return yeet; + } +} diff --git a/src/TableNode.java b/src/TableNode.java new file mode 100644 index 0000000..ac8e2bd --- /dev/null +++ b/src/TableNode.java @@ -0,0 +1,27 @@ +public class TableNode { + + private Card card; + private TableNode next; + + public TableNode(Card z) { + card = z; + next = null; + + } + + public void setNext(TableNode b) { + + next = b; + } + + public TableNode getNext() { + + return next; + + } + + public Card getCard() { + + return card; + } +} \ No newline at end of file diff --git a/src/TableTest.java b/src/TableTest.java new file mode 100644 index 0000000..475cca9 --- /dev/null +++ b/src/TableTest.java @@ -0,0 +1,64 @@ +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 testAddCards() { + Table t = new Table(); + + Card c1 = new Card(1, 1, 1, 1); + Card c2 = new Card(1, 2, 1, 1); + Card c3 = new Card(1, 3, 1, 1); + + t.add(c1); + assertEquals(1, t.numCards()); + t.add(c2); + assertEquals(2, t.numCards()); + t.add(c3); + assertEquals(3, t.numCards()); + } + public void testRemoveOneSet() { + Table t = new Table(); + + Card c1 = new Card(1, 1, 1, 1); + Card c2 = new Card(1, 2, 1, 1); + Card c3 = new Card(1, 3, 1, 1); + Card c4 = new Card(3, 1, 3, 1); + + t.add(c1); + t.add(c2); + t.add(c3); + t.add(c4); + + assertEquals(1, t.numSets()); + assertEquals(4, t.numCards()); + assertTrue(c1.isSet(c2, c3)); + + t.removeSet(c1, c2, c3); + } + public void testGetCard() { + Card c1 = new Card(1, 1, 1, 1); + Card c2 = new Card(2, 2, 2, 2); + Card c3 = new Card(3, 3, 3, 3); + + Table z = new Table(); + + z.add(c1); + z.add(c2); + z.add(c3); + + assertEquals("3PSS", z.getCard(0).toString()); + assertEquals("2GTD", z.getCard(1).toString()); + assertEquals("1ROO", z.getCard(2).toString()); + } +}