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


public boolean equals(Object obj) {
public Card(int number, int colorObject, int shadetype, int shapeKind ){
quantity = changeValue(number);
color = changeValue(colorObject);
shade = changeValue(shadetype);
shape = changeValue(shapeKind);
}

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


public int getQuantity(){

return quantity;
}

public int getColor(){

return color;
}

public int getShading(){

return shade;
}

public int getShape(){

return shape;
}

public boolean isSet (Card cardM,Card cardMM){
if (((((color + cardM.getColor() + cardMM.getColor()) % 3) == 0)) &&
(((quantity + cardM.getQuantity() + cardMM.getQuantity()) % 3) == 0) &&
(((shade + cardM.getShading() + cardMM.getShading()) % 3) == 0) &&
(((shape + cardM.getShape() + cardMM.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(shade == 1) {
str += "O";
}
if(shade == 2) {
str += "T";
}
if(shade == 3) {
str += "S";
}
if(shape == 1) {
str += "O";
}
if(shape == 2) {
str += "D";
}
if(shape == 3) {
str += "S";
}
return str;
}

private int fixValue(int valueToFix) {
if (valueToFix < 1 || valueToFix > 3)
return (((valueToFix % 3) + 3) % 3) + 1;
else
return valueToFix;
}
public boolean equals(Object obj) {
Card that = (Card)obj;

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










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

public class CardTest extends TestCase {

public void testCard() {
Card c1 = new Card(3,2,3,2);
assertEquals("3GSD",c1.toString());

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

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

public void testTrueSet() {
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));
}
}
50 changes: 42 additions & 8 deletions src/Deck.java
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,47 @@
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 = 0;
private ArrayList<Card> cards;

public Deck(){
cards = new ArrayList<Card>(81);

for(int mar1 = 1; mar1 <= 3; mar1++){
for (int mar2 = 1; mar2<= 3; mar2++){
for (int mar3 = 1; mar3 <= 3; mar3++){
for(int mar4 = 1; mar4 <= 3; mar4++){
cards.add(new Card(mar1, mar2, mar3, mar4));
nextCardIndex=0;
}
}
}
}
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 All @@ -15,26 +52,23 @@ public Deck(String filename) {
int position = 0;

while((line = infile.readLine()) != null) {
// Blank lines might contain white space, so trim it off

line = line.trim();

// ignore blank lines
if(line.length() == 0)
continue;

// ignore comments
if(line.startsWith("#"))
continue;

// a valid line contains 4 integers

StringTokenizer tokenizer = new StringTokenizer(line);

int quantity = Integer.parseInt(tokenizer.nextToken());
int color = Integer.parseInt(tokenizer.nextToken());
int shading = Integer.parseInt(tokenizer.nextToken());
int shade = Integer.parseInt(tokenizer.nextToken());
int shape = Integer.parseInt(tokenizer.nextToken());

cards.add(new Card(quantity, color, shading, shape));
cards.add(new Card(quantity, color, shade, shape));
nextCardIndex = 0;
}
}
Expand Down
43 changes: 43 additions & 0 deletions src/Decktest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import junit.framework.TestCase;

public class Decktest extends TestCase {

public void testHasNextGetNext0Par() {
// Tests Deck() with hasNext() and getNext
Deck d = new Deck();
Card c;
int sumQuan = 0;
int sumCo = 0;
int sumShade = 0;
int sumShape = 0;
int sumWhole = 0;
int total = 0;
while(d.hasNext()){
c = d.getNext();
sumQuan += c.getQuantity();
sumCo += c.getColor();
sumShade += c.getShade();
sumShape += c.getShape();
total++;
}
sumWhole = sumQuan + sumCo + sumShade + sumShape;
assertEquals(162, sumQuan);
assertEquals(162, sumCo);
assertEquals(162, sumShade);
assertEquals(162, sumShape);
assertEquals(648, sumWhole);
assertEquals(81, total);
}
public void testDeckHasGetNextDat(){
// Tests hasNext() and getNext() methods with Deck(file.dat)
Deck d = new Deck("3cardNew.dat");

assertEquals(true, d.hasNext());
assertEquals("1GSO", d.getNext().toString());
assertEquals(true, d.hasNext());
assertEquals("2RTS", d.getNext().toString());
assertEquals(true, d.hasNext());
assertEquals("3RTO", d.getNext().toString());
assertEquals(false, d.hasNext());
}
}
89 changes: 89 additions & 0 deletions src/Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
public class Game {
private Deck d;
private Table t;

public Game (){
d = new Deck();
t = new Table();

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

public Game(String str){
d = new Deck(str);
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 (t.numSets() == 0 && d.hasNext() == true) {
for (int i = 0; i < 3; i++) {
if(d.hasNext() == true) {
t.add(d.getNext());
}
}
return;
}
if (d.hasNext() == true && t.numSets() != 0) {
for (int mar1 = 0; mar1 < t.numCards() - 2; mar1++){
for (int mar2 = mar1 + 1; mar2 < t.numCards() - 1; mar2++){
for (int mar3 = mar2 + 1; mar3 < t.numCards(); mar3++){

if (t.getCard(mar1).isSet(t.getCard(mar2), t.getCard(mar3))){
t.removeSet(t.getCard(mar1), t.getCard(mar2), t.getCard(mar3));

while (t.numCards() < 12)
{
if (d.hasNext() == false)
return;

t.add(d.getNext());
}
return;
}
}
}
}
}

if (t.numSets() != 0 && d.hasNext() == false){
for (int mar1 = 0; mar1 < t.numCards() - 2; mar1++){
for (int mar2 = mar1 + 1; mar2 < t.numCards() - 1; mar2++){
for (int mar3 = mar2 + 1; mar3 < t.numCards(); mar3++){

if (t.getCard(mar1).isSet(t.getCard(mar2), t.getCard(mar3))){
t.removeSet(t.getCard(mar1), t.getCard(mar2), t.getCard(mar3));
return;
}
}
}
}
return;
}
}




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

return false;
}
}




27 changes: 27 additions & 0 deletions src/MonteCarloSim.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
public class MonteCarloSim
{
public static void main(String[] args)
{
double nStartSets = 0;
double nEndingSets = 0;
double nCards = 0;

for (int i = 0; i < 1000000; i++)
{
Game game = new Game();
nCards += game.numCards();
nStartSets += game.numSets();

while (!game.isGameOver())
{
game.playRound();
}

nEndingSets += game.numCards();
}

System.out.println(nStartSets/1000000);
System.out.println(nEndingSets/1000000);
System.out.println(nCards/1000000);
}
}
Loading