Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finished Card class and CardTest files #13

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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: 104 additions & 5 deletions src/Card.java
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,12 +1,111 @@
public class Card {
// Create the rest of this class yourself
private int Q;
private int C;
private int S;
private int Sh;
private String str;

public Card(int Quantity, int Color, int Shading, int Shape) {
Q = Fixer(Quantity);
C = Fixer(Color);
S = Fixer(Shading);
Sh = Fixer(Shape);
}

private int Fixer(int value) {
if (value < 1 || value > 3) {
return ((((value % 3) + 3) % 3) + 1);
}
else {
return value;
}
}


public int getQuantity() {
return Q;
}

public int getColor() {
return C;
}

public int getShading() {
return S;
}

public int getShape() {
return Sh;
}

public boolean isSet(Card card2, Card card3)
{
if(((Q + card2.getQuantity() + card3.getQuantity()) % 3 == 0) &&
((C + card2.getColor() + card3.getColor()) % 3 == 0) &&
((S + card2.getShading() + card3.getShading()) % 3 == 0) &&
((Sh + card2.getShape() + card3.getShape()) % 3 == 0))
{
return true;
}

else {
return false;
}
}

public String toString() {
String str = "";

if (Q == 1) {
str += 1;
}
else if (Q == 2) {
str += 2;
}
else if (Q == 3) {
str += 3;
}

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

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

if (Sh == 1) {
str += 'O';
}
else if (Sh == 2) {
str += 'D';
}
else if (Sh == 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();
return Q == that.getQuantity() &&
C == that.getColor() &&
S == that.getShading() &&
Sh == that.getShape();
}
}

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

public class CardTest extends TestCase {
public void testNewCardAllSame() {
Card a = new Card(1, 1, 1, 1);
Card b = new Card(2, 2, 2, 2);
Card c = new Card(3, 3, 3, 3);

assertEquals(1, a.getQuantity());
assertEquals(1, a.getColor());
assertEquals(1, a.getShading());
assertEquals(1, a.getShape());

assertEquals(2, b.getQuantity());
assertEquals(2, b.getColor());
assertEquals(2, b.getShading());
assertEquals(2, b.getShape());

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

public void testNewCardFix1() {
Card a = new Card(4, 1, 1, 1);
Card b = new Card(1, 4, 1, 1);
Card c = new Card(1, 1, 4, 1);
Card d = new Card(1, 1, 1, 4);

assertEquals(2, a.getQuantity());
assertEquals(1, a.getColor());
assertEquals(1, a.getShading());
assertEquals(1, a.getShape());

assertEquals(1, b.getQuantity());
assertEquals(2, b.getColor());
assertEquals(1, b.getShading());
assertEquals(1, b.getShape());

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

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

public void testNewCardFixNegative() {
Card a = new Card(-3, 1, 1, 1);
Card b = new Card(1, -3, 1, 1);
Card c = new Card(1, 1, -3, 1);
Card d = new Card(1, 1, 1, -3);

assertEquals(1, a.getQuantity());
assertEquals(1, a.getColor());
assertEquals(1, a.getShading());
assertEquals(1, a.getShape());

assertEquals(1, b.getQuantity());
assertEquals(1, b.getColor());
assertEquals(1, b.getShading());
assertEquals(1, b.getShape());

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

assertEquals(1, d.getQuantity());
assertEquals(1, d.getColor());
assertEquals(1, d.getShading());
assertEquals(1, d.getShape());
}

public void testNewCardFixAll() {
Card a = new Card(-3, 4, 7, 6);

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

public void testNewCardisSetTrue() {
Card a = new Card(1, 1, 1, 1);
Card b = new Card(1, 2, 1, 1);
Card c = new Card(1, 3, 1, 1);

assertTrue(a.isSet(b, c));
}

public void testNewCardisSetFalse() {
Card a = new Card(1, 1, 1, 1);
Card b = new Card(2, 2, 2, 2);
Card c = new Card(3, 3, 3, 3);

assertTrue(a.isSet(b, c));
}


public void testNewCardtoString() {
Card a = new Card(-1, 1, 1, 3);
Card b = new Card(1, 2, 3, 2);
Card c = new Card(2, 5, 10, -6);

assertEquals("3ROS", a.toString());
assertEquals("1GSD", b.toString());
assertEquals("2PTO", c.toString());
}


}
41 changes: 40 additions & 1 deletion src/Deck.java
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,29 @@
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
private int nextCardIndex;
private ArrayList<Card> cards;

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 c = new Card(Q, C, S, Sh);
cards.add(c);
}
}
}
}
Collections.shuffle(cards);
}



public Deck(String filename) {
cards = new ArrayList<Card>(81);
Expand Down Expand Up @@ -42,4 +62,23 @@ public Deck(String filename) {
throw new RuntimeException("Error while reading file: " + e.toString());
}
}

public boolean hasNext() {
if (nextCardIndex < cards.size()) {
return true;
}
else {
return false;
}
}

public Card getNext() {
if (hasNext() != false) {
nextCardIndex += 1;
return cards.get(nextCardIndex - 1);
}
else {
return null;
}
}
}
46 changes: 46 additions & 0 deletions src/DeckTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import junit.framework.TestCase;

public class DeckTest extends TestCase {
public void testoneCard() {
Deck d = new Deck("oneCard.dat");

assertTrue(d.hasNext());
assertEquals("1GSO", d.getNext().toString());
assertFalse(d.hasNext());
}

public void testoneSetTrue() {
Deck d = new Deck("oneSetTrue.dat");
Card c = d.getNext();
assertTrue(d.hasNext());
Card c2 = d.getNext();
assertTrue(d.hasNext());
Card c3 = d.getNext();
assertFalse(d.hasNext());

assertEquals("1ROO", c.toString());
assertEquals("1GOO", c2.toString());
assertEquals("1POO", c3.toString());

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

}

public void testoneSetFalse() {
Deck d = new Deck("oneSetFalse.dat");
Card c = d.getNext();
assertTrue(d.hasNext());
Card c2 = d.getNext();
assertTrue(d.hasNext());
Card c3 = d.getNext();
assertFalse(d.hasNext());


assertEquals("1ROO", c.toString());
assertEquals("2GTD", c2.toString());
assertEquals("3PSO", c3.toString());

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

}
Loading