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
3 changes: 3 additions & 0 deletions src/3card.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
2 0 3 1
3 1 2 1
1 3 2 0
113 changes: 111 additions & 2 deletions src/Card.java
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,12 +1,121 @@
public class Card {
// Create the rest of this class yourself
public int quantity;
public int color;
public int shading;
public int shape;

public Card (int cardQuantity, int cardColor, int cardShading, int cardShape){
quantity = formValue(cardQuantity);
color = formValue(cardColor);;
shading = formValue(cardShading);;
shape = formValue(cardShape);;
}

private int formValue(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 cardA, Card cardB) {
if ((((quantity + cardA.getQuantity() + cardB.getQuantity())%3)==0) &&
(((color + cardA.getColor() + cardB.getColor())%3)==0) &&
(((shading + cardA.getShading() + cardB.getShading())%3)==0) &&
(((shape + cardA.getShape() + cardB.getShape())%3)==0)) {
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 = "";
String quant = "";
String col = "";
String shade = "";
String sha = "";

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

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

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

if (shape == 1) {
str += "O";
}
else if (shape == 2) {
str += "D";
}
else {
str += "S";
}

return str = quant+ col + shade + sha;
}

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

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

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

pubic class CardTest extends TestCase{

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

public void testNotSet() {
Card c1 = new Card(1, 1, 1, 3);
Card c2 = new Card(2, 2, 3, 2);
Card c3 = new Card(3, 2, 2, 3);
assertEquals(false, c1.isSet(c2, c3));
}
}
39 changes: 39 additions & 0 deletions src/Deck.java
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,47 @@
import java.io.FileReader;
import java.util.StringTokenizer;
import java.util.ArrayList;
import java.util.Collections;


public class Deck {
public ArrayList<Card> cards;
public int nextCardIndex;

public Deck() {
cards = new ArrayList<Card>(81);
for(int var1 = 1; var1 <= 3; var1++) {
for(int var2 = 1; var2 <= 3; var2++) {
for(int var3 = 1; var3 <= 3; var3++) {
for(int var4 = 1; var4 <= 3; var4++) {
cards.add(new Card(var1, var2, var3, var4));
}
}
}
}
int nextCardIndex = 0;
Collections.shuffle(cards);
}

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

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

// Implement the rest of this class yourself

public Deck(String filename) {
Expand Down
28 changes: 28 additions & 0 deletions src/DeckTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import junit.framework.TestCase;
public class DeckTest extends TestCase {

public void testWorking() {
Deck d = new Deck();
assertEquals(81, d.cards.size());
}

public void testhasNext() {
Deck d = new Deck();
assertTrue(d.hasNext());
}

public void testDeckCards() {
Deck d = new Deck();
Card c1 = new Card(2, 3, 0, 1);
assertTrue(d.hasNext());
assertEquals(false, c1.equals(d.getNext()));
Card c2 = new Card(3, 1, 2, 1);
assertTrue(d.hasNext());
assertEquals(false, c2.equals(d.getNext()));
Card c3 = new Card(1, 3, 2, 1);
assertTrue(d.hasNext());
assertEquals(false, c3.equals(d.getNext()));
}

}

95 changes: 95 additions & 0 deletions src/Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
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 filename) {
d = new Deck(filename);
t = new Table();
for (int i=0; i < 12 && d.hasNext(); i++) {
t.add(d.getNext());
}
}

public int numSets() {
return t.numSets();
}

public int numCards() {
return t.numCards();
}

public void playRound() {
if (d.hasNext() == true && t.numSets() == 0) {
for (int r = 0; r < 3; r++) {
if (d.hasNext() == false)
return;
t.add(d.getNext());
}
return;
}

if (d.hasNext() == true && t.numSets() != 0) {
for (int i = 0; i < t.numCards() - 2; i++) {
for (int j = i + 1; j < t.numCards() - 1; j++) {
for (int k = j + 1; k < t.numCards(); k++) {
if (t.getCard(i).isSet(t.getCard(j), t.getCard(k)) == true) {
t.removeSet(t.getCard(i), t.getCard(j), t.getCard(k));
for (int r = 0; t.numCards() < 12; r++) {
if (d.hasNext() == false) {
return;
}
t.add(d.getNext());
}
return;
}
}
}
}
}

if (t.numCards() > 12 && t.numSets() > 0) {
for (int i = 0; i < t.numCards() - 2; i++) {
for (int j = i + 1; j < t.numCards() - 1; j++) {
for (int k = j + 1; k < t.numCards(); k++) {
if (t.getCard(i).isSet(t.getCard(j), t.getCard(k)) == true) {
t.removeSet(t.getCard(i), t.getCard(j), t.getCard(k));
return;
}
}
}
}
}


if (d.hasNext() == false && t.numSets() != 0) {
for (int i = 0; i < t.numCards() - 2; i++) {
for (int j = i + 1; j < t.numCards() - 1; j++) {
for (int k = j + 1; k < t.numCards(); k++) {
if (t.getCard(i).isSet(t.getCard(j), t.getCard(k))) {
t.removeSet(t.getCard(i), t.getCard(j), t.getCard(k));
return;
}
}
}
}
}
return;
}

public boolean isGameOver() {
if (!d.hasNext() && t.numSets() == 0) {
return true;
}
else {
return false;
}
}
}
7 changes: 7 additions & 0 deletions src/GameTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import junit.framework.TestCase;
public class GameTest extends TestCase {
public void testThisGame() {
Game g = new Game();
assertEquals(g.numCards(), 12);
}
}
21 changes: 21 additions & 0 deletions src/MonteCarloSim.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
public class MonteCarloSim {
public static void main(String[] args) {
double countSet = 0;
double countCard = 0;
int simulation = 10000;

for (int i = 0; i < simulation; i++) {
Game g = new Game();
countSet += g.numSets();

while (g.isGameOver() == false) {
g.playRound();
}
countCard += g.numCards();
}
System.out.println("The average number of sets is:" + countSet/simulation);
System.out.println("The average number of cards is:" + countCard/simulation);
}
}


Loading