Set, the card game: Difference between revisions

Line 32:
 
::* The wikipedia article, [[wp:Set (card game)|Set (card game)]]
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">import Base: vec, string, print
import Random: shuffle
import Combinatorics: combinations
 
const NUMBERS = ["one", "two", "three"]
const SHADINGS = ["solid", "striped", "open"]
const COLORS = ["red", "green", "purple"]
const SYMBOLS = ["diamond", "oval", "squiggle"]
 
struct SetCard
number::UInt8
shading::UInt8
color::UInt8
symbol::UInt8
function SetCard(num, sha, col, sym)
@assert all(i -> 1 <= i <= 3, (num, sha, col, sym))
return new(num, sha, col, sym)
end
end
 
num(s::SetCard) = NUMBERS[s.number]
sha(s::SetCard) = SHADINGS[s.shading]
col(s::SetCard) = COLORS[s.color]
sym(s::SetCard) = SYMBOLS[s.symbol]
Base.vec(sc::SetCard) = [sc.number, sc.shading, sc.color, sc.symbol]
function Base.string(sc::SetCard)
plu = sc.number == 1 ? "" : "s"
return "(" * join([num(sc), sha(sc), col(sc), sym(sc)], " ") * plu * ")"
end
Base.print(io:: IO, sc::SetCard) = print(io, string(sc))
Base.print(io:: IO, vsc::Vector{SetCard}) = print(io, "[" * join(string.(vsc), ", ") * "]")
 
""" True if the 3 cards form a set according to the Set game rules """
function allsameordifferent(sc1::SetCard, sc2, sc3)
a, b, c = vec(sc1), vec(sc2), vec(sc3)
return all(i -> a[i] == b[i] == c[i] || a[i] != b[i] && a[i] != c[i] && b[i] != c[i], eachindex(a))
end
 
""" Return a vector of the sets in the vector of SetCards """
function process_deal(cards::Vector{SetCard})
return filter(combo -> allsameordifferent(combo...), collect(combinations(cards, 3)))
end
 
function testcardsets()
pack = vec([SetCard(n, sh, c, sy) for n in 1:3, sh in 1:3, c in 1:3, sy in 1:3])
numcards = 81
while !isnothing(numcards)
print("\n\nEnter number of cards to deal (3 to 81, or just a space to exit) => ")
numcards = tryparse(Int, readline())
if !isnothing(numcards) && 3 <= numcards <= 81
deal = shuffle(pack)[begin:numcards]
sets = process_deal(deal)
println("\nThe deal is:\n$deal\n\nThere are $(length(sets)) sets.")
foreach(println, sets)
end
end
end
 
testcardsets()
</syntaxhighlight>{{out}}
<pre>
Enter number of cards to deal (3 to 81, or just a space to exit) => 4
 
The deal is:
[(one striped red squiggle), (one striped purple diamond), (three solid purple diamonds), (three open red ovals)]
 
There are 0 sets.
 
 
Enter number of cards to deal (3 to 81, or just a space to exit) => 12
 
The deal is:
[(one striped green squiggle), (one solid green oval), (one open green oval), (one striped red diamond), (one open purple oval), (two open purple squiggles), (one solid red diamond), (three open purple diamonds), (three open green diamonds), (three striped red ovals), (three open green ovals), (two open red ovals)]
 
There are 3 sets.
[(one striped green squiggle), (one open purple oval), (one solid red diamond)]
[(one open purple oval), (two open purple squiggles), (three open purple diamonds)]
[(one open purple oval), (three open green ovals), (two open red ovals)]
 
 
Enter number of cards to deal (3 to 81, or just a space to exit) => 16
 
The deal is:
[(three open purple squiggles), (one open red diamond), (three striped purple diamonds), (three solid red diamonds), (one solid purple diamond), (one solid green oval), (three solid red ovals), (three solid purple ovals), (one solid green diamond), (two solid green diamonds), (two open purple squiggles), (one open purple squiggle), (three striped purple squiggles), (two solid green squiggles), (three solid red squiggles), (one open purple diamond)]
 
There are 6 sets.
[(three open purple squiggles), (three striped purple diamonds), (three solid purple ovals)]
[(three open purple squiggles), (two open purple squiggles), (one open purple squiggle)]
[(one open red diamond), (three striped purple diamonds), (two solid green diamonds)]
[(three solid red diamonds), (one solid purple diamond), (two solid green diamonds)]
[(three solid red diamonds), (three solid red ovals), (three solid red squiggles)]
[(one solid purple diamond), (three solid red ovals), (two solid green squiggles)]
 
 
Enter number of cards to deal (3 to 81, or just a space to exit) =>
</pre>
 
=={{header|Phix}}==
4,102

edits