How to resolve the algorithm Set puzzle step by step in the C# programming language
How to resolve the algorithm Set puzzle step by step in the C# programming language
Table of Contents
Problem Statement
Set Puzzles are created with a deck of cards from the Set Game™. The object of the puzzle is to find sets of 3 cards in a rectangle of cards that have been dealt face up. There are 81 cards in a deck. Each card contains a unique variation of the following four features: color, symbol, number and shading. Three cards form a set if each feature is either the same on each card, or is different on each card. For instance: all 3 cards are red, all 3 cards have a different symbol, all 3 cards have a different number of symbols, all 3 cards are striped. There are two degrees of difficulty: basic and advanced. The basic mode deals 9 cards, that contain exactly 4 sets; the advanced mode deals 12 cards that contain exactly 6 sets. When creating sets you may use the same card more than once.
Write code that deals the cards (9 or 12, depending on selected mode) from a shuffled deck in which the total number of sets that could be found is 4 (or 6, respectively); and print the contents of the cards and the sets. For instance: DEALT 9 CARDS:
CONTAINING 4 SETS:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Set puzzle step by step in the C# programming language
The above code implements a Set puzzle game in C#.
-
The
Feature
Struct: It represents a specific attribute of a card, such as its number, color, shading, or symbol. Each feature has an integerValue
and a stringName
. -
The
Card
Struct: It represents a single card in the game. Each card has four features:Number
,Color
,Shading
, andSymbol
. It has anEquals
method for comparing cards and aToString
method for printing their descriptions. -
Main
Method:- Creates a
deck
of all possible cards using a LINQ comprehension. - Deals cards into two different hands: one with 9 cards and 4 sets, and another with 12 cards and 6 sets.
- Creates a
-
Deal
Method:- Shuffles the deck and deals a specific number of cards based on the given
size
andtarget
number of sets. - Finds and prints all sets of three cards that meet the game criteria (described in
IsSet
).
- Shuffles the deck and deals a specific number of cards based on the given
-
Shuffle
Method:- Shuffles an array using the Fisher-Yates shuffling algorithm.
-
IsSet
Method:- Determines if a given set of three cards is valid. The criteria are as follows:
- Either all three features of the same type (e.g., numbers) are the same, or all three are different.
- This applies to all four features (number, color, shading, and symbol).
- Determines if a given set of three cards is valid. The criteria are as follows:
-
AreSameOrDifferent
Method:- Checks if three integers are either all the same or all different based on their sum modulo 3.
-
To
Extension Method:- Creates a range of integers from a given
start
toend
(excludingend
).
- Creates a range of integers from a given
Overall, this program simulates a Set puzzle game by generating a deck of cards, shuffling them, dealing hands, and finding and displaying sets of cards that meet the specified criteria.
Source code in the csharp programming language
using System;
using System.Collections.Generic;
using static System.Linq.Enumerable;
public static class SetPuzzle
{
static readonly Feature[] numbers = { (1, "One"), (2, "Two"), (3, "Three") };
static readonly Feature[] colors = { (1, "Red"), (2, "Green"), (3, "Purple") };
static readonly Feature[] shadings = { (1, "Open"), (2, "Striped"), (3, "Solid") };
static readonly Feature[] symbols = { (1, "Oval"), (2, "Squiggle"), (3, "Diamond") };
private readonly struct Feature
{
public Feature(int value, string name) => (Value, Name) = (value, name);
public int Value { get; }
public string Name { get; }
public static implicit operator int(Feature f) => f.Value;
public static implicit operator Feature((int value, string name) t) => new Feature(t.value, t.name);
public override string ToString() => Name;
}
private readonly struct Card : IEquatable<Card>
{
public Card(Feature number, Feature color, Feature shading, Feature symbol) =>
(Number, Color, Shading, Symbol) = (number, color, shading, symbol);
public Feature Number { get; }
public Feature Color { get; }
public Feature Shading { get; }
public Feature Symbol { get; }
public override string ToString() => $"{Number} {Color} {Shading} {Symbol}(s)";
public bool Equals(Card other) => Number == other.Number && Color == other.Color && Shading == other.Shading && Symbol == other.Symbol;
}
public static void Main() {
Card[] deck = (
from number in numbers
from color in colors
from shading in shadings
from symbol in symbols
select new Card(number, color, shading, symbol)
).ToArray();
var random = new Random();
Deal(deck, 9, 4, random);
Console.WriteLine();
Console.WriteLine();
Deal(deck, 12, 6, random);
}
static void Deal(Card[] deck, int size, int target, Random random) {
List<(Card a, Card b, Card c)> sets;
do {
Shuffle(deck, random.Next);
sets = (
from i in 0.To(size - 2)
from j in (i + 1).To(size - 1)
from k in (j + 1).To(size)
select (deck[i], deck[j], deck[k])
).Where(IsSet).ToList();
} while (sets.Count != target);
Console.WriteLine("The board:");
foreach (Card card in deck.Take(size)) Console.WriteLine(card);
Console.WriteLine();
Console.WriteLine("Sets:");
foreach (var s in sets) Console.WriteLine(s);
}
static void Shuffle<T>(T[] array, Func<int, int, int> rng) {
for (int i = 0; i < array.Length; i++) {
int r = rng(i, array.Length);
(array[r], array[i]) = (array[i], array[r]);
}
}
static bool IsSet((Card a, Card b, Card c) t) =>
AreSameOrDifferent(t.a.Number, t.b.Number, t.c.Number) &&
AreSameOrDifferent(t.a.Color, t.b.Color, t.c.Color) &&
AreSameOrDifferent(t.a.Shading, t.b.Shading, t.c.Shading) &&
AreSameOrDifferent(t.a.Symbol, t.b.Symbol, t.c.Symbol);
static bool AreSameOrDifferent(int a, int b, int c) => (a + b + c) % 3 == 0;
static IEnumerable<int> To(this int start, int end) => Range(start, end - start - 1);
}
You may also check:How to resolve the algorithm Bitwise operations step by step in the MAXScript programming language
You may also check:How to resolve the algorithm Sequence of primes by trial division step by step in the Eiffel programming language
You may also check:How to resolve the algorithm JSON step by step in the Oz programming language
You may also check:How to resolve the algorithm Averages/Arithmetic mean step by step in the CoffeeScript programming language
You may also check:How to resolve the algorithm Vector products step by step in the AppleScript programming language