Skip to content
81 changes: 69 additions & 12 deletions src/Card.java
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,12 +1,69 @@
public class Card {
// Create the rest of this class yourself

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

return quantity == that.getQuantity() &&
color == that.getColor() &&
shading == that.getShading() &&
shape == that.getShape();
}
}
public class Card{
private int quantity;
private int color;
private int shading;
private int shape;

public Card(int cardQuantity, int cardColor, int cardShading, int cardShape){
quantity = ((((cardQuantity % 3)+3)%3)+1);
color = ((((cardColor % 3)+3)%3)+1);
shading = ((((cardShading % 3)+3)%3)+1);
shape = ((((cardShape % 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 cardB, Card cardC){
return
(((quantity + cardB.getQuantity() + cardC.getQuantity()) % 3) == 0) &&

(((color + cardB.getColor() + cardC.getColor()) % 3) == 0) &&

(((shading + cardB.getShading() + cardC.getShading()) % 3) == 0) &&

(((shape + cardB.getShape() + cardC.getShape()) % 3) == 0);
}

public String toString(){
String[] Scolor = new String[3];
Scolor[0] = "R";
Scolor[1] = "G";
Scolor[2] = "P";

String[] Sshading = new String[3];
Sshading[0] = "O";
Sshading[1] = "T";
Sshading[2] = "S";

String[] Sshape = new String[3];
Sshape[0] = "O";
Sshape[1] = "D";
Sshape[2] = "S";

return quantity + Scolor[color-1] + Sshading[shading-1] + Sshape[shape-1];
}

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

return quantity == that.getQuantity() &&
color == that.getColor() &&
shading == that.getShading() &&
shape == that.getShape();
}
}
// d
46 changes: 46 additions & 0 deletions src/CardTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import junit.framework.TestCase;

public class CardTest extends TestCase{

public void testCardInt(){

Card c1 = new Card(7,1,1,1);

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

Card card3 = new Card(0, -1, -2, -3);
assertEquals(1, card3.getQuantity());
assertEquals(3, card3.getColor());
assertEquals(2, card3.getShading());
assertEquals(1, card3.getShape());
}

public void testSet(){

Card cardA = new Card(1,1,1,1);
Card cardB = new Card(2,1,1,1);
Card cardC = new Card(3,1,1,1);
Card cardD = new Card(1,1,1,3);

assertEquals(true, cardA.isSet(cardB, cardC));
assertEquals(false, cardA.isSet(cardC, cardD));
}

public void testCardString(){

Card card1 = new Card(0, 1, 1, 1);
Card card2 = new Card(-2, -4, 0, -5);
Card card3 = new Card(-4, -3, -8, 8);

assertEquals("1GTD", card1.toString());
assertEquals("2POD", card2.toString());
assertEquals("3RTS", card3.toString());
}

}



49 changes: 46 additions & 3 deletions src/Deck.java
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
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>();
private int nextCardIndex;

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

try {
String line;
BufferedReader infile = new BufferedReader(new FileReader(filename));
int position = 0;
Expand Down Expand Up @@ -42,4 +44,45 @@ public Deck(String filename) {
throw new RuntimeException("Error while reading file: " + e.toString());
}
}

public Deck(){
for (int quantity = 1; quantity < 4; quantity++) {
for (int color = 1; color < 4; color++) {
for (int shading = 1; shading < 4; shading++) {
for (int shape = 1; shape < 4; shape++) {
cards.add(new Card(quantity, color, shading, shape));
}
}
}
}
Collections.shuffle(cards);
}

public boolean hasNext(){
//for (nextCardIndex = 0; nextCardIndex <= cards.size(); nextCardIndex++){
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);
}
}
}





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

while (d.hasNext() == true){
t.add(d.getNext());

if (t.numCards() > 11){
return;
}
}
}

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

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

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

if (d.hasNext() && t.numSets() != 0){
for (int x = 0; x < t.numCards() - 2; x++){
for (int y = x + 1; y < t.numCards() - 1; y++){
for (int z = y + 1; z < t.numCards(); z++){
if (t.getCard(x).isSet(t.getCard(y), t.getCard(z))){
t.removeSet(t.getCard(x), t.getCard(y), t.getCard(z));

if (t.numCards() < 12 && d.hasNext()) {
for (int o = 0; o < 3; o++) {
if (d.hasNext()) {
t.add(d.getNext());
}
else {
return;
}
}
}
return;
}
}
}
}
}
}
//f
public boolean isGameOver(){
if (d.hasNext() == false && t.numSets() == 0){
return true;
}
else{
return false;
}
}
}
32 changes: 32 additions & 0 deletions src/MCSim.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
public class MCSim{
public static void main (String[] args){

double setCount = 0;
double cardCount = 0;
int sim = 100001;

for (int i = 0; i <= sim; i++){

Game game1 = new Game();
setCount += game1.numSets();

while (game1.isGameOver()){
game1.playRound();
}
}

System.out.println("The average number of sets from 100,000 random sets of cards is " + setCount / sim);

for (int i = 0; i <= sim; i++){

Game game2 = new Game();
cardCount += game2.numCards();

while (game2.isGameOver()){
game2.playRound();
}
}

System.out.println("The average number of cards on the table when the game ends is " + cardCount / sim);
}
}
Loading