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
125 changes: 125 additions & 0 deletions src/Card2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
public class Card
{
public int quantity = 0;
public int color = 0;
public int shading = 0;
public int shape = 0;

public Card(int quan, int col, int shade, int shap)
{
if(quan < 1 || quan > 3)
{
if(quan <0)
quantity = (((quan%3) +3) %3)+1;
else
quantity = (quan % 3)+1;
}
else
quantity = quan;

if(col < 1 || col > 3)
{
if(col <0)
color = (((col % 3) + 3) % 3)+1;
else
color = (col % 3)+1;
}
else
color = col;

if(shade < 1 || shade > 3)
{
if(shade <0)
shading = (((shade % 3) + 3) % 3)+1;
else
shading = (shade % 3)+1;
}
else
shading = shade;

if(shap < 1 || shap > 3)
{
if(shap <0)
shape = (((shap % 3) + 3) % 3)+1;
else
shape = (shap % 3)+1;
}
else
shape = shap;
}
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)
{
Card that = (Card)obj;

return (quantity == that.getQuantity() &&
color == that.getColor() &&
shading == that.getShading() &&
shape == that.getShape() );
}
public boolean isSet(Card card2, Card card3)
{
boolean quanCheck = false;
boolean colCheck = false;
boolean shadeCheck = false;
boolean shapeCheck = false;

if(((quantity + card2.getQuantity() + card3.getQuantity() )%3) == 0)
quanCheck = true;
if(((color + card2.getColor() + card3.getColor() )%3) == 0)
colCheck = true;
if(((shading + card2.getShading() + card3.getShading() )%3) == 0)
shadeCheck = true;
if(((shape + card2.getShape() + card3.getShape() )%3) == 0)
shapeCheck = true;

return (quanCheck == true && colCheck == true && shadeCheck == true && shapeCheck == true);
}

public String toString()
{
//Quantity
String info = "" + quantity;

//Color
if(color == 1)
info += "R";
else if(color == 2)
info += "G";
else if(color == 3)
info += "P";

//Shading
if(shading == 1)
info += "O";
else if(shading == 2)
info += "T";
else if(shading == 3)
info += "S";

//Shape
if(shape == 1)
info += "O";
else if(shape == 2)
info += "D";
else if(shape == 3)
info += "S";

return info;
}
}
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
{
Card a = new Card(1,1,1,1);
Card b = new Card(2,2,3,3);
Card c = new Card(1,2,3,1);
Card d = new Card(-4,-5,6,7);

public void testGetQuantity()
{
assertEquals(1,a.getQuantity());
assertEquals(2,b.getQuantity());
assertEquals(1,c.getQuantity());
assertEquals(3,d.getQuantity());
}
public void testGetColor()
{
assertEquals(1,a.getColor());
assertEquals(2,b.getColor());
assertEquals(2,c.getColor());
assertEquals(2,d.getColor());
}
public void testGetShading()
{
assertEquals(1,a.getShading());
assertEquals(3,b.getShading());
assertEquals(3,c.getShading());
assertEquals(1,d.getShading());
}
public void testGetShape()
{
assertEquals(1,a.getShape());
assertEquals(3,b.getShape());
assertEquals(1,c.getShape());
assertEquals(2,d.getShape());
}
public void testEquals()
{
a = b;
assertEquals(true,a.equals(b));
assertEquals(false,c.equals(d));
}
public void testIsString()
{
a = new Card(1,1,1,1);
b = new Card(2,2,2,2);
c = new Card(3,3,3,3);

assertEquals("1ROO",a.toString());
assertEquals("2GTD",b.toString());
assertEquals("3PSS",c.toString());
}
public void testIsSet()
{
a = new Card(1,1,1,1);
b = new Card(2,2,2,2);
c = new Card(3,3,3,3);
d = new Card(1,2,3,1);

assertEquals(true,a.isSet(b,c));
assertEquals(false,a.isSet(c,d));
}
}
84 changes: 84 additions & 0 deletions src/Deck2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.StringTokenizer;
import java.util.ArrayList;
import java.util.Collections;

public class Deck
{
private int nextCardIndex = 0;
private ArrayList<Card> cards = new ArrayList<Card>(81);

public Deck()
{
for(int quan = 1; quan < 4; quan++)
{
for(int col = 1; col < 4; col++)
{
for(int shade = 1; shade < 4; shade++)
{
for(int shap = 1; shap < 4; shap++)
{
Card card = new Card(quan,col,shade,shap);
cards.add(card);
}
}
}
}
nextCardIndex = 0;
Collections.shuffle(cards);
}
public boolean hasNext()
{
if(nextCardIndex < cards.size())
return true;
else
return false;
}
public Card getNext()
{
if(hasNext() == false)
return null;
nextCardIndex ++;
return cards.get(nextCardIndex-1);
}

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

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

// ignore blank lines
if(line.length() == 0)
continue;

// ignore comments
if(line.startsWith("#"))
continue;

// a valid line contains 4 integers
StringTokenizer tokenizer = new StringTokenizer(line);

int quantity = Integer.parseInt(tokenizer.nextToken());
int color = Integer.parseInt(tokenizer.nextToken());
int shading = Integer.parseInt(tokenizer.nextToken());
int shape = Integer.parseInt(tokenizer.nextToken());

cards.add(new Card(quantity, color, shading, shape));
nextCardIndex = 0;
}
}
catch(Exception e)
{
throw new RuntimeException("Error while reading file: " + e.toString());
}
}
}
25 changes: 25 additions & 0 deletions src/DeckTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import junit.framework.TestCase;

public class DeckTest extends TestCase
{
Deck d1 = new Deck();
Card c1 = new Card(1,1,1,1);

public void testHasNext()
{
assertEquals(true,d1.hasNext());
while(d1.getNext() != null)
{
c1 = d1.getNext();
}
assertEquals(false,d1.hasNext());
}
public void testGetNext()
{
while(d1.hasNext() == true)
{
c1 = d1.getNext();
}
assertEquals(null,d1.getNext());
}
}
Loading