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
109 changes: 107 additions & 2 deletions src/Card.java
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,12 +1,117 @@
public class Card {
public class Card
{
private int quantity;
private int color;
private int shading;
private int shape;
// Create the rest of this class yourself
public Card(int q, int c, int s, int sh)
{
quantity = fixValue(q);
color = fixValue(c);
shading = fixValue(s);
shape = fixValue(sh);
}
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) {
public boolean isSet(Card c, Card d)
{
if((c.getQuantity() + d.getQuantity() + quantity) % 3 == 0 &&
(c.getColor() + d.getColor() +color) % 3 == 0 &&
(c.getShading() + d.getShading() + shading) % 3 == 0 &&
(c.getShape() + d.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 += "V";
}
if(shape == 2)
{
str += "D";
}
if(shape == 3)
{
str += "Q";
}

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;

return quantity == that.getQuantity() &&
color == that.getColor() &&
shading == that.getShading() &&
shape == that.getShape();
}


}
27 changes: 27 additions & 0 deletions src/CardTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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 testCardSet() {
Card c1 = new Card(3,4,2,4);
Card c2 = new Card(2,-4,2,1);
Card c3 = new Card(1,1,2,5);

assertTrue(c1.isSet(c2, c3));
assertEquals("3GTD",c1.toString());
assertEquals("2PTV",c2.toString());
assertEquals("1RTQ",c3.toString());
}

}
67 changes: 62 additions & 5 deletions src/Deck.java
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,47 @@
import java.io.FileReader;
import java.util.StringTokenizer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;

public class Deck {
public class Deck
{
// Implement the rest of this class yourself
private int nextCardIndex = 0;
private ArrayList<Card> cards;

public Deck(String filename) {
public Deck()
{
cards = new ArrayList<Card>(81);
for(int q = 1; q<=3; q++)
{
for(int c = 1; c<=3; c++)
{
for(int s = 1; s<=3; s++)
{
for(int sh = 1; sh<=3; sh++)
{
Card leaf = new Card(q, c, s, sh);
}
}
}
}
Collections.shuffle(cards);
}


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

try {
try
{
String line;
BufferedReader infile = new BufferedReader(new FileReader(filename));
int position = 0;

while((line = infile.readLine()) != null) {
while((line = infile.readLine()) != null)
{
// Blank lines might contain white space, so trim it off
line = line.trim();

Expand All @@ -38,8 +66,37 @@ public Deck(String filename) {
nextCardIndex = 0;
}
}
catch(Exception e) {
catch(Exception e)
{
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.
if(hasNext() == true)
{
nextCardIndex++;
return cards.get(nextCardIndex-1);
}
else
{
return null;
}
//You can call hasNext() within this method to see if you should return
//a card or null
}
}
31 changes: 31 additions & 0 deletions src/DeckTester.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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 DeckTester 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("1ROO", d.getNext().toString());
}

public void testIsSet(){
Deck d = new Deck("CardSet.dat");
assertTrue(d.hasNext());
assertEquals
}

public void testNoSet(){
}

}
90 changes: 90 additions & 0 deletions src/Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
public class Game
{
private Table t;
private Deck d;

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

for(int i = 0; i< 12; i++)
{
if(d.hasNext() == true)
{
t.add(d.getNext());
}
}
}

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

int j = 0;
while(d.hasNext() == true)
{
t.add(d.getNext());
j++;

if(t.numCards() == 12)
return;
}
}

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

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

public void playRound()
{
if(numSets() >=1)
{
for(int j = 0; j < numCards()-2; j++)
{
for(int k = j + 1; k< numCards()-1; k++)
{
for(int x = k + 1; x< numCards(); x++)
{
if(t.getCard(j).isSet(t.getCard(k), t.getCard(x)))
{
t.removeSet(t.getCard(j), t.getCard(k), t.getCard(x));
if(numCards() < 12)
{
for(int y = 0; y < 3 && d.hasNext(); y++)
{
t.add(d.getNext());
}
}
return;

}
}
}
}

}
if(numSets() == 0)
{

for(int i = 0; i<3 && d.hasNext(); i++)
t.add(d.getNext());
return;
}
}

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