How to resolve the algorithm Set, the card game step by step in the Java programming language
How to resolve the algorithm Set, the card game step by step in the Java programming language
Table of Contents
Problem Statement
The card game, Set, is played with a pack of 81 cards, each of which depicts either one, two, or three diamonds, ovals, or squiggles. The symbols are coloured red, green, or purple, and the shading is either solid, striped, or open. No two cards are identical. In the game a number of cards are layed out face up and the players try to identify "sets" within the cards. A set is three cards where either the symbols on the cards are all the same or they are all different, the number of symbols on the cards are all the same or all different, the colours are all the same or all different, and the shadings are all the same or all different. For example, this is a set: because each card depicts a different symbol, the number of symbols on each card is different, the colours are all the same, and the shadings are all different. This is not a set: because two of the cards are green and one is purple, so the colours are neither all the same nor all different.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Set, the card game step by step in the Java programming language
Explanation of the Java Code
This Java program simulates a card game called Set, where players try to find sets of three cards that share specific characteristics.
Main Class: SetTheCardGame
main
method:- Creates a deck of 81 cards with varying numbers, colors, shadings, and shapes.
- Deals different numbers of cards (4, 8, and 12) to simulate gameplay.
- Identifies and prints any sets (three cards that meet specific criteria) found in the dealt cards.
Card Class
- Represents a single card with four attributes: number, color, shading, and shape.
- Has a
toString
method to display the card's attributes in a specific format.
Helper Methods
createPackOfCards
: Creates a list of 81 unique cards with all possible combinations of attributes.isGameSet
: Checks if a list of three cards meets the criteria for a set (all same or all different for each attribute).allSameOrAllDifferent
: Determines if all elements in a list are the same or all different.combinations
: Generates all possible combinations ofchoose
elements from a list.
Gameplay Simulation
- The
main
method deals different numbers of cards and iterates over them. - For each deal, it identifies and prints any sets found using the
combinations
andisGameSet
methods. - The sets are displayed in the console.
Output
The output of the program will vary depending on the card deals and the sets found. It will display the dealt cards and any sets identified, separated by number of cards dealt.
Example Output
Cards dealt: 4
[TWO RED SOLID OVAL]
[ONE RED SOLID DIAMOND]
[THREE GREEN OPEN SQUIGGLE]
[THREE RED STRIPED DIAMOND]
Sets found:
[ONE RED SOLID DIAMOND] [THREE RED STRIPED DIAMOND] [TWO RED SOLID OVAL]
------------------------
Cards dealt: 8
[THREE GREEN OPEN SQUIGGLE]
[TWO RED SOLID OVAL]
[THREE GREEN STRIPED DIAMOND]
[TWO GREEN OPEN SQUIGGLE]
[ONE GREEN OPEN DIAMOND]
[ONE PURPLE OPEN SQUIGGLE]
[TWO PURPLE SOLID SQUIGGLE]
[ONE PURPLE SOLID OVAL]
Sets found:
[ONE GREEN OPEN DIAMOND] [ONE PURPLE OPEN SQUIGGLE] [ONE PURPLE SOLID OVAL]
[ONE PURPLE OPEN SQUIGGLE] [TWO PURPLE SOLID SQUIGGLE] [THREE GREEN OPEN SQUIGGLE]
------------------------
Cards dealt: 12
[ONE GREEN OPEN SQUIGGLE]
[THREE GREEN STRIPED DIAMOND]
[ONE PURPLE OPEN SQUIGGLE]
[TWO GREEN OPEN SQUIGGLE]
[ONE GREEN OPEN DIAMOND]
[THREE PURPLE STRIPED SQUIGGLE]
[TWO PURPLE SOLID SQUIGGLE]
[ONE PURPLE SOLID OVAL]
[ONE RED SOLID DIAMOND]
[TWO RED SOLID OVAL]
[THREE RED STRIPED DIAMOND]
[ONE RED SOLID SQUIGGLE]
Sets found:
[ONE GREEN OPEN DIAMOND] [ONE PURPLE OPEN SQUIGGLE] [ONE PURPLE SOLID OVAL]
[ONE PURPLE OPEN SQUIGGLE] [TWO PURPLE SOLID SQUIGGLE] [THREE GREEN OPEN SQUIGGLE]
[ONE GREEN OPEN SQUIGGLE] [ONE RED SOLID SQUIGGLE] [THREE RED STRIPED DIAMOND]
[ONE RED SOLID DIAMOND] [THREE RED STRIPED DIAMOND] [TWO RED SOLID OVAL]
Source code in the java programming language
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public final class SetTheCardGame {
public static void main(String[] args) {
List<Card> pack = createPackOfCards();
for ( int cardCount : List.of( 4, 8, 12 ) ) {
Collections.shuffle(pack);
List<Card> deal = pack.subList(0, cardCount);
System.out.println("Cards dealt: " + cardCount);
for ( Card card : deal ) {
System.out.println(card);
}
System.out.println();
System.out.println("Sets found: ");
for ( List<Card> combination : combinations(deal, 3) ) {
if ( isGameSet(combination) ) {
for ( Card card : combination ) {
System.out.print(card + " ");
}
System.out.println();
}
}
System.out.println("-------------------------" + System.lineSeparator());
}
}
private static interface Feature {}
private static enum Number implements Feature { ONE, TWO, THREE }
private static enum Colour implements Feature { GREEN, RED, PURPLE }
private static enum Shading implements Feature { OPEN, SOLID, STRIPED }
private static enum Shape implements Feature { DIAMOND, OVAL, SQUIGGLE }
private static record Card(Number number, Colour colour, Shading shading, Shape shape) {
public String toString() {
return "[" + number + " " + colour + " " + shading + " " + shape + "]";
}
}
private static List<Card> createPackOfCards() {
List<Card> pack = new ArrayList<Card>(81);
for ( Number number : Number.values() ) {
for ( Colour colour : Colour.values() ) {
for ( Shading shading : Shading.values() ) {
for ( Shape shape : Shape.values() ) {
pack.add( new Card(number, colour, shading, shape) );
}
}
}
}
return pack;
}
private static boolean isGameSet(List<Card> triple) {
return allSameOrAllDifferent(triple.stream().map( c -> (Feature) c.number ).toList()) &&
allSameOrAllDifferent(triple.stream().map( c -> (Feature) c.colour ).toList()) &&
allSameOrAllDifferent(triple.stream().map( c -> (Feature) c.shading ).toList()) &&
allSameOrAllDifferent(triple.stream().map( c -> (Feature) c.shape ).toList());
}
private static boolean allSameOrAllDifferent(List<Feature> features) {
Set<Feature> featureSet = new HashSet<Feature>(features);
return featureSet.size() == 1 || featureSet.size() == 3;
}
private static <T> List<List<T>> combinations(List<T> list, int choose) {
List<List<T>> combinations = new ArrayList<List<T>>();
List<Integer> combination = IntStream.range(0, choose).boxed().collect(Collectors.toList());
while ( combination.get(choose - 1) < list.size() ) {
combinations.add(combination.stream().map( i -> list.get(i) ).toList());
int temp = choose - 1;
while ( temp != 0 && combination.get(temp) == list.size() - choose + temp ) {
temp -= 1;
}
combination.set(temp, combination.get(temp) + 1);
for ( int i = temp + 1; i < choose; i++ ) {
combination.set(i, combination.get(i - 1) + 1);
}
}
return combinations;
}
}
You may also check:How to resolve the algorithm Sequence of primorial primes step by step in the Clojure programming language
You may also check:How to resolve the algorithm Pernicious numbers step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Averages/Arithmetic mean step by step in the Lucid programming language
You may also check:How to resolve the algorithm Averages/Median step by step in the PL/I programming language
You may also check:How to resolve the algorithm Percentage difference between images step by step in the E programming language