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
97 changes: 96 additions & 1 deletion src/Card.java
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,5 +1,100 @@
import java.util.ArrayList;

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) {
if (q < 1 || q > 3) {
quantity = (((q % 3) + 3) % 3) + 1;
}
else {
quantity = q;
}
if (c < 1 || c > 3) {
color = (((c % 3) + 3) % 3) + 1;
}
else {
color = c;
}
if (shd < 1 || shd > 3) {
shading = (((shd % 3) + 3) % 3) + 1;
}
else {
shading = shd;
}
if (shp < 1 || shp > 3) {
shape = (((shp % 3) + 3) % 3) + 1;
}
else {
shape = 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) {
int quantityset = quantity + c1.getQuantity() + c2.getQuantity();
int colorset = color + c1.getColor() + c2.getColor();
int shadeset = shading + c1.getShading() + c2.getShading();
int shapeset = shape + c1.getShape() + c2.getShape();
if (quantityset % 3 == 0 && colorset % 3 == 0 && shadeset % 3 == 0 && shapeset % 3 == 0) {
return true;
}
else {
return false;
}
}

public String toString() {
String string = "";
ArrayList<String> quant = new ArrayList<String>();
quant.add("");
quant.add("1");
quant.add("2");
quant.add("3");

ArrayList<String> col = new ArrayList<String>();
col.add("");
col.add("R");
col.add("G");
col.add("P");

ArrayList<String> shad = new ArrayList<String>();
shad.add("");
shad.add("O");
shad.add("T");
shad.add("S");

ArrayList<String> shap = new ArrayList<String>();
shap.add("");
shap.add("O");
shap.add("D");
shap.add("S");

string += quant.get(getQuantity());
string += col.get(getColor());
string += shad.get(getShading());
string += shap.get(getShape());

return string;
}

public boolean equals(Object obj) {
Card that = (Card)obj;
Expand Down
86 changes: 86 additions & 0 deletions src/CardTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import junit.framework.TestCase;

public class CardTest extends TestCase {

public void testOneCard() {
Card c = new Card(1, 1, 1, 1);

assertEquals(1, c.getQuantity());
assertEquals(1, c.getColor());
assertEquals(1, c.getShading());
assertEquals(1, c.getShape());
assertEquals("1ROO", c.toString());
}

public void testInvalidValues() {
Card c = new Card(-2, 4, 8, 0);

assertEquals(2, c.getQuantity());
assertEquals(2, c.getColor());
assertEquals(3, c.getShading());
assertEquals(1, c.getShape());
assertEquals("2GSO", c.toString());
}

public void testMixedValidityValues() {
Card c = new Card(2, 1, 5, 0);

assertEquals(2, c.getQuantity());
assertEquals(1, c.getColor());
assertEquals(3, c.getShading());
assertEquals(1, c.getShape());
assertEquals("2RSO", c.toString());
}

public void testNotSet() {
Card c1 = new Card(2, 1, 5, 0);
Card c2 = new Card(1, 1, 3, 2);
Card c3 = new Card(2, 1, 1, 1);

assertEquals(2, c1.getQuantity());
assertEquals(1, c1.getColor());
assertEquals(3, c1.getShading());
assertEquals(1, c1.getShape());
assertEquals("2RSO", c1.toString());

assertEquals(1, c2.getQuantity());
assertEquals(1, c2.getColor());
assertEquals(3, c2.getShading());
assertEquals(2, c2.getShape());
assertEquals("1RSD", c2.toString());

assertEquals(2, c3.getQuantity());
assertEquals(1, c3.getColor());
assertEquals(1, c3.getShading());
assertEquals(1, c3.getShape());
assertEquals("2ROO", c3.toString());

assertFalse(c1.isSet(c2, c3));
}

public void testSet() {
Card c1 = new Card(2, 1, 2, 3);
Card c2 = new Card(2, 1, 3, 2);
Card c3 = new Card(2, 1, 1, 1);

assertEquals(2, c1.getQuantity());
assertEquals(1, c1.getColor());
assertEquals(2, c1.getShading());
assertEquals(3, c1.getShape());
assertEquals("2RTS", c1.toString());

assertEquals(2, c2.getQuantity());
assertEquals(1, c2.getColor());
assertEquals(3, c2.getShading());
assertEquals(2, c2.getShape());
assertEquals("2RSD", c2.toString());

assertEquals(2, c3.getQuantity());
assertEquals(1, c3.getColor());
assertEquals(1, c3.getShading());
assertEquals(1, c3.getShape());
assertEquals("2ROO", c3.toString());

assertTrue(c1.isSet(c2, c3));
}
}
50 changes: 49 additions & 1 deletion src/Deck.java
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,35 @@
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
//Instrance variables must be cards and nextCardIndex for the provided constructor
//to work
public ArrayList<Card> cards;
public int nextCardIndex;

public Deck() {
// Creates a deck with all standard 81 set cards, shuffled
//Usage (in test code, and in the later code); Deck d = new Deck()
cards = new ArrayList<Card>(81);

for (int q = 1; q <= 3; q++) {
for (int c = 1; c <= 3; c++) {
for (int shd = 1; shd <= 3; shd++) {
for (int shp = 1; shp <= 3; shp++) {
cards.add(new Card (q, c, shd, shp));
}
}
}
}
Collections.shuffle(cards);
nextCardIndex = 0;
}

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

try {
Expand Down Expand Up @@ -42,4 +66,28 @@ public Deck(String filename) {
throw new RuntimeException("Error while reading file: " + e.toString());
}
}

public boolean hasNext() {
//Returns true if any cards are 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
//You can call hasNext within this method to see if you should return
//a card or return null
if (hasNext() == false) {
return null;
}
else {
int cardIndex = nextCardIndex;
nextCardIndex += 1;
return cards.get(cardIndex);
}
}
}
41 changes: 41 additions & 0 deletions src/DeckTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import junit.framework.TestCase;

public class DeckTest extends TestCase {

public void testDeck() {
Deck d = new Deck();

int num = 0;
while (d.hasNext() == true) {
d.getNext();
num += 1;
}

assertEquals(81, num);
}

public void testFile() {
Deck d = new Deck("deck.dat");

int num = 0;
while (d.hasNext() == true) {
num += 1;
d.getNext();
}

assertEquals(9, num);
}

public void testEmpty() {
Deck d = new Deck("empty.dat");

int num = 0;
while (d.hasNext() == true) {
num += 1;
d.getNext();
}

assertFalse(d.hasNext());
assertEquals(null, d.getNext());
}
}
88 changes: 88 additions & 0 deletions src/Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
public class Game {
Table t;
Deck d;

public Game() {
t = new Table();
d = new Deck();

for (int a = 0; a < 12; a++) {
t.add(d.getNext());
}
}

public Game(String filename) {
t = new Table();
d = new Deck(filename);

while (d.hasNext()) {
t.add(d.getNext());
if (t.numCards() > 11) {
return;
}
}
}

public int numSets() {
return t.numSets();
}

public int numCards() {
return t.numCards();

}

public void playRound() {
if (t.numSets() == 0 && d.hasNext() && t.numCards() > 11) {
for (int c = 0; c < 3; c++) {
if (d.hasNext()) {
t.add(d.getNext());
}
}
return;
}

if (t.numCards() == 0 && d.hasNext()) {
for (int c = 0; c < 12; c++) {
if (d.hasNext()) {
t.add(d.getNext());
}
}
return;
}

if (t.numSets() != 0) {
for (int i = 0; i < (t.numCards() - 2); i++) {
for (int j = i + 1; j < (t.numCards() - 1); j++) {
for (int k = j + 1; k < t.numCards(); k++) {
if (t.getCard(k).isSet(t.getCard(i), t.getCard(j))) {
t.removeSet(t.getCard(k), t.getCard(i), t.getCard(j));
if (t.numCards() < 12 && d.hasNext()) {
for (int c = 0; c < 3; c++) {
if (d.hasNext()) {
t.add(d.getNext());
}
else {
return;
}
}
}
return;
}
}
}
}
}
}

public boolean isGameOver() {
if (!d.hasNext() && t.numSets() == 0) {
return true;
}

else {
return false;
}
}
}

Loading