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
52 changes: 51 additions & 1 deletion src/Card.java
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,5 +1,55 @@
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 a, int b, int c, int d) {
quantity = fixValue(a);
color = fixValue(b);
shading = fixValue(c);
shape = fixValue(d);
}

// fixes values outside the range
// by Professor Sommers
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 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 String toString() {
String[][] array = {{"R", "G", "P"}, {"O", "T", "S"}, {"O", "D", "S"}};

return quantity + array[0][color - 1] + array[1][shading - 1] + array[2][shape - 1];
}

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

public class CardTest extends TestCase {

public void testValues() {
Card c = new Card(1, 2, 3, 1);

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

public void testValuesOutOfRange() {
Card c = new Card(7, 9, -4, 0);

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

// 1 1 1
public void testSameSet() {
Card c = new Card(3, 2, 2, 1);
Card c2 = new Card(1, 3, 1, 1);
Card c3 = new Card(2, 1, 3, 1);

assertTrue(c.isSet(c2, c3));
}

// 1 2 3
public void testRowSet() {
Card c = new Card(3, 1, 2, 3);
Card c2 = new Card(1, 2, 1, 2);
Card c3 = new Card(2, 3, 3, 1);

assertTrue(c.isSet(c2, c3));
}

public void testNoSet() {
Card c = new Card(1, 1, 3, 3);
Card c2 = new Card(1, 2, 1, 1);
Card c3 = new Card(2, 1, 3, 1);

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

public void testString() {
Card c = new Card(1, 2, 3, 1);

// Should be 1GSO
assertEquals("1GSO", c.toString());

System.out.println(c);
}

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

assertEquals(c1, c2);
}
}
45 changes: 44 additions & 1 deletion src/Deck.java
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,37 @@
import java.io.FileReader;
import java.util.StringTokenizer;
import java.util.ArrayList;
import java.util.Collections;

// instance variables must be named cards and nextCardIndex
// for provided constructor to work.
public class Deck {
// Implement the rest of this class yourself
private ArrayList<Card> cards;
private int nextCardIndex;

// Creates deck with 81 set cards shuffled
public Deck() {
cards = new ArrayList<Card>(81);
// while(hasNext()) {
// cards.add(getNext());
// }
for(int a = 1; a < 4; a++) {
for(int b = 1; b < 4; b++) {
for(int c = 1; c < 4; c++) {
for(int d = 1; d < 4; d++) {
cards.add(new Card(a, b, c, d));
}
}
}
}
nextCardIndex = 0;
Collections.shuffle(cards);
}

// Crates deck from file named filename
// Does not shuffle the deck
// Usage in test code and later code
// Deck d = new Deck("example.dot");
public Deck(String filename) {
cards = new ArrayList<Card>(81);

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

// Returns next card in deck if there is one or otherwise null
public boolean hasNext() {
return nextCardIndex >= 0 && nextCardIndex < cards.size();
}
// You can call hasNext() within this method to see if you
// should return a card or return null
public Card getNext() {
if(!(hasNext())) {
return null;
}
else {
nextCardIndex += 1;
return cards.get(nextCardIndex - 1);
}
}
}
47 changes: 47 additions & 0 deletions src/DeckTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import junit.framework.TestCase;

public class DeckTest extends TestCase {

public void test4cards() {
Deck d = new Deck("4cards.dat");
assertEquals(4, d.cards.size());
// System.out.println(d.cards);
}

public void test0cards() {
//System.out.println(System.getProperty("user.dir"));
Deck d = new Deck("0cards.dat");
assertEquals(0, d.cards.size());
// System.out.println(d.cards);
}

public void testGenCards() {
Deck d = new Deck();
assertEquals(81, d.cards.size());
// System.out.println(d.cards);

int quantity = 0;
int color = 0;
int shading = 0;
int shape = 0;
while(d.hasNext()) {
Card c = d.getNext();
if(c.getQuantity() == 1) {
quantity++;
}
if(c.getColor() == 1) {
color++;
}
if(c.getShading() == 1) {
shading++;
}
if(c.getShape() == 1) {
shape++;
}
}
assertEquals(27, quantity);
assertEquals(27, color);
assertEquals(27, shading);
assertEquals(27, shape);
}
}
103 changes: 103 additions & 0 deletions src/Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
public class Game {
Table t;
Deck d;

Game() {
t = new Table();
d = new Deck();
for(int i = 0; i < 12; i++) {
if(d.hasNext()) {
t.add(d.getNext());
}
else {
break;
}
}
}

Game(String deck) {
t = new Table();
d = new Deck(deck);
for(int i = 0; i < 12; i++) {
if(d.hasNext()) {
t.add(d.getNext());
}
else {
break;
}
}
}

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

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

public void playRound() {
if(numSets() == 0 && d.hasNext()) {
//System.out.println("t0");
if(d.hasNext()) {
t.add(d.getNext());
}
if(d.hasNext()) {
t.add(d.getNext());
}
if(d.hasNext()) {
t.add(d.getNext());
}
return;
}

if(d.hasNext() && numCards() == 12 && numSets() > 0) {
//remove one set and add 3 cards
findSet();
if(d.hasNext()) {
//System.out.println("t1");
t.add(d.getNext());
}
if(d.hasNext()) {
//System.out.println("t2");
t.add(d.getNext());
}
if(d.hasNext()) {
//System.out.println("t3");
t.add(d.getNext());
}
return;
}

if(numCards() > 12 && numSets() > 0) {
//System.out.println("t4");
//remove one set but don't add 3 cards
findSet();
return;
}

if(numSets() > 0 && d.hasNext() == false) {
//System.out.println("t5");
//remove one set
findSet();
return;
}
}

private void findSet() {
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++) {
if(t.getCard(a).isSet(t.getCard(b), t.getCard(c))) {
t.removeSet(t.getCard(a), t.getCard(b), t.getCard(c));
return;
}
}
}
}
}

public boolean isGameOver() {
return d.hasNext() == false && numSets() == 0;
}
}
Loading