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

public Card(int theQuantity, int theColor, int theShading, int theShape){
quantity = formatValue(theQuantity);
color = formatValue(theColor);
shading = formatValue(theShading);
shape = formatValue(theShape);
}

private int formatValue(int val){
if(val < 4 && val > 0){
return val;
} else if (val >= 0){
return val % 3 + 1;
} else {
return (((val % 3) + 3) % 3) + 1;
}

}
public int getQuantity(){
return quantity;
}

public int getColor(){
return color;
}

public int getShading(){
return shading;
}

public int getShape(){
return shape;
}

public boolean isSet(Card second, Card third){
boolean qtyIsRight = isRightForSet(quantity, second.getQuantity(), third.getQuantity());
boolean colorIsRight = isRightForSet(color, second.getColor(), third.getColor());
boolean shadingIsRight = isRightForSet(shading, second.getShading(), third.getShading());
boolean shapeIsRight = isRightForSet(shape, second.getShape(), third.getShape());

if(qtyIsRight && colorIsRight && shadingIsRight && shapeIsRight){
return true;
} else {
return false;
}

}
public boolean isRightForSet(int val1, int val2, int val3){
if((val1 + val2 + val3) % 3 == 0){
return true;
} else {
return false;
}
}

public String toString(){
String str = "";
if(quantity == 1){
str += "1";
} else if(quantity == 2){
str += "2";
} else if(quantity == 3){
str += "3";
}


if(color == 1){
str += "R";
} else if(color == 2){
str += "G";
} else if(color == 3){
str += "P";
}

if(shading == 1){
str += "O";
} else if(shading == 2){
str += "T";
} else if(shading == 3){
str += "S";
}

if(shape == 1){
str += "O";
} else if (shape == 2){
str += "D";
} else if (shape == 3){
str += "S";
}
return str;
}
public boolean equals(Object obj) {
Card that = (Card)obj;

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


}

69 changes: 69 additions & 0 deletions CardTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import junit.framework.TestCase;

public class CardTest extends TestCase {

public void testConstructorAndGetters(){
int expectedQuantity = 2;
int expectedColor = 1;
int expectedShape = 3;
int expectedShading = 2;

Card card = new Card(expectedQuantity, expectedColor, expectedShading, expectedShape);

assertEquals(expectedQuantity, card.getQuantity());
assertEquals(expectedColor, card.getColor());
assertEquals(expectedShading, card.getShading());
assertEquals(expectedShape, card.getShape());
}

public void testConstructorModulo(){
int expectedQuantity = 2;
int expectedColor = 1;
int expectedShape = 3;
int expectedShading = 3;

Card card = new Card(4, 6, 11, -10);

assertEquals(expectedQuantity, card.getQuantity());
assertEquals(expectedColor, card.getColor());
assertEquals(expectedShading, card.getShading());
assertEquals(expectedShape, card.getShape());
}

public void testToString(){
Card card = new Card(1,1,2,3);
assertEquals("1RTS", card.toString());
Card card2 = new Card(2,3,3,1);
assertEquals("2PSO", card2.toString());
Card card3 = new Card(3,2,2,2);
assertEquals("3GTD", card3.toString());
}

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

assertTrue(card0.isSet(card2, card1));
assertTrue(card0.isSet(card3, card4));
assertFalse(card0.isSet(card1, card3));
assertFalse(card0.isSet(card2, card4));

}

public void testIsSetTransitive(){
Card card0 = new Card(1,1,3,1);
Card card1 = new Card(1,2,2,1);
Card card2 = new Card(1,3,1,1);
Card card3 = new Card(2,1,3,3);

assertTrue(card0.isSet(card2, card1));
assertTrue(card1.isSet(card0, card2));
assertTrue(card2.isSet(card1, card0));
assertFalse(card0.isSet(card2, card3));
assertFalse(card2.isSet(card0, card3));
assertFalse(card3.isSet(card2, card0));
}
}
98 changes: 97 additions & 1 deletion src/Card.java
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,6 +1,99 @@
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 theQuantity, int theColor, int theShading, int theShape){
quantity = formatValue(theQuantity);
color = formatValue(theColor);
shading = formatValue(theShading);
shape = formatValue(theShape);
}

private int formatValue(int val){
if(val < 4 && val > 0){
return val;
} else if (val >= 0){
return val % 3 + 1;
} else {
return (((val % 3) + 3) % 3) + 1;
}

}
public int getQuantity(){
return quantity;
}

public int getColor(){
return color;
}

public int getShading(){
return shading;
}

public int getShape(){
return shape;
}

public boolean isSet(Card second, Card third){
boolean qtyIsRight = isRightForSet(quantity, second.getQuantity(), third.getQuantity());
boolean colorIsRight = isRightForSet(color, second.getColor(), third.getColor());
boolean shadingIsRight = isRightForSet(shading, second.getShading(), third.getShading());
boolean shapeIsRight = isRightForSet(shape, second.getShape(), third.getShape());

if(qtyIsRight && colorIsRight && shadingIsRight && shapeIsRight){
return true;
} else {
return false;
}

}
public boolean isRightForSet(int val1, int val2, int val3){
if((val1 + val2 + val3) % 3 == 0){
return true;
} else {
return false;
}
}

public String toString(){
String str = "";
if(quantity == 1){
str += "1";
} else if(quantity == 2){
str += "2";
} else if(quantity == 3){
str += "3";
}


if(color == 1){
str += "R";
} else if(color == 2){
str += "G";
} else if(color == 3){
str += "P";
}

if(shading == 1){
str += "O";
} else if(shading == 2){
str += "T";
} else if(shading == 3){
str += "S";
}

if(shape == 1){
str += "O";
} else if (shape == 2){
str += "D";
} else if (shape == 3){
str += "S";
}
return str;
}
public boolean equals(Object obj) {
Card that = (Card)obj;

Expand All @@ -9,4 +102,7 @@ public boolean equals(Object obj) {
shading == that.getShading() &&
shape == that.getShape();
}


}

69 changes: 69 additions & 0 deletions src/CardTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import junit.framework.TestCase;

public class CardTest extends TestCase {

public void testConstructorAndGetters(){
int expectedQuantity = 2;
int expectedColor = 1;
int expectedShape = 3;
int expectedShading = 2;

Card card = new Card(expectedQuantity, expectedColor, expectedShading, expectedShape);

assertEquals(expectedQuantity, card.getQuantity());
assertEquals(expectedColor, card.getColor());
assertEquals(expectedShading, card.getShading());
assertEquals(expectedShape, card.getShape());
}

public void testConstructorModulo(){
int expectedQuantity = 2;
int expectedColor = 1;
int expectedShape = 3;
int expectedShading = 3;

Card card = new Card(4, 6, 11, -10);

assertEquals(expectedQuantity, card.getQuantity());
assertEquals(expectedColor, card.getColor());
assertEquals(expectedShading, card.getShading());
assertEquals(expectedShape, card.getShape());
}

public void testToString(){
Card card = new Card(1,1,2,3);
assertEquals("1RTS", card.toString());
Card card2 = new Card(2,3,3,1);
assertEquals("2PSO", card2.toString());
Card card3 = new Card(3,2,2,2);
assertEquals("3GTD", card3.toString());
}

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

assertTrue(card0.isSet(card2, card1));
assertTrue(card0.isSet(card3, card4));
assertFalse(card0.isSet(card1, card3));
assertFalse(card0.isSet(card2, card4));

}

public void testIsSetTransitive(){
Card card0 = new Card(1,1,3,1);
Card card1 = new Card(1,2,2,1);
Card card2 = new Card(1,3,1,1);
Card card3 = new Card(2,1,3,3);

assertTrue(card0.isSet(card2, card1));
assertTrue(card1.isSet(card0, card2));
assertTrue(card2.isSet(card1, card0));
assertFalse(card0.isSet(card2, card3));
assertFalse(card2.isSet(card0, card3));
assertFalse(card3.isSet(card2, card0));
}
}
Loading