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
61 changes: 60 additions & 1 deletion src/Card.java
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,7 +1,66 @@
public class Card {
// Create the rest of this class yourself

public boolean equals(Object obj) {
private int quantity;
private int color;
private int shading;
private int shape;

public Card(int cardQuantity, int cardColor, int cardShading, int cardShape)
{
quantity = (cardQuantity % 3 + 3) % 3;
if (quantity == 0)
quantity += 3;

color = (cardColor % 3 + 3) % 3;
if (color == 0)
color += 3;

shading = (cardShading % 3 + 3) % 3;
if (shading == 0)
shading += 3;

shape = (cardShape % 3 + 3) % 3;
if (shape == 0)
shape += 3;
}

public int getQuantity()
{
return quantity;
}

public int getColor()
{
return color;
}

public int getShading()
{
return shading;
}

public int getShape()
{
return shape;
}

public String toString()
{
char[] letters = new char[] {'R', 'G', 'P', 'O', 'T', 'S', 'O', 'D', 'S'};
return String.valueOf(quantity) + letters[color - 1] + letters[shading + 2] + letters[shape + 5];
}

public boolean isSet(Card card2, Card card3)
{
return (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;
}

public boolean equals(Object obj)
{
Card that = (Card)obj;

return quantity == that.getQuantity() &&
Expand Down
93 changes: 93 additions & 0 deletions src/CardTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
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 testCardQueeries()
{
Card card1 = new Card(3, 2, 2, 1);
//a card with numbers between 1 and 3
assertEquals(3, card1.getQuantity());
assertEquals(2, card1.getColor());
assertEquals(2, card1.getShading());
assertEquals(1, card1.getShape());

Card card2 = new Card(4, 5, 6, 7);
//a card with numbers greater than 3
assertEquals(1, card2.getQuantity());
assertEquals(2, card2.getColor());
assertEquals(3, card2.getShading());
assertEquals(1, card2.getShape());

Card card3 = new Card(0, -1, -2, -3);
//a card with numbers lower than 1
assertEquals(3, card3.getQuantity());
assertEquals(2, card3.getColor());
assertEquals(1, card3.getShading());
assertEquals(3, card3.getShape());
}

public void testCardEquals()
{
Card card1 = new Card(1, 2, 3, 1);
Card card2 = new Card(1, 2, 3, 1);
Card card3 = new Card(4, 5, 6, 7);
Card card4 = new Card(-2, -1, 0, -5);
Card card5 = new Card(1, 1, 1, 1);

assertEquals(true, card1.equals(card2));
assertEquals(true, card1.equals(card3));
assertEquals(true, card1.equals(card4));
assertEquals(true, card2.equals(card3));
assertEquals(true, card2.equals(card4));
assertEquals(true, card3.equals(card4));

assertEquals(false, card5.equals(card1));
assertEquals(false, card5.equals(card2));
assertEquals(false, card5.equals(card3));
assertEquals(false, card5.equals(card4));

}

public void testCardString()
{
Card card1 = new Card(1, 1, 1, 2);
Card card2 = new Card(3, 2, 3, 2);
Card card3 = new Card(4, 5, 6, 7);
Card card4 = new Card(0, -1, -2, -3);

assertEquals("1ROD", card1.toString());
assertEquals("3GSD", card2.toString());
assertEquals("1GSO", card3.toString());
assertEquals("3GOS", card4.toString());
}

public void testCardSet()
{
Card card1 = new Card(1, 1, 1, 1);
Card card2 = new Card(2, 1, 1, 1);
Card card3 = new Card(3, 1, 1, 1);
Card card4 = new Card(2, 2, 2, 2);
Card card5 = new Card(3, 3, 3, 3);

assertEquals(true, card1.isSet(card2, card3));
assertEquals(true, card1.isSet(card4, card5));
assertEquals(false, card1.isSet(card2, card4));
assertEquals(true, card3.isSet(card2, card1));
assertEquals(true, card2.isSet(card1, card3));
assertEquals(false, card2.isSet(card1, card4));
assertEquals(false, card4.isSet(card1, card2));
assertEquals(false, card4.isSet(card3, card2));

}
}
50 changes: 46 additions & 4 deletions src/Deck.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,55 @@
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(String filename) {
public Deck()
{
cards = new ArrayList<Card>(81);
nextCardIndex = 0;

try {
for (int i = 1; i < 4; i++)
{
for (int j = 1; j < 4; j++)
{
for (int k = 1; k < 4; k++)
{
for (int l = 1; l < 4; l++)
{
cards.add(new Card(i, j, k, l));
}
}
}
}

Collections.shuffle(cards);
}


public boolean hasNext()
{
return nextCardIndex < cards.size();
}

public Card getNext()
{
if (hasNext() == false)
return null;

nextCardIndex += 1;
return cards.get(nextCardIndex - 1);
}

public Deck(String filename)
{
cards = new ArrayList<Card>(81);

try
{
String line;
BufferedReader infile = new BufferedReader(new FileReader(filename));
int position = 0;
Expand Down Expand Up @@ -38,7 +79,8 @@ public Deck(String filename) {
nextCardIndex = 0;
}
}
catch(Exception e) {
catch(Exception e)
{
throw new RuntimeException("Error while reading file: " + e.toString());
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/DeckNoSetsFirstTable.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
1 1 1 1
1 1 1 2
1 1 2 1
1 1 2 3
1 2 1 1
1 2 3 1
2 1 1 1
2 3 1 3
3 3 2 1
3 1 2 3
3 3 3 3
3 2 1 2
75 changes: 75 additions & 0 deletions src/DeckTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
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 testDeckCreation()
{
Deck deck = new Deck();
int i = 0;

while (deck.hasNext())
{
deck.getNext();
i += 1;
}

assertEquals(81, i);
}

public void testCostumDeckCreation()
{
Deck deck = new Deck("DeckTester.txt");
Card card1 = new Card(1, 1, 1, 1);
Card card2 = new Card(1, 1, 1, 2);
Card card3 = new Card(1, 1, 1, 3);
Card card4 = new Card(1, 1, 2, 1);

assertEquals(true, deck.hasNext());
assertEquals(true, deck.getNext().equals(card1));

assertEquals(true, deck.hasNext());
assertEquals(true, deck.getNext().equals(card2));

assertEquals(true, deck.hasNext());
assertEquals(true, deck.getNext().equals(card3));

assertEquals(true, deck.hasNext());
assertEquals(true, deck.getNext().equals(card4));

assertEquals(false, deck.hasNext());
assertEquals(null, deck.getNext());
}

public void testEmptyDeck()
{
Deck deck = new Deck("emptydeck.txt");

assertEquals(false, deck.hasNext());
assertEquals(null, deck.getNext());
}

public void testOneDeck()
{
Deck deck = new Deck("onedeck.txt");
Card card = new Card(1, 1, 1, 1);

assertEquals(true, deck.hasNext());
assertEquals(true, deck.getNext().equals(card));

assertEquals(false, deck.hasNext());
assertEquals(null, deck.getNext());
}
}

4 changes: 4 additions & 0 deletions src/DeckTester.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1 1 1 1
1 1 1 2
1 1 1 3
1 1 2 1
4 changes: 4 additions & 0 deletions src/DeckTester.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1 1 1 1
1 1 1 2
1 1 1 3
1 1 2 1
15 changes: 15 additions & 0 deletions src/FullTableAndSets.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
1 1 1 1
1 1 1 2
1 1 1 3
2 2 2 2
3 3 3 3
2 2 2 3
2 2 2 1
3 3 3 2
3 3 3 1
2 1 2 2
3 2 2 1
3 1 1 3
1 2 2 3
3 2 1 1
3 1 2 3
Loading