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

Published on 12 May 2024 09:40 PM

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

Source code in the wren programming language

import "random" for Random
import "./ioutil" for Input
import "./fmt" for Fmt
import "./perm" for Comb

var nums = ["one", "two", "three"]
var shas = ["solid", "striped", "open"]
var cols = ["red", "green", "purple"]
var syms = ["diamond", "oval", "squiggle"]

var pack = List.filled(81, null)
var i = 0
for (num in 0..2) {
    for (sha in 0..2) {
        for (col in 0..2) {
            for (sym in 0..2) {
                pack[i] = [nums[num], shas[sha], cols[col], syms[sym]]
                i = i + 1
            }
        }
    }
}

var printCards = Fn.new { |cards|
    for (card in cards) {
        var pl = card[0] != "one" ? "s" : ""
        Fmt.print("$s $s $s $s$s", card[0], card[1], card[2], card[3], pl)
    }
}

var findSets = Fn.new { |cards|
    var sets = []
    var trios = Comb.list(cards, 3)
    for (trio in trios) {
        var t1 = trio[0]
        var t2 = trio[1]
        var t3 = trio[2]
        var found = true
        for (i in 0..3) {
            if (t1[i] == t2[i] && t2[i] == t3[i]) continue
            if (t1[i] != t2[i] && t2[i] != t3[i] && t1[i] != t3[i]) continue
            found = false
            break
        }
        if (found) sets.add(trio)
    }
    Fmt.print("Sets present: $d\n", sets.count)
    if (sets.count > 0) {
        for (set in sets) {
            printCards.call(set)
            System.print()
        }
    }
}

var prompt = "Enter number of cards to deal - 3 to 81 or q to quit: "
Input.quit = "q"
while(true) {
    Random.new().shuffle(pack) // shuffle for each deal
    var i = Input.integer(prompt, 3, 81)
    if (i == Input.quit) return
    var dealt = pack[0...i]
    System.print()
    printCards.call(dealt)
    System.print()
    findSets.call(dealt)
}

  

You may also check:How to resolve the algorithm Increment a numerical string step by step in the Eiffel programming language
You may also check:How to resolve the algorithm Polyspiral step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Fixed length records step by step in the Go programming language
You may also check:How to resolve the algorithm Letter frequency step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Anagrams/Deranged anagrams step by step in the Go programming language