Question: Creating a version of the card game SET in Java, using classes Card, Triad, Group, Deck, and Simulation. Below I have the classes Card, Triad,

Creating a version of the card game SET in Java, using classes Card, Triad, Group, Deck, and Simulation. Below I have the classes Card, Triad, Deck, and Group but I'm having trouble with the Simulation class and looking for some help. In my version the Simulation class is the main method and this class should allow the user to input a number of cards to deal, and after dealing out that number of cards 10,000 times print how many times there was at least one set and what was the average number of sets dealt. In the card game a SET is defined as a set if with respect to each of the four attributes, the cards are either all the same or all dierent. For Example: First Card is one green oval with a solid shading, Second Card has two green ovals with solid shading and the Third Card has three green ovals with solid shading, all cards have the same shape (ovals), the same color (green), and the same shading (solid), and each card has a dierent number of ovals. If that Third Card was three green squiggles with solid shading it wouldn't be a set because the shape wasn't an oval.

public class Card

{ //public enum declarations public enum Shape{Oval, Squiggle, Diamond}; public enum Shading{Solid, Striped, Outlined}; public enum Number{One, Two, Three}; public enum Color{Red, Green, Purple}

// class number variable declaration Shading shading; Color color; Shape shape; Number number; //Constructor public Card(Shape shape, Color color, Shading shading, Number number) { this.shape = shape; this.color = color; this.shading = shading; this.number = number; }

public Shading getShading() { return shading; } public Shape getShape() { return shape; } public Color getColor() { return color; } public Number getNumber() { return number; } @Override public String toString() { String cardString = ""; cardString += shape +" "; cardString += color +" "; cardString += shading +" "; cardString += number; return cardString; } } ***************************************************************

public class Triad { private Card[] cards = new Card[3];

public Triad(Card cardOne, Card cardTwo, Card cardThree) { cards[0] = cardOne; cards[1] = cardTwo; cards[2] = cardThree; }

private boolean allSameOrDifferent(Enum a, Enum b, Enum c) { if (a == b && b == c) return true; else if (a != b && a != c && b != c) return true; else return false; }

public boolean isSet() { if (allSameOrDifferent(cards[0].getShape(),cards[1].getShape(),cards[2].getShape()) && allSameOrDifferent(cards[0].getColor(),cards[1].getColor(),cards[2].getColor()) && allSameOrDifferent(cards[0].getShading(),cards[1].getShading(),cards[2].getShading()) && allSameOrDifferent(cards[0].getNumber(),cards[1].getNumber(),cards[2].getNumber())) return true; else return false; }

@Override public String toString() { String triadString = ""; String[] triadCards = new String[0]; for (Card card: cards){ if (triadCards.length == 0){ triadCards = card.toString().split(" "); for (int i = 0; i < triadCards.length; i ++) { triadCards[i] = String.format("%-20s",triadCards[i]); } } else { String [] cardStrings = card.toString().split(" "); for (int i = 0; i < cardStrings.length; i++){ triadCards[i] += " | " + String.format("%-20s",cardStrings[i]); } } } triadString += String.join(" ",triadCards); return triadString; }

public static void main(String[] args){ Card testCard = new Card(Card.Shape.Squiggle,Card.Color.Purple,Card.Shading.Striped,Card.Number.Three); Card testCard2 = new Card(Card.Shape.Diamond,Card.Color.Purple,Card.Shading.Striped,Card.Number.Three); Card testCard3 = new Card(Card.Shape.Diamond,Card.Color.Purple,Card.Shading.Striped,Card.Number.Three); Triad testTriad = new Triad(testCard, testCard2, testCard3); System.out.println(testTriad); System.out.println(testTriad.isSet());

} } **************************************************************

import java.util.Collections;

public class Deck extends Group { public Deck() { super(); for(Card.Shape shape: Card.Shape.values()) { for(Card.Color color: Card.Color.values()) { for(Card.Shading shading: Card.Shading.values()) { for(Card.Number number: Card.Number.values()) { addCard(new Card(shape, color, shading, number)); } } } } }

public boolean hasCardsRemaining() { return cards.size() > 0; }

public Card draw() { return cards.remove(cards.size() - 1); }

public void shuffle() { Collections.shuffle(cards); }

public static void main(String[] args) { Deck testDeck = new Deck(); System.out.println(testDeck.getNumCards()); testDeck.printSets(); } } *******************************************************************************

import java.util.ArrayList; import java.util.Iterator;

public class Group implements Iterable {

protected ArrayList cards = new ArrayList<>(); protected ArrayList sets = new ArrayList<>(); protected boolean dirty = false;

public void addCard(Card card) { dirty = true; cards.add(card); }

public int getNumCards() { return cards.size(); }

private void updateSets(){ for (int i=0; i< cards.size(); i++) { for(int j=i+1; j < cards.size(); j++) { for(int k=j+1; k< cards.size(); k++) { Triad possibleSet = new Triad(cards.get(i),cards.get(j),cards.get(k)); if (possibleSet.isSet()) { sets.add(possibleSet); } } } }

dirty = false; }

public int getNumSets() { if (dirty) updateSets(); return sets.size(); }

public ArrayList getSets() { if (dirty) updateSets(); return sets; }

public void printSets() { if (dirty) updateSets(); System.out.println("There are " + sets.size() + " sets in this group."); for (Triad set:sets) { System.out.println(set); System.out.println(); } }

@Override public Iterator iterator(){ return cards.iterator(); }

@Override public String toString() { String outputString = ""; outputString += "There are " + cards.size() + " cards in this group: "; for(Card card: cards){ outputString += "------------ "; outputString += card; } return outputString; }

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!