How to resolve the algorithm Set, the card game step by step in the Raku programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Set, the card game step by step in the Raku 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 Raku programming language

Source code in the raku programming language

my @attributes = , , , ;

sub face ($_) { .polymod(3 xx 3).kv.map({ @attributes[$^k;$^v] }) ~ ('s' if $_%3) }

sub sets (@cards) { @cards.combinations(3).race.grep: { !(sum ([Z+] $_».polymod(3 xx 3)) »%» 3) } }

for 4,8,12 -> $deal {
    my @cards = (^81).pick($deal);
    my @sets = @cards.&sets;
    say "\nCards dealt: $deal";
    for @cards { put .&face };
    say "\nSets found: {+@sets}";
    for @sets { put .map(&face).join("\n"), "\n" };
}

say "\nIn the whole deck, there are {+(^81).&sets} sets.";


  

You may also check:How to resolve the algorithm Array concatenation step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Documentation step by step in the Lambdatalk programming language
You may also check:How to resolve the algorithm Hash from two arrays step by step in the Clojure programming language
You may also check:How to resolve the algorithm Pick random element step by step in the GW-BASIC programming language
You may also check:How to resolve the algorithm Approximate equality step by step in the Factor programming language