diff --git a/src/Card.java b/src/Card.java old mode 100755 new mode 100644 index 9eed9a5..a7ed9ed --- a/src/Card.java +++ b/src/Card.java @@ -1,6 +1,102 @@ public class Card { + private int quantity; + private int color; + private int shading; + private int shape; // Create the rest of this class yourself + public Card (int isQuant, int isCol, int isShad, int isShap) { + quantity = fixValue(isQuant); + color = fixValue(isCol); + shading = fixValue(isShad); + shape = fixValue(isShap); + + } + + private int fixValue(int valueToFix) { + if (valueToFix < 1 || valueToFix > 3) + return (((valueToFix % 3) + 3) % 3) + 1; + else + return valueToFix; + } + + + public int getQuantity() { + + return quantity; + } + + public int getColor() { + + return color; + } + + public int getShading() { + + return shading; + } + + public int getShape() { + + return shape; + } + + public boolean isSet(Card c1, Card c2) { + if ((((quantity + c1.getQuantity() + c2.getQuantity()) % 3) == 0) && + (((color + c1.getColor() + c2.getColor()) % 3) == 0) && + (((shading + c1.getShading() + c2.getShading()) % 3) == 0) && + (((shape + c1.getShape() + c2.getShape()) % 3) == 0)) { + return true; + } + else { + return false; + } + + + } + + public String toString() { + String str = ""; + str += quantity; + if (color == 1) { + str += "R"; + } + + if (color == 2) { + str += "G"; + } + + if (color == 3) { + str += "P"; + } + + if (shading == 1) { + str += "O"; + } + + if (shading == 2) { + str += "T"; + } + + if (shading == 3) { + str += "S"; + } + + if (shape == 1) { + str += "O"; + } + + if (shape == 2) { + str += "D"; + } + + if (shape == 3) { + str += "S"; + } + return str; + + } + public boolean equals(Object obj) { Card that = (Card)obj; diff --git a/src/CardTest.java b/src/CardTest.java new file mode 100644 index 0000000..589c8c5 --- /dev/null +++ b/src/CardTest.java @@ -0,0 +1,44 @@ +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 testCardWorks() { + Card c1 = new Card(1,2,3,2); + assertEquals("1GSD",c1.toString()); + + Card c2 = new Card(2,3,1,3); + assertEquals("2POS",c2.toString()); + + Card c3 = new Card(3,1,2,1); + assertEquals("3RTO",c3.toString()); + + + + + } + + public void testTruthSet() { + Card c1 = new Card(1,2,3,2); + Card c2 = new Card(2,3,1,3); + Card c3 = new Card(3,1,2,1); + assertEquals(true, c1.isSet(c2, c3)); + } + + public void testFalseSet() { + Card c1 = new Card(1,3,3,1); + Card c2 = new Card(3,3,3,3); + Card c3 = new Card(3,2,1,3); + assertEquals(false, c1.isSet(c2, c3)); +} +} diff --git a/src/Deck.java b/src/Deck.java old mode 100755 new mode 100644 index ab3a2a3..44ec0be --- a/src/Deck.java +++ b/src/Deck.java @@ -2,9 +2,44 @@ import java.io.FileReader; import java.util.StringTokenizer; import java.util.ArrayList; +import java.util.Collections; public class Deck { // Implement the rest of this class yourself + private ArrayList cards = new ArrayList(81); + private int nextCardIndex = 0; + + public Deck() { + for ( int i = 1; i <= 3; i++) { + for (int a = 1; a <= 3; a++) { + for (int b = 1; b <= 3; b++) { + for (int c = 1; c<=3; c++) { + Card card = new Card(i,a,b,c); + } + } + } + } + Collections.shuffle(cards); + } + + public boolean hasNext() { + if (nextCardIndex < cards.size()) { + return true; + } + else { + return false; + } + } + + public Card getNext() { + if (hasNext() == false) { + return null; + } + else { + nextCardIndex += 1; + return cards.get(nextCardIndex - 1); + } + } public Deck(String filename) { cards = new ArrayList(81); diff --git a/src/DeckTest.java b/src/DeckTest.java new file mode 100644 index 0000000..cf37f0d --- /dev/null +++ b/src/DeckTest.java @@ -0,0 +1,34 @@ +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 testDeckCard() { + Deck d = new Deck("threeCards.dat"); + Card c1 = new Card(1,2,3,2); + + assertEquals(true, d.getNext().equals(c1)); + assertEquals(true, d.hasNext()); + + Card c2 = new Card(2,3,1,3); + + assertEquals(true, d.getNext().equals(c2)); + assertEquals(true, d.hasNext()); + + Card c3 = new Card(3,1,2,1); + + assertEquals(true, d.getNext().equals(c3)); + assertEquals(false, d.hasNext()); + } + +} diff --git a/src/Game.java b/src/Game.java new file mode 100644 index 0000000..88877b7 --- /dev/null +++ b/src/Game.java @@ -0,0 +1,99 @@ +public class Game { + private Table t; + private Deck d; + + public Game() { + + d = new Deck(); + t = new Table(); + + for (int i = 0; i < 12; i++) { + t.add(d.getNext()); + } + } + + public Game(String cardGame) { + + t = new Table(); + d = new Deck(cardGame); + 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() { + //worked with Hailey Kester on the playRound in lab + + if (t.numSets() == 0 && d.hasNext() == true) { + for (int i = 0; i < 3; i++) { + if(d.hasNext() == true) { + t.add(d.getNext()); + } + } + return; + } + + if (t.numSets() > 0) { + for (int a = 0; a < t.numCards() - 2; a++) { + for (int b = a + 1; b < t.numCards() - 1; b++) { + for (int c = b + 1; c < t.numCards(); c++) { + Card c1 = t.getCard(a); + Card c2 = t.getCard(b); + Card c3 = t.getCard(c); + + if (c1.isSet(c2,c3)) { + t.removeSet(c1, c2, c3); + if (t.numCards() < 12 && d.hasNext()) { + for (int i = 0; i < 3; i++) { + if(d.hasNext() == true) { + t.add(d.getNext()); + } + return; + } + } + } + } + } + } + } + } + + + + + + + + + + + + public boolean isGameOver() { + + if (d.hasNext() == false && t.numSets() == 0) { + 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..f26dfe5 --- /dev/null +++ b/src/GameTest.java @@ -0,0 +1,24 @@ +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 testGameRound() { + + Game g1 = new Game(); + assertEquals(g1.numCards(), 12); + g1.playRound(); + + } + +} diff --git a/src/MonteCarloSimulation.java b/src/MonteCarloSimulation.java new file mode 100644 index 0000000..a470350 --- /dev/null +++ b/src/MonteCarloSimulation.java @@ -0,0 +1,26 @@ +public class MonteCarloSimulation { + + public static void main(String[] args) { + + double avgSets = 0; + int a = 0; + while (a <= 1000001) { + Game g1 = new Game(); + avgSets += g1.numSets(); + } + System.out.println("Avg # of sets is " + avgSets); + + double avgCards = 0; + int b = 0; + while (b <= 100001) { + Game g2 = new Game(); + while(g2.isGameOver() == false) { + g2.playRound(); + } + avgCards += g2.numCards(); + a++; + } + System.out.println("Avg # of cards is " + avgCards); + } +} + \ No newline at end of file diff --git a/src/Table.java b/src/Table.java new file mode 100644 index 0000000..0079c60 --- /dev/null +++ b/src/Table.java @@ -0,0 +1,139 @@ +public class Table { + private TableNode head; + private int isLength; + + public Table() { + head = null; + isLength = 0; + } + + public void add(Card card) { + TableNode node = new TableNode(card); + isLength += 1; + if (head == null) { + head = node; + } + else { + node.setNext(head); + head = node; + } + } + + private boolean onTable(Card card) { + //got help from Hailey Kester in lab + TableNode temp = head; + while (temp != null) { + if (temp.getCard().equals(card)) { + return true; + } + temp = temp.getNext(); + } + return false; + } + + private void removeCard(Card card) { + //help from hailey kester in lab + TableNode temp = findTemp(card); + isLength -= 1; + if (temp == null) { + head = head.getNext(); + } + else { + TableNode john = temp.getNext(); + temp.setNext(john.getNext()); + } + } + + private TableNode findTemp(Card card) { + //help from hailey kester in lab + TableNode temp = null; + TableNode john = head; + while (john != null) { + Card johnCard = john.getCard(); + if (johnCard.equals(card)) { + return temp; + } + temp = john; + john = john.getNext(); + } + return null; + } + + + public void removeSet(Card c1, Card c2 , Card c3) { + //got help from Hailey Kester in lab + TableNode temp = head; + TableNode john = null; + + 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 temp = head; + int card = 0; + if (head == null) { + return 0; + } + else { + while (temp != null) { + temp = temp.getNext(); + card += 1; + } + return card; + + + } + } + + public Card getCard(int index) { + TableNode temp = head; + if (index >= isLength || temp == null) { + return null; + } + + if (index == 0) { + return temp.getCard(); + } + + else { + for (int i = 0; i < index; i++) { + temp = temp.getNext(); + } + return temp.getCard(); + } + } + + public int numSets() { + int theSet = 0; + for (int a = 0; a < isLength - 2; a++) { + for (int b = a + 1; b < isLength - 1; b++) { + for (int c = b + 1; c < isLength; c++) { + Card c1 = getCard(a); + Card c2 = getCard(b); + Card c3 = getCard(c); + + if(c1.isSet(c2, c3)) { + theSet += 1; + } + } + } + } + return theSet; + } +} \ No newline at end of file diff --git a/src/TableNode.java b/src/TableNode.java new file mode 100644 index 0000000..92520c3 --- /dev/null +++ b/src/TableNode.java @@ -0,0 +1,27 @@ +public class TableNode { + private Card card; + private TableNode next; + + public TableNode(Card c1) { + + card = c1; + 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..320cdb9 --- /dev/null +++ b/src/TableNodeTest.java @@ -0,0 +1,35 @@ +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 testTableNodeCards() { + Card c1 = new Card(1, 2, 3, 2); + TableNode n1 = new TableNode(c1); + assertEquals(null, n1.getNext()); + assertEquals(c1, n1.getCard()); + + Card c2 = new Card(2, 3, 1, 3); + TableNode n2 = new TableNode(c2); + assertEquals(null, n2.getNext()); + assertEquals(c2, n2.getCard()); + + Card c3 = new Card(1, 3, 2, 3); + TableNode n3 = new TableNode(c3); + assertEquals(null, n3.getNext()); + assertEquals(c3, n3.getCard()); + + + } + +} diff --git a/src/TableTest.java b/src/TableTest.java new file mode 100644 index 0000000..063dfff --- /dev/null +++ b/src/TableTest.java @@ -0,0 +1,34 @@ +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 testTable() { + Table t = new Table(); + + Card c1 = new Card(1,2,3,2); + t.add(c1); + + Card c2 = new Card(2,3,1,3); + t.add(c2); + + Card c3 = new Card(1,3,2,3); + t.add(c3); + + assertEquals(3, t.numCards()); + assertEquals(true, t.getCard(0).equals(c3)); + assertEquals(true, t.getCard(1).equals(c2)); + assertEquals(true, t.getCard(2).equals(c1)); + } + +} diff --git a/src/threeCards.dat b/src/threeCards.dat new file mode 100644 index 0000000..e7593df --- /dev/null +++ b/src/threeCards.dat @@ -0,0 +1,3 @@ +1 2 3 2 +2 3 1 3 +3 1 2 1 \ No newline at end of file