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
96 changes: 96 additions & 0 deletions src/Card.java
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,6 +1,102 @@
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 isQuant, int isCol, int isShad, int isShap) {
quantity = fixValue(isQuant);
color = fixValue(isCol);
shading = fixValue(isShad);
shape = fixValue(isShap);

}

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


public int getQuantity() {

return quantity;
}

public int getColor() {

return color;
}

public int getShading() {

return shading;
}

public int getShape() {

return shape;
}

public boolean isSet(Card c1, Card c2) {
if ((((quantity + c1.getQuantity() + c2.getQuantity()) % 3) == 0) &&
(((color + c1.getColor() + c2.getColor()) % 3) == 0) &&
(((shading + c1.getShading() + c2.getShading()) % 3) == 0) &&
(((shape + c1.getShape() + c2.getShape()) % 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 += "O";
}

if (shape == 2) {
str += "D";
}

if (shape == 3) {
str += "S";
}
return str;

}

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

Expand Down
44 changes: 44 additions & 0 deletions src/CardTest.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 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 testCardWorks() {
Card c1 = new Card(1,2,3,2);
assertEquals("1GSD",c1.toString());

Card c2 = new Card(2,3,1,3);
assertEquals("2POS",c2.toString());

Card c3 = new Card(3,1,2,1);
assertEquals("3RTO",c3.toString());




}

public void testTruthSet() {
Card c1 = new Card(1,2,3,2);
Card c2 = new Card(2,3,1,3);
Card c3 = new Card(3,1,2,1);
assertEquals(true, c1.isSet(c2, c3));
}

public void testFalseSet() {
Card c1 = new Card(1,3,3,1);
Card c2 = new Card(3,3,3,3);
Card c3 = new Card(3,2,1,3);
assertEquals(false, c1.isSet(c2, c3));
}
}
35 changes: 35 additions & 0 deletions src/Deck.java
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,44 @@
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 ArrayList<Card> cards = new ArrayList<Card>(81);
private int nextCardIndex = 0;

public Deck() {
for ( int i = 1; i <= 3; i++) {
for (int a = 1; a <= 3; a++) {
for (int b = 1; b <= 3; b++) {
for (int c = 1; c<=3; c++) {
Card card = new Card(i,a,b,c);
}
}
}
}
Collections.shuffle(cards);
}

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

public Card getNext() {
if (hasNext() == false) {
return null;
}
else {
nextCardIndex += 1;
return cards.get(nextCardIndex - 1);
}
}

public Deck(String filename) {
cards = new ArrayList<Card>(81);
Expand Down
34 changes: 34 additions & 0 deletions src/DeckTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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 testDeckCard() {
Deck d = new Deck("threeCards.dat");
Card c1 = new Card(1,2,3,2);

assertEquals(true, d.getNext().equals(c1));
assertEquals(true, d.hasNext());

Card c2 = new Card(2,3,1,3);

assertEquals(true, d.getNext().equals(c2));
assertEquals(true, d.hasNext());

Card c3 = new Card(3,1,2,1);

assertEquals(true, d.getNext().equals(c3));
assertEquals(false, d.hasNext());
}

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

public Game() {

d = new Deck();
t = new Table();

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

public Game(String cardGame) {

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

public int numSets() {

return t.numSets();

}

public int numCards() {

return t.numCards();

}

public void playRound() {
//worked with Hailey Kester on the playRound in lab

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

if (t.numSets() > 0) {
for (int a = 0; a < t.numCards() - 2; a++) {
for (int b = a + 1; b < t.numCards() - 1; b++) {
for (int c = b + 1; c < t.numCards(); c++) {
Card c1 = t.getCard(a);
Card c2 = t.getCard(b);
Card c3 = t.getCard(c);

if (c1.isSet(c2,c3)) {
t.removeSet(c1, c2, c3);
if (t.numCards() < 12 && d.hasNext()) {
for (int i = 0; i < 3; i++) {
if(d.hasNext() == true) {
t.add(d.getNext());
}
return;
}
}
}
}
}
}
}
}











public boolean isGameOver() {

if (d.hasNext() == false && t.numSets() == 0) {
return true;
}

else {
return false;
}
}
}




24 changes: 24 additions & 0 deletions src/GameTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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 GameTest 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 testGameRound() {

Game g1 = new Game();
assertEquals(g1.numCards(), 12);
g1.playRound();

}

}
26 changes: 26 additions & 0 deletions src/MonteCarloSimulation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
public class MonteCarloSimulation {

public static void main(String[] args) {

double avgSets = 0;
int a = 0;
while (a <= 1000001) {
Game g1 = new Game();
avgSets += g1.numSets();
}
System.out.println("Avg # of sets is " + avgSets);

double avgCards = 0;
int b = 0;
while (b <= 100001) {
Game g2 = new Game();
while(g2.isGameOver() == false) {
g2.playRound();
}
avgCards += g2.numCards();
a++;
}
System.out.println("Avg # of cards is " + avgCards);
}
}

Loading