diff --git a/src/Card2.java b/src/Card2.java new file mode 100644 index 0000000..cc029da --- /dev/null +++ b/src/Card2.java @@ -0,0 +1,125 @@ +public class Card +{ + public int quantity = 0; + public int color = 0; + public int shading = 0; + public int shape = 0; + + public Card(int quan, int col, int shade, int shap) + { + if(quan < 1 || quan > 3) + { + if(quan <0) + quantity = (((quan%3) +3) %3)+1; + else + quantity = (quan % 3)+1; + } + else + quantity = quan; + + if(col < 1 || col > 3) + { + if(col <0) + color = (((col % 3) + 3) % 3)+1; + else + color = (col % 3)+1; + } + else + color = col; + + if(shade < 1 || shade > 3) + { + if(shade <0) + shading = (((shade % 3) + 3) % 3)+1; + else + shading = (shade % 3)+1; + } + else + shading = shade; + + if(shap < 1 || shap > 3) + { + if(shap <0) + shape = (((shap % 3) + 3) % 3)+1; + else + shape = (shap % 3)+1; + } + else + shape = shap; + } + public int getQuantity() + { + return quantity; + } + public int getColor() + { + return color; + } + public int getShading() + { + return shading; + } + public int getShape() + { + return shape; + } + 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 card2, Card card3) + { + boolean quanCheck = false; + boolean colCheck = false; + boolean shadeCheck = false; + boolean shapeCheck = false; + + if(((quantity + card2.getQuantity() + card3.getQuantity() )%3) == 0) + quanCheck = true; + if(((color + card2.getColor() + card3.getColor() )%3) == 0) + colCheck = true; + if(((shading + card2.getShading() + card3.getShading() )%3) == 0) + shadeCheck = true; + if(((shape + card2.getShape() + card3.getShape() )%3) == 0) + shapeCheck = true; + + return (quanCheck == true && colCheck == true && shadeCheck == true && shapeCheck == true); + } + + public String toString() + { + //Quantity + String info = "" + quantity; + + //Color + if(color == 1) + info += "R"; + else if(color == 2) + info += "G"; + else if(color == 3) + info += "P"; + + //Shading + if(shading == 1) + info += "O"; + else if(shading == 2) + info += "T"; + else if(shading == 3) + info += "S"; + + //Shape + if(shape == 1) + info += "O"; + else if(shape == 2) + info += "D"; + else if(shape == 3) + info += "S"; + + return info; + } +} diff --git a/src/CardTest.java b/src/CardTest.java new file mode 100644 index 0000000..6445982 --- /dev/null +++ b/src/CardTest.java @@ -0,0 +1,64 @@ +import junit.framework.TestCase; + +public class CardTest extends TestCase +{ + Card a = new Card(1,1,1,1); + Card b = new Card(2,2,3,3); + Card c = new Card(1,2,3,1); + Card d = new Card(-4,-5,6,7); + + public void testGetQuantity() + { + assertEquals(1,a.getQuantity()); + assertEquals(2,b.getQuantity()); + assertEquals(1,c.getQuantity()); + assertEquals(3,d.getQuantity()); + } + public void testGetColor() + { + assertEquals(1,a.getColor()); + assertEquals(2,b.getColor()); + assertEquals(2,c.getColor()); + assertEquals(2,d.getColor()); + } + public void testGetShading() + { + assertEquals(1,a.getShading()); + assertEquals(3,b.getShading()); + assertEquals(3,c.getShading()); + assertEquals(1,d.getShading()); + } + public void testGetShape() + { + assertEquals(1,a.getShape()); + assertEquals(3,b.getShape()); + assertEquals(1,c.getShape()); + assertEquals(2,d.getShape()); + } + public void testEquals() + { + a = b; + assertEquals(true,a.equals(b)); + assertEquals(false,c.equals(d)); + } + public void testIsString() + { + a = new Card(1,1,1,1); + b = new Card(2,2,2,2); + c = new Card(3,3,3,3); + + assertEquals("1ROO",a.toString()); + assertEquals("2GTD",b.toString()); + assertEquals("3PSS",c.toString()); + } + public void testIsSet() + { + a = new Card(1,1,1,1); + b = new Card(2,2,2,2); + c = new Card(3,3,3,3); + d = new Card(1,2,3,1); + + assertEquals(true,a.isSet(b,c)); + assertEquals(false,a.isSet(c,d)); + } +} diff --git a/src/Deck2.java b/src/Deck2.java new file mode 100644 index 0000000..fd0ffcd --- /dev/null +++ b/src/Deck2.java @@ -0,0 +1,84 @@ +import java.io.BufferedReader; +import java.io.FileReader; +import java.util.StringTokenizer; +import java.util.ArrayList; +import java.util.Collections; + +public class Deck +{ + private int nextCardIndex = 0; + private ArrayList cards = new ArrayList(81); + + public Deck() + { + for(int quan = 1; quan < 4; quan++) + { + for(int col = 1; col < 4; col++) + { + for(int shade = 1; shade < 4; shade++) + { + for(int shap = 1; shap < 4; shap++) + { + Card card = new Card(quan,col,shade,shap); + cards.add(card); + } + } + } + } + nextCardIndex = 0; + Collections.shuffle(cards); + } + public boolean hasNext() + { + if(nextCardIndex < cards.size()) + return true; + else + return false; + } + public Card getNext() + { + if(hasNext() == false) + return null; + nextCardIndex ++; + return cards.get(nextCardIndex-1); + } + + public Deck(String filename) + { + 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()); + } + } +} diff --git a/src/DeckTest.java b/src/DeckTest.java new file mode 100644 index 0000000..2bffc4b --- /dev/null +++ b/src/DeckTest.java @@ -0,0 +1,25 @@ +import junit.framework.TestCase; + +public class DeckTest extends TestCase +{ + Deck d1 = new Deck(); + Card c1 = new Card(1,1,1,1); + + public void testHasNext() + { + assertEquals(true,d1.hasNext()); + while(d1.getNext() != null) + { + c1 = d1.getNext(); + } + assertEquals(false,d1.hasNext()); + } + public void testGetNext() + { + while(d1.hasNext() == true) + { + c1 = d1.getNext(); + } + assertEquals(null,d1.getNext()); + } +} diff --git a/src/Game.java b/src/Game.java new file mode 100644 index 0000000..158df94 --- /dev/null +++ b/src/Game.java @@ -0,0 +1,100 @@ +public class Game +{ + private int removedCards = 0; + private int sets = 0; + private int numRounds = 0; + Table t = new Table(); + Deck d; + + public Game() + { + d = new Deck(); + for(int i=0; i<12; i++) + { + if(d.hasNext()) + t.add(d.getNext()); + } + } + + public Game(String filename) + { + d = new Deck(filename); + for(int i=0; i<12; i++) + { + if(d.hasNext()) + t.add(d.getNext()); + } + } + + public int numSets() + { + return t.numSets(); + } + + public int numCards() + { + return t.numCards(); + } + + public void playRound() + { + if(isGameOver() == false) + { + if(t.numSets() > 0) + { + Card a = null; + Card b = null; + Card c = null; + boolean findSet = false; + + for(int i=0; i= 0 && index < cardsOnTable) + for(int i=0; i < index; i++) + current = current.getNext(); + else + return null; + return current.getCard(); + } + public void removeCard(Card remove) + { + TableNode current = head; + for(int i=0; i < numCards(); i++) + { + if((current.getCard()).equals(remove)) + { + head = current.getNext(); + cardsOnTable--; + return; + } + if(((current.getNext()).getCard()).equals(remove)) + { + current.setNext((current.getNext()).getNext()); + cardsOnTable--; + return; + } + current = current.getNext(); + } + } +} diff --git a/src/TableNode.java b/src/TableNode.java new file mode 100644 index 0000000..e2835ed --- /dev/null +++ b/src/TableNode.java @@ -0,0 +1,24 @@ +public class TableNode +{ + private Card card; + private 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; + } +} diff --git a/src/TableNodeTest.java b/src/TableNodeTest.java new file mode 100644 index 0000000..8e075f8 --- /dev/null +++ b/src/TableNodeTest.java @@ -0,0 +1,30 @@ +import junit.framework.TestCase; + +public class TableNodeTest extends TestCase +{ + Card card1 = new Card(1,1,1,1); + TableNode node1 = new TableNode(card1); + Card card2 = new Card(1,2,3,4); + TableNode node2 = new TableNode(card2); + + public void testGetCard() + { + assertEquals(card1,node1.getCard()); + assertEquals(card2,node2.getCard()); + } + + public void testGetNext() + { + assertEquals(null,node1.getNext()); + assertEquals(null,node2.getNext()); + } + + public void testSetNext() + { + node1.setNext(node2); + node2.setNext(null); + + assertEquals(node2,node1.getNext()); + assertEquals(null,node2.getNext()); + } +}