Skip to content

Card part of Set Project Complete! #7

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

Open
wants to merge 7 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
3 changes: 3 additions & 0 deletions src/3cards.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
1 2 3 1
2 1 3 2
3 3 1 2
125 changes: 124 additions & 1 deletion src/Card.java
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,6 +1,129 @@
public class Card {
// Create the rest of this class yourself
private int quantity;
private int color;
private int shading;
private int shape;
private int value;

private String quantString;
private String colorString;
private String shadingString;
private String shapeString;

public Card(int theQuantity, int theColor, int theShading, int theShape) {
if (theQuantity < 1 || theQuantity > 3) {
quantity = (((theQuantity % 3) + 3) % 3) + 1;
}
else {
quantity = theQuantity;
}

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

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

if (theShape < 1 || theShape > 3) {
shape = (((theShape % 3) + 3) % 3) + 1;
}
else {
shape = theShape;
}



}
//Got the valueToFix from class//
private int the(int valueToFix) {
if (valueToFix < 1 || valueToFix > 3)
return (((valueToFix % 3) + 3) % 3) + 1;
else
return valueToFix;
}

//getValue() goes with valueToFix above//
public int getValue() {
return value;
}



public int getQuantity() {
return quantity;
}

public int getColor() {
return color;
}

public int getShading() {
return shading;
}

public int getShape() {
return shape;
}

public boolean isSet(Card card1, Card card2) {
boolean isSet = true;
return (quantity + card1.getQuantity() + card2.getQuantity()) % 3 == 0 &&
(color + card1.getColor() + card2.getColor()) % 3 == 0 &&
(shading + card1.getShading() + card2.getShading()) % 3 == 0 &&
(shape + card1.getShape() + card2.getShape()) % 3 == 0;


}

public String toString() {
String string = "";
String quantString = "";
String colorString = "";
String shadingString = "";
String shapeString = "";

if (quantity == 1)
quantString = "1";
if (quantity == 2)
quantString = "2";
if (quantity == 3)
quantString = "3";


if (color == 1)
colorString = "R";
if (color == 2)
colorString = "G";
if (color == 3)
colorString = "P";

if (shading == 1)
shadingString = "O";
if (shading == 2)
shadingString = "T";
if (shading == 3)
shadingString = "S";

if (shape == 1)
shapeString = "O";
if (shape == 2)
shapeString = "D";
if (shape == 3)
shapeString = "S";

return string = quantString + colorString + shadingString + shapeString;

}


public boolean equals(Object obj) {
Card that = (Card)obj;

Expand Down
77 changes: 77 additions & 0 deletions src/CardTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
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 testTheSetValues() {
Card card1 = new Card(1, 1, 1, 1);
assertEquals(1, card1.getQuantity());
assertEquals(1, card1.getColor());
assertEquals(1, card1.getShading());
assertEquals(1, card1.getShape());
assertEquals("1ROO", card1.toString());
}

public void testNegativeSetValues() {
Card card1 = new Card(-4, -4, -4, -4);
assertEquals("3PSS", card1.toString());
}

public void testSetIsLegal() {
Card card1 = new Card(2, 2, 1, 1);
Card card2 = new Card(2, 2, 2, 2);
Card card3 = new Card(2, 2, 3, 3);
assertEquals(true, card1.isSet(card2, card3));

}

public void testSetIsNotLegal() {
Card card1 = new Card(1, 1, 2, 2);
Card card2 = new Card(2, 1, 2, 2);
Card card3 = new Card(2, 2, 2, 2);
assertEquals(false, card2.isSet(card1, card3));
}

public void testAllCardsSame() {
Card card1 = new Card(3, 2, 1, 3);
Card card2 = new Card(3, 2, 1, 3);
Card card3 = new Card(3, 2, 1, 3);
assertEquals(true, card3.isSet(card1, card2));
}

public void testSameColor() {
Card card1 = new Card(1, 1, 2, 3);
Card card2 = new Card(2, 1, 1, 2);
Card card3 = new Card(3, 1, 3, 1);
assertEquals(true, card1.isSet(card2, card3));
}

public void testAllRandomCards() {
Card card1 = new Card(1, 2, 3, 1);
Card card2 = new Card(2, 3, 1, 2);
Card card3 = new Card(3, 1, 2, 3);
assertEquals(true, card2.isSet(card1, card3));
}

public void testSameShade() {
Card card1 = new Card(1, 3, 3, 2);
Card card2 = new Card(2, 2, 3, 2);
Card card3 = new Card(2, 3, 3, 2);
assertEquals(false, card3.isSet(card2, card1));
}




}
52 changes: 51 additions & 1 deletion src/Deck.java
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,59 @@
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
public ArrayList<Card> cards = new ArrayList<Card>(81);
public int nextCardIndex;

public Deck() {
//Deck d = new Deck()//
//while(d.hasNext())//
cards = new ArrayList<Card>(81);
for (int cardA = 1; cardA < 4; cardA++) {
for (int cardB = 1; cardB < 4; cardB++) {
for (int cardC = 1; cardC < 4; cardC++) {
for (int cardD = 1; cardD < 4; cardD++) {
cards.add(new Card(cardA, cardB, cardC, cardD));
}
}
}
}
nextCardIndex = 0;
Collections.shuffle(cards);

}

public boolean hasNext() {
//False when nextCardIndex is out of bounds, so when it is the same as the length//
//True if nextCardIndex is valid//
if (nextCardIndex >= cards.size()) {
return false;
}
else {
return true;
}


}

public Card getNext() {
//Got from notes taken in class :if hasNext() returns false, then it wil return null but if not increment index first then return one less than index//
//so then we would return (nextCardIndex - 1) if hasNext() is true//
if (hasNext() == true) {
nextCardIndex++;
return cards.get(nextCardIndex - 1);
}
else {
return null;
}



}



public Deck(String filename) {
cards = new ArrayList<Card>(81);
Expand Down
44 changes: 44 additions & 0 deletions src/DeckTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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 DeckTest 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 testDeckWorks() {
Deck d = new Deck();
assertEquals(81, d.cards.size());

}

public void testHasNext() {
Deck d = new Deck();
assertEquals(true, d.hasNext());
}

public void testEachCardsPosition() {
Deck d = new Deck("3cards.dat");

Card card1 = new Card(1, 2, 3, 1);
assertEquals(true, d.hasNext());
assertEquals(true, card1.equals(d.getNext()));

Card card2 = new Card(2, 1, 3, 2);
assertEquals(true, d.hasNext());
assertEquals(true, card2.equals(d.getNext()));

Card card3 = new Card(3, 3, 1, 2);
assertEquals(true, d.hasNext());
assertEquals(true, card3.equals(d.getNext()));

}

}
Loading