Playing cards: Difference between revisions

→‎{{header|Julia}}: A new entry for Julia
(added new lua code)
(→‎{{header|Julia}}: A new entry for Julia)
Line 1,950:
};
}</lang>
 
=={{header|Julia}}==
'''Deck Types and Constructors'''
 
A deck consists of an array of integers and a <tt>DeckDesign</tt> type, which defines the meanings of the cards. This is a somewhat simplified implementation. While strictly speaking, a deck should be an array of cards, it is sufficient here to use integers. Indeed, cards and hands are just decks with smaller numbers of cards.
 
<lang Julia>
type DeckDesign{T<:Integer,U<:String}
rlen::T
slen::T
ranks::Array{U,1}
suits::Array{U,1}
hlen::T
end
 
type Deck{T<:Integer}
cards::Array{T,1}
design::DeckDesign
end
 
Deck(n::Integer, des::DeckDesign) = Deck([n], des)
 
function pokerlayout()
r = [map(string, 2:10), "J", "Q", "K", "A"]
r = map(utf8, r)
s = ["\u2663", "\u2666", "\u2665", "\u2660"]
DeckDesign(13, 4, r, s, 5)
end
 
function fresh(des::DeckDesign)
Deck(collect(1:des.rlen*des.slen), des)
end
</lang>
 
'''Define a Few of the Standard Methods'''
 
Many of these definitions are simply passed through to the card array component of the deck. But note that <tt>size</tt> returns parameters appropriate to a complete deck. This behavior is helpful when assigning meaning to any list of cards.
<lang Julia>
Base.isempty(d::Deck) = isempty(d.cards)
Base.empty!(d::Deck) = empty!(d.cards)
Base.length(d::Deck) = length(d.cards)
Base.endof(d::Deck) = endof(d.cards)
Base.shuffle!(d::Deck) = shuffle!(d.cards)
Base.sort!(d::Deck) = sort!(d.cards)
Base.getindex(d::Deck, r) = Deck(getindex(d.cards, r), d.design)
Base.size(d::Deck) = (d.design.rlen, d.design.slen)
function Base.print(d::Deck)
sz = size(d)
r = map(x->d.design.ranks[ind2sub(sz, x)[1]], d.cards)
s = map(x->d.design.suits[ind2sub(sz, x)[2]], d.cards)
join(r.*s, " ")
end
</lang>
 
'''Define Some Special Methods'''
 
<tt>deal!</tt> method is the only deck specific method required to complete the essentials for this task.
 
<lang Julia>
function deal!{T<:Integer}(d::Deck, hlen::T)
if hlen < length(d)
hand = Deck(d.cards[1:hlen], d.design)
d.cards = d.cards[hlen+1:end]
else
hand = d
empty!(d)
end
return hand
end
 
function deal!(d::Deck)
deal!(d, d.design.hlen)
end
 
function pretty(d::Deck)
s = ""
llen = d.design.rlen
dlen = length(d)
for i in 1:llen:dlen
j = min(i+llen-1, dlen)
s *= print(d[i:j])*"\n"
end
chop(s)
end
</lang>
 
'''Main'''
<lang Julia>
d = fresh(pokerlayout())
println("A new poker deck:")
println(pretty(d))
 
shuffle!(d)
println()
println("The deck shuffled:")
println(pretty(d))
 
n = 4
println()
println("Deal ", n, " hands:")
for i in 1:n
h = deal!(d)
println(pretty(h))
end
 
println()
println("And now the deck contains:")
println(pretty(d))
</lang>
 
{{out}}
<pre>
A new poker deck:
2♣ 3♣ 4♣ 5♣ 6♣ 7♣ 8♣ 9♣ 10♣ J♣ Q♣ K♣ A♣
2♦ 3♦ 4♦ 5♦ 6♦ 7♦ 8♦ 9♦ 10♦ J♦ Q♦ K♦ A♦
2♥ 3♥ 4♥ 5♥ 6♥ 7♥ 8♥ 9♥ 10♥ J♥ Q♥ K♥ A♥
2♠ 3♠ 4♠ 5♠ 6♠ 7♠ 8♠ 9♠ 10♠ J♠ Q♠ K♠ A♠
 
The deck shuffled:
Q♥ 6♠ 4♣ 2♠ 5♣ 10♣ 9♠ K♥ 7♠ 3♠ 2♥ 4♥ A♣
6♥ A♥ 3♥ 3♦ K♦ 10♦ 10♠ 8♣ 4♦ 8♦ K♣ 5♦ 6♣
7♦ 9♦ 6♦ 8♥ 10♥ 5♥ J♠ 9♣ 8♠ Q♦ 2♦ K♠ Q♣
J♣ J♥ 7♥ J♦ 2♣ 4♠ 5♠ 9♥ A♦ 3♣ 7♣ A♠ Q♠
 
Deal 4 hands:
Q♥ 6♠ 4♣ 2♠ 5♣
10♣ 9♠ K♥ 7♠ 3♠
2♥ 4♥ A♣ 6♥ A♥
3♥ 3♦ K♦ 10♦ 10♠
 
And now the deck contains:
8♣ 4♦ 8♦ K♣ 5♦ 6♣ 7♦ 9♦ 6♦ 8♥ 10♥ 5♥ J♠
9♣ 8♠ Q♦ 2♦ K♠ Q♣ J♣ J♥ 7♥ J♦ 2♣ 4♠ 5♠
9♥ A♦ 3♣ 7♣ A♠ Q♠
</pre>
 
=={{header|K}}==