Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 80 additions & 1 deletion src/Card.java
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,5 +1,84 @@
public class Card {
// Create the rest of this class yourself
private int quantity;
private int color;
private int shading;
private int shape;

public Card(int q, int c, int shd, int shp){
quantity = fixValue(q);
color = fixValue(c);
shading = fixValue(shd);
shape = fixValue(shp);
}

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((c1.getQuantity() + c2.getQuantity() + quantity) % 3 == 0 &&
(c1.getColor() + c2.getColor() + color) % 3 == 0 &&
(c1.getShading() + c2.getShading() + shading) % 3 == 0 &&
(c1.getShape() + c2.getShape() + shape) % 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;
}



private int fixValue(int valueToFix) {
if (valueToFix < 1 || valueToFix > 3)
return (((valueToFix % 3) + 3) % 3) + 1;
else
return valueToFix;
}

public boolean equals(Object obj) {
Card that = (Card)obj;
Expand Down
66 changes: 66 additions & 0 deletions src/CardTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
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 testAllOnes() {
Card c1 = new Card(1, 1, 1, 1);
Card c2 = new Card(1, 1, 1, 1);

assertEquals(c1, c2);
}

public void testThreeCards() {
Card c1 = new Card(1, 1, 1, 1);
Card c2 = new Card(1, 1, 1, 1);
Card c3 = new Card(1, 1, 1, 1);

assertEquals(c1, c2);
assertEquals(c1, c3);
assertEquals(c2, c3);
}
public void testIsSet() {
Card c1 = new Card(1, 1, 1, 1);
Card c2 = new Card(1, 1, 1, 1);
Card c3 = new Card(1, 1, 1, 1);

assertTrue(c1.isSet(c2,c3));
}
public void testNotSet() {
Card c1 = new Card(1, 2, 3, 1);
Card c2 = new Card(2, 3, 3, 1);
Card c3 = new Card(2, 3, 3, 3);

assertFalse(c1.isSet(c2,c3));
}
public void testToString() {
Card c1 = new Card(1, 2, 3, 1);

assertEquals("1GSO", c1.toString());
}
public void testInvalidNumbers() {
Card c1 = new Card(8, 7, 6, 4);

assertEquals("3GOD", c1.toString());
}
public void testNegativeNumbers() {
Card c1 = new Card(-8, -7, -6, -4);

assertEquals("2POS", c1.toString());
}
public void testNegativeAndPositiveNumbers() {
Card c1 = new Card(-8, 3, -6, 4);

assertEquals("2POD", c1.toString());
}
}
45 changes: 44 additions & 1 deletion src/Deck.java
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,52 @@
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 <Card> cards;
private int nextCardIndex;

public Deck() {
// Creates a deck with all standard 81 set cards, shuffled.
cards = new ArrayList<Card>(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 c = new Card(quantity, color, shading, shape);
cards.add(c);
}
}
}
}
Collections.shuffle(cards);
}

// public Deck(String filename) {
// Creates a pre-defined deck from a file named filename, and does not shuffle it.
// Usage (in test code, and in the later code):
// Deck d = new Deck("example.dat");
// }

public boolean hasNext() {
// Returns true if any cards left, false otherwise.
if(nextCardIndex < cards.size())
return true;
else
return false;
}

public Card getNext() {
// Returns the next card in the deck if there is one, null otherwise.
if (hasNext() == true) {
nextCardIndex += 1;
return cards.get(nextCardIndex -1);
}
else
return null;
}

public Deck(String filename) {
cards = new ArrayList<Card>(81);
Expand Down
62 changes: 62 additions & 0 deletions src/DeckTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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 testOneCard() {
Deck d = new Deck("oneCard.dat");

assertTrue(d.hasNext());
assertEquals("1GSO", d.getNext().toString());
assertFalse(d.hasNext());
}
public void testOneSet() {
Deck d = new Deck("oneSet.dat");

assertTrue(d.hasNext());
assertEquals("1ROO", d.getNext().toString());
assertTrue(d.hasNext());
assertEquals("1GTD", d.getNext().toString());
assertTrue(d.hasNext());
assertEquals("1PSS", d.getNext().toString());
assertFalse(d.hasNext());
}
public void testNoSet() {
Deck d = new Deck("noSet.dat");

assertTrue(d.hasNext());
assertEquals("1GSO", d.getNext().toString());
assertTrue(d.hasNext());
assertEquals("1GOO", d.getNext().toString());
assertTrue(d.hasNext());
assertEquals("1ROO", d.getNext().toString());
assertFalse(d.hasNext());
}
public void testSixCardsWithOneSet() {
Deck d = new Deck("sixCards.dat");

assertTrue(d.hasNext());
assertEquals("1ROO", d.getNext().toString());
assertTrue(d.hasNext());
assertEquals("2GTD", d.getNext().toString());
assertTrue(d.hasNext());
assertEquals("3PSS", d.getNext().toString());
assertTrue(d.hasNext());
assertEquals("1GSO", d.getNext().toString());
assertTrue(d.hasNext());
assertEquals("1RTD", d.getNext().toString());
assertTrue(d.hasNext());
assertEquals("3GOD", d.getNext().toString());
assertFalse(d.hasNext());
}
}
104 changes: 104 additions & 0 deletions src/Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
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 filename) {
// uses a preset deck
d = new Deck(filename);
t = new Table();

while (d.hasNext() == true) {
t.add(d.getNext());

if (t.numCards() == 12) {
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 (t.numSets() > 0 && d.hasNext() == true) {
for (int i = 0; i < t.numCards() - 2; i++) {
for (int j = i + 1; j < t.numCards() - 1; j++) {
for (int h = j + 1; h < t.numCards(); h++) {

Card c1 = t.getCard(i);
Card c2 = t.getCard(j);
Card c3 = t.getCard(h);

if (c1.isSet(c2,c3)) {
t.removeSet(c1,c2,c3);
}

if (numCards() < 12 && d.hasNext() == true) {

while ( t.numCards() < 12) {

if (d.hasNext() == false) {
return;
}
t.add(d.getNext());
}
return;
}
}
}
}
}

else if (t.numSets() != 0 && d.hasNext() == false) {
for (int i = 0; i < t.numCards() - 2; i++) {
for (int j = i + 1; j < t.numCards() - 1; j++) {
for (int h = j + 1; h < t.numCards(); h++) {

Card c1 = t.getCard(i);
Card c2 = t.getCard(j);
Card c3 = t.getCard(h);

if (c1.isSet(c2,c3)) {
t.removeSet(c1,c2,c3);
return;
}
}
}
}
}
return;
}

public boolean isGameOver() {
if (d.hasNext() == false && t.numSets() == 0) {
return true;
}
else {
return false;
}
}
}
19 changes: 19 additions & 0 deletions src/GameAverage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
public class GameAverage {
public static void main(String[] args) {

float setSum = 0;
float cardSum = 0;

for (int i = 0; i < 1000000; i++) {
Game g = new Game();
setSum += g.numSets();

while (g.isGameOver() == false) {
g.playRound();
}
cardSum += g.numCards();
}
System.out.println ("Average number of sets from 12 cards: " + setSum/1000000);
System.out.println ("Average number of remaining cards at the end of a game: " + cardSum/1000000);
}
}
Loading