How to resolve the algorithm Set, the card game step by step in the Ruby programming language
How to resolve the algorithm Set, the card game step by step in the Ruby 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 Ruby programming language
The provided code is a Ruby program that deals a hand of cards and checks for sets of three cards that share a common attribute (number, shading, color, or symbol) while having different values for the other three attributes. A detailed explanation of the code is as follows:
-
Constants and Data Structures:
ATTRIBUTES
is an array of attribute names ([:number, :shading, :colour, :symbol]
) used to represent the attributes of a card.Card
is aStruct
that takes the attribute names as arguments and defines ato_s
method to represent a card as a string by joining its attribute values with a space.combis
is a list of combinations of attributes using%i[]
for symbols, resulting in a list of tuples ([:one, :solid, :red, :diamond]
,[:one, :solid, :red, :oval]
, and so on).PACK
is an array ofCard
objects created by mapping thecombis
list toCard.new
to construct all possible cards.
-
Helper Method:
set?
is a helper method that takes a trio of cards and checks if they are a set by verifying that each attribute has unique values among the three cards.
-
Main Logic:
- The code deals hands of different sizes (4, 8, and 12) by sampling from
PACK
and prints the dealt hand. - It then finds sets within the hand using
combination(3)
to generate all possible combinations of three cards and filters them using theset?
method. - The number of sets found is printed, and each set is printed on a separate line.
- The code deals hands of different sizes (4, 8, and 12) by sampling from
The program demonstrates a concise and efficient way to deal cards, check for sets, and display the results.
Source code in the ruby programming language
ATTRIBUTES = [:number, :shading, :colour, :symbol]
Card = Struct.new(*ATTRIBUTES){ def to_s = values.join(" ") }
combis = %i[one two three].product(%i[solid striped open], %i[red green purple], %i[diamond oval squiggle])
PACK = combis.map{|combi| Card.new(*combi) }
def set?(trio) = ATTRIBUTES.none?{|attr| trio.map(&attr).uniq.size == 2 }
[4, 8, 12].each do |hand_size|
puts "#{"_"*40}\n\nCards dealt: #{hand_size}"
puts hand = PACK.sample(hand_size)
sets = hand.combination(3).select{|h| set? h }
puts "\n#{sets.size} sets found"
sets.each{|set| puts set, ""}
end
You may also check:How to resolve the algorithm Sum digits of an integer step by step in the Lua programming language
You may also check:How to resolve the algorithm Greedy algorithm for Egyptian fractions step by step in the RPL programming language
You may also check:How to resolve the algorithm State name puzzle step by step in the Wren programming language
You may also check:How to resolve the algorithm Perfect shuffle step by step in the R programming language
You may also check:How to resolve the algorithm Determine if a string is squeezable step by step in the REXX programming language