Playing cards: Difference between revisions

Added Elixir
No edit summary
(Added Elixir)
Line 743:
A of ♣, Q of ♣, 6 of ♠, Q of ♠, K of ♠, 7 of ♦, 7 of ♣, 3 of ♦, 2 of ♠, 8 of ♥, A of ♦, 2 of ♥, 9 of ♣
8 of ♠, 10 of ♥, 3 of ♠, 10 of ♣, 9 of ♥, 10 of ♠, 3 of ♣, J of ♠, K of ♣, K of ♦, A of ♠</pre>
 
=={{Header|Clojure}}==
<lang Clojure>(def suits [:club :diamond :heart :spade])
Line 1,229 ⟶ 1,230:
=={{header|E}}==
See [[Playing Cards/E]]
 
=={{header|Elixir}}==
{{trans|Erlang}}
<lang elixir>defmodule Card do
defstruct pip: nil, suit: nil
end
 
defmodule Playing_cards do
@pips ~w[2 3 4 5 6 7 8 9 10 Jack Queen King Ace]a
@suits ~w[Clubs Hearts Spades Diamonds]a
@pip_value Enum.with_index(@pips)
@suit_value Enum.with_index(@suits)
def deal( n_cards, deck ), do: Enum.split( deck, n_cards )
def deal( n_hands, n_cards, deck ) do
Enum.reduce(1..n_hands, {[], deck}, fn _,{acc,d} ->
{hand, new_d} = deal(n_cards, d)
{[hand | acc], new_d}
end)
end
def deck, do: (for x <- @suits, y <- @pips, do: %Card{suit: x, pip: y})
def print( cards ), do: IO.puts (for x <- cards, do: "\t#{inspect x}")
def shuffle( deck ), do: Enum.shuffle( deck )
def sort_pips( cards ), do: Enum.sort_by( cards, &@pip_value[&1.pip] )
def sort_suits( cards ), do: Enum.sort_by( cards, &(@suit_value[&1.suit]) )
def task do
shuffled = shuffle( deck )
{hand, new_deck} = deal( 3, shuffled )
{hands, _deck} = deal( 2, 3, new_deck )
IO.write "Hand:"
print( hand )
IO.puts "Hands:"
for x <- hands, do: print(x)
end
end
 
Playing_cards.task</lang>
 
{{out}}
<pre>
Hand: %Card{pip: :Ace, suit: :Spades} %Card{pip: :"5", suit: :Hearts} %Card{pip: :"7", suit: :Clubs}
Hands:
%Card{pip: :"7", suit: :Hearts} %Card{pip: :"4", suit: :Spades} %Card{pip: :"9", suit: :Spades}
%Card{pip: :"6", suit: :Spades} %Card{pip: :Ace, suit: :Hearts} %Card{pip: :"2", suit: :Diamonds}
</pre>
 
=={{header|Erlang}}==
Line 1,469 ⟶ 1,522:
END PROGRAM</lang>
This creates a new deck, shuffles it, deals five cards to hand, prints the cards in hand and then prints the cards remaining in the deck.
 
=={{header|Go}}==
<lang go>package cards
Line 3,188 ⟶ 3,242:
format('~a of ~a~n', [Pip, Suit]).
</lang>
 
=={{header|PureBasic}}==
This approach keeps track of the cards in an abbrieviated form but allows them to be expanded to a more wordy form when they are dealt or shown.
Line 3,598 ⟶ 3,653:
remainder of deck: 10♠ K♣ 7♠ 3♦ 5♥ 6♦ J♦ A♠ 6♠ 8♠ 10♣ 8♣ 10♥ J♠ Q♥ 2♣ 6♥ A♦ 4♦ 2♦ 8♥ 3♥ 4♣ A♣ J♣ 2♠ 5♦ 8♦ 9♦ 9♣ 9♥ 4♥
</pre>
 
 
=={{header|Ring}}==
Anonymous user