Set, the card game: Difference between revisions

Content added Content deleted
m (→‎{{header|Raku}}: handle plurals corrrectly)
m (→‎{{header|Julia}}: remove unused code)
Line 122: Line 122:


=={{header|Julia}}==
=={{header|Julia}}==
<syntaxhighlight lang="julia">import Base: vec, string, print
<syntaxhighlight lang="julia">import Random: shuffle
import Random: shuffle
import Combinatorics: combinations
import Combinatorics: combinations


Line 132: Line 131:


struct SetCard
struct SetCard
number::UInt8
t::Tuple{UInt8, UInt8, UInt8, UInt8}
shading::UInt8
color::UInt8
symbol::UInt8
function SetCard(num, sha, col, sym)
function SetCard(num, sha, col, sym)
@assert all(i -> 1 <= i <= 3, (num, sha, col, sym))
@assert all(i -> 1 <= i <= 3, (num, sha, col, sym))
return new(num, sha, col, sym)
return new(tuple(num, sha, col, sym))
end
end
end
end


num(s::SetCard) = NUMBERS[s.number]
Base.vec(s::SetCard) = vec(s.t)
sha(s::SetCard) = SHADINGS[s.shading]
function Base.string(s::SetCard)
return "(" *
col(s::SetCard) = COLORS[s.color]
join([NUMBERS[s.t[1]], SHADINGS[s.t[2]], COLORS[s.t[3]], SYMBOLS[s.t[4]]], " ") *
sym(s::SetCard) = SYMBOLS[s.symbol]
(s.t[1] == 1 ? "" : "s") * ")"
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
end
Base.print(io:: IO, sc::SetCard) = print(io, string(sc))
Base.print(io:: IO, sc::SetCard) = print(io, string(sc))
Base.print(io:: IO, vsc::Vector{SetCard}) = print(io, "[" * join(string.(vsc), ", ") * "]")
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 """
""" Return an iterator for a vector of the sets found in the dealt `cards` """
function allsameordifferent(sc1::SetCard, sc2, sc3)
a, b, c = vec(sc1), vec(sc2), vec(sc3)
return all(i -> (a[i] + b[i] + c[i]) % 3 == 0, eachindex(a))
end

""" Return a vector of the sets in the vector of SetCards """
function process_deal(cards::Vector{SetCard})
function process_deal(cards::Vector{SetCard})
return Iterators.filter(combo -> allsameordifferent(combo...), combinations(cards, 3))
return Iterators.filter(combinations(cards, 3)) do c
return all(i -> (c[1].t[i] + c[2].t[i] + c[3].t[i]) % 3 == 0, eachindex(c[1].t))
end
end
end