Time for an 2014 update…
This commit is contained in:
parent
372c577f83
commit
09687c4926
2520 changed files with 34227 additions and 7318 deletions
25
Task/Playing-cards/CoffeeScript/playing-cards.coffee
Normal file
25
Task/Playing-cards/CoffeeScript/playing-cards.coffee
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
#translated from JavaScript example
|
||||
class Card
|
||||
constructor: (@pip, @suit) ->
|
||||
|
||||
toString: => "#{@pip}#{@suit}"
|
||||
|
||||
class Deck
|
||||
pips = '2 3 4 5 6 7 8 9 10 J Q K A'.split ' '
|
||||
suits = '♣ ♥ ♠ ♦'.split ' '
|
||||
|
||||
constructor: (@cards) ->
|
||||
if not @cards?
|
||||
@cards = []
|
||||
for suit in suits
|
||||
for pip in pips
|
||||
@cards.push new Card(pip, suit)
|
||||
|
||||
toString: => "[#{@cards.join(', ')}]"
|
||||
|
||||
shuffle: =>
|
||||
for card, i in @cards
|
||||
randomCard = parseInt @cards.length * Math.random()
|
||||
@cards[i] = @cards.splice(randomCard, 1, card)[0]
|
||||
|
||||
deal: -> @cards.shift()
|
||||
|
|
@ -1,43 +1,32 @@
|
|||
import std.stdio, std.random, std.string, std.array;
|
||||
import std.stdio, std.typecons, std.algorithm, std.traits, std.array,
|
||||
std.range, std.random;
|
||||
|
||||
struct Card {
|
||||
enum suits = ["Clubs", "Hearts", "Spades", "Diamonds"];
|
||||
enum pips = ["2", "3", "4", "5", "6", "7", "8", "9", "10",
|
||||
"Jack", "Queen", "King", "Ace"];
|
||||
string pip, suit;
|
||||
enum Pip {Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten,
|
||||
Jack, Queen, King, Ace}
|
||||
enum Suit {Diamonds, Spades, Hearts, Clubs}
|
||||
alias Card = Tuple!(Pip, Suit);
|
||||
|
||||
string toString() {
|
||||
return pip ~ " of " ~ suit;
|
||||
}
|
||||
Card[] newDeck() /*pure nothrow*/ {
|
||||
return cartesianProduct([EnumMembers!Pip], [EnumMembers!Suit])
|
||||
.array;
|
||||
}
|
||||
|
||||
class Deck {
|
||||
Card[] deck;
|
||||
alias shuffleDeck = randomShuffle;
|
||||
|
||||
this() {
|
||||
foreach (suit; Card.suits)
|
||||
foreach (pip; Card.pips)
|
||||
deck ~= Card(pip, suit);
|
||||
}
|
||||
Card dealCard(ref Card[] deck) pure nothrow {
|
||||
immutable card = deck.back;
|
||||
deck.popBack;
|
||||
return card;
|
||||
}
|
||||
|
||||
void shuffle() {
|
||||
deck.randomShuffle();
|
||||
}
|
||||
|
||||
Card deal() {
|
||||
auto card = deck.back;
|
||||
deck.popBack();
|
||||
return card;
|
||||
}
|
||||
|
||||
override string toString() {
|
||||
return format("%(%s\n%)", deck);
|
||||
}
|
||||
void show(in Card[] deck) {
|
||||
writefln("Deck:\n%(%s\n%)\n", deck);
|
||||
}
|
||||
|
||||
void main() {
|
||||
auto deck = new Deck; // Make A new deck.
|
||||
deck.shuffle(); // Shuffle the deck.
|
||||
writeln("Card: ", deck.deal()); // Deal from the deck.
|
||||
writeln(deck); // Print the current contents of the deck.
|
||||
auto d = newDeck;
|
||||
d.show;
|
||||
d.shuffleDeck;
|
||||
while (!d.empty)
|
||||
d.dealCard.writeln;
|
||||
}
|
||||
|
|
|
|||
38
Task/Playing-cards/Erlang/playing-cards.erl
Normal file
38
Task/Playing-cards/Erlang/playing-cards.erl
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
-module( playing_cards ).
|
||||
|
||||
-export( [deal/2, deal/3, deck/0, print/1, shuffle/1, sort_pips/1, sort_suites/1, task/0] ).
|
||||
|
||||
-record( card, {pip, suite} ).
|
||||
|
||||
-spec( deal( N_cards::integer(), Deck::[#card{}]) -> {Hand::[#card{}], Deck::[#card{}]} ).
|
||||
deal( N_cards, Deck ) -> lists:split( N_cards, Deck ).
|
||||
-spec( deal( N_hands::integer(), N_cards::integer(), Deck::[#card{}]) -> {List_of_hands::[[#card{}]], Deck::[#card{}]} ).
|
||||
deal( N_hands, N_cards, Deck ) -> lists:foldl( fun deal_hands/2, {lists:duplicate(N_hands, []), Deck}, lists:seq(1, N_cards * N_hands) ).
|
||||
|
||||
deck() -> [#card{suite=X, pip=Y} || X <- suites(), Y <- pips()].
|
||||
|
||||
print( Cards ) -> [io:fwrite( " ~p", [X]) || X <- Cards], io:nl().
|
||||
|
||||
shuffle( Deck ) -> knuth_shuffle:list( Deck ).
|
||||
|
||||
sort_pips( Cards ) -> lists:keysort( #card.pip, Cards ).
|
||||
|
||||
sort_suites( Cards ) -> lists:keysort( #card.suite, Cards ).
|
||||
|
||||
task() ->
|
||||
Deck = deck(),
|
||||
Shuffled = shuffle( Deck ),
|
||||
{Hand, New_deck} = deal( 3, Shuffled ),
|
||||
{Hands, _Deck} = deal( 2, 3, New_deck ),
|
||||
io:fwrite( "Hand:" ),
|
||||
print( Hand ),
|
||||
io:fwrite( "Hands:~n" ),
|
||||
[print(X) || X <- Hands].
|
||||
|
||||
|
||||
|
||||
deal_hands( _N, {[Hand | T], [Card | Deck]} ) -> {T ++ [[Card | Hand]], Deck}.
|
||||
|
||||
pips() -> ["2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"].
|
||||
|
||||
suites() -> ["Clubs", "Hearts", "Spades", "Diamonds"].
|
||||
25
Task/Playing-cards/Python/playing-cards-2.py
Normal file
25
Task/Playing-cards/Python/playing-cards-2.py
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
from pokerhand import Card, suit, face
|
||||
from itertools import product
|
||||
from random import randrange
|
||||
|
||||
class Deck():
|
||||
def __init__(self):
|
||||
self.__deck = [Card(f, s) for f,s in product(face, suit)]
|
||||
|
||||
def __repr__(self):
|
||||
return 'Deck of ' + ' '.join(repr(card) for card in self.__deck)
|
||||
|
||||
def shuffle(self):
|
||||
pass
|
||||
|
||||
def deal(self):
|
||||
return self.__deck.pop(randrange(len(self.__deck)))
|
||||
|
||||
if __name__ == '__main__':
|
||||
deck = Deck()
|
||||
print('40 cards from a deck:\n')
|
||||
for i in range(5):
|
||||
for j in range(8):
|
||||
print(deck.deal(), end=' ')
|
||||
print()
|
||||
print('\nThe remaining cards are a', deck)
|
||||
Loading…
Add table
Add a link
Reference in a new issue