Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
5
Task/Playing-cards/00-META.yaml
Normal file
5
Task/Playing-cards/00-META.yaml
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
category:
|
||||
- Cards
|
||||
from: http://rosettacode.org/wiki/Playing_cards
|
||||
note: Games
|
||||
21
Task/Playing-cards/00-TASK.txt
Normal file
21
Task/Playing-cards/00-TASK.txt
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
;Task:
|
||||
Create a data structure and the associated methods to define and manipulate a deck of [[wp:Playing-cards#Anglo-American-French|playing cards]].
|
||||
|
||||
The deck should contain 52 unique cards.
|
||||
|
||||
The methods must include the ability to:
|
||||
:::* make a new deck
|
||||
:::* shuffle (randomize) the deck
|
||||
:::* deal from the deck
|
||||
:::* print the current contents of a deck
|
||||
|
||||
Each card must have a pip value and a suit value which constitute the unique value of the card.
|
||||
|
||||
Related tasks:
|
||||
* [[Card shuffles]]
|
||||
* [[Deal cards_for_FreeCell]]
|
||||
* [[War Card_Game]]
|
||||
* [[Poker hand_analyser]]
|
||||
* [[Go Fish]]
|
||||
|
||||
|
||||
52
Task/Playing-cards/ACL2/playing-cards.acl2
Normal file
52
Task/Playing-cards/ACL2/playing-cards.acl2
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
(defun remove-nth (i xs)
|
||||
(if (or (zp i) (endp (rest xs)))
|
||||
(rest xs)
|
||||
(cons (first xs)
|
||||
(remove-nth (1- i) (rest xs)))))
|
||||
|
||||
(defthm remove-nth-shortens
|
||||
(implies (consp xs)
|
||||
(< (len (remove-nth i xs)) (len xs))))
|
||||
|
||||
:set-state-ok t
|
||||
|
||||
(defun shuffle-r (xs ys state)
|
||||
(declare (xargs :measure (len xs)))
|
||||
(if (endp xs)
|
||||
(mv ys state)
|
||||
(mv-let (idx state)
|
||||
(random$ (len xs) state)
|
||||
(shuffle-r (remove-nth idx xs)
|
||||
(cons (nth idx xs) ys)
|
||||
state))))
|
||||
|
||||
(defun shuffle (xs state)
|
||||
(shuffle-r xs nil state))
|
||||
|
||||
(defun cross-r (x ys)
|
||||
(if (endp ys)
|
||||
nil
|
||||
(cons (cons x (first ys))
|
||||
(cross-r x (rest ys)))))
|
||||
|
||||
(defun cross (xs ys)
|
||||
(if (or (endp xs) (endp ys))
|
||||
nil
|
||||
(append (cross-r (first xs) ys)
|
||||
(cross (rest xs) ys))))
|
||||
|
||||
(defun make-deck ()
|
||||
(cross '(ace 2 3 4 5 6 7 8 9 10 jack queen king)
|
||||
'(hearts diamonds clubs spades)))
|
||||
|
||||
(defun print-card (card)
|
||||
(cw "~x0 of ~x1~%" (car card) (cdr card)))
|
||||
|
||||
(defun print-deck (deck)
|
||||
(if (endp deck)
|
||||
nil
|
||||
(progn$ (print-card (first deck))
|
||||
(print-deck (rest deck)))))
|
||||
|
||||
(defun draw-from-deck (deck)
|
||||
(mv (first deck) (rest deck)))
|
||||
75
Task/Playing-cards/ALGOL-68/playing-cards.alg
Normal file
75
Task/Playing-cards/ALGOL-68/playing-cards.alg
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
MODE CARD = STRUCT(STRING pip, suit); # instance attributes #
|
||||
|
||||
# class members & attributes #
|
||||
STRUCT(
|
||||
PROC(REF CARD, STRING, STRING)VOID init,
|
||||
FORMAT format,
|
||||
PROC(REF CARD)STRING repr,
|
||||
[]STRING suits, pips
|
||||
) class card = (
|
||||
# PROC init = # (REF CARD self, STRING pip, suit)VOID:(
|
||||
pip OF self:=pip;
|
||||
suit OF self :=suit
|
||||
),
|
||||
# format = # $"("g" OF "g")"$,
|
||||
# PROC repr = # (REF CARD self)STRING: (
|
||||
HEAP STRING out; putf(BOOK out,(format OF class card,self)); out
|
||||
),
|
||||
# suits = # ("Clubs","Hearts","Spades","Diamonds"),
|
||||
# pips = # ("2","3","4","5","6","7","8","9","10","Jack","Queen","King","Ace")
|
||||
);
|
||||
|
||||
MODE DECK = STRUCT(REF[]CARD deck); # instance attributes #
|
||||
|
||||
# class members & attributes #
|
||||
STRUCT(
|
||||
PROC(REF DECK)VOID init, shuffle,
|
||||
PROC(REF DECK)STRING repr,
|
||||
PROC(REF DECK)CARD deal
|
||||
) class deck = (
|
||||
|
||||
# PROC init = # (REF DECK self)VOID:(
|
||||
HEAP[ UPB suits OF class card * UPB pips OF class card ]CARD new;
|
||||
FOR suit TO UPB suits OF class card DO
|
||||
FOR pip TO UPB pips OF class card DO
|
||||
new[(suit-1)*UPB pips OF class card + pip] :=
|
||||
((pips OF class card)[pip], (suits OF class card)[suit])
|
||||
OD
|
||||
OD;
|
||||
deck OF self := new
|
||||
),
|
||||
|
||||
# PROC shuffle = # (REF DECK self)VOID:
|
||||
FOR card TO UPB deck OF self DO
|
||||
CARD this card = (deck OF self)[card];
|
||||
INT random card = random int(LWB deck OF self,UPB deck OF self);
|
||||
(deck OF self)[card] := (deck OF self)[random card];
|
||||
(deck OF self)[random card] := this card
|
||||
OD,
|
||||
|
||||
# PROC repr = # (REF DECK self)STRING: (
|
||||
FORMAT format = $"("n(UPB deck OF self-1)(f(format OF class card)", ")f(format OF class card)")"$;
|
||||
HEAP STRING out; putf(BOOK out,(format, deck OF self)); out
|
||||
),
|
||||
|
||||
# PROC deal = # (REF DECK self)CARD: (
|
||||
(shuffle OF class deck)(self);
|
||||
(deck OF self)[UPB deck OF self]
|
||||
)
|
||||
);
|
||||
|
||||
# associate a STRING with a FILE for easy text manipulation #
|
||||
OP BOOK = (REF STRING string)REF FILE:(
|
||||
HEAP FILE book;
|
||||
associate(book, string);
|
||||
book
|
||||
);
|
||||
|
||||
# Pick a random integer between from [lwb..upb] #
|
||||
PROC random int = (INT lwb, upb)INT:
|
||||
ENTIER(random * (upb - lwb + 1) + lwb);
|
||||
|
||||
DECK deck;
|
||||
(init OF class deck)(deck);
|
||||
(shuffle OF class deck)(deck);
|
||||
print (((repr OF class deck)(deck), new line))
|
||||
2
Task/Playing-cards/APL/playing-cards.apl
Normal file
2
Task/Playing-cards/APL/playing-cards.apl
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
deck←,'23456789TJQKA'∘.,'CDSH'
|
||||
shuffle←{⍵[?⍨⍴⍵]}
|
||||
440
Task/Playing-cards/ATS/playing-cards.ats
Normal file
440
Task/Playing-cards/ATS/playing-cards.ats
Normal file
|
|
@ -0,0 +1,440 @@
|
|||
(* ****** ****** *)
|
||||
//
|
||||
#include
|
||||
"share/atspre_staload.hats"
|
||||
#include
|
||||
"share/HATS/atspre_staload_libats_ML.hats"
|
||||
//
|
||||
(* ****** ****** *)
|
||||
//
|
||||
abst@ype
|
||||
pip_type = int
|
||||
abst@ype
|
||||
suit_type = int
|
||||
//
|
||||
abst@ype
|
||||
card_type = int
|
||||
//
|
||||
(* ****** ****** *)
|
||||
|
||||
typedef pip = pip_type
|
||||
typedef suit = suit_type
|
||||
|
||||
(* ****** ****** *)
|
||||
|
||||
typedef card = card_type
|
||||
|
||||
(* ****** ****** *)
|
||||
//
|
||||
extern
|
||||
fun
|
||||
pip_make: natLt(13) -> pip
|
||||
extern
|
||||
fun
|
||||
pip_get_name: pip -> string
|
||||
extern
|
||||
fun
|
||||
pip_get_value: pip -> intBtwe(1, 13)
|
||||
//
|
||||
extern
|
||||
fun
|
||||
suit_make: natLt(4) -> suit
|
||||
extern
|
||||
fun
|
||||
suit_get_name: suit -> string
|
||||
extern
|
||||
fun
|
||||
suit_get_value: suit -> intBtwe(1, 4)
|
||||
//
|
||||
overload .name with pip_get_name
|
||||
overload .name with suit_get_name
|
||||
overload .value with pip_get_value
|
||||
overload .value with suit_get_value
|
||||
//
|
||||
(* ****** ****** *)
|
||||
//
|
||||
(*
|
||||
| Two | Three | Four | Five
|
||||
| Six | Seven | Eight | Nine
|
||||
| Ten | Jack | Queen | King | Ace
|
||||
*)
|
||||
//
|
||||
(*
|
||||
| Spade | Heart | Diamond | Club
|
||||
*)
|
||||
//
|
||||
(* ****** ****** *)
|
||||
|
||||
local
|
||||
|
||||
assume
|
||||
pip_type = natLt(13)
|
||||
|
||||
in (* in-of-local *)
|
||||
|
||||
implement
|
||||
pip_make(x) = x
|
||||
implement
|
||||
pip_get_value(x) = x + 1
|
||||
|
||||
end // end of [local]
|
||||
|
||||
(* ****** ****** *)
|
||||
|
||||
local
|
||||
|
||||
assume
|
||||
suit_type = natLt(4)
|
||||
|
||||
in (* in-of-local *)
|
||||
|
||||
implement
|
||||
suit_make(x) = x
|
||||
implement
|
||||
suit_get_value(x) = x + 1
|
||||
|
||||
end // end of [local]
|
||||
|
||||
(* ****** ****** *)
|
||||
|
||||
implement
|
||||
pip_get_name
|
||||
(x) =
|
||||
(
|
||||
case+
|
||||
x.value()
|
||||
of // case+
|
||||
| 1 => "Ace"
|
||||
| 2 => "Two"
|
||||
| 3 => "Three"
|
||||
| 4 => "Four"
|
||||
| 5 => "Five"
|
||||
| 6 => "Six"
|
||||
| 7 => "Seven"
|
||||
| 8 => "Eight"
|
||||
| 9 => "Nine"
|
||||
| 10 => "Ten"
|
||||
| 11 => "Jack"
|
||||
| 12 => "Queen"
|
||||
| 13 => "King"
|
||||
)
|
||||
|
||||
(* ****** ****** *)
|
||||
//
|
||||
implement
|
||||
suit_get_name
|
||||
(x) =
|
||||
(
|
||||
case+
|
||||
x.value()
|
||||
of // case+
|
||||
| 1 => "S" | 2 => "H" | 3 => "D" | 4 => "C"
|
||||
) (* end of [suit_get_name] *)
|
||||
//
|
||||
(* ****** ****** *)
|
||||
//
|
||||
extern
|
||||
fun
|
||||
card_get_pip: card -> pip
|
||||
extern
|
||||
fun
|
||||
card_get_suit: card -> suit
|
||||
//
|
||||
extern
|
||||
fun
|
||||
card_make: natLt(52) -> card
|
||||
extern
|
||||
fun
|
||||
card_make_suit_pip: (suit, pip) -> card
|
||||
//
|
||||
(* ****** ****** *)
|
||||
|
||||
extern
|
||||
fun
|
||||
fprint_pip : fprint_type(pip)
|
||||
extern
|
||||
fun
|
||||
fprint_suit : fprint_type(suit)
|
||||
extern
|
||||
fun
|
||||
fprint_card : fprint_type(card)
|
||||
|
||||
(* ****** ****** *)
|
||||
|
||||
overload .pip with card_get_pip
|
||||
overload .suit with card_get_suit
|
||||
|
||||
(* ****** ****** *)
|
||||
|
||||
implement
|
||||
fprint_val<card> = fprint_card
|
||||
|
||||
(* ****** ****** *)
|
||||
|
||||
overload fprint with fprint_pip
|
||||
overload fprint with fprint_suit
|
||||
overload fprint with fprint_card
|
||||
|
||||
(* ****** ****** *)
|
||||
|
||||
local
|
||||
|
||||
assume
|
||||
card_type = natLt(52)
|
||||
|
||||
in (* in-of-local *)
|
||||
//
|
||||
implement
|
||||
card_get_pip
|
||||
(x) = pip_make(nmod(x, 13))
|
||||
implement
|
||||
card_get_suit
|
||||
(x) = suit_make(ndiv(x, 13))
|
||||
//
|
||||
implement
|
||||
card_make(xy) = xy
|
||||
//
|
||||
implement
|
||||
card_make_suit_pip(x, y) =
|
||||
(x.value()-1) * 13 + (y.value()-1)
|
||||
//
|
||||
end // end of [local]
|
||||
|
||||
(* ****** ****** *)
|
||||
//
|
||||
implement
|
||||
fprint_pip(out, x) =
|
||||
fprint!(out, x.name())
|
||||
implement
|
||||
fprint_suit(out, x) =
|
||||
fprint!(out, x.name())
|
||||
//
|
||||
implement
|
||||
fprint_card(out, c) =
|
||||
fprint!(out, c.suit(), "(", c.pip(), ")")
|
||||
//
|
||||
(* ****** ****** *)
|
||||
//
|
||||
absvtype
|
||||
deck_vtype(n:int) = ptr
|
||||
//
|
||||
vtypedef deck(n:int) = deck_vtype(n)
|
||||
//
|
||||
(* ****** ****** *)
|
||||
//
|
||||
extern
|
||||
fun
|
||||
deck_get_size
|
||||
{n:nat}(!deck(n)): int(n)
|
||||
//
|
||||
extern
|
||||
fun
|
||||
deck_is_empty
|
||||
{n:nat}(!deck(n)): bool(n==0)
|
||||
//
|
||||
overload iseqz with deck_is_empty
|
||||
//
|
||||
(* ****** ****** *)
|
||||
//
|
||||
extern
|
||||
fun
|
||||
deck_free{n:int}(deck(n)): void
|
||||
//
|
||||
overload .free with deck_free
|
||||
//
|
||||
(* ****** ****** *)
|
||||
//
|
||||
extern
|
||||
fun
|
||||
deck_make_full((*void*)): deck(52)
|
||||
//
|
||||
(* ****** ****** *)
|
||||
//
|
||||
extern
|
||||
fun
|
||||
fprint_deck
|
||||
{n:nat}(FILEref, !deck(n)): void
|
||||
//
|
||||
overload fprint with fprint_deck
|
||||
//
|
||||
(* ****** ****** *)
|
||||
//
|
||||
extern
|
||||
fun
|
||||
deck_shuffle
|
||||
{n:nat}(!deck(n) >> _): void
|
||||
//
|
||||
overload .shuffle with deck_shuffle
|
||||
//
|
||||
(* ****** ****** *)
|
||||
//
|
||||
extern
|
||||
fun
|
||||
deck_takeout_top
|
||||
{n:pos}(!deck(n) >> deck(n-1)): card
|
||||
//
|
||||
(* ****** ****** *)
|
||||
|
||||
local
|
||||
//
|
||||
datavtype
|
||||
deck(int) =
|
||||
| {n:nat}
|
||||
Deck(n) of
|
||||
(
|
||||
int(n)
|
||||
, list_vt(card, n)
|
||||
) // end of [Deck]
|
||||
//
|
||||
assume
|
||||
deck_vtype(n:int) = deck(n)
|
||||
//
|
||||
in (* in-of-local *)
|
||||
|
||||
implement
|
||||
deck_get_size
|
||||
(deck) =
|
||||
(
|
||||
let val+Deck(n, _) = deck in n end
|
||||
)
|
||||
|
||||
implement
|
||||
deck_is_empty
|
||||
(deck) =
|
||||
(
|
||||
let val+Deck(n, _) = deck in n = 0 end
|
||||
)
|
||||
|
||||
(* ****** ****** *)
|
||||
//
|
||||
implement
|
||||
deck_free(deck) =
|
||||
(
|
||||
let val+~Deck(n, xs) = deck in free(xs) end
|
||||
) (* end of [deck_free] *)
|
||||
//
|
||||
(* ****** ****** *)
|
||||
|
||||
implement
|
||||
deck_make_full
|
||||
((*void*)) = let
|
||||
//
|
||||
val xys =
|
||||
list_make_intrange(0, 52)
|
||||
//
|
||||
val cards =
|
||||
list_vt_mapfree_fun<natLt(52)><card>(xys, lam xy => card_make(xy))
|
||||
//
|
||||
in
|
||||
Deck(52, cards)
|
||||
end // end of [deck_make_full]
|
||||
|
||||
(* ****** ****** *)
|
||||
|
||||
implement
|
||||
fprint_deck
|
||||
(out, deck) = let
|
||||
//
|
||||
val+Deck(n, xs) = deck
|
||||
//
|
||||
in
|
||||
//
|
||||
fprint_list_vt(out, xs)
|
||||
//
|
||||
end // end of [fprint_deck]
|
||||
|
||||
(* ****** ****** *)
|
||||
|
||||
implement
|
||||
deck_shuffle
|
||||
(deck) =
|
||||
fold@(deck) where
|
||||
{
|
||||
//
|
||||
val+@Deck(n, xs) = deck
|
||||
//
|
||||
implement
|
||||
list_vt_permute$randint<>
|
||||
(n) = randint(n)
|
||||
//
|
||||
val ((*void*)) =
|
||||
(xs := list_vt_permute(xs))
|
||||
//
|
||||
} (* end of [deck_shuffle] *)
|
||||
|
||||
(* ****** ****** *)
|
||||
|
||||
implement
|
||||
deck_takeout_top
|
||||
(deck) = let
|
||||
//
|
||||
val+@Deck(n, xs) = deck
|
||||
//
|
||||
val+
|
||||
~list_vt_cons(x0, xs_tl) = xs
|
||||
//
|
||||
val ((*void*)) = n := n - 1
|
||||
val ((*void*)) = (xs := xs_tl)
|
||||
//
|
||||
in
|
||||
fold@(deck); x0(*top*)
|
||||
end // end of [deck_takeout_top]
|
||||
|
||||
end // end of [local]
|
||||
|
||||
(* ****** ****** *)
|
||||
|
||||
implement
|
||||
main0((*void*)) =
|
||||
{
|
||||
//
|
||||
val () =
|
||||
println!
|
||||
(
|
||||
"Hello from [Playing_cards]!"
|
||||
) (* println! *)
|
||||
//
|
||||
val out = stdout_ref
|
||||
//
|
||||
val theDeck =
|
||||
deck_make_full((*void*))
|
||||
//
|
||||
val ((*void*)) =
|
||||
fprintln!(out, "theDeck = ", theDeck)
|
||||
//
|
||||
val ((*void*)) =
|
||||
theDeck.shuffle((*void*))
|
||||
//
|
||||
val ((*void*)) =
|
||||
fprintln!(out, "theDeck = ", theDeck)
|
||||
//
|
||||
val ((*void*)) =
|
||||
loop_deal(theDeck) where
|
||||
{
|
||||
//
|
||||
fun
|
||||
loop_deal{n:nat}
|
||||
(
|
||||
deck: !deck(n) >> deck(0)
|
||||
) : void =
|
||||
(
|
||||
if (
|
||||
iseqz(deck)
|
||||
) then ((*void*))
|
||||
else
|
||||
let
|
||||
val card =
|
||||
deck_takeout_top(deck)
|
||||
in
|
||||
fprintln!(out, card); loop_deal(deck)
|
||||
end // end of [let]
|
||||
// end of [else]
|
||||
)
|
||||
//
|
||||
} (* end of [val] *)
|
||||
//
|
||||
val ((*freed*)) = theDeck.free()
|
||||
//
|
||||
} (* end of [main0] *)
|
||||
|
||||
(* ****** ****** *)
|
||||
156
Task/Playing-cards/Action-/playing-cards.action
Normal file
156
Task/Playing-cards/Action-/playing-cards.action
Normal file
|
|
@ -0,0 +1,156 @@
|
|||
DEFINE PTR="CARD"
|
||||
DEFINE MAXDECKSIZE="52"
|
||||
|
||||
TYPE Deck=[
|
||||
PTR cards ;BYTE ARRAY
|
||||
BYTE first,count]
|
||||
|
||||
BYTE FUNC IsEmpty(Deck POINTER d)
|
||||
IF d.count=0 THEN
|
||||
RETURN (1)
|
||||
FI
|
||||
RETURN (0)
|
||||
|
||||
BYTE FUNC IsFull(Deck POINTER d)
|
||||
IF d.count=MAXDECKSIZE THEN
|
||||
RETURN (1)
|
||||
FI
|
||||
RETURN (0)
|
||||
|
||||
BYTE FUNC Index(Deck POINTER d BYTE i)
|
||||
RETURN ((d.first+i) MOD MAXDECKSIZE)
|
||||
|
||||
PROC InitEmpty(Deck POINTER d BYTE ARRAY crds)
|
||||
d.cards=crds
|
||||
d.first=0
|
||||
d.count=0
|
||||
RETURN
|
||||
|
||||
PROC InitFull(Deck POINTER d BYTE ARRAY crds)
|
||||
BYTE i
|
||||
|
||||
d.cards=crds
|
||||
d.first=0
|
||||
d.count=MAXDECKSIZE
|
||||
FOR i=0 TO MAXDECKSIZE-1
|
||||
DO
|
||||
crds(i)=i
|
||||
OD
|
||||
RETURN
|
||||
|
||||
PROC Shuffle(Deck POINTER d)
|
||||
BYTE i,j,i2,j2,tmp
|
||||
BYTE ARRAY crds
|
||||
|
||||
crds=d.cards
|
||||
i=d.count-1
|
||||
WHILE i>0
|
||||
DO
|
||||
j=Rand(i)
|
||||
i2=Index(d,i)
|
||||
j2=Index(d,j)
|
||||
tmp=crds(i2)
|
||||
crds(i2)=crds(j2)
|
||||
crds(j2)=tmp
|
||||
i==-1
|
||||
OD
|
||||
RETURN
|
||||
|
||||
BYTE FUNC Deal(Deck POINTER d)
|
||||
BYTE ARRAY crds
|
||||
BYTE c
|
||||
|
||||
IF IsEmpty(d) THEN
|
||||
PrintE("Error: deck is empty!")
|
||||
Break()
|
||||
FI
|
||||
|
||||
crds=d.cards
|
||||
c=crds(d.first)
|
||||
d.count==-1
|
||||
d.first==+1
|
||||
IF d.first=MAXDECKSIZE THEN
|
||||
d.first=0
|
||||
FI
|
||||
RETURN (c)
|
||||
|
||||
PROC Add(Deck POINTER d BYTE c)
|
||||
BYTE ARRAY crds
|
||||
BYTE i
|
||||
|
||||
IF IsFull(d) THEN
|
||||
PrintE("Error: deck is full!")
|
||||
Break()
|
||||
FI
|
||||
|
||||
crds=d.cards
|
||||
i=Index(d,d.count)
|
||||
crds(i)=c
|
||||
d.count==+1
|
||||
RETURN
|
||||
|
||||
PROC PrintCard(BYTE c)
|
||||
BYTE s,v
|
||||
BYTE ARRAY fig=['J 'Q 'K 'A]
|
||||
BYTE ARRAY suit=[16 96 0 123]
|
||||
|
||||
s=c/13 v=c MOD 13
|
||||
IF v<=8 THEN
|
||||
PrintB(v+2)
|
||||
ELSE
|
||||
Put(fig(v-9))
|
||||
FI
|
||||
Put(suit(s))
|
||||
RETURN
|
||||
|
||||
PROC PrintDeck(Deck POINTER d)
|
||||
BYTE i,i2
|
||||
BYTE ARRAY crds
|
||||
|
||||
crds=d.cards
|
||||
IF IsEmpty(d) THEN
|
||||
Print("Empty")
|
||||
ELSE
|
||||
FOR i=0 TO d.count-1
|
||||
DO
|
||||
i2=Index(d,i)
|
||||
PrintCard(crds(i2))
|
||||
IF i<d.count-1 THEN
|
||||
Put(' )
|
||||
FI
|
||||
OD
|
||||
FI
|
||||
RETURN
|
||||
|
||||
PROC Main()
|
||||
BYTE LMARGIN=$52,oldLMARGIN,i,c
|
||||
BYTE ARRAY crds(MAXDECKSIZE),crds1(MAXDECKSIZE),crds2(MAXDECKSIZE)
|
||||
Deck d,d1,d2
|
||||
|
||||
oldLMARGIN=LMARGIN
|
||||
LMARGIN=0 ;remove left margin on the screen
|
||||
Put(125) PutE() ;clear the screen
|
||||
|
||||
PrintE("New deck:")
|
||||
InitFull(d,crds)
|
||||
PrintDeck(d) PutE() PutE()
|
||||
|
||||
PrintE("Shuffle:")
|
||||
Shuffle(d)
|
||||
PrintDeck(d) PutE() PutE()
|
||||
|
||||
PrintE("Deal cards:")
|
||||
InitEmpty(d1,crds1) InitEmpty(d2,crds2)
|
||||
FOR i=1 TO 5
|
||||
DO
|
||||
c=Deal(d) Add(d1,c)
|
||||
c=Deal(d) Add(d2,c)
|
||||
OD
|
||||
Print(" Player 1: ") PrintDeck(d1) PutE()
|
||||
Print(" Player 2: ") PrintDeck(d2) PutE() PutE()
|
||||
|
||||
PrintE("Deck after dealing:")
|
||||
PrintDeck(d)
|
||||
|
||||
LMARGIN=oldLMARGIN ;restore left margin on the screen
|
||||
RETURN
|
||||
56
Task/Playing-cards/Arturo/playing-cards.arturo
Normal file
56
Task/Playing-cards/Arturo/playing-cards.arturo
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
ranks: 1..13
|
||||
suits: [`♣`, `♦`, `♥`, `♠`]
|
||||
|
||||
define :card [rank, suit][
|
||||
init: [
|
||||
ensure ->
|
||||
and? -> contains? ranks this\rank
|
||||
-> contains? suits this\suit
|
||||
]
|
||||
|
||||
print: [
|
||||
R: ø
|
||||
case [this\rank=]
|
||||
when? [1] -> R: `A`
|
||||
when? [11]-> R: `J`
|
||||
when? [12]-> R: `Q`
|
||||
when? [13]-> R: `K`
|
||||
else -> R: this\rank
|
||||
~{|R||this\suit|}
|
||||
]
|
||||
]
|
||||
|
||||
define :deck [][
|
||||
init: [
|
||||
this\cards: []
|
||||
loop ranks 'rank ->
|
||||
loop suits 'suit ->
|
||||
this\cards: this\cards ++ to :card @[rank, suit]
|
||||
]
|
||||
]
|
||||
|
||||
shuffleUp: function [this :deck][
|
||||
this\cards: shuffle this\cards
|
||||
]
|
||||
|
||||
deal: function [this :deck, cnt :integer][
|
||||
if cnt > size this\cards ->
|
||||
panic "Not enough cards in deck"
|
||||
|
||||
cards: []
|
||||
do.times: cnt [
|
||||
dealt: sample this\cards
|
||||
'cards ++ dealt
|
||||
this\cards: remove this\cards dealt
|
||||
]
|
||||
return cards
|
||||
]
|
||||
|
||||
; create a new deck
|
||||
Deck: to :deck []
|
||||
|
||||
; and shuffle it
|
||||
shuffleUp Deck
|
||||
|
||||
; deal 5 cards
|
||||
print deal Deck 5
|
||||
84
Task/Playing-cards/AutoHotkey/playing-cards.ahk
Normal file
84
Task/Playing-cards/AutoHotkey/playing-cards.ahk
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
suits := ["♠", "♦", "♥", "♣"]
|
||||
values := [2,3,4,5,6,7,8,9,10,"J","Q","K","A"]
|
||||
Gui, font, s14
|
||||
Gui, add, button, w190 gNewDeck, New Deck
|
||||
Gui, add, button, x+10 wp gShuffle, Shuffle
|
||||
Gui, add, button, x+10 wp gDeal, Deal
|
||||
Gui, add, text, xs w600 , Current Deck:
|
||||
Gui, add, Edit, xs wp r4 vDeck
|
||||
Gui, add, text, xs , Hands:
|
||||
Gui, add, Edit, x+10 w60 vHands gHands
|
||||
Gui, add, UpDown,, 1
|
||||
Edits := 0
|
||||
|
||||
Hands:
|
||||
Gui, Submit, NoHide
|
||||
loop, % Edits
|
||||
GuiControl,Hide, Hand%A_Index%
|
||||
|
||||
loop, % Hands
|
||||
GuiControl,Show, % "Hand" A_Index
|
||||
|
||||
loop, % Hands - Edits
|
||||
{
|
||||
Edits++
|
||||
Gui, add, ListBox, % "x" (Edits=1?"s":"+10") " w60 r13 vHand" Edits
|
||||
}
|
||||
Gui, show, AutoSize
|
||||
return
|
||||
;-----------------------------------------------
|
||||
GuiClose:
|
||||
ExitApp
|
||||
return
|
||||
;-----------------------------------------------
|
||||
NewDeck:
|
||||
cards := [], deck := Dealt:= ""
|
||||
|
||||
loop, % Hands
|
||||
GuiControl,, Hand%A_Index%, |
|
||||
|
||||
for each, suit in suits
|
||||
for each, value in values
|
||||
cards.Insert(value suit)
|
||||
|
||||
for each, card in cards
|
||||
deck .= card (mod(A_Index, 13) ? " " : "`n")
|
||||
GuiControl,, Deck, % deck
|
||||
GuiControl,, Dealt
|
||||
GuiControl, Enable, Button2
|
||||
GuiControl, Enable, Hands
|
||||
return
|
||||
;-----------------------------------------------
|
||||
shuffle:
|
||||
gosub, NewDeck
|
||||
shuffled := [], deck := ""
|
||||
loop, 52 {
|
||||
Random, rnd, 1, % cards.MaxIndex()
|
||||
shuffled[A_Index] := cards.RemoveAt(rnd)
|
||||
}
|
||||
for each, card in shuffled
|
||||
{
|
||||
deck .= card (mod(A_Index, 13) ? " " : "`n")
|
||||
cards.Insert(card)
|
||||
}
|
||||
GuiControl,, Deck, % deck
|
||||
return
|
||||
;-----------------------------------------------
|
||||
Deal:
|
||||
Gui, Submit, NoHide
|
||||
if ( Hands > cards.MaxIndex())
|
||||
return
|
||||
|
||||
deck := ""
|
||||
loop, % Hands
|
||||
GuiControl,, Hand%A_Index%, % cards.RemoveAt(1)
|
||||
|
||||
GuiControl, Disable, Button2
|
||||
GuiControl, Disable, Hands
|
||||
GuiControl,, Dealt, % Dealt
|
||||
|
||||
for each, card in cards
|
||||
deck .= card (mod(A_Index, 13) ? " " : "`n")
|
||||
GuiControl,, Deck, % deck
|
||||
return
|
||||
;-----------------------------------------------
|
||||
51
Task/Playing-cards/AutoIt/playing-cards.autoit
Normal file
51
Task/Playing-cards/AutoIt/playing-cards.autoit
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
|
||||
#AutoIt3Wrapper_Change2CUI=y
|
||||
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
|
||||
#include <Array.au3>
|
||||
|
||||
; ## GLOBALS ##
|
||||
Global $SUIT = ["D", "H", "S", "C"]
|
||||
Global $FACE = [2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K", "A"]
|
||||
Global $DECK[52]
|
||||
|
||||
; ## CREATES A NEW DECK
|
||||
Func NewDeck()
|
||||
|
||||
For $i = 0 To 3
|
||||
For $x = 0 To 12
|
||||
_ArrayPush($DECK, $FACE[$x] & $SUIT[$i])
|
||||
Next
|
||||
Next
|
||||
|
||||
EndFunc ;==>NewDeck
|
||||
|
||||
; ## SHUFFLE DECK
|
||||
Func Shuffle()
|
||||
|
||||
_ArrayShuffle($DECK)
|
||||
|
||||
EndFunc ;==>Shuffle
|
||||
|
||||
; ## DEAL A CARD
|
||||
Func Deal()
|
||||
|
||||
Return _ArrayPop($DECK)
|
||||
|
||||
EndFunc ;==>Deal
|
||||
|
||||
; ## PRINT DECK
|
||||
Func Print()
|
||||
|
||||
ConsoleWrite(_ArrayToString($DECK) & @CRLF)
|
||||
|
||||
EndFunc ;==>Print
|
||||
|
||||
|
||||
#Region ;#### USAGE ####
|
||||
NewDeck()
|
||||
Print()
|
||||
Shuffle()
|
||||
Print()
|
||||
ConsoleWrite("DEALT: " & Deal() & @CRLF)
|
||||
Print()
|
||||
#EndRegion ;#### USAGE ####
|
||||
46
Task/Playing-cards/BASIC256/playing-cards.basic
Normal file
46
Task/Playing-cards/BASIC256/playing-cards.basic
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
suite$ = "CDHS" #Club, Diamond, Heart, Spade
|
||||
card$ = "A23456789TJQK" #Cards Ace to King
|
||||
card = 0
|
||||
|
||||
dim n(55) #make ordered deck
|
||||
for i = 1 to 52 # of 52 cards
|
||||
n[i] = i
|
||||
next i
|
||||
|
||||
for i = 1 to 52 * 3 #shuffle deck 3 times
|
||||
i1 = int(rand * 52) + 1
|
||||
i2 = int(rand * 52) + 1
|
||||
h2 = n[i1]
|
||||
n[i1] = n[i2]
|
||||
n[i2] = h2
|
||||
next i
|
||||
|
||||
for hand = 1 to 4 #4 hands
|
||||
for deal = 1 to 13 #deal each 13 cards
|
||||
card += 1 #next card in deck
|
||||
s = (n[card] mod 4) + 1 #determine suite
|
||||
c = (n[card] mod 13) + 1 #determine card
|
||||
print mid(card$,c,1);mid(suite$,s,1);" "; #show the card
|
||||
next deal
|
||||
print
|
||||
next hand
|
||||
end
|
||||
|
||||
function word$(sr$, wn, delim$)
|
||||
j = wn
|
||||
if j = 0 then j += 1
|
||||
res$ = "" : s$ = sr$ : d$ = delim$
|
||||
if d$ = "" then d$ = " "
|
||||
sd = length(d$) : sl = length(s$)
|
||||
while true
|
||||
n = instr(s$,d$) : j -= 1
|
||||
if j = 0 then
|
||||
if n=0 then res$=s$ else res$=mid(s$,1,n-1)
|
||||
return res$
|
||||
end if
|
||||
if n = 0 then return res$
|
||||
if n = sl - sd then res$ = "" : return res$
|
||||
sl2 = sl-n : s$ = mid(s$,n+1,sl2) : sl = sl2
|
||||
end while
|
||||
return res$
|
||||
end function
|
||||
47
Task/Playing-cards/BBC-BASIC/playing-cards.basic
Normal file
47
Task/Playing-cards/BBC-BASIC/playing-cards.basic
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
DIM Deck{ncards%, card&(51)}, Suit$(3), Rank$(12)
|
||||
Suit$() = "Clubs", "Diamonds", "Hearts", "Spades"
|
||||
Rank$() = "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", \
|
||||
\ "Eight", "Nine", "Ten", "Jack", "Queen", "King"
|
||||
|
||||
PRINT "Creating a new deck..."
|
||||
PROCnewdeck(deck1{})
|
||||
PRINT "Shuffling the deck..."
|
||||
PROCshuffle(deck1{})
|
||||
PRINT "The first few cards are:"
|
||||
FOR card% = 1 TO 8
|
||||
PRINT FNcardname(deck1.card&(card%))
|
||||
NEXT
|
||||
PRINT "Dealing three cards from the deck:"
|
||||
FOR card% = 1 TO 3
|
||||
PRINT FNcardname(FNdeal(deck1{}))
|
||||
NEXT
|
||||
PRINT "Number of cards remaining in the deck = " ; deck1.ncards%
|
||||
END
|
||||
|
||||
REM Make a new deck:
|
||||
DEF PROCnewdeck(RETURN deck{})
|
||||
LOCAL N%
|
||||
DIM deck{} = Deck{}
|
||||
FOR N% = 0 TO 51
|
||||
deck.card&(N%) = N%
|
||||
deck.ncards% += 1
|
||||
NEXT
|
||||
ENDPROC
|
||||
|
||||
REM Shuffle a deck:
|
||||
DEF PROCshuffle(deck{})
|
||||
LOCAL N%
|
||||
FOR N% = 52 TO 2 STEP -1
|
||||
SWAP deck.card&(N%-1), deck.card&(RND(N%)-1)
|
||||
NEXT
|
||||
ENDPROC
|
||||
|
||||
REM Deal from the 'bottom' of the deck:
|
||||
DEF FNdeal(deck{})
|
||||
IF deck.ncards% = 0 THEN ERROR 100, "Deck is empty"
|
||||
deck.ncards% -= 1
|
||||
= deck.card&(deck.ncards%)
|
||||
|
||||
REM Return the name of a card:
|
||||
DEF FNcardname(card&)
|
||||
= Rank$(card& >> 2) + " of " + Suit$(card& AND 3)
|
||||
99
Task/Playing-cards/Batch-File/playing-cards.bat
Normal file
99
Task/Playing-cards/Batch-File/playing-cards.bat
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
@echo off
|
||||
setlocal enabledelayedexpansion
|
||||
|
||||
call:newdeck deck
|
||||
echo new deck:
|
||||
echo.
|
||||
call:showcards deck
|
||||
echo.
|
||||
echo shuffling:
|
||||
echo.
|
||||
call:shuffle deck
|
||||
call:showcards deck
|
||||
echo.
|
||||
echo dealing 5 cards to 4 players
|
||||
call:deal deck 5 hand1 hand2 hand3 hand4
|
||||
echo.
|
||||
echo player 1 & call:showcards hand1
|
||||
echo.
|
||||
echo player 2 & call:showcards hand2
|
||||
echo.
|
||||
echo player 3 & call:showcards hand3
|
||||
echo.
|
||||
echo player 4 & call:showcards hand4
|
||||
echo.
|
||||
call:count %deck% cnt
|
||||
echo %cnt% cards remaining in the deck
|
||||
echo.
|
||||
call:showcards deck
|
||||
echo.
|
||||
|
||||
exit /b
|
||||
|
||||
:getcard deck hand :: deals 1 card to a player
|
||||
set "loc1=!%~1!"
|
||||
set "%~2=!%~2!!loc1:~0,3!"
|
||||
set "%~1=!loc1:~3!"
|
||||
exit /b
|
||||
|
||||
:deal deck n player1 player2...up to 7
|
||||
set "loc=!%~1!"
|
||||
set "cards=%~2"
|
||||
set players=%3 %4 %5 %6 %7 %8 %9
|
||||
for /L %%j in (1,1,!cards!) do (
|
||||
for %%k in (!players!) do call:getcard loc %%k)
|
||||
set "%~1=!loc!"
|
||||
exit /b
|
||||
|
||||
:newdeck [deck] ::creates a deck of cards
|
||||
:: in the parentheses below there are ascii chars 3,4,5 and 6 representing the suits
|
||||
for %%i in ( ♠ ♦ ♥ ♣ ) do (
|
||||
for %%j in (20 31 42 53 64 75 86 97 T8 J9 QA KB AC) do set loc=!loc!%%i%%j
|
||||
)
|
||||
set "%~1=!loc!"
|
||||
exit /b
|
||||
|
||||
:showcards [deck] :: prints a deck or a hand
|
||||
set "loc=!%~1!"
|
||||
for /L %%j in (0,39,117) do (
|
||||
set s=
|
||||
for /L %%i in (0,3,36) do (
|
||||
set /a n=%%i+%%j
|
||||
call set s=%%s%% %%loc:~!n!,2%%
|
||||
)
|
||||
if "!s: =!" neq "" echo(!s!
|
||||
set /a n+=1
|
||||
if "%loc:~!n!,!%" equ "" goto endloop
|
||||
)
|
||||
:endloop
|
||||
exit /b
|
||||
|
||||
:count deck count
|
||||
set "loc1=%1"
|
||||
set /a cnt1=0
|
||||
for %%i in (96 48 24 12 6 3 ) do if "!loc1:~%%i,1!" neq "" set /a cnt1+=%%i & set loc1=!loc1:~%%i!
|
||||
set /a cnt1=cnt1/3+1
|
||||
set "%~2=!cnt1!"
|
||||
exit /b
|
||||
|
||||
:shuffle (deck) :: shuffles a deck
|
||||
set "loc=!%~1!"
|
||||
call:count %loc%, cnt
|
||||
set /a cnt-=1
|
||||
for /L %%i in (%cnt%,-1,0) do (
|
||||
SET /A "from=%%i,to=(!RANDOM!*(%%i-1)/32768)"
|
||||
call:swap loc from to
|
||||
)
|
||||
set "%~1=!loc!"
|
||||
exit /b
|
||||
|
||||
:swap deck from to :: swaps two cards
|
||||
set "arr=!%~1!"
|
||||
set /a "from=!%~2!*3,to=!%~3!*3"
|
||||
set temp1=!arr:~%from%,3!
|
||||
set temp2=!arr:~%to%,3!
|
||||
set arr=!arr:%temp1%=@@@!
|
||||
set arr=!arr:%temp2%=%temp1%!
|
||||
set arr=!arr:@@@=%temp2%!
|
||||
set "%~1=!arr!"
|
||||
exit /b
|
||||
76
Task/Playing-cards/C++/playing-cards-1.cpp
Normal file
76
Task/Playing-cards/C++/playing-cards-1.cpp
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
#include <deque>
|
||||
#include <algorithm>
|
||||
#include <ostream>
|
||||
#include <iterator>
|
||||
|
||||
namespace cards
|
||||
{
|
||||
class card
|
||||
{
|
||||
public:
|
||||
enum pip_type { two, three, four, five, six, seven, eight, nine, ten,
|
||||
jack, queen, king, ace, pip_count };
|
||||
enum suite_type { hearts, spades, diamonds, clubs, suite_count };
|
||||
enum { unique_count = pip_count * suite_count };
|
||||
|
||||
card(suite_type s, pip_type p): value(s + suite_count * p) {}
|
||||
|
||||
explicit card(unsigned char v = 0): value(v) {}
|
||||
|
||||
pip_type pip() { return pip_type(value / suite_count); }
|
||||
|
||||
suite_type suite() { return suite_type(value % suite_count); }
|
||||
|
||||
private:
|
||||
unsigned char value;
|
||||
};
|
||||
|
||||
const char* const pip_names[] =
|
||||
{ "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
|
||||
"jack", "queen", "king", "ace" };
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, card::pip_type pip)
|
||||
{
|
||||
return os << pip_names[pip];
|
||||
}
|
||||
|
||||
const char* const suite_names[] =
|
||||
{ "hearts", "spades", "diamonds", "clubs" };
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, card::suite_type suite)
|
||||
{
|
||||
return os << suite_names[suite];
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, card c)
|
||||
{
|
||||
return os << c.pip() << " of " << c.suite();
|
||||
}
|
||||
|
||||
class deck
|
||||
{
|
||||
public:
|
||||
deck()
|
||||
{
|
||||
for (int i = 0; i < card::unique_count; ++i) {
|
||||
cards.push_back(card(i));
|
||||
}
|
||||
}
|
||||
|
||||
void shuffle() { std::random_shuffle(cards.begin(), cards.end()); }
|
||||
|
||||
card deal() { card c = cards.front(); cards.pop_front(); return c; }
|
||||
|
||||
typedef std::deque<card>::const_iterator const_iterator;
|
||||
const_iterator begin() const { return cards.cbegin(); }
|
||||
const_iterator end() const { return cards.cend(); }
|
||||
private:
|
||||
std::deque<card> cards;
|
||||
};
|
||||
|
||||
inline std::ostream& operator<<(std::ostream& os, const deck& d)
|
||||
{
|
||||
std::copy(d.begin(), d.end(), std::ostream_iterator<card>(os, "\n"));
|
||||
return os;
|
||||
}
|
||||
}
|
||||
9
Task/Playing-cards/C++/playing-cards-2.cpp
Normal file
9
Task/Playing-cards/C++/playing-cards-2.cpp
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
#include <iostream>
|
||||
|
||||
int main(int argc, const char** args) {
|
||||
cards::deck d;
|
||||
d.shuffle();
|
||||
std::cout << d;
|
||||
|
||||
return 0;
|
||||
}
|
||||
32
Task/Playing-cards/C++/playing-cards-3.cpp
Normal file
32
Task/Playing-cards/C++/playing-cards-3.cpp
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
const std::vector<std::string> cards = { "🂡", "🂱", "🃁", "🃑",
|
||||
"🂢", "🂲", "🃂", "🃒",
|
||||
"🂣", "🂳", "🃃", "🃓",
|
||||
"🂤", "🂴", "🃄", "🃔",
|
||||
"🂥", "🂵", "🃅", "🃕",
|
||||
"🂦", "🂶", "🃆", "🃖",
|
||||
"🂧", "🂷", "🃇", "🃗",
|
||||
"🂨", "🂸", "🂸", "🂸",
|
||||
"🂩", "🂩", "🃉", "🃙",
|
||||
"🂪", "🂺", "🃊", "🃚",
|
||||
"🂫", "🂻", "🃋", "🃛",
|
||||
"🂭", "🂽", "🃍", "🃝",
|
||||
"🂮", "🂾", "🃎", "🃞"
|
||||
};
|
||||
|
||||
int main(int argc, const char* args[]) {
|
||||
auto deck(cards);
|
||||
std::random_shuffle(deck.begin(), deck.end());
|
||||
uint i = 1;
|
||||
for (auto &card: deck) {
|
||||
std::cout << card;
|
||||
if (i++ % 13 == 0)
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
62
Task/Playing-cards/C-sharp/playing-cards.cs
Normal file
62
Task/Playing-cards/C-sharp/playing-cards.cs
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public struct Card
|
||||
{
|
||||
public Card(string rank, string suit) : this()
|
||||
{
|
||||
Rank = rank;
|
||||
Suit = suit;
|
||||
}
|
||||
|
||||
public string Rank { get; }
|
||||
public string Suit { get; }
|
||||
|
||||
public override string ToString() => $"{Rank} of {Suit}";
|
||||
}
|
||||
|
||||
public class Deck : IEnumerable<Card>
|
||||
{
|
||||
static readonly string[] ranks = { "Two", "Three", "Four", "Five", "Six",
|
||||
"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace" };
|
||||
static readonly string[] suits = { "Clubs", "Diamonds", "Hearts", "Spades" };
|
||||
readonly List<Card> cards;
|
||||
|
||||
public Deck() {
|
||||
cards = (from suit in suits
|
||||
from rank in ranks
|
||||
select new Card(rank, suit)).ToList();
|
||||
}
|
||||
|
||||
public int Count => cards.Count;
|
||||
|
||||
public void Shuffle() {
|
||||
// using Knuth Shuffle (see at http://rosettacode.org/wiki/Knuth_shuffle)
|
||||
var random = new Random();
|
||||
for (int i = 0; i < cards.Count; i++) {
|
||||
int r = random.Next(i, cards.Count);
|
||||
var temp = cards[i];
|
||||
cards[i] = cards[r];
|
||||
cards[r] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
public Card Deal() {
|
||||
int last = cards.Count - 1;
|
||||
Card card = cards[last];
|
||||
//Removing from the front will shift the other items back 1 spot,
|
||||
//so that would be an O(n) operation. Removing from the back is O(1).
|
||||
cards.RemoveAt(last);
|
||||
return card;
|
||||
}
|
||||
|
||||
public IEnumerator<Card> GetEnumerator() {
|
||||
//Reverse enumeration of the list so that they are returned in the order they would be dealt.
|
||||
//LINQ's Reverse() copies the entire list. Let's avoid that.
|
||||
for (int i = cards.Count - 1; i >= 0; i--)
|
||||
yield return cards[i];
|
||||
}
|
||||
|
||||
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
}
|
||||
94
Task/Playing-cards/C/playing-cards.c
Normal file
94
Task/Playing-cards/C/playing-cards.c
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <locale.h>
|
||||
|
||||
int locale_ok = 0;
|
||||
|
||||
wchar_t s_suits[] = L"♠♥♦♣";
|
||||
/* if your file can't contain unicode, use the next line instead */
|
||||
//wchar_t s_suits[] = L"\x2660\x2665\x2666\x2663";
|
||||
|
||||
const char *s_suits_ascii[] = { "S", "H", "D", "C" };
|
||||
const char *s_nums[] = { "WHAT",
|
||||
"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K",
|
||||
"OVERFLOW"
|
||||
};
|
||||
|
||||
typedef struct { int suit, number, _s; } card_t, *card;
|
||||
typedef struct { int n; card_t cards[52]; } deck_t, *deck;
|
||||
|
||||
void show_card(card c)
|
||||
{
|
||||
if (locale_ok)
|
||||
printf(" %lc%s", s_suits[c->suit], s_nums[c->number]);
|
||||
else
|
||||
printf(" %s%s", s_suits_ascii[c->suit], s_nums[c->number]);
|
||||
}
|
||||
|
||||
deck new_deck()
|
||||
{
|
||||
int i, j, k;
|
||||
deck d = malloc(sizeof(deck_t));
|
||||
d->n = 52;
|
||||
for (i = k = 0; i < 4; i++)
|
||||
for (j = 1; j <= 13; j++, k++) {
|
||||
d->cards[k].suit = i;
|
||||
d->cards[k].number = j;
|
||||
}
|
||||
return d;
|
||||
}
|
||||
|
||||
void show_deck(deck d)
|
||||
{
|
||||
int i;
|
||||
printf("%d cards:", d->n);
|
||||
for (i = 0; i < d->n; i++)
|
||||
show_card(d->cards + i);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
int cmp_card(const void *a, const void *b)
|
||||
{
|
||||
int x = ((card)a)->_s, y = ((card)b)->_s;
|
||||
return x < y ? -1 : x > y;
|
||||
}
|
||||
|
||||
card deal_card(deck d)
|
||||
{
|
||||
if (!d->n) return 0;
|
||||
return d->cards + --d->n;
|
||||
}
|
||||
|
||||
void shuffle_deck(deck d)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < d->n; i++)
|
||||
d->cards[i]._s = rand();
|
||||
qsort(d->cards, d->n, sizeof(card_t), cmp_card);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int i, j;
|
||||
deck d = new_deck();
|
||||
|
||||
locale_ok = (0 != setlocale(LC_CTYPE, ""));
|
||||
|
||||
printf("New deck, "); show_deck(d);
|
||||
|
||||
printf("\nShuffle and deal to three players:\n");
|
||||
shuffle_deck(d);
|
||||
for (i = 0; i < 3; i++) {
|
||||
for (j = 0; j < 5; j++)
|
||||
show_card(deal_card(d));
|
||||
printf("\n");
|
||||
}
|
||||
printf("Left in deck "); show_deck(d);
|
||||
|
||||
/* freeing the data struct requires just free(), but it depends on the
|
||||
* situation: there might be cards dealt out somewhere, which is not
|
||||
* in the scope of this task.
|
||||
*/
|
||||
//free(d);
|
||||
return 0;
|
||||
}
|
||||
118
Task/Playing-cards/CLU/playing-cards.clu
Normal file
118
Task/Playing-cards/CLU/playing-cards.clu
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
% Represents one playing card.
|
||||
card = cluster is make, parse, unparse, equal, all_cards
|
||||
rep = struct[pip: int, suit: char]
|
||||
own suits: string := "CHSD";
|
||||
own pips: sequence[string] := sequence[string]$
|
||||
["A","2","3","4","5","6","7","8","9","10","J","Q","K"]
|
||||
|
||||
find_pip = proc (pip: string) returns (int) signals (bad_format)
|
||||
for i: int in int$from_to(1, sequence[string]$size(pips)) do
|
||||
if pips[i] = pip then return(i) end
|
||||
end
|
||||
signal bad_format
|
||||
end find_pip
|
||||
|
||||
make = proc (pip: int, suit: char) returns (cvt) signals (bad_format)
|
||||
if string$indexc(suit, suits) = 0
|
||||
cor ~(pip >= 1 cand pip <= 13)
|
||||
then signal bad_format
|
||||
end
|
||||
return(rep${pip: pip, suit: suit})
|
||||
end make
|
||||
|
||||
parse = proc (s: string) returns (cvt) signals (bad_format)
|
||||
size: int := string$size(s)
|
||||
if size<2 cor size>3 then signal bad_format end
|
||||
pip: string := string$substr(s, 1, size-1)
|
||||
suit: char := string$rest(s, size-1)[1]
|
||||
return(down(make(find_pip(pip), suit))) resignal bad_format
|
||||
end parse
|
||||
|
||||
unparse = proc (c: cvt) returns (string)
|
||||
return( pips[c.pip] || string$c2s(c.suit) )
|
||||
end unparse
|
||||
|
||||
equal = proc (a, b: cvt) returns (bool)
|
||||
return( a.pip = b.pip cand a.suit = b.suit )
|
||||
end equal
|
||||
|
||||
% Yield all cards in the canonical order
|
||||
all_cards = iter () yields (cvt)
|
||||
for suit: char in string$chars(suits) do
|
||||
for pip: int in int$from_to(1,sequence[string]$size(pips)) do
|
||||
yield(down(make(pip, suit)))
|
||||
end
|
||||
end
|
||||
end all_cards
|
||||
end card
|
||||
|
||||
% Represents a deck
|
||||
deck = cluster is new, shuffle, cards, deal, unparse
|
||||
rep = array[card]
|
||||
|
||||
new = proc () returns (cvt)
|
||||
d: rep := rep$new()
|
||||
for c: card in card$all_cards() do rep$addh(d, c) end
|
||||
return(d)
|
||||
end new
|
||||
|
||||
shuffle = proc (d: cvt)
|
||||
lo: int := rep$low(d)
|
||||
hi: int := rep$high(d)
|
||||
for i: int in int$from_to_by(hi, lo+1, -1) do
|
||||
j: int := lo + random$next(i-lo)
|
||||
c: card := d[i]
|
||||
d[i] := d[j]
|
||||
d[j] := c
|
||||
end
|
||||
end shuffle
|
||||
|
||||
cards = iter (d: cvt) yields (card)
|
||||
for c: card in rep$elements(d) do yield(c) end
|
||||
end cards
|
||||
|
||||
deal = proc (d: cvt) returns (card) signals (empty)
|
||||
if rep$empty(d) then signal empty end
|
||||
return(rep$reml(d))
|
||||
end deal
|
||||
|
||||
unparse = proc (d: cvt) returns (string)
|
||||
ss: stream := stream$create_output()
|
||||
n: int := 0
|
||||
for c: card in cards(up(d)) do
|
||||
if n~=0 cand n//13=0 then stream$putc(ss, '\n')
|
||||
elseif n~=0 then stream$putc(ss, ' ')
|
||||
end
|
||||
stream$puts(ss, card$unparse(c))
|
||||
n := n+1
|
||||
end
|
||||
return(stream$get_contents(ss))
|
||||
end unparse
|
||||
end deck
|
||||
|
||||
start_up = proc ()
|
||||
po: stream := stream$primary_output()
|
||||
% seed the RNG
|
||||
d_: date := now()
|
||||
random$seed(d_.second + 60*(d_.minute + 60*d_.hour))
|
||||
|
||||
% make a new deck
|
||||
d: deck := deck$new()
|
||||
stream$putl(po, "New deck: ")
|
||||
stream$putl(po, deck$unparse(d))
|
||||
|
||||
% shuffle the deck
|
||||
deck$shuffle(d)
|
||||
stream$putl(po, "\nShuffled deck: ")
|
||||
stream$putl(po, deck$unparse(d))
|
||||
|
||||
% deal some cards
|
||||
stream$puts(po, "\nDealing 10 cards:")
|
||||
for i: int in int$from_to(1, 10) do
|
||||
stream$puts(po, " " || card$unparse(deck$deal(d)))
|
||||
end
|
||||
|
||||
% show remaining deck
|
||||
stream$putl(po, "\n\nRemaining cards in deck:")
|
||||
stream$putl(po, deck$unparse(d))
|
||||
end start_up
|
||||
177
Task/Playing-cards/COBOL/playing-cards.cobol
Normal file
177
Task/Playing-cards/COBOL/playing-cards.cobol
Normal file
|
|
@ -0,0 +1,177 @@
|
|||
identification division.
|
||||
program-id. playing-cards.
|
||||
|
||||
environment division.
|
||||
configuration section.
|
||||
repository.
|
||||
function all intrinsic.
|
||||
|
||||
data division.
|
||||
working-storage section.
|
||||
77 card usage index.
|
||||
01 deck.
|
||||
05 cards occurs 52 times ascending key slot indexed by card.
|
||||
10 slot pic 99.
|
||||
10 hand pic 99.
|
||||
10 suit pic 9.
|
||||
10 symbol pic x(4).
|
||||
10 rank pic 99.
|
||||
|
||||
01 filler.
|
||||
05 suit-name pic x(8) occurs 4 times.
|
||||
|
||||
*> Unicode U+1F0Ax, Bx, Cx, Dx "f09f82a0" "82b0" "8380" "8390"
|
||||
01 base-s constant as 4036985504.
|
||||
01 base-h constant as 4036985520.
|
||||
01 base-d constant as 4036985728.
|
||||
01 base-c constant as 4036985744.
|
||||
|
||||
01 sym pic x(4) comp-x.
|
||||
01 symx redefines sym pic x(4).
|
||||
77 s pic 9.
|
||||
77 r pic 99.
|
||||
77 c pic 99.
|
||||
77 hit pic 9.
|
||||
77 limiter pic 9(6).
|
||||
|
||||
01 spades constant as 1.
|
||||
01 hearts constant as 2.
|
||||
01 diamonds constant as 3.
|
||||
01 clubs constant as 4.
|
||||
|
||||
01 players constant as 3.
|
||||
01 cards-per constant as 5.
|
||||
01 deal pic 99.
|
||||
01 player pic 99.
|
||||
|
||||
01 show-tally pic zz.
|
||||
01 show-rank pic z(5).
|
||||
01 arg pic 9(10).
|
||||
|
||||
procedure division.
|
||||
cards-main.
|
||||
perform seed
|
||||
perform initialize-deck
|
||||
perform shuffle-deck
|
||||
perform deal-deck
|
||||
perform display-hands
|
||||
goback.
|
||||
|
||||
*> ********
|
||||
seed.
|
||||
accept arg from command-line
|
||||
if arg not equal 0 then
|
||||
move random(arg) to c
|
||||
end-if
|
||||
.
|
||||
|
||||
initialize-deck.
|
||||
move "spades" to suit-name(spades)
|
||||
move "hearts" to suit-name(hearts)
|
||||
move "diamonds" to suit-name(diamonds)
|
||||
move "clubs" to suit-name(clubs)
|
||||
|
||||
perform varying s from 1 by 1 until s > 4
|
||||
after r from 1 by 1 until r > 13
|
||||
compute c = (s - 1) * 13 + r
|
||||
evaluate s
|
||||
when spades compute sym = base-s + r
|
||||
when hearts compute sym = base-h + r
|
||||
when diamonds compute sym = base-d + r
|
||||
when clubs compute sym = base-c + r
|
||||
end-evaluate
|
||||
if r > 11 then compute sym = sym + 1 end-if
|
||||
move s to suit(c)
|
||||
move r to rank(c)
|
||||
move symx to symbol(c)
|
||||
move zero to slot(c)
|
||||
move zero to hand(c)
|
||||
end-perform
|
||||
.
|
||||
|
||||
shuffle-deck.
|
||||
move zero to limiter
|
||||
perform until exit
|
||||
compute c = random() * 52.0 + 1.0
|
||||
move zero to hit
|
||||
perform varying tally from 1 by 1 until tally > 52
|
||||
if slot(tally) equal c then
|
||||
move 1 to hit
|
||||
exit perform
|
||||
end-if
|
||||
if slot(tally) equal 0 then
|
||||
if tally < 52 then move 1 to hit end-if
|
||||
move c to slot(tally)
|
||||
exit perform
|
||||
end-if
|
||||
end-perform
|
||||
if hit equal zero then exit perform end-if
|
||||
if limiter > 999999 then
|
||||
display "too many shuffles, deck invalid" upon syserr
|
||||
exit perform
|
||||
end-if
|
||||
add 1 to limiter
|
||||
end-perform
|
||||
sort cards ascending key slot
|
||||
.
|
||||
|
||||
display-card.
|
||||
>>IF ENGLISH IS DEFINED
|
||||
move rank(tally) to show-rank
|
||||
evaluate rank(tally)
|
||||
when 1 display " ace" with no advancing
|
||||
when 2 thru 10 display show-rank with no advancing
|
||||
when 11 display " jack" with no advancing
|
||||
when 12 display "queen" with no advancing
|
||||
when 13 display " king" with no advancing
|
||||
end-evaluate
|
||||
display " of " suit-name(suit(tally)) with no advancing
|
||||
>>ELSE
|
||||
display symbol(tally) with no advancing
|
||||
>>END-IF
|
||||
.
|
||||
|
||||
display-deck.
|
||||
perform varying tally from 1 by 1 until tally > 52
|
||||
move tally to show-tally
|
||||
display "Card: " show-tally
|
||||
" currently in hand " hand(tally)
|
||||
" is " with no advancing
|
||||
perform display-card
|
||||
display space
|
||||
end-perform
|
||||
.
|
||||
|
||||
display-hands.
|
||||
perform varying player from 1 by 1 until player > players
|
||||
move player to tally
|
||||
display "Player " player ": " with no advancing
|
||||
perform varying deal from 1 by 1 until deal > cards-per
|
||||
perform display-card
|
||||
add players to tally
|
||||
end-perform
|
||||
display space
|
||||
end-perform
|
||||
display "Stock: " with no advancing
|
||||
subtract players from tally
|
||||
add 1 to tally
|
||||
perform varying tally from tally by 1 until tally > 52
|
||||
perform display-card
|
||||
>>IF ENGLISH IS DEFINED
|
||||
display space
|
||||
>>END-IF
|
||||
end-perform
|
||||
display space
|
||||
.
|
||||
|
||||
deal-deck.
|
||||
display "Dealing " cards-per " cards to " players " players"
|
||||
move 1 to tally
|
||||
perform varying deal from 1 by 1 until deal > cards-per
|
||||
after player from 1 by 1 until player > players
|
||||
move player to hand(tally)
|
||||
add 1 to tally
|
||||
end-perform
|
||||
.
|
||||
|
||||
end program playing-cards.
|
||||
81
Task/Playing-cards/Ceylon/playing-cards.ceylon
Normal file
81
Task/Playing-cards/Ceylon/playing-cards.ceylon
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
import com.vasileff.ceylon.random.api { ... }
|
||||
|
||||
"""Run the example code for Rosetta Code ["Playing cards" task] (http://rosettacode.org/wiki/Playing_cards)."""
|
||||
shared void run() {
|
||||
variable value deck = Deck();
|
||||
print("New deck (``deck.size`` cards): ``deck``
|
||||
");
|
||||
|
||||
deck = deck.shuffle();
|
||||
print("Shuffeled deck (``deck.size`` cards): ``deck``
|
||||
");
|
||||
|
||||
print("Deal three hands: ");
|
||||
for (i in 1..3) {
|
||||
value [hand, _deck] = deck.deal();
|
||||
print("- Dealt ``hand.size`` cards to hand ``i`` : ``join(hand)``");
|
||||
deck = _deck;
|
||||
}
|
||||
|
||||
print("
|
||||
Deck (``deck.size`` cards) after dealing three hands: ``deck``");
|
||||
|
||||
}
|
||||
|
||||
abstract class Suit() of clubs | hearts | spades | diamonds {}
|
||||
|
||||
object clubs extends Suit() { string = "♣"; }
|
||||
object hearts extends Suit() { string = "♥"; }
|
||||
object spades extends Suit() { string = "♠"; }
|
||||
object diamonds extends Suit() { string = "♦"; }
|
||||
|
||||
abstract class Pip() of two | three | four | five | six | seven | eight | nine | ten | jack | queen | king | ace {}
|
||||
object two extends Pip() { string = "2"; }
|
||||
object three extends Pip() { string = "3"; }
|
||||
object four extends Pip() { string = "4"; }
|
||||
object five extends Pip() { string = "5"; }
|
||||
object six extends Pip() { string = "6"; }
|
||||
object seven extends Pip() { string = "7"; }
|
||||
object eight extends Pip() { string = "8"; }
|
||||
object nine extends Pip() { string = "9"; }
|
||||
object ten extends Pip() { string = "10"; }
|
||||
object jack extends Pip() { string = "J"; }
|
||||
object queen extends Pip() { string = "Q"; }
|
||||
object king extends Pip() { string = "K"; }
|
||||
object ace extends Pip() { string = "A"; }
|
||||
|
||||
class Card(shared Pip pip, shared Suit suit) {
|
||||
string = "``pip`` of ``suit``";
|
||||
}
|
||||
|
||||
|
||||
String join(Card[] cards) => ", ".join { *cards };
|
||||
|
||||
class Deck (cards = [ for (suit in `Suit`.caseValues) for (pip in `Pip`.caseValues) Card(pip, suit) ]) {
|
||||
shared Card[] cards;
|
||||
|
||||
shared Deck shuffle(Random rnd = platformRandom())
|
||||
=> if (nonempty cards)
|
||||
then Deck( [*randomize(cards, rnd)] )
|
||||
else this;
|
||||
|
||||
shared Integer size => cards.size;
|
||||
|
||||
shared Boolean empty => cards.empty;
|
||||
|
||||
string => if (size > 13)
|
||||
then "\n " + "\n ". join { *cards.partition(13).map((cards) => join(cards)) }
|
||||
else join(cards);
|
||||
|
||||
shared [Card[], Deck] deal(Integer handSize = 5) {
|
||||
if (handSize >= cards.size) {
|
||||
return [cards, Deck([])];
|
||||
}
|
||||
else {
|
||||
return [
|
||||
cards.initial(handSize),
|
||||
Deck(cards.skip(handSize).sequence())
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
12
Task/Playing-cards/Clojure/playing-cards.clj
Normal file
12
Task/Playing-cards/Clojure/playing-cards.clj
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
(def suits [:club :diamond :heart :spade])
|
||||
(def pips [:ace 2 3 4 5 6 7 8 9 10 :jack :queen :king])
|
||||
|
||||
(defn deck [] (for [s suits p pips] [s p]))
|
||||
|
||||
(def shuffle clojure.core/shuffle)
|
||||
(def deal first)
|
||||
(defn output [deck]
|
||||
(doseq [[suit pip] deck]
|
||||
(println (format "%s of %ss"
|
||||
(if (keyword? pip) (name pip) pip)
|
||||
(name suit)))))
|
||||
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()
|
||||
22
Task/Playing-cards/Common-Lisp/playing-cards.lisp
Normal file
22
Task/Playing-cards/Common-Lisp/playing-cards.lisp
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
(defconstant +suits+
|
||||
'(club diamond heart spade)
|
||||
"Card suits are the symbols club, diamond, heart, and spade.")
|
||||
|
||||
(defconstant +pips+
|
||||
'(ace 2 3 4 5 6 7 8 9 10 jack queen king)
|
||||
"Card pips are the numbers 2 through 10, and the symbols ace, jack,
|
||||
queen and king.")
|
||||
|
||||
(defun make-deck (&aux (deck '()))
|
||||
"Returns a list of cards, where each card is a cons whose car is a
|
||||
suit and whose cdr is a pip."
|
||||
(dolist (suit +suits+ deck)
|
||||
(dolist (pip +pips+)
|
||||
(push (cons suit pip) deck))))
|
||||
|
||||
(defun shuffle (list)
|
||||
"Returns a shuffling of list, by sorting it with a random
|
||||
predicate. List may be modified."
|
||||
(sort list #'(lambda (x y)
|
||||
(declare (ignore x y))
|
||||
(zerop (random 2)))))
|
||||
31
Task/Playing-cards/D/playing-cards-1.d
Normal file
31
Task/Playing-cards/D/playing-cards-1.d
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import std.stdio, std.typecons, std.algorithm, std.traits, std.array,
|
||||
std.range, std.random;
|
||||
|
||||
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);
|
||||
|
||||
auto newDeck() pure nothrow @safe {
|
||||
return cartesianProduct([EnumMembers!Pip], [EnumMembers!Suit]);
|
||||
}
|
||||
|
||||
alias shuffleDeck = randomShuffle;
|
||||
|
||||
Card dealCard(ref Card[] deck) pure nothrow @safe @nogc {
|
||||
immutable card = deck.back;
|
||||
deck.popBack;
|
||||
return card;
|
||||
}
|
||||
|
||||
void show(in Card[] deck) @safe {
|
||||
writefln("Deck:\n%(%s\n%)\n", deck);
|
||||
}
|
||||
|
||||
void main() /*@safe*/ {
|
||||
auto d = newDeck.array;
|
||||
d.show;
|
||||
d.shuffleDeck;
|
||||
while (!d.empty)
|
||||
d.dealCard.writeln;
|
||||
}
|
||||
111
Task/Playing-cards/D/playing-cards-2.d
Normal file
111
Task/Playing-cards/D/playing-cards-2.d
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
import std.stdio, std.random, std.algorithm, std.string, std.range;
|
||||
|
||||
struct Card {
|
||||
static immutable suits = "Club Heart Diamond Spade".split;
|
||||
static immutable pips = "Ace 2 3 4 5 6 7 8 9 10 J Q K".split;
|
||||
enum nPack = suits.length * pips.length;
|
||||
|
||||
static bool rankAceTop = true;
|
||||
int pip, suit;
|
||||
|
||||
string toString() pure const {
|
||||
return format("%3s of %-7s", pips[pip], suits[suit])
|
||||
.rightJustify(15);
|
||||
}
|
||||
|
||||
@property int order() const nothrow {
|
||||
immutable pipOrder = (!rankAceTop) ?
|
||||
pip :
|
||||
(pip ? pip - 1 : 12);
|
||||
return pipOrder * suits.length + suit;
|
||||
}
|
||||
|
||||
bool opEqual(in Card rhs) const pure nothrow {
|
||||
return pip == rhs.pip && suit == rhs.suit;
|
||||
}
|
||||
|
||||
int opCmp(in Card rhs) const nothrow {
|
||||
return order - rhs.order;
|
||||
}
|
||||
}
|
||||
|
||||
final class Deck {
|
||||
private Card[] cards;
|
||||
|
||||
this(in bool initShuffle = true, in int pack = 0) {
|
||||
cards.length = 0;
|
||||
foreach (immutable p; 0 .. pack)
|
||||
foreach (immutable c; 0 .. Card.nPack)
|
||||
cards ~= Card((c / Card.suits.length) %
|
||||
Card.pips.length,
|
||||
c % Card.suits.length);
|
||||
|
||||
if (initShuffle)
|
||||
cards.randomShuffle;
|
||||
}
|
||||
|
||||
@property size_t length() const pure nothrow {
|
||||
return cards.length;
|
||||
}
|
||||
|
||||
Deck add(in Card c) pure nothrow {
|
||||
cards ~= c;
|
||||
return this;
|
||||
}
|
||||
|
||||
Deck deal(in int loc, Deck toDeck = null) pure nothrow {
|
||||
if (toDeck !is null)
|
||||
toDeck.add(cards[loc]);
|
||||
cards = cards[0 .. loc] ~ cards[loc + 1 .. $];
|
||||
return this;
|
||||
}
|
||||
|
||||
Deck dealTop(Deck toDeck = null) pure nothrow {
|
||||
return deal(length - 1, toDeck);
|
||||
}
|
||||
|
||||
Card opIndex(in int loc) const pure nothrow {
|
||||
return cards[loc];
|
||||
}
|
||||
|
||||
alias opIndex peek;
|
||||
|
||||
Deck showDeck() {
|
||||
this.writeln;
|
||||
return this;
|
||||
}
|
||||
|
||||
Deck shuffle() {
|
||||
cards.randomShuffle;
|
||||
return this;
|
||||
}
|
||||
|
||||
Deck sortDeck() {
|
||||
cards.sort!q{a > b};
|
||||
return this;
|
||||
}
|
||||
|
||||
override string toString() pure const {
|
||||
return format("%(%(%s%)\n%)", cards.chunks(4));
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
Deck[4] guests;
|
||||
foreach (ref g; guests)
|
||||
g = new Deck; // Empty deck.
|
||||
|
||||
auto host = new Deck(false, 1);
|
||||
writeln("Host");
|
||||
host.shuffle.showDeck;
|
||||
|
||||
while (host.length > 0)
|
||||
foreach (ref g; guests)
|
||||
if (host.length > 0)
|
||||
host.dealTop(g);
|
||||
|
||||
foreach (immutable i, g; guests) {
|
||||
writefln("Player #%d", i + 1);
|
||||
g.sortDeck.showDeck;
|
||||
}
|
||||
}
|
||||
125
Task/Playing-cards/Delphi/playing-cards.delphi
Normal file
125
Task/Playing-cards/Delphi/playing-cards.delphi
Normal file
|
|
@ -0,0 +1,125 @@
|
|||
program Cards;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses
|
||||
SysUtils, Classes;
|
||||
|
||||
type
|
||||
TPip = (pTwo, pThree, pFour, pFive, pSix, pSeven, pEight, pNine, pTen, pJack, pQueen, pKing, pAce);
|
||||
TSuite = (sDiamonds, sSpades, sHearts, sClubs);
|
||||
|
||||
const
|
||||
cPipNames: array[TPip] of string = ('2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A');
|
||||
cSuiteNames: array[TSuite] of string = ('Diamonds', 'Spades', 'Hearts', 'Clubs');
|
||||
|
||||
type
|
||||
TCard = class
|
||||
private
|
||||
FSuite: TSuite;
|
||||
FPip: TPip;
|
||||
public
|
||||
constructor Create(aSuite: TSuite; aPip: TPip);
|
||||
function ToString: string; override;
|
||||
|
||||
property Pip: TPip read FPip;
|
||||
property Suite: TSuite read FSuite;
|
||||
end;
|
||||
|
||||
TDeck = class
|
||||
private
|
||||
FCards: TList;
|
||||
public
|
||||
constructor Create;
|
||||
destructor Destroy; override;
|
||||
procedure Shuffle;
|
||||
function Deal: TCard;
|
||||
function ToString: string; override;
|
||||
end;
|
||||
|
||||
{ TCard }
|
||||
|
||||
constructor TCard.Create(aSuite: TSuite; aPip: TPip);
|
||||
begin
|
||||
FSuite := aSuite;
|
||||
FPip := aPip;
|
||||
end;
|
||||
|
||||
function TCard.ToString: string;
|
||||
begin
|
||||
Result := Format('%s of %s', [cPipNames[Pip], cSuiteNames[Suite]])
|
||||
end;
|
||||
|
||||
{ TDeck }
|
||||
|
||||
constructor TDeck.Create;
|
||||
var
|
||||
pip: TPip;
|
||||
suite: TSuite;
|
||||
begin
|
||||
FCards := TList.Create;
|
||||
for suite := Low(TSuite) to High(TSuite) do
|
||||
for pip := Low(TPip) to High(TPip) do
|
||||
FCards.Add(TCard.Create(suite, pip));
|
||||
end;
|
||||
|
||||
function TDeck.Deal: TCard;
|
||||
begin
|
||||
Result := FCards[0];
|
||||
FCards.Delete(0);
|
||||
end;
|
||||
|
||||
destructor TDeck.Destroy;
|
||||
var
|
||||
i: Integer;
|
||||
c: TCard;
|
||||
begin
|
||||
for i := FCards.Count - 1 downto 0 do begin
|
||||
c := FCards[i];
|
||||
FCards.Delete(i);
|
||||
c.Free;
|
||||
end;
|
||||
FCards.Free;
|
||||
inherited;
|
||||
end;
|
||||
|
||||
procedure TDeck.Shuffle;
|
||||
var
|
||||
i, j: Integer;
|
||||
temp: TCard;
|
||||
begin
|
||||
Randomize;
|
||||
for i := FCards.Count - 1 downto 0 do begin
|
||||
j := Random(FCards.Count);
|
||||
temp := FCards[j];
|
||||
FCards.Delete(j);
|
||||
FCards.Add(temp);
|
||||
end;
|
||||
end;
|
||||
|
||||
function TDeck.ToString: string;
|
||||
var
|
||||
i: Integer;
|
||||
begin
|
||||
for i := 0 to FCards.Count - 1 do
|
||||
Writeln(TCard(FCards[i]).ToString);
|
||||
end;
|
||||
|
||||
begin
|
||||
with TDeck.Create do
|
||||
try
|
||||
Shuffle;
|
||||
ToString;
|
||||
Writeln;
|
||||
with Deal do
|
||||
try
|
||||
Writeln(ToString);
|
||||
finally
|
||||
Free;
|
||||
end;
|
||||
finally
|
||||
Free;
|
||||
end;
|
||||
|
||||
Readln;
|
||||
end.
|
||||
41
Task/Playing-cards/Elixir/playing-cards.elixir
Normal file
41
Task/Playing-cards/Elixir/playing-cards.elixir
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
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
|
||||
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"].
|
||||
19
Task/Playing-cards/Factor/playing-cards.factor
Normal file
19
Task/Playing-cards/Factor/playing-cards.factor
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
USING: formatting grouping io kernel math qw random sequences
|
||||
vectors ;
|
||||
IN: rosetta-code.playing-cards
|
||||
|
||||
CONSTANT: pips qw{ A 2 3 4 5 6 7 8 9 10 J Q K }
|
||||
CONSTANT: suits qw{ ♥ ♣ ♦ ♠ }
|
||||
|
||||
: <deck> ( -- vec ) 52 <iota> >vector ;
|
||||
|
||||
: card>str ( n -- str )
|
||||
13 /mod [ suits nth ] [ pips nth ] bi* prepend ;
|
||||
|
||||
: print-deck ( seq -- )
|
||||
13 group [ [ card>str "%-4s" printf ] each nl ] each ;
|
||||
|
||||
<deck> ! make new deck
|
||||
randomize ! shuffle the deck
|
||||
dup pop drop ! deal from the deck (and discard)
|
||||
print-deck ! print the deck
|
||||
66
Task/Playing-cards/Fantom/playing-cards.fantom
Normal file
66
Task/Playing-cards/Fantom/playing-cards.fantom
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
enum class Suit { clubs, diamonds, hearts, spades }
|
||||
enum class Pips { ace, two, three, four, five,
|
||||
six, seven, eight, nine, ten, jack, queen, king
|
||||
}
|
||||
|
||||
class Card
|
||||
{
|
||||
readonly Suit suit
|
||||
readonly Pips pips
|
||||
|
||||
new make (Suit suit, Pips pips)
|
||||
{
|
||||
this.suit = suit
|
||||
this.pips = pips
|
||||
}
|
||||
|
||||
override Str toStr ()
|
||||
{
|
||||
"card: $pips of $suit"
|
||||
}
|
||||
}
|
||||
|
||||
class Deck
|
||||
{
|
||||
Card[] deck := [,]
|
||||
|
||||
new make ()
|
||||
{
|
||||
Suit.vals.each |val|
|
||||
{
|
||||
Pips.vals.each |pip| { deck.add (Card(val, pip)) }
|
||||
}
|
||||
}
|
||||
|
||||
Void shuffle (Int swaps := 50)
|
||||
{
|
||||
swaps.times { deck.swap(Int.random(0..deck.size-1), Int.random(0..deck.size-1)) }
|
||||
}
|
||||
|
||||
Card[] deal (Int cards := 1)
|
||||
{
|
||||
if (cards > deck.size) throw ArgErr("$cards is larger than ${deck.size}")
|
||||
Card[] result := [,]
|
||||
cards.times { result.push (deck.removeAt (0)) }
|
||||
return result
|
||||
}
|
||||
|
||||
Void show ()
|
||||
{
|
||||
deck.each |card| { echo (card.toStr) }
|
||||
}
|
||||
}
|
||||
|
||||
class PlayingCards
|
||||
{
|
||||
public static Void main ()
|
||||
{
|
||||
deck := Deck()
|
||||
deck.shuffle
|
||||
Card[] hand := deck.deal (7)
|
||||
echo ("Dealt hand is:")
|
||||
hand.each |card| { echo (card.toStr) }
|
||||
echo ("Remaining deck is:")
|
||||
deck.show
|
||||
}
|
||||
}
|
||||
32
Task/Playing-cards/Forth/playing-cards.fth
Normal file
32
Task/Playing-cards/Forth/playing-cards.fth
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
require random.fs \ RANDOM ( n -- 0..n-1 ) is called CHOOSE in other Forths
|
||||
|
||||
create pips s" A23456789TJQK" mem,
|
||||
create suits s" DHCS" mem, \ diamonds, hearts, clubs, spades
|
||||
: .card ( c -- )
|
||||
13 /mod swap
|
||||
pips + c@ emit
|
||||
suits + c@ emit ;
|
||||
|
||||
create deck 52 allot
|
||||
variable dealt
|
||||
|
||||
: new-deck
|
||||
52 0 do i deck i + c! loop 0 dealt ! ;
|
||||
: .deck
|
||||
52 dealt @ ?do deck i + c@ .card space loop cr ;
|
||||
: shuffle
|
||||
51 0 do
|
||||
52 i - random i + ( rand-index ) deck +
|
||||
deck i + c@ over c@
|
||||
deck i + c! swap c!
|
||||
loop ;
|
||||
: cards-left ( -- n ) 52 dealt @ - ;
|
||||
: deal-card ( -- c )
|
||||
cards-left 0= abort" Deck empty!"
|
||||
deck dealt @ + c@ 1 dealt +! ;
|
||||
: .hand ( n -- )
|
||||
0 do deal-card .card space loop cr ;
|
||||
|
||||
new-deck shuffle .deck
|
||||
5 .hand
|
||||
cards-left . \ 47
|
||||
66
Task/Playing-cards/Fortran/playing-cards-1.f
Normal file
66
Task/Playing-cards/Fortran/playing-cards-1.f
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
MODULE Cards
|
||||
|
||||
IMPLICIT NONE
|
||||
|
||||
TYPE Card
|
||||
CHARACTER(5) :: value
|
||||
CHARACTER(8) :: suit
|
||||
END TYPE Card
|
||||
|
||||
TYPE(Card) :: deck(52), hand(52)
|
||||
TYPE(Card) :: temp
|
||||
|
||||
CHARACTER(5) :: pip(13) = (/"Two ", "Three", "Four ", "Five ", "Six ", "Seven", "Eight", "Nine ", "Ten ", &
|
||||
"Jack ", "Queen", "King ", "Ace "/)
|
||||
CHARACTER(8) :: suits(4) = (/"Clubs ", "Diamonds", "Hearts ", "Spades "/)
|
||||
INTEGER :: i, j, n, rand, dealt = 0
|
||||
REAL :: x
|
||||
|
||||
CONTAINS
|
||||
|
||||
SUBROUTINE Init_deck
|
||||
! Create deck
|
||||
DO i = 1, 4
|
||||
DO j = 1, 13
|
||||
deck((i-1)*13+j) = Card(pip(j), suits(i))
|
||||
END DO
|
||||
END DO
|
||||
END SUBROUTINE Init_deck
|
||||
|
||||
SUBROUTINE Shuffle_deck
|
||||
! Shuffle deck using Fisher-Yates algorithm
|
||||
DO i = 52-dealt, 1, -1
|
||||
CALL RANDOM_NUMBER(x)
|
||||
rand = INT(x * i) + 1
|
||||
temp = deck(rand)
|
||||
deck(rand) = deck(i)
|
||||
deck(i) = temp
|
||||
END DO
|
||||
END SUBROUTINE Shuffle_deck
|
||||
|
||||
SUBROUTINE Deal_hand(number)
|
||||
! Deal from deck to hand
|
||||
INTEGER :: number
|
||||
DO i = 1, number
|
||||
hand(i) = deck(dealt+1)
|
||||
dealt = dealt + 1
|
||||
END DO
|
||||
END SUBROUTINE
|
||||
|
||||
SUBROUTINE Print_hand
|
||||
! Print cards in hand
|
||||
DO i = 1, dealt
|
||||
WRITE (*, "(3A)") TRIM(deck(i)%value), " of ", TRIM(deck(i)%suit)
|
||||
END DO
|
||||
WRITE(*,*)
|
||||
END SUBROUTINE Print_hand
|
||||
|
||||
SUBROUTINE Print_deck
|
||||
! Print cards in deck
|
||||
DO i = dealt+1, 52
|
||||
WRITE (*, "(3A)") TRIM(deck(i)%value), " of ", TRIM(deck(i)%suit)
|
||||
END DO
|
||||
WRITE(*,*)
|
||||
END SUBROUTINE Print_deck
|
||||
|
||||
END MODULE Cards
|
||||
11
Task/Playing-cards/Fortran/playing-cards-2.f
Normal file
11
Task/Playing-cards/Fortran/playing-cards-2.f
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
PROGRAM Playing_Cards
|
||||
|
||||
USE Cards
|
||||
|
||||
CALL Init_deck
|
||||
CALL Shuffle_deck
|
||||
CALL Deal_hand(5)
|
||||
CALL Print_hand
|
||||
CALL Print_deck
|
||||
|
||||
END PROGRAM
|
||||
59
Task/Playing-cards/FreeBASIC/playing-cards.basic
Normal file
59
Task/Playing-cards/FreeBASIC/playing-cards.basic
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
#define NSWAPS 100
|
||||
|
||||
type card
|
||||
suit : 2 as ubyte
|
||||
face : 4 as ubyte
|
||||
'the remaining 2 bits are unused
|
||||
end type
|
||||
|
||||
dim shared as string*8 Suits(0 to 3) = {"Spades", "Clubs", "Hearts", "Diamonds"}
|
||||
dim shared as string*8 Faces(0 to 12) = {"Ace", "Two", "Three", "Four", "Five",_
|
||||
"Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"}
|
||||
|
||||
sub newdeck( deck() as card )
|
||||
'produces an unshuffled deck of 52 cards
|
||||
redim preserve deck(0 to 51) as card
|
||||
for s as ubyte = 0 to 3
|
||||
for f as ubyte = 0 to 12
|
||||
deck(13*s + f).suit = s
|
||||
deck(13*s + f).face = f
|
||||
next f
|
||||
next s
|
||||
end sub
|
||||
|
||||
function deal( deck() as card ) as card
|
||||
'deals one card from the top of the deck, returning that
|
||||
'card and removing it from the deck
|
||||
dim as card dealt = deck(ubound(deck))
|
||||
redim preserve deck(0 to ubound(deck) - 1)
|
||||
return dealt
|
||||
end function
|
||||
|
||||
function card_name( c as card ) as string
|
||||
'returns the name of a single given card
|
||||
return Faces(c.face) + " of " + Suits(c.suit)
|
||||
end function
|
||||
|
||||
sub print_deck( deck() as card )
|
||||
'displays the contents of the deck,
|
||||
'with the top card (next to be dealt) first
|
||||
for i as byte = ubound(deck) to 0 step -1
|
||||
print card_name( deck(i) )
|
||||
next i
|
||||
end sub
|
||||
|
||||
sub shuffle_deck( deck() as card )
|
||||
dim as integer n = ubound(deck)+1
|
||||
for i as integer = 1 to NSWAPS
|
||||
swap deck( int(rnd*n) ), deck( int(rnd*n) )
|
||||
next i
|
||||
end sub
|
||||
|
||||
redim as card deck(0 to 0) 'allocate a new deck
|
||||
newdeck(deck()) 'set up the new deck
|
||||
print "Dealing a card: ", card_name( deal( deck() ) )
|
||||
for j as integer = 1 to 41 'deal another 41 cards and discard them
|
||||
deal(deck())
|
||||
next j
|
||||
shuffle_deck(deck()) 'shuffle the remaining cards
|
||||
print_deck(deck()) 'display the last ten cards
|
||||
174
Task/Playing-cards/Go/playing-cards-1.go
Normal file
174
Task/Playing-cards/Go/playing-cards-1.go
Normal file
|
|
@ -0,0 +1,174 @@
|
|||
package cards
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
)
|
||||
|
||||
// A Suit represents one of the four standard suites.
|
||||
type Suit uint8
|
||||
|
||||
// The four standard suites.
|
||||
const (
|
||||
Spade Suit = 3
|
||||
Heart Suit = 2
|
||||
Diamond Suit = 1
|
||||
Club Suit = 0
|
||||
)
|
||||
|
||||
func (s Suit) String() string {
|
||||
const suites = "CDHS" // or "♣♢♡♠"
|
||||
return suites[s : s+1]
|
||||
}
|
||||
|
||||
// Rank is the rank or pip value of a card from Ace==1 to King==13.
|
||||
type Rank uint8
|
||||
|
||||
// The ranks from Ace to King.
|
||||
const (
|
||||
Ace Rank = 1
|
||||
Two Rank = 2
|
||||
Three Rank = 3
|
||||
Four Rank = 4
|
||||
Five Rank = 5
|
||||
Six Rank = 6
|
||||
Seven Rank = 7
|
||||
Eight Rank = 8
|
||||
Nine Rank = 9
|
||||
Ten Rank = 10
|
||||
Jack Rank = 11
|
||||
Queen Rank = 12
|
||||
King Rank = 13
|
||||
)
|
||||
|
||||
func (r Rank) String() string {
|
||||
const ranks = "A23456789TJQK"
|
||||
return ranks[r-1 : r]
|
||||
}
|
||||
|
||||
// A Card represets a specific playing card.
|
||||
// It's an encoded representation of Rank and Suit
|
||||
// with a valid range of [0,51].
|
||||
type Card uint8
|
||||
|
||||
// NewCard returns the Card representation for the specified rank and suit.
|
||||
func NewCard(r Rank, s Suit) Card {
|
||||
return Card(13*uint8(s) + uint8(r-1))
|
||||
}
|
||||
|
||||
// RankSuit returns the rank and suit of the card.
|
||||
func (c Card) RankSuit() (Rank, Suit) {
|
||||
return Rank(c%13 + 1), Suit(c / 13)
|
||||
}
|
||||
|
||||
// Rank returns the rank of the card.
|
||||
func (c Card) Rank() Rank {
|
||||
return Rank(c%13 + 1)
|
||||
}
|
||||
|
||||
// Suit returns the suit of the card.
|
||||
func (c Card) Suit() Suit {
|
||||
return Suit(c / 13)
|
||||
}
|
||||
|
||||
func (c Card) String() string {
|
||||
return c.Rank().String() + c.Suit().String()
|
||||
}
|
||||
|
||||
// A Deck represents a set of zero or more cards in a specific order.
|
||||
type Deck []Card
|
||||
|
||||
// NewDeck returns a regular 52 deck of cards in A-K order.
|
||||
func NewDeck() Deck {
|
||||
d := make(Deck, 52)
|
||||
for i := range d {
|
||||
d[i] = Card(i)
|
||||
}
|
||||
return d
|
||||
}
|
||||
|
||||
// String returns a string representation of the cards in the deck with
|
||||
// a newline ('\n') separating the cards into groups of thirteen.
|
||||
func (d Deck) String() string {
|
||||
s := ""
|
||||
for i, c := range d {
|
||||
switch {
|
||||
case i == 0: // do nothing
|
||||
case i%13 == 0:
|
||||
s += "\n"
|
||||
default:
|
||||
s += " "
|
||||
}
|
||||
s += c.String()
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// Shuffle randomises the order of the cards in the deck.
|
||||
func (d Deck) Shuffle() {
|
||||
for i := range d {
|
||||
j := rand.Intn(i + 1)
|
||||
d[i], d[j] = d[j], d[i]
|
||||
}
|
||||
}
|
||||
|
||||
// Contains returns true if the specified card is withing the deck.
|
||||
func (d Deck) Contains(tc Card) bool {
|
||||
for _, c := range d {
|
||||
if c == tc {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// AddDeck adds the specified deck(s) to this one at the end/bottom.
|
||||
func (d *Deck) AddDeck(decks ...Deck) {
|
||||
for _, o := range decks {
|
||||
*d = append(*d, o...)
|
||||
}
|
||||
}
|
||||
|
||||
// AddCard adds the specified card to this deck at the end/bottom.
|
||||
func (d *Deck) AddCard(c Card) {
|
||||
*d = append(*d, c)
|
||||
}
|
||||
|
||||
// Draw removes the selected number of cards from the top of the deck,
|
||||
// returning them as a new deck.
|
||||
func (d *Deck) Draw(n int) Deck {
|
||||
old := *d
|
||||
*d = old[n:]
|
||||
return old[:n:n]
|
||||
}
|
||||
|
||||
// DrawCard draws a single card off the top of the deck,
|
||||
// removing it from the deck.
|
||||
// It returns false if there are no cards in the deck.
|
||||
func (d *Deck) DrawCard() (Card, bool) {
|
||||
if len(*d) == 0 {
|
||||
return 0, false
|
||||
}
|
||||
old := *d
|
||||
*d = old[1:]
|
||||
return old[0], true
|
||||
}
|
||||
|
||||
// Deal deals out cards from the deck one at a time to multiple players.
|
||||
// The initial hands (decks) of each player are provided as arguments and the
|
||||
// modified hands are returned. The initial hands can be empty or nil.
|
||||
// E.g. Deal(7, nil, nil, nil) deals out seven cards to three players
|
||||
// each starting with no cards.
|
||||
// If there are insufficient cards in the deck the hands are partially dealt and
|
||||
// the boolean return is set to false (true otherwise).
|
||||
func (d *Deck) Deal(cards int, hands ...Deck) ([]Deck, bool) {
|
||||
for i := 0; i < cards; i++ {
|
||||
for j := range hands {
|
||||
if len(*d) == 0 {
|
||||
return hands, false
|
||||
}
|
||||
hands[j] = append(hands[j], (*d)[0])
|
||||
*d = (*d)[1:]
|
||||
}
|
||||
}
|
||||
return hands, true
|
||||
}
|
||||
45
Task/Playing-cards/Go/playing-cards-2.go
Normal file
45
Task/Playing-cards/Go/playing-cards-2.go
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"whatever/path/was/used/cards"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// We do not call rand.Seed() in order for the results to be repeatable.
|
||||
//rand.Seed(time.Now().UnixNano())
|
||||
d := cards.NewDeck()
|
||||
fmt.Println("fresh deck")
|
||||
fmt.Println(d)
|
||||
|
||||
d.Shuffle()
|
||||
fmt.Println("\nshuffled")
|
||||
fmt.Println(d)
|
||||
|
||||
h := d.Draw(5)
|
||||
fmt.Println("\n5 cards drawn")
|
||||
fmt.Println(h)
|
||||
|
||||
fmt.Println("\nrank, suit values of cards in drawn:")
|
||||
fmt.Println("Card Rank Suit")
|
||||
for _, c := range h {
|
||||
fmt.Printf("%v : %v=%2[2]d %v=%2[3]d\n", c, c.Rank(), c.Suit())
|
||||
}
|
||||
|
||||
ans := h.Contains(cards.NewCard(cards.Queen, cards.Spade))
|
||||
fmt.Println("Drawn cards include the Queen of Spades?", ans)
|
||||
ans = h.Contains(cards.NewCard(cards.Jack, cards.Spade))
|
||||
fmt.Println("Drawn cards include the Jack of Spades?", ans)
|
||||
|
||||
p, _ := d.Deal(7, nil, nil)
|
||||
fmt.Println("\nDealing 7 cards to two players")
|
||||
fmt.Println("Player1:", p[0])
|
||||
fmt.Println("Player2:", p[1])
|
||||
|
||||
fmt.Println("\n", len(d), " cards left in deck")
|
||||
fmt.Println(d)
|
||||
|
||||
d.AddDeck(h, p[0], p[0])
|
||||
fmt.Println("\nReturning the cards to the deck")
|
||||
fmt.Println(d)
|
||||
}
|
||||
37
Task/Playing-cards/Groovy/playing-cards-1.groovy
Normal file
37
Task/Playing-cards/Groovy/playing-cards-1.groovy
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import groovy.transform.TupleConstructor
|
||||
|
||||
enum Pip {
|
||||
ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING
|
||||
}
|
||||
enum Suit {
|
||||
DIAMONDS, SPADES, HEARTS, CLUBS
|
||||
}
|
||||
|
||||
@TupleConstructor
|
||||
class Card {
|
||||
final Pip pip
|
||||
final Suit suit
|
||||
|
||||
String toString() { "$pip of $suit" }
|
||||
}
|
||||
|
||||
class Deck {
|
||||
private LinkedList cards = []
|
||||
|
||||
Deck() { reset() }
|
||||
|
||||
void reset() {
|
||||
cards = []
|
||||
Suit.values().each { suit ->
|
||||
Pip.values().each { pip ->
|
||||
cards << new Card(pip, suit)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Card deal() { cards.poll() }
|
||||
|
||||
void shuffle() { Collections.shuffle cards }
|
||||
|
||||
String toString() { cards.isEmpty() ? "Empty Deck" : "Deck $cards" }
|
||||
}
|
||||
3
Task/Playing-cards/Groovy/playing-cards-2.groovy
Normal file
3
Task/Playing-cards/Groovy/playing-cards-2.groovy
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
Deck deck = new Deck()
|
||||
deck.shuffle()
|
||||
(0..<5).each { println deck.deal() }
|
||||
27
Task/Playing-cards/Haskell/playing-cards.hs
Normal file
27
Task/Playing-cards/Haskell/playing-cards.hs
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import System.Random
|
||||
|
||||
data Pip = Two | Three | Four | Five | Six | Seven | Eight | Nine | Ten |
|
||||
Jack | Queen | King | Ace
|
||||
deriving (Ord, Enum, Bounded, Eq, Show)
|
||||
|
||||
data Suit = Diamonds | Spades | Hearts | Clubs
|
||||
deriving (Ord, Enum, Bounded, Eq, Show)
|
||||
|
||||
type Card = (Pip, Suit)
|
||||
|
||||
fullRange :: (Bounded a, Enum a) => [a]
|
||||
fullRange = [minBound..maxBound]
|
||||
|
||||
fullDeck :: [Card]
|
||||
fullDeck = [(pip, suit) | pip <- fullRange, suit <- fullRange]
|
||||
|
||||
insertAt :: Int -> a -> [a] -> [a]
|
||||
insertAt 0 x ys = x:ys
|
||||
insertAt n _ [] = error "insertAt: list too short"
|
||||
insertAt n x (y:ys) = y : insertAt (n-1) x ys
|
||||
|
||||
shuffle :: RandomGen g => g -> [a] -> [a]
|
||||
shuffle g xs = shuffle' g xs 0 [] where
|
||||
shuffle' g [] _ ys = ys
|
||||
shuffle' g (x:xs) n ys = shuffle' g' xs (n+1) (insertAt k x ys) where
|
||||
(k,g') = randomR (0,n) g
|
||||
29
Task/Playing-cards/Hy/playing-cards.hy
Normal file
29
Task/Playing-cards/Hy/playing-cards.hy
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
(import [random [shuffle]])
|
||||
(setv pips (.split "2 3 4 5 6 7 8 9 10 J Q K A"))
|
||||
(setv suits (.split "♥ ♦ ♣ ♠"))
|
||||
(setv cards_per_hand 5)
|
||||
|
||||
(defn make_deck [pips suits]
|
||||
(lfor
|
||||
x pips
|
||||
y suits
|
||||
(+ x y)))
|
||||
|
||||
(defn deal_hand [num_cards deck]
|
||||
(setv delt (cut deck None num_cards))
|
||||
(setv new_deck (lfor
|
||||
x deck
|
||||
:if (not (in x delt))
|
||||
x))
|
||||
[delt new_deck])
|
||||
|
||||
|
||||
(if (= __name__ "__main__")
|
||||
(do
|
||||
(setv deck (make_deck pips suits))
|
||||
(shuffle deck)
|
||||
(setv [first_hand deck] (deal_hand cards_per_hand deck))
|
||||
(setv [second_hand deck] (deal_hand cards_per_hand deck))
|
||||
(print "\nThe first hand delt was:" (.join " " (map str first_hand)))
|
||||
(print "\nThe second hand delt was:" (.join " " (map str second_hand)))
|
||||
(print "\nThe remaining cards in the deck are...\n" (.join " " (map str deck)))))
|
||||
53
Task/Playing-cards/Icon/playing-cards.icon
Normal file
53
Task/Playing-cards/Icon/playing-cards.icon
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
procedure main(arglist)
|
||||
|
||||
cards := 2 # cards per hand
|
||||
players := 5 # players to deal to
|
||||
|
||||
write("New deck : ", showcards(D := newcarddeck())) # create and show a new deck
|
||||
write("Shuffled : ", showcards(D := shufflecards(D))) # shuffle it
|
||||
|
||||
H := list(players)
|
||||
every H[1 to players] := [] # hands for each player
|
||||
|
||||
every ( c := 1 to cards ) & ( p := 1 to players ) do
|
||||
put(H[p], dealcard(D)) # deal #players hands of #cards
|
||||
|
||||
every write("Player #",p := 1 to players,"'s hand : ",showcards(H[p]))
|
||||
write("Remaining: ",showcards(D)) # show the rest of the deck
|
||||
end
|
||||
|
||||
record card(suit,pip) #: datatype for a card suit x pip
|
||||
|
||||
procedure newcarddeck() #: return a new standard deck
|
||||
local D
|
||||
|
||||
every put(D := [], card(suits(),pips()))
|
||||
return D
|
||||
end
|
||||
|
||||
procedure suits() #: generate suits
|
||||
suspend !["H","S","D","C"]
|
||||
end
|
||||
|
||||
procedure pips() #: generate pips
|
||||
suspend !["2","3","4","5","6","7","8","9","10","J","Q","K","A"]
|
||||
end
|
||||
|
||||
procedure shufflecards(D) #: shuffle a list of cards
|
||||
every !D :=: ?D # see INL#9
|
||||
return D
|
||||
end
|
||||
|
||||
procedure dealcard(D) #: deal a card (from the top)
|
||||
return get(D)
|
||||
end
|
||||
|
||||
procedure showcards(D) #: return a string of all cards in the given list (deck/hand/etc.)
|
||||
local s
|
||||
every (s := "") ||:= card2string(!D) || " "
|
||||
return s
|
||||
end
|
||||
|
||||
procedure card2string(x) #: return a string version of a card
|
||||
return x.pip || x.suit
|
||||
end
|
||||
42
Task/Playing-cards/J/playing-cards-1.j
Normal file
42
Task/Playing-cards/J/playing-cards-1.j
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
NB. playingcards.ijs
|
||||
NB. Defines a Rosetta Code playing cards class
|
||||
NB. Multiple decks may be used, one for each instance of this class.
|
||||
|
||||
coclass 'rcpc' NB. Rosetta Code playing cards class
|
||||
|
||||
NB. Class objects
|
||||
Ranks=: _2 ]\ ' A 2 3 4 5 6 7 8 910 J Q K'
|
||||
Suits=: ucp '♦♣♥♠'
|
||||
DeckPrototype=: (] #: i.@:*/)Ranks ,&# Suits
|
||||
|
||||
NB. Class methods
|
||||
create=: monad define
|
||||
1: TheDeck=: DeckPrototype
|
||||
)
|
||||
|
||||
destroy=: codestroy
|
||||
|
||||
sayCards=: ({&Ranks@{. , {&Suits@{:)"1
|
||||
|
||||
shuffle=: monad define
|
||||
1: TheDeck=: ({~ ?~@#) TheDeck
|
||||
)
|
||||
|
||||
NB.*dealCards v Deals y cards [to x players]
|
||||
NB. x is: optional number of players, defaults to one
|
||||
NB. Used monadically, the player-axis is omitted from output.
|
||||
dealCards=: verb define
|
||||
{. 1 dealCards y
|
||||
:
|
||||
'Too few cards in deck' assert (# TheDeck) >: ToBeDealt=. x*y
|
||||
CardsOffTop=. ToBeDealt {. TheDeck
|
||||
TheDeck =: ToBeDealt }. TheDeck
|
||||
(x,y)$ CardsOffTop
|
||||
)
|
||||
|
||||
NB.*pcc v "Print" current contents of the deck.
|
||||
pcc=: monad define
|
||||
sayCards TheDeck
|
||||
)
|
||||
|
||||
newDeck_z_=: conew&'rcpc'
|
||||
23
Task/Playing-cards/J/playing-cards-2.j
Normal file
23
Task/Playing-cards/J/playing-cards-2.j
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
load '~user/playingcards.ijs'
|
||||
coinsert 'rcpc' NB. insert rcpc class in the path of current locale
|
||||
pc=: newDeck ''
|
||||
$TheDeck__pc
|
||||
52 2
|
||||
shuffle__pc ''
|
||||
1
|
||||
sayCards 2 dealCards__pc 5 NB. deal two hands of five cards
|
||||
3♦
|
||||
4♦
|
||||
K♠
|
||||
A♦
|
||||
K♦
|
||||
|
||||
5♠
|
||||
10♣
|
||||
Q♥
|
||||
2♣
|
||||
9♣
|
||||
$TheDeck__pc NB. deck size has been reduced by the ten cards dealt
|
||||
42 2
|
||||
destroy__pc ''
|
||||
1
|
||||
2
Task/Playing-cards/Java/playing-cards-1.java
Normal file
2
Task/Playing-cards/Java/playing-cards-1.java
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
public enum Pip { Two, Three, Four, Five, Six, Seven,
|
||||
Eight, Nine, Ten, Jack, Queen, King, Ace }
|
||||
1
Task/Playing-cards/Java/playing-cards-2.java
Normal file
1
Task/Playing-cards/Java/playing-cards-2.java
Normal file
|
|
@ -0,0 +1 @@
|
|||
public enum Suit { Diamonds, Spades, Hearts, Clubs }
|
||||
13
Task/Playing-cards/Java/playing-cards-3.java
Normal file
13
Task/Playing-cards/Java/playing-cards-3.java
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
public class Card {
|
||||
private final Suit suit;
|
||||
private final Pip value;
|
||||
|
||||
public Card(Suit s, Pip v) {
|
||||
suit = s;
|
||||
value = v;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return value + " of " + suit;
|
||||
}
|
||||
}
|
||||
24
Task/Playing-cards/Java/playing-cards-4.java
Normal file
24
Task/Playing-cards/Java/playing-cards-4.java
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class Deck {
|
||||
private final LinkedList<Card> deck= new LinkedList<Card>();
|
||||
|
||||
public Deck() {
|
||||
for (Suit s : Suit.values())
|
||||
for (Pip v : Pip.values())
|
||||
deck.add(new Card(s, v));
|
||||
}
|
||||
|
||||
public Card deal() {
|
||||
return deck.poll();
|
||||
}
|
||||
|
||||
public void shuffle() {
|
||||
Collections.shuffle(deck); // I'm such a cheater
|
||||
}
|
||||
|
||||
public String toString(){
|
||||
return deck.toString();
|
||||
}
|
||||
}
|
||||
31
Task/Playing-cards/JavaScript/playing-cards.js
Normal file
31
Task/Playing-cards/JavaScript/playing-cards.js
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
function Card(pip, suit) {
|
||||
this.pip = pip;
|
||||
this.suit = suit;
|
||||
|
||||
this.toString = function () {
|
||||
return this.pip + ' ' + this.suit;
|
||||
};
|
||||
}
|
||||
|
||||
function Deck() {
|
||||
var pips = '2 3 4 5 6 7 8 9 10 Jack Queen King Ace'.split(' ');
|
||||
var suits = 'Clubs Hearts Spades Diamonds'.split(' ');
|
||||
this.deck = [];
|
||||
for (var i = 0; i < suits.length; i++)
|
||||
for (var j = 0; j < pips.length; j++)
|
||||
this.deck.push(new Card(pips[j], suits[i]));
|
||||
|
||||
this.toString = function () {
|
||||
return '[' + this.deck.join(', ') + ']';
|
||||
};
|
||||
|
||||
this.shuffle = function () {
|
||||
for (var i = 0; i < this.deck.length; i++)
|
||||
this.deck[i] = this.deck.splice(
|
||||
parseInt(this.deck.length * Math.random()), 1, this.deck[i])[0];
|
||||
};
|
||||
|
||||
this.deal = function () {
|
||||
return this.deck.shift();
|
||||
};
|
||||
}
|
||||
70
Task/Playing-cards/Jq/playing-cards.jq
Normal file
70
Task/Playing-cards/Jq/playing-cards.jq
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
# Uncomment for gojq:
|
||||
# def _nwise($n):
|
||||
# def nw: if length <= $n then . else .[0:$n] , (.[$n:] | nw) end;
|
||||
# nw;
|
||||
|
||||
# Output: a prn in range(0;$n) where $n is .
|
||||
def prn:
|
||||
if . == 1 then 0
|
||||
else . as $n
|
||||
| (($n-1)|tostring|length) as $w
|
||||
| [limit($w; inputs)] | join("") | tonumber
|
||||
| if . < $n then . else ($n | prn) end
|
||||
end;
|
||||
|
||||
def knuthShuffle:
|
||||
length as $n
|
||||
| if $n <= 1 then .
|
||||
else {i: $n, a: .}
|
||||
| until(.i == 0;
|
||||
.i += -1
|
||||
| (.i + 1 | prn) as $j
|
||||
| .a[.i] as $t
|
||||
| .a[.i] = .a[$j]
|
||||
| .a[$j] = $t)
|
||||
| .a
|
||||
end;
|
||||
|
||||
def Pip: ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];
|
||||
|
||||
def Suit: ["♦", "♣", "♥", "♠"];
|
||||
|
||||
def Card(pip; suit): pip + suit;
|
||||
|
||||
def Deck: Card(Pip[]; Suit[]);
|
||||
|
||||
# Deal one round of the cards in .deck to the players
|
||||
# represented by .hands,
|
||||
# but if there are not enough cards, do nothing except add .error to the input
|
||||
# Input {deck, hands}
|
||||
def deal:
|
||||
(.hands | length) as $nplayers
|
||||
| if $nplayers > (.deck|length) then .error = "not enough cards"
|
||||
else reduce range(0;$nplayers) as $i (.; .hands[$i] += [.deck[$i]])
|
||||
| .deck |= .[$nplayers:]
|
||||
|
||||
# Deal $n cards to $nplayers players if there are enough cards in the input deck
|
||||
# Input: a deck
|
||||
# Output: {deck, hands}
|
||||
def deal($n; $nplayers):
|
||||
if $n * $nplayers > length then "deal/2: not enough cards" | error
|
||||
else {deck: ., hands: [range(0; $nplayers)|[]]}
|
||||
| until( .hands[0]|length == $n; deal)
|
||||
end ;
|
||||
|
||||
# display an entire deck or else just the cards
|
||||
def display:
|
||||
if length == 52 then _nwise(13) | join(" ")
|
||||
else join(" ")
|
||||
end;
|
||||
|
||||
def task:
|
||||
Deck
|
||||
| "After creation, the deck consists of:", display, "",
|
||||
(knuthShuffle
|
||||
| "After shuffling:", display, "",
|
||||
( deal(5; 4)
|
||||
| "After dealing 5 cards each to 4 players, the hands are:",
|
||||
(.hands[] | display), "", "... leaving \(.deck|length) cards") ) ;
|
||||
|
||||
task
|
||||
25
Task/Playing-cards/Julia/playing-cards-1.julia
Normal file
25
Task/Playing-cards/Julia/playing-cards-1.julia
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
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
|
||||
14
Task/Playing-cards/Julia/playing-cards-2.julia
Normal file
14
Task/Playing-cards/Julia/playing-cards-2.julia
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
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
|
||||
25
Task/Playing-cards/Julia/playing-cards-3.julia
Normal file
25
Task/Playing-cards/Julia/playing-cards-3.julia
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
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
|
||||
20
Task/Playing-cards/Julia/playing-cards-4.julia
Normal file
20
Task/Playing-cards/Julia/playing-cards-4.julia
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
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))
|
||||
6
Task/Playing-cards/K/playing-cards-1.k
Normal file
6
Task/Playing-cards/K/playing-cards-1.k
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
v:"A23456789TJQK" / values
|
||||
s:"SHCD" / suites
|
||||
|
||||
/ create a new deck
|
||||
newdeck:{deck::,/s,'\:v}
|
||||
newdeck();
|
||||
4
Task/Playing-cards/K/playing-cards-2.k
Normal file
4
Task/Playing-cards/K/playing-cards-2.k
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
show:{`0:$,/-3$$deck}
|
||||
|
||||
show()
|
||||
SA S2 S3 S4 S5 S6 S7 S8 S9 ST SJ SQ SK HA H2 H3 H4 H5 H6 H7 H8 H9 HT HJ HQ HK CA C2 C3 C4 C5 C6 C7 C8 C9 CT CJ CQ CK DA D2 D3 D4 D5 D6 D7 D8 D9 DT DJ DQ DK
|
||||
4
Task/Playing-cards/K/playing-cards-3.k
Normal file
4
Task/Playing-cards/K/playing-cards-3.k
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
shuffle:{deck::(-#deck)?deck}
|
||||
|
||||
shuffle();show()
|
||||
S8 CA D5 D2 SJ D6 DJ H7 S4 S9 SQ SK S5 D8 C4 HT DA H3 S6 S2 DT HA C2 C5 D9 ST C7 DK S3 HQ D7 DQ C8 D3 SA CJ CQ CT H4 H2 CK H9 H5 C3 C6 H6 D4 HJ C9 S7 HK H8
|
||||
12
Task/Playing-cards/K/playing-cards-4.k
Normal file
12
Task/Playing-cards/K/playing-cards-4.k
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
deal1:{|((#deck)-x)_|deck}
|
||||
deal:{c:deal1[x];deck::(deck _dvl c);c}
|
||||
|
||||
deal[5]
|
||||
("S8"
|
||||
"CA"
|
||||
"D5"
|
||||
"D2"
|
||||
"SJ")
|
||||
|
||||
#deck / 5 cards are removed
|
||||
47
|
||||
16
Task/Playing-cards/K/playing-cards-5.k
Normal file
16
Task/Playing-cards/K/playing-cards-5.k
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
{deal@5}'!3
|
||||
(("D6"
|
||||
"DJ"
|
||||
"H7"
|
||||
"S4"
|
||||
"S9")
|
||||
("SQ"
|
||||
"SK"
|
||||
"S5"
|
||||
"D8"
|
||||
"C4")
|
||||
("HT"
|
||||
"DA"
|
||||
"H3"
|
||||
"S6"
|
||||
"S2"))
|
||||
4
Task/Playing-cards/K/playing-cards-6.k
Normal file
4
Task/Playing-cards/K/playing-cards-6.k
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
#deck
|
||||
32
|
||||
show()
|
||||
DT HA C2 C5 D9 ST C7 DK S3 HQ D7 DQ C8 D3 SA CJ CQ CT H4 H2 CK H9 H5 C3 C6 H6 D4 HJ C9 S7 HK H8
|
||||
34
Task/Playing-cards/Kotlin/playing-cards-1.kotlin
Normal file
34
Task/Playing-cards/Kotlin/playing-cards-1.kotlin
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
const val FACES = "23456789TJQKA"
|
||||
const val SUITS = "shdc"
|
||||
|
||||
fun createDeck(): List<String> {
|
||||
val cards = mutableListOf<String>()
|
||||
FACES.forEach { face -> SUITS.forEach { suit -> cards.add("$face$suit") } }
|
||||
return cards
|
||||
}
|
||||
|
||||
fun dealTopDeck(deck: List<String>, n: Int) = deck.take(n)
|
||||
|
||||
fun dealBottomDeck(deck: List<String>, n: Int) = deck.takeLast(n).reversed()
|
||||
|
||||
fun printDeck(deck: List<String>) {
|
||||
for (i in deck.indices) {
|
||||
print("${deck[i]} ")
|
||||
if ((i + 1) % 13 == 0 || i == deck.size - 1) println()
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
var deck = createDeck()
|
||||
println("After creation, deck consists of:")
|
||||
printDeck(deck)
|
||||
deck = deck.shuffled()
|
||||
println("\nAfter shuffling, deck consists of:")
|
||||
printDeck(deck)
|
||||
val dealtTop = dealTopDeck(deck, 10)
|
||||
println("\nThe 10 cards dealt from the top of the deck are:")
|
||||
printDeck(dealtTop)
|
||||
val dealtBottom = dealBottomDeck(deck, 10)
|
||||
println("\nThe 10 cards dealt from the bottom of the deck are:")
|
||||
printDeck(dealtBottom)
|
||||
}
|
||||
32
Task/Playing-cards/Kotlin/playing-cards-2.kotlin
Normal file
32
Task/Playing-cards/Kotlin/playing-cards-2.kotlin
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
class Deck : ArrayList<String> {
|
||||
constructor() { FACES.forEach { face -> SUITS.forEach { add("$face$it") } } }
|
||||
constructor(c: Collection<String>) { addAll(c) }
|
||||
|
||||
fun dealTop(n: Int) = Deck(take(n))
|
||||
fun dealBottom(n: Int) = Deck(takeLast(n).reversed())
|
||||
|
||||
fun print() {
|
||||
forEachIndexed { i, s ->
|
||||
print("$s ")
|
||||
if ((i + 1) % 13 == 0 || i == size - 1) println()
|
||||
}
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val FACES = "23456789TJQKA"
|
||||
const val SUITS = "shdc"
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val deck = Deck()
|
||||
println("After creation, deck consists of:")
|
||||
deck.print()
|
||||
deck.shuffle()
|
||||
println("\nAfter shuffling, deck consists of:")
|
||||
deck.print()
|
||||
println("\nThe 10 cards dealt from the top of the deck are:")
|
||||
deck.dealTop(10).print()
|
||||
println("\nThe 10 cards dealt from the bottom of the deck are:")
|
||||
deck.dealBottom(10).print()
|
||||
}
|
||||
92
Task/Playing-cards/Liberty-BASIC/playing-cards.basic
Normal file
92
Task/Playing-cards/Liberty-BASIC/playing-cards.basic
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
Dim deckCards(52)
|
||||
Dim holdCards(1, 1)
|
||||
Print "The Sorted Deck"
|
||||
Call sortDeck
|
||||
Call dealDeck
|
||||
Print: Print
|
||||
Print "The Shuffled Deck"
|
||||
Call shuffleDeck
|
||||
Call dealDeck
|
||||
Print: Print
|
||||
nPlayers = 4
|
||||
nCards = 5
|
||||
ct = 0
|
||||
Redim holdCards(nPlayers, nCards)
|
||||
Print "Dealing ";nCards;" cards to ";nPlayers;" players"
|
||||
For i = 1 to nPlayers
|
||||
Print "Player #";i,,
|
||||
Next i
|
||||
Print
|
||||
For i = 1 to nCards
|
||||
For j = 1 to nPlayers
|
||||
ct = ct + 1
|
||||
holdCards(j, i) = deckCards(ct)
|
||||
card = deckCards(ct)
|
||||
value = value(card)
|
||||
suit$ = suit$(card)
|
||||
pip$ = pip$(value)
|
||||
Print card;": ";pip$;" of ";suit$,
|
||||
Next j
|
||||
Print
|
||||
Next i
|
||||
Print: Print
|
||||
Print "The cards in memory / array"
|
||||
For i = 1 to nPlayers
|
||||
Print "Player #";i;" is holding"
|
||||
For j = 1 to nCards
|
||||
card = holdCards(i, j)
|
||||
value = value(card)
|
||||
suit$ = suit$(card)
|
||||
pip$ = pip$(value)
|
||||
Print card;": ";pip$;" of ";suit$
|
||||
Next j
|
||||
Print
|
||||
Next i
|
||||
|
||||
End
|
||||
|
||||
Sub dealDeck
|
||||
For i = 1 to 52
|
||||
card = deckCards(i)
|
||||
value = value(card)
|
||||
suit$ = suit$(card)
|
||||
pip$ = pip$(value)
|
||||
Print i, card, value, pip$;" of ";suit$
|
||||
Next i
|
||||
End Sub
|
||||
|
||||
Sub sortDeck
|
||||
For i = 1 to 52
|
||||
deckCards(i) = i
|
||||
Next i
|
||||
End Sub
|
||||
|
||||
Sub shuffleDeck
|
||||
For i = 52 to 1 Step -1
|
||||
x = Int(Rnd(1) * i) + 1
|
||||
temp = deckCards(x)
|
||||
deckCards(x) = deckCards(i)
|
||||
deckCards(i) = temp
|
||||
Next i
|
||||
End Sub
|
||||
|
||||
Function suit$(deckValue)
|
||||
cardSuit$ = "Spades Hearts Clubs Diamonds"
|
||||
suit = Int(deckValue / 13)
|
||||
If deckValue Mod 13 = 0 Then
|
||||
suit = suit - 1
|
||||
End If
|
||||
suit$ = Word$(cardSuit$, suit + 1)
|
||||
End Function
|
||||
|
||||
Function value(deckValue)
|
||||
value = deckValue Mod 13
|
||||
If value = 0 Then
|
||||
value = 13
|
||||
End If
|
||||
End Function
|
||||
|
||||
Function pip$(faceValue)
|
||||
pipLabel$ = "Ace Deuce Three Four Five Six Seven Eight Nine Ten Jack Queen King"
|
||||
pip$ = Word$(pipLabel$, faceValue)
|
||||
End Function
|
||||
33
Task/Playing-cards/Logo/playing-cards.logo
Normal file
33
Task/Playing-cards/Logo/playing-cards.logo
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
make "suits {Diamonds Hearts Clubs Spades}
|
||||
make "pips {Ace Two Three Four Five Six Seven Eight Nine Ten Jack Queen King}
|
||||
|
||||
to card :n
|
||||
output (sentence item 1 + modulo :n 13 :pips "of item 1 + int quotient :n 13 :suits)
|
||||
end
|
||||
|
||||
to new.deck
|
||||
make "deck listtoarray iseq 0 51
|
||||
make "top 1
|
||||
end
|
||||
|
||||
to swap :i :j :a
|
||||
localmake "t item :i :a
|
||||
setitem :i :a item :j :a
|
||||
setitem :j :a :t
|
||||
end
|
||||
to shuffle.deck
|
||||
for [i [count :deck] 2] [swap 1 + random :i :i :deck]
|
||||
end
|
||||
|
||||
to show.deck
|
||||
for [i :top [count :deck]] [show card item :i :deck]
|
||||
end
|
||||
|
||||
to deal.card
|
||||
show card item :top :deck
|
||||
make "top :top + 1
|
||||
end
|
||||
|
||||
new.deck
|
||||
shuffle.deck
|
||||
repeat 5 [deal.card]
|
||||
64
Task/Playing-cards/Lua/playing-cards-1.lua
Normal file
64
Task/Playing-cards/Lua/playing-cards-1.lua
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
suits = {"Clubs", "Diamonds", "Hearts", "Spades"}
|
||||
faces = {2,3,4,5,6,7,8,9,10,"Jack","Queen","King","Ace"}
|
||||
--a stack is a set of cards. a stack of length 1 acts as a card; the stack constructor only creates decks.
|
||||
|
||||
stack = setmetatable({
|
||||
--shuffles a stack
|
||||
__unm = function(z)
|
||||
local ret = {}
|
||||
for i = #z, 1, -1 do
|
||||
ret[#ret + 1] = table.remove(z,math.random(i))
|
||||
end
|
||||
return setmetatable(ret, stack)
|
||||
end,
|
||||
--puts two stacks together
|
||||
__add = function(z, z2)
|
||||
for i = 1, #z2 do
|
||||
z[#z+1] = table.remove(z2)
|
||||
end
|
||||
return z
|
||||
end,
|
||||
--removes n cards from a stack and returns a stack of those cards
|
||||
__sub = function(z, n)
|
||||
local ret = {}
|
||||
for i = 1, n do
|
||||
ret[i] = table.remove(z)
|
||||
end
|
||||
return setmetatable(ret, stack)
|
||||
end,
|
||||
--breaks a stack into n equally sized stacks and returns them all
|
||||
deal = function(z, n)
|
||||
local ret = {}
|
||||
for i = 1, #z/n do
|
||||
ret[i] = table.remove(z)
|
||||
end
|
||||
if n > 1 then return setmetatable(ret, stack), stack.deal(z,n-1)
|
||||
else return setmetatable(ret, stack)
|
||||
end
|
||||
end,
|
||||
--returns a and b as strings, concatenated together. Simple enough, right?
|
||||
__concat = function(a, b)
|
||||
if getmetatable(a) == stack then
|
||||
return stack.stackstring(a) .. b
|
||||
else
|
||||
return a .. stack.stackstring(b)
|
||||
end
|
||||
end,
|
||||
stackstring = function(st, ind)
|
||||
ind = ind or 1
|
||||
if not st[ind] then return "" end
|
||||
return st[ind] and (faces[math.ceil(st[ind]/4)] .. " of " .. suits[st[ind]%4+1] .. "\n" .. stack.stackstring(st, ind+1)) or ""
|
||||
end}, {
|
||||
--creates a deck
|
||||
__call = function(z)
|
||||
local ret = {}
|
||||
for i = 1, 52 do ret[i] = i end
|
||||
return -setmetatable(ret,z)
|
||||
end})
|
||||
|
||||
print(stack() .. "\n")
|
||||
a, b, c, d = stack.deal(stack(), 4)
|
||||
print(a .. "\n\n\n")
|
||||
print(b + c .. "\n\n\n")
|
||||
print(d - 4 .. "")
|
||||
print(-b .. "")
|
||||
58
Task/Playing-cards/Lua/playing-cards-2.lua
Normal file
58
Task/Playing-cards/Lua/playing-cards-2.lua
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
local tPlayers = {} -- cards of players
|
||||
local tBoard = {} -- cards in a board
|
||||
local nPlayers = 5 -- number of players
|
||||
|
||||
local tDeck = {
|
||||
'2d', '3d', '4d', '5d', '6d', '7d', '8d', '9d', 'Td', 'Jd', 'Qd', 'Kd', 'Ad', -- DIAMONDS
|
||||
'2s', '3s', '4s', '5s', '6s', '7s', '8s', '9s', 'Ts', 'Js', 'Qs', 'Ks', 'As', -- SPADES
|
||||
'2h', '3h', '4h', '5h', '6h', '7h', '8h', '9h', 'Th', 'Jh', 'Qh', 'Kh', 'Ah', -- HEARTS
|
||||
'2c', '3c', '4c', '5c', '6c', '7c', '8c', '9c', 'Tc', 'Jc', 'Qc', 'Kc', 'Ac'} -- CLUBS
|
||||
|
||||
local function shuffle() -- Fisher–Yates shuffle
|
||||
i = #tDeck
|
||||
while i > 1 do
|
||||
i = i - 1
|
||||
j = math.random(1, i)
|
||||
tDeck[j], tDeck[i] = tDeck[i], tDeck[j]
|
||||
end
|
||||
return tDeck
|
||||
end
|
||||
|
||||
local function cardTransfer(to, amount, from)
|
||||
for f = 1, amount do
|
||||
table.insert(to, #to+1, from[#from])
|
||||
from[#from] = nil
|
||||
end
|
||||
end
|
||||
|
||||
----||EXAMPLE OF USE||----
|
||||
print('FRESH DECK \n', table.concat(tDeck, ' '), '\n')
|
||||
|
||||
shuffle()
|
||||
|
||||
print('SHUFFLED DECK \n', table.concat(tDeck, ' '), '\n')
|
||||
|
||||
for a = 1, nPlayers do
|
||||
tPlayers[a] = {}
|
||||
cardTransfer(tPlayers[a], 2, tDeck)
|
||||
end
|
||||
|
||||
cardTransfer(tBoard, 5, tDeck)
|
||||
|
||||
print('BOARD\n', table.concat(tBoard, ' '), '\n')
|
||||
|
||||
for b = 1, nPlayers do
|
||||
print('PLAYER #'..b..': ', table.concat(tPlayers[b], ' '))
|
||||
end
|
||||
|
||||
print('\nREMAINING\n', table.concat(tDeck, ' '), '\n')
|
||||
|
||||
for c = 1, #tPlayers do
|
||||
for d = 1, #tPlayers[c] do
|
||||
cardTransfer(tDeck, d, tPlayers[c])
|
||||
end
|
||||
end
|
||||
|
||||
cardTransfer(tDeck, 5, tBoard)
|
||||
|
||||
print('ALL CARDS IN THE DECK\n', table.concat(tDeck, ' '), '\n')
|
||||
20
Task/Playing-cards/Lua/playing-cards-3.lua
Normal file
20
Task/Playing-cards/Lua/playing-cards-3.lua
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
FRESH DECK
|
||||
2d 3d 4d 5d 6d 7d 8d 9d Td Jd Qd Kd Ad 2s 3s 4s 5s 6s 7s 8s 9s Ts Js Qs Ks As 2h 3h 4h 5h 6h 7h 8h 9h Th Jh Qh Kh Ah 2c 3c 4c 5c 6c 7c 8c 9c Tc Jc Qc Kc Ac
|
||||
|
||||
SHUFFLED DECK
|
||||
7c 3d 8h 7h 7s 9c 8c Ks 8s 2s 5s 8d 2h 3h Jc 6h Td Ts Jh Tc 6s Kd 7d 4h 4d 5d Qd 5h 5c Kh 9d 2d Ah 6d 3c Js 9h Qh 4c 3s As Kc Qs Ad Th 4s Jd Ac Qc 2c 9s 6c
|
||||
|
||||
BOARD
|
||||
Kc As 3s 4c Qh
|
||||
|
||||
PLAYER #1: 6c 9s
|
||||
PLAYER #2: 2c Qc
|
||||
PLAYER #3: Ac Jd
|
||||
PLAYER #4: 4s Th
|
||||
PLAYER #5: Ad Qs
|
||||
|
||||
REMAINING
|
||||
7c 3d 8h 7h 7s 9c 8c Ks 8s 2s 5s 8d 2h 3h Jc 6h Td Ts Jh Tc 6s Kd 7d 4h 4d 5d Qd 5h 5c Kh 9d 2d Ah 6d 3c Js 9h
|
||||
|
||||
ALL CARDS IN THE DECK
|
||||
7c 3d 8h 7h 7s 9c 8c Ks 8s 2s 5s 8d 2h 3h Jc 6h Td Ts Jh Tc 6s Kd 7d 4h 4d 5d Qd 5h 5c Kh 9d 2d Ah 6d 3c Js 9h 9s 6c Qc 2c Jd Ac Th 4s Qs Ad Qh 4c 3s As Kc
|
||||
108
Task/Playing-cards/M2000-Interpreter/playing-cards.m2000
Normal file
108
Task/Playing-cards/M2000-Interpreter/playing-cards.m2000
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
Module PlayCards {
|
||||
Font "Arial" ' Ensure characters exist for Suits
|
||||
Cls 15,0
|
||||
Pen 0
|
||||
Inventory Suits = "♠":=0, "♥":=4, "♦":=4, "♣":=0 'suit -> color
|
||||
Inventory Cards = "two":=2, "three":=3, "four":=4, "five":=5
|
||||
Append Cards, "six":=6, "seven":=7, "eight":=8, "nine":=9
|
||||
Append Cards, "ten":=10, "jack":=10, "queen":=10, "king":=10, "ace":=1
|
||||
DealerMoney=0
|
||||
PrintCardOnly = Lambda Suits, Cards (k, nl=True)-> {
|
||||
For k {
|
||||
Pen Suits(.suit!) {
|
||||
If nl then {
|
||||
Print Part @(10), Eval$(Suits, .suit)+Eval$(Cards, .card)
|
||||
Print
|
||||
} Else Print Eval$(Suits, .suit)+Eval$(Cards, .card),
|
||||
}
|
||||
}
|
||||
}
|
||||
' Using a stack object
|
||||
StackPack = Stack
|
||||
Module AppendArray (N, A) {
|
||||
Stack N {Data !A}
|
||||
}
|
||||
Class OneCard {
|
||||
suit=-1, Card
|
||||
Class:
|
||||
Module OneCard {
|
||||
\\ ? for optional reading
|
||||
read ? .suit, .card
|
||||
}
|
||||
}
|
||||
Decks=1
|
||||
Dim Pack(Len(Cards)*Len(Suits)*Decks)
|
||||
k=0
|
||||
\\ fill cards to Pack()
|
||||
For times=1 To Decks {
|
||||
N=each(Suits)
|
||||
While N {
|
||||
M=each(Cards)
|
||||
While M {
|
||||
Pack(k)=OneCard(N^, M^)
|
||||
k++
|
||||
}
|
||||
}
|
||||
}
|
||||
DisplayAll() ' in order
|
||||
Suffle()
|
||||
DisplayAll() ' at random positions
|
||||
Print
|
||||
Card=OneCard()
|
||||
Print "Cards in Deck:";Len(StackPack)
|
||||
For i=1 to 60 {
|
||||
NextCard()
|
||||
Print "Get Card:";
|
||||
Call PrintCardOnly(Card)
|
||||
Print
|
||||
Print "Cards in Deck:";Len(StackPack)
|
||||
DisplayDeck()
|
||||
Print
|
||||
}
|
||||
|
||||
Sub Suffle()
|
||||
Print
|
||||
Local N=Len(Pack())-1, N2, i, j, total=N*4+4, cur=1
|
||||
For j=1 To 4 {
|
||||
For i=0 To N {
|
||||
If cur Mod 4=3 Then Print Over format$("Suffle {0:0}%",cur/total*100)
|
||||
N2=random(0, N)
|
||||
While N2=i {N2=random(0, N)}
|
||||
Swap Pack(i), Pack(N2)
|
||||
cur++
|
||||
}
|
||||
}
|
||||
AppendArray StackPack, Pack()
|
||||
Print
|
||||
End Sub
|
||||
Sub DisplayDeck()
|
||||
local m=each(StackPack)
|
||||
While m {
|
||||
Call PrintCardOnly(StackItem(m), False)
|
||||
}
|
||||
End Sub
|
||||
Sub DisplayAll()
|
||||
For k=0 To Len(Pack())-1 {
|
||||
PrintCard(k)
|
||||
}
|
||||
End Sub
|
||||
Sub PrintCard(k)
|
||||
For Pack(k) {
|
||||
Pen Suits(.suit!) {
|
||||
Print Eval$(Suits, .suit)+Eval$(Cards, .card),
|
||||
}
|
||||
}
|
||||
End Sub
|
||||
Sub NextCard()
|
||||
If Len(StackPack)=0 Then {
|
||||
Suffle()
|
||||
Stack StackPack {
|
||||
Drop Random(0, 51)
|
||||
}
|
||||
}
|
||||
Stack StackPack {
|
||||
Read Card
|
||||
}
|
||||
End Sub
|
||||
}
|
||||
PlayCards
|
||||
40
Task/Playing-cards/M4/playing-cards.m4
Normal file
40
Task/Playing-cards/M4/playing-cards.m4
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
define(`randSeed',141592653)dnl
|
||||
define(`setRand',
|
||||
`define(`randSeed',ifelse(eval($1<10000),1,`eval(20000-$1)',`$1'))')dnl
|
||||
define(`rand_t',`eval(randSeed^(randSeed>>13))')dnl
|
||||
define(`random',
|
||||
`define(`randSeed',eval((rand_t^(rand_t<<18))&0x7fffffff))randSeed')dnl
|
||||
define(`for',
|
||||
`ifelse($#,0,``$0'',
|
||||
`ifelse(eval($2<=$3),1,
|
||||
`pushdef(`$1',$2)$4`'popdef(`$1')$0(`$1',incr($2),$3,`$4')')')')dnl
|
||||
define(`foreach', `pushdef(`$1')_foreach($@)popdef(`$1')')dnl
|
||||
define(`_arg1', `$1')dnl
|
||||
define(`_foreach', `ifelse(`$2', `()', `',
|
||||
`define(`$1', _arg1$2)$3`'$0(`$1', (shift$2), `$3')')')dnl
|
||||
define(`new',`define(`$1[size]',0)')dnl
|
||||
define(`append',
|
||||
`define(`$1[size]',incr(defn(`$1[size]')))`'define($1[defn($1[size])],$2)')
|
||||
define(`deck',
|
||||
`new($1)foreach(`x',(Ace,2,3,4,5,6,7,8,9,10,Jack,Queen,King),
|
||||
`foreach(`y',(Clubs,Diamonds,Hearts,Spades),
|
||||
`append(`$1',`x of y')')')')dnl
|
||||
define(`show',
|
||||
`for(`x',1,defn($1[size]),`defn($1[x])ifelse(x,defn($1[size]),`',`, ')')')dnl
|
||||
define(`swap',`define($1[$2],defn($1[$4]))define($1[$4],$3)')dnl
|
||||
define(`shuffle',
|
||||
`for(`x',1,defn($1[size]),
|
||||
`swap($1,x,defn($1[x]),eval(1+random%defn($1[size])))')')dnl
|
||||
define(`deal',
|
||||
`ifelse($#,0,``$0'',
|
||||
`ifelse(defn($1[size]),0,
|
||||
`NULL',
|
||||
defn($1[defn($1[size])])define($1[size],decr(defn($1[size]))))')')dnl
|
||||
dnl
|
||||
deck(`b')
|
||||
show(`b')
|
||||
shuffling shuffle(`b')
|
||||
show(`b')
|
||||
deal deal(`b')
|
||||
deal deal(`b')
|
||||
show(`b')
|
||||
3
Task/Playing-cards/Mathematica/playing-cards.math
Normal file
3
Task/Playing-cards/Mathematica/playing-cards.math
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
MakeDeck[] := Tuples[{{"Ace ", 2, 3 , 4 , 5, 6 , 7 , 8 , 9 , 10, "Jack" , "Queen", "King"}, {♦ , ♣, ♥ , ♠}}]
|
||||
DeckShuffle[deck_] := RandomSample[deck, Length@deck]
|
||||
DealFromDeck[] := (Print@First@deck; deck = deck[[2 ;; All]];)
|
||||
47
Task/Playing-cards/MiniScript/playing-cards.mini
Normal file
47
Task/Playing-cards/MiniScript/playing-cards.mini
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
suits = ["Spades", "Clubs", "Hearts", "Diamonds"]
|
||||
pips = ["Ace","Two","Three","Four","Five","Six","Seven",
|
||||
"Eight","Nine","Ten","Jack","Queen","King"]
|
||||
|
||||
Card = {}
|
||||
Card.str = function()
|
||||
return self.pip + " of " + self.suit + " (value: " + self.value + ")"
|
||||
end function
|
||||
|
||||
//Build Deck
|
||||
deck = []
|
||||
for s in suits.indexes
|
||||
for p in pips.indexes
|
||||
card = new Card
|
||||
card.suit = suits[s]
|
||||
card.pip = pips[p]
|
||||
card.value = s * 100 + p
|
||||
deck.push card
|
||||
end for
|
||||
end for
|
||||
|
||||
draw = function(count=7)
|
||||
hand = []
|
||||
for i in range(1, count)
|
||||
hand.push deck.pop
|
||||
end for
|
||||
return hand
|
||||
end function
|
||||
|
||||
display = function(stack)
|
||||
for card in stack
|
||||
print card.str
|
||||
end for
|
||||
end function
|
||||
|
||||
print "Deck created. Cards in Deck: " + deck.len
|
||||
|
||||
deck.shuffle
|
||||
print "Deck Shuffled"
|
||||
|
||||
hand = draw
|
||||
print "First hand: "
|
||||
display hand
|
||||
|
||||
print
|
||||
print deck.len + " cards left in deck:"
|
||||
display deck
|
||||
91
Task/Playing-cards/Nim/playing-cards.nim
Normal file
91
Task/Playing-cards/Nim/playing-cards.nim
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
import random, strutils
|
||||
|
||||
type
|
||||
Suit* = enum ♥, ♦, ♣, ♠
|
||||
|
||||
Rank* {.pure.} = enum
|
||||
Ace = (1, "A")
|
||||
Two = (2, "2")
|
||||
Three = (3, "3")
|
||||
Four = (4, "4")
|
||||
Five = (5, "5")
|
||||
Six = (6, "6")
|
||||
Seven = (7, "7")
|
||||
Eight = (8, "8")
|
||||
Nine = (9, "9")
|
||||
Ten = (10, "10")
|
||||
Jack = (11, "J")
|
||||
Queen = (12, "Q")
|
||||
King = (13, "K")
|
||||
|
||||
Card* = tuple[rank: Rank; suit: Suit]
|
||||
|
||||
# Sequences of cards: synonyms for seq[Card].
|
||||
Deck* = seq[Card]
|
||||
Hand* = seq[Card]
|
||||
|
||||
var initRandom = false # True if "randomize" has been called.
|
||||
|
||||
|
||||
proc `$`*(c: Card): string =
|
||||
## Return the representation of a card.
|
||||
$c.rank & $c.suit
|
||||
|
||||
|
||||
proc initDeck*(): Deck =
|
||||
## Initialize a deck.
|
||||
for suit in Suit:
|
||||
for rank in Rank:
|
||||
result.add (rank, suit)
|
||||
|
||||
|
||||
proc shuffle*(cards: var seq[Card]) =
|
||||
## Shuffle a list of cards (deck or hand).
|
||||
if not initRandom:
|
||||
randomize()
|
||||
initRandom = true
|
||||
random.shuffle(cards)
|
||||
|
||||
|
||||
func `$`*(cards: seq[Card]): string =
|
||||
## Return the representation of a list o cards.
|
||||
cards.join(" ")
|
||||
|
||||
|
||||
func dealOne*(cards: var seq[Card]): Card =
|
||||
## Deal one card from a list of cards.
|
||||
assert cards.len > 0
|
||||
cards.pop()
|
||||
|
||||
|
||||
## Draw one card from a list of cards.
|
||||
let draw* = dealOne
|
||||
|
||||
|
||||
func deal*(deck: var Deck; nPlayers: Positive; nCards: Positive): seq[Hand] =
|
||||
## Deal "nCards" cards to "nPlayers" players.
|
||||
assert deck.len >= nCards * nPlayers
|
||||
result.setLen(nPlayers)
|
||||
for n in 1..nCards:
|
||||
for p in 0..<nPlayers:
|
||||
result[p].add deck.pop()
|
||||
|
||||
|
||||
when isMainModule:
|
||||
import strformat
|
||||
|
||||
var deck = initDeck()
|
||||
deck.shuffle()
|
||||
echo "Initial deck after shuffling: "
|
||||
for i in 0..2: echo deck[(i * 13)..(i * 13 + 12)], " ..."
|
||||
echo deck[^13..^1]
|
||||
|
||||
echo "\nDeal eight cards for five players from the deck:"
|
||||
var hands = deck.deal(5, 8)
|
||||
for i, hand in hands: echo &"Player {i + 1} hand: ", hand
|
||||
echo "Remaining cards: ", deck
|
||||
|
||||
echo "\nAfter player 1 drew a card from the deck: "
|
||||
hands[0].add deck.draw()
|
||||
echo "Player 1 hand: ", hands[0]
|
||||
echo "Remaining cards: ", deck
|
||||
15
Task/Playing-cards/OCaml/playing-cards.ocaml
Normal file
15
Task/Playing-cards/OCaml/playing-cards.ocaml
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
type pip = Two | Three | Four | Five | Six | Seven | Eight | Nine | Ten |
|
||||
Jack | Queen | King | Ace
|
||||
let pips = [Two; Three; Four; Five; Six; Seven; Eight; Nine; Ten;
|
||||
Jack; Queen; King; Ace]
|
||||
type suit = Diamonds | Spades | Hearts | Clubs
|
||||
let suits = [Diamonds; Spades; Hearts; Clubs]
|
||||
type card = pip * suit
|
||||
let full_deck = Array.of_list (List.concat (List.map (fun pip -> List.map (fun suit -> (pip, suit)) suits) pips))
|
||||
let shuffle deck =
|
||||
for i = Array.length deck - 1 downto 1 do
|
||||
let j = Random.int (i+1) in
|
||||
let temp = deck.(i) in
|
||||
deck.(i) <- deck.(j);
|
||||
deck.(j) <- temp
|
||||
done
|
||||
20
Task/Playing-cards/PARI-GP/playing-cards.parigp
Normal file
20
Task/Playing-cards/PARI-GP/playing-cards.parigp
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
name(n)=Str(["A",2,3,4,5,6,7,8,9,10,"J","Q","K"][(n+3)>>2],["h","d","s","c"][n%4+1]);
|
||||
newdeck()={
|
||||
v=vector(52,i,i);
|
||||
};
|
||||
deal()={
|
||||
my(n=name(v[1]));
|
||||
v=vecextract(v,2^#v-2);
|
||||
n
|
||||
};
|
||||
printdeck(){
|
||||
apply(name,v)
|
||||
};
|
||||
shuffle()={
|
||||
forstep(n=#v,2,-1,
|
||||
my(i=random(n)+1,t=v[i]);
|
||||
v[i]=v[n];
|
||||
v[n]=t
|
||||
);
|
||||
v
|
||||
};
|
||||
276
Task/Playing-cards/PHP/playing-cards-1.php
Normal file
276
Task/Playing-cards/PHP/playing-cards-1.php
Normal file
|
|
@ -0,0 +1,276 @@
|
|||
class Card
|
||||
{
|
||||
// if unable to save as UTF-8, use other, non-UTF-8, symbols here
|
||||
protected static $suits = array( '♠', '♥', '♦', '♣' );
|
||||
|
||||
protected static $pips = array( '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A' );
|
||||
|
||||
protected $suit;
|
||||
|
||||
protected $suitOrder;
|
||||
|
||||
protected $pip;
|
||||
|
||||
protected $pipOrder;
|
||||
|
||||
protected $order;
|
||||
|
||||
public function __construct( $suit, $pip )
|
||||
{
|
||||
if( !in_array( $suit, self::$suits ) )
|
||||
{
|
||||
throw new InvalidArgumentException( 'Invalid suit' );
|
||||
}
|
||||
if( !in_array( $pip, self::$pips ) )
|
||||
{
|
||||
throw new InvalidArgumentException( 'Invalid pip' );
|
||||
}
|
||||
|
||||
$this->suit = $suit;
|
||||
$this->pip = $pip;
|
||||
}
|
||||
|
||||
public function getSuit()
|
||||
{
|
||||
return $this->suit;
|
||||
}
|
||||
|
||||
public function getPip()
|
||||
{
|
||||
return $this->pip;
|
||||
}
|
||||
|
||||
public function getSuitOrder()
|
||||
{
|
||||
// lazy evaluate suit order
|
||||
if( !isset( $this->suitOrder ) )
|
||||
{
|
||||
// cache suit order
|
||||
$this->suitOrder = array_search( $this->suit, self::$suits );
|
||||
}
|
||||
|
||||
return $this->suitOrder;
|
||||
}
|
||||
|
||||
public function getPipOrder()
|
||||
{
|
||||
// lazy evaluate pip order
|
||||
if( !isset( $this->pipOrder ) )
|
||||
{
|
||||
// cache pip order
|
||||
$this->pipOrder = array_search( $this->pip, self::$pips );
|
||||
}
|
||||
|
||||
return $this->pipOrder;
|
||||
}
|
||||
|
||||
public function getOrder()
|
||||
{
|
||||
// lazy evaluate order
|
||||
if( !isset( $this->order ) )
|
||||
{
|
||||
$suitOrder = $this->getSuitOrder();
|
||||
$pipOrder = $this->getPipOrder();
|
||||
// cache order
|
||||
$this->order = $pipOrder * count( self::$suits ) + $suitOrder;
|
||||
}
|
||||
|
||||
return $this->order;
|
||||
}
|
||||
|
||||
public function compareSuit( Card $other )
|
||||
{
|
||||
return $this->getSuitOrder() - $other->getSuitOrder();
|
||||
}
|
||||
|
||||
public function comparePip( Card $other )
|
||||
{
|
||||
return $this->getPipOrder() - $other->getPipOrder();
|
||||
}
|
||||
|
||||
public function compare( Card $other )
|
||||
{
|
||||
return $this->getOrder() - $other->getOrder();
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return $this->suit . $this->pip;
|
||||
}
|
||||
|
||||
public static function getSuits()
|
||||
{
|
||||
return self::$suits;
|
||||
}
|
||||
|
||||
public static function getPips()
|
||||
{
|
||||
return self::$pips;
|
||||
}
|
||||
}
|
||||
|
||||
class CardCollection
|
||||
implements Countable, Iterator
|
||||
{
|
||||
protected $cards = array();
|
||||
|
||||
protected function __construct( array $cards = array() )
|
||||
{
|
||||
foreach( $cards as $card )
|
||||
{
|
||||
$this->addCard( $card );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Countable::count() implementation
|
||||
*/
|
||||
public function count()
|
||||
{
|
||||
return count( $this->cards );
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterator::key() implementation
|
||||
*/
|
||||
public function key()
|
||||
{
|
||||
return key( $this->cards );
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterator::valid() implementation
|
||||
*/
|
||||
public function valid()
|
||||
{
|
||||
return null !== $this->key();
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterator::next() implementation
|
||||
*/
|
||||
public function next()
|
||||
{
|
||||
next( $this->cards );
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterator::current() implementation
|
||||
*/
|
||||
public function current()
|
||||
{
|
||||
return current( $this->cards );
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterator::rewind() implementation
|
||||
*/
|
||||
public function rewind()
|
||||
{
|
||||
reset( $this->cards );
|
||||
}
|
||||
|
||||
public function sort( $comparer = null )
|
||||
{
|
||||
$comparer = $comparer ?: function( $a, $b ) {
|
||||
return $a->compare( $b );
|
||||
};
|
||||
|
||||
if( !is_callable( $comparer ) )
|
||||
{
|
||||
throw new InvalidArgumentException( 'Invalid comparer; comparer should be callable' );
|
||||
}
|
||||
|
||||
usort( $this->cards, $comparer );
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function toString()
|
||||
{
|
||||
return implode( ' ', $this->cards );
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return $this->toString();
|
||||
}
|
||||
|
||||
protected function addCard( Card $card )
|
||||
{
|
||||
if( in_array( $card, $this->cards ) )
|
||||
{
|
||||
throw new DomainException( 'Card is already present in this collection' );
|
||||
}
|
||||
|
||||
$this->cards[] = $card;
|
||||
}
|
||||
}
|
||||
|
||||
class Deck
|
||||
extends CardCollection
|
||||
{
|
||||
public function __construct( $shuffled = false )
|
||||
{
|
||||
foreach( Card::getSuits() as $suit )
|
||||
{
|
||||
foreach( Card::getPips() as $pip )
|
||||
{
|
||||
$this->addCard( new Card( $suit, $pip ) );
|
||||
}
|
||||
}
|
||||
|
||||
if( $shuffled )
|
||||
{
|
||||
$this->shuffle();
|
||||
}
|
||||
}
|
||||
|
||||
public function deal( $amount = 1, CardCollection $cardCollection = null )
|
||||
{
|
||||
if( !is_int( $amount ) || $amount < 1 )
|
||||
{
|
||||
throw new InvalidArgumentException( 'Invalid amount; amount should be an integer, larger than 0' );
|
||||
}
|
||||
|
||||
if( $amount > count( $this->cards ) )
|
||||
{
|
||||
throw new RangeException( 'Invalid amount; requested amount is larger than the amount of available cards' );
|
||||
}
|
||||
|
||||
$cards = array_splice( $this->cards, 0, $amount );
|
||||
|
||||
$cardCollection = $cardCollection ?: new CardCollection;
|
||||
|
||||
foreach( $cards as $card )
|
||||
{
|
||||
$cardCollection->addCard( $card );
|
||||
}
|
||||
|
||||
return $cardCollection;
|
||||
}
|
||||
|
||||
public function shuffle()
|
||||
{
|
||||
shuffle( $this->cards );
|
||||
}
|
||||
}
|
||||
|
||||
class Hand
|
||||
extends CardCollection
|
||||
{
|
||||
// override CardCollection __constructor
|
||||
// to allow public instantiation
|
||||
// but disallow instantiation with cards
|
||||
public function __construct() {}
|
||||
|
||||
public function play( $position )
|
||||
{
|
||||
if( !isset( $this->cards[ $position ] ) )
|
||||
{
|
||||
throw new OutOfBoundsException( 'Invalid position; position is not present in this hand' );
|
||||
}
|
||||
|
||||
$result = array_splice( $this->cards, $position, 1 );
|
||||
return $result[ 0 ];
|
||||
}
|
||||
}
|
||||
57
Task/Playing-cards/PHP/playing-cards-2.php
Normal file
57
Task/Playing-cards/PHP/playing-cards-2.php
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
// if viewing in a browser, lets output a text/plain header with utf-8 charset
|
||||
header( 'Content-Type: text/plain; charset=utf-8' );
|
||||
|
||||
// create a new Deck
|
||||
$deck = new Deck();
|
||||
|
||||
// show the cards in the new deck
|
||||
echo count( $deck ) . ' cards in the new deck: ' . PHP_EOL . $deck . PHP_EOL;
|
||||
|
||||
// sort the deck, default sort
|
||||
$deck->sort();
|
||||
|
||||
// show the cards in the sorted deck
|
||||
echo PHP_EOL . count( $deck ) . ' cards in the new deck, default sort: ' . PHP_EOL . $deck . PHP_EOL;
|
||||
|
||||
// sort the deck, custom sort
|
||||
$deck->sort( function( $a, $b ) {
|
||||
return $a->compareSuit( $b ) ?: $a->comparePip( $b );
|
||||
} );
|
||||
|
||||
// show the cards in the sorted deck
|
||||
echo PHP_EOL . count( $deck ) . ' cards in the new deck, custom sort: ' . PHP_EOL . $deck . PHP_EOL;
|
||||
|
||||
// shuffle the deck
|
||||
$deck->shuffle();
|
||||
|
||||
// show the cards in the shuffled deck
|
||||
echo PHP_EOL . count( $deck ) . ' cards in the new deck, shuffled: ' . PHP_EOL . $deck . PHP_EOL . PHP_EOL;
|
||||
|
||||
// intialize four player hands
|
||||
$players = array(
|
||||
new Hand,
|
||||
new Hand,
|
||||
new Hand,
|
||||
new Hand
|
||||
);
|
||||
|
||||
// three deal rounds, with amounts: 2, 2, 3
|
||||
foreach( array( 2, 2, 3 ) as $amount )
|
||||
{
|
||||
// deal this rounds amount to player
|
||||
foreach( $players as $hand )
|
||||
{
|
||||
$deck->deal( $amount, $hand );
|
||||
}
|
||||
}
|
||||
|
||||
foreach( $players as $p => $hand )
|
||||
{
|
||||
// sort player cards, default sort
|
||||
$hand->sort();
|
||||
// show player cards
|
||||
echo 'Player ' . ( $p + 1 ) . ' got dealt the following ' . count( $hand ) . ' cards (sorted): ' . $hand . PHP_EOL;
|
||||
}
|
||||
|
||||
// show the remaining cards in the deck
|
||||
echo PHP_EOL . count( $deck ) . ' cards remaining in the deck: ' . PHP_EOL . $deck . PHP_EOL;
|
||||
43
Task/Playing-cards/Perl/playing-cards-1.pl
Normal file
43
Task/Playing-cards/Perl/playing-cards-1.pl
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
package Playing_Card_Deck;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
@Playing_Card_Deck::suits = qw
|
||||
[Diamonds Clubs Hearts Spades];
|
||||
@Playing_Card_Deck::pips = qw
|
||||
[Two Three Four Five Six Seven Eight Nine Ten
|
||||
Jack King Queen Ace];
|
||||
# I choose to fully qualify these names rather than declare them
|
||||
# with "our" to keep them from escaping into the scope of other
|
||||
# packages in the same file. Another possible solution is to use
|
||||
# "our" or "my", but to enclose this entire package in a bare block.
|
||||
|
||||
sub new
|
||||
# Creates a new deck-object. The cards are initially neatly ordered.
|
||||
{my $invocant = shift;
|
||||
my $class = ref($invocant) || $invocant;
|
||||
my @cards;
|
||||
foreach my $suit (@Playing_Card_Deck::suits)
|
||||
{foreach my $pip (@Playing_Card_Deck::pips)
|
||||
{push(@cards, {suit => $suit, pip => $pip});}}
|
||||
return bless([@cards], $class);}
|
||||
|
||||
sub deal
|
||||
# Removes the top card of the given deck and returns it as a hash
|
||||
# with the keys "suit" and "pip".
|
||||
{return %{ shift( @{shift()} ) };}
|
||||
|
||||
sub shuffle
|
||||
# Randomly permutes the cards in the deck. It uses the algorithm
|
||||
# described at:
|
||||
# http://en.wikipedia.org/wiki/Fisher-Yates_shuffle#The_modern_algorithm
|
||||
{our @deck; local *deck = shift;
|
||||
# @deck is now an alias of the invocant's referent.
|
||||
for (my $n = $#deck ; $n ; --$n)
|
||||
{my $k = int rand($n + 1);
|
||||
@deck[$k, $n] = @deck[$n, $k] if $k != $n;}}
|
||||
|
||||
sub print_cards
|
||||
# Prints out a description of every card in the deck, in order.
|
||||
{print "$_->{pip} of $_->{suit}\n" foreach @{shift()};}
|
||||
5
Task/Playing-cards/Perl/playing-cards-2.pl
Normal file
5
Task/Playing-cards/Perl/playing-cards-2.pl
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
my $deck = Playing_Card_Deck->new;
|
||||
$deck->shuffle;
|
||||
my %card = $deck->deal;
|
||||
print uc("$card{pip} OF $card{suit}\n");
|
||||
$deck->print_cards;
|
||||
79
Task/Playing-cards/Phix/playing-cards.phix
Normal file
79
Task/Playing-cards/Phix/playing-cards.phix
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
-->
|
||||
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Playing_cards.exw</span>
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">deal</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">deck</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">nhands</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">ncards</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">hands</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">({},</span><span style="color: #000000;">nhands</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">ncards</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">h</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">nhands</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">hands</span><span style="color: #0000FF;">[</span><span style="color: #000000;">h</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">deck</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #000000;">deck</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">deck</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..$]</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">deck</span><span style="color: #0000FF;">,</span><span style="color: #000000;">hands</span><span style="color: #0000FF;">}</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #000080;font-style:italic;">--console:</span>
|
||||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">show_cards</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]-</span><span style="color: #000000;">1</span>
|
||||
<span style="color: #004080;">string</span> <span style="color: #000000;">sep</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">13</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">or</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)?</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">:</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"23456789TJQKA"</span><span style="color: #0000FF;">[</span><span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">13</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]&</span><span style="color: #008000;">"SHDC"</span><span style="color: #0000FF;">[</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">/</span><span style="color: #000000;">13</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]&</span><span style="color: #000000;">sep</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
|
||||
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">deck</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">hands</span>
|
||||
|
||||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">console_show</span><span style="color: #0000FF;">()</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">hands</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"hand%d:\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #000000;">show_cards</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">hands</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]))</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"remaining cards(%d):\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">deck</span><span style="color: #0000FF;">)})</span>
|
||||
<span style="color: #000000;">show_cards</span><span style="color: #0000FF;">(</span><span style="color: #000000;">deck</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
|
||||
|
||||
<span style="color: #000080;font-style:italic;">--GUI:</span>
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">cards_to_utf8</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">utf32</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">pip</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">13</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">utf32</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">0x1F0A1</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">pip</span><span style="color: #0000FF;">+(</span><span style="color: #000000;">pip</span><span style="color: #0000FF;">></span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">((</span><span style="color: #000000;">c</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">13</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">#10</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">12</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">and</span> <span style="color: #000000;">i</span><span style="color: #0000FF;"><</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">utf32</span> <span style="color: #0000FF;">&=</span> <span style="color: #008000;">'\n'</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #7060A8;">utf32_to_utf8</span><span style="color: #0000FF;">(</span><span style="color: #000000;">utf32</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
|
||||
|
||||
<span style="color: #008080;">constant</span> <span style="color: #000000;">FONT</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">`FONT="Arial, %d"`</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">92</span><span style="color: #0000FF;">})</span>
|
||||
|
||||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">gui_show</span><span style="color: #0000FF;">()</span>
|
||||
<span style="color: #7060A8;">IupOpen</span><span style="color: #0000FF;">()</span>
|
||||
<span style="color: #7060A8;">IupSetGlobal</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"UTF8MODE"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"YES"</span><span style="color: #0000FF;">)</span>
|
||||
|
||||
<span style="color: #004080;">Ihandles</span> <span style="color: #000000;">lh</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">hands</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupLabel</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"hand%d:"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">}))</span>
|
||||
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">h</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupLabel</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cards_to_utf8</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">hands</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])),</span><span style="color: #000000;">FONT</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">lh</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">&</span><span style="color: #000000;">h</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #000000;">lh</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">IupLabel</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"remaining cards:"</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">lh</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">IupLabel</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cards_to_utf8</span><span style="color: #0000FF;">(</span><span style="color: #000000;">deck</span><span style="color: #0000FF;">),</span><span style="color: #000000;">FONT</span><span style="color: #0000FF;">)</span>
|
||||
|
||||
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">dlg</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupDialog</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">IupVbox</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lh</span><span style="color: #0000FF;">))</span>
|
||||
<span style="color: #7060A8;">IupShow</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #7060A8;">IupMainLoop</span><span style="color: #0000FF;">()</span>
|
||||
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
|
||||
|
||||
<span style="color: #008080;">constant</span> <span style="color: #000000;">DECKSIZE</span><span style="color: #0000FF;">=</span><span style="color: #000000;">52</span>
|
||||
<span style="color: #000000;">deck</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">shuffle</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">DECKSIZE</span><span style="color: #0000FF;">))</span>
|
||||
<span style="color: #000000;">show_cards</span><span style="color: #0000FF;">(</span><span style="color: #000000;">deck</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #0000FF;">{</span><span style="color: #000000;">deck</span><span style="color: #0000FF;">,</span><span style="color: #000000;">hands</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">deal</span><span style="color: #0000FF;">(</span><span style="color: #000000;">deck</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">console_show</span><span style="color: #0000FF;">()</span>
|
||||
<span style="color: #000000;">gui_show</span><span style="color: #0000FF;">()</span>
|
||||
<!--
|
||||
92
Task/Playing-cards/Picat/playing-cards.picat
Normal file
92
Task/Playing-cards/Picat/playing-cards.picat
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
go =>
|
||||
% Create and print the deck
|
||||
deck(Deck),
|
||||
print_deck(Deck),
|
||||
nl,
|
||||
|
||||
% Shuffle the deck
|
||||
print_deck(shuffle(Deck)),
|
||||
nl,
|
||||
|
||||
% Deal 3 cards
|
||||
Deck := deal(Deck,Card1),
|
||||
Deck := deal(Deck,Card2),
|
||||
Deck := deal(Deck,Card3),
|
||||
|
||||
println(deal1=Card1),
|
||||
println(deal2=Card2),
|
||||
println(deal3=Card3),
|
||||
% The remaining deck
|
||||
print_deck(Deck),
|
||||
nl,
|
||||
|
||||
% Deal 5 cards
|
||||
Deck := deal(Deck,Card4),
|
||||
Deck := deal(Deck,Card5),
|
||||
Deck := deal(Deck,Card6),
|
||||
Deck := deal(Deck,Card7),
|
||||
Deck := deal(Deck,Card8),
|
||||
|
||||
println(cards4_to_8=[Card4,Card5,Card6,Card7,Card8]),
|
||||
nl,
|
||||
|
||||
% Deal 5 cards
|
||||
Deck := deal_n(Deck,5,FiveCards),
|
||||
println(fiveCards=FiveCards),
|
||||
print_deck(Deck),
|
||||
nl,
|
||||
|
||||
% And deal some more cards
|
||||
% This chaining works since deal/1 returns the remaining deck
|
||||
Deck := Deck.deal(Card9).deal(Card10).deal(Card11).deal(Card12),
|
||||
println("4_more_cards"=[Card9,Card10,Card11,Card12]),
|
||||
print_deck(Deck),
|
||||
|
||||
nl.
|
||||
|
||||
% suits(Suits) => Suits = ["♠","♥","♦","♣"].
|
||||
suits(Suits) => Suits = ["C","H","S","D"].
|
||||
values(Values) => Values = ["A","2","3","4","5","6","7","8","9","T","J","Q","K"].
|
||||
|
||||
% Create a (sorted) deck.
|
||||
deck(Deck) =>
|
||||
suits(Suits),
|
||||
values(Values),
|
||||
Deck =[S++V :V in Values, S in Suits].sort().
|
||||
|
||||
% Shuffle a deck
|
||||
shuffle(Deck) = Deck2 =>
|
||||
Deck2 = Deck,
|
||||
Len = Deck2.length,
|
||||
foreach(I in 1..Len)
|
||||
R2 = random(1,Len),
|
||||
Deck2 := swap(Deck2,I,R2)
|
||||
end.
|
||||
|
||||
% Swap position I <=> J in list L
|
||||
swap(L,I,J) = L2, list(L) =>
|
||||
L2 = L,
|
||||
T = L2[I],
|
||||
L2[I] := L2[J],
|
||||
L2[J] := T.
|
||||
|
||||
|
||||
% The first card is returned as the out parameter Card.
|
||||
% The deck is returned as the function value.
|
||||
deal(Deck, Card) = Deck.tail() => Card = Deck.first().
|
||||
|
||||
% Deal N cards
|
||||
deal_n(Deck, N, Cards) = [Deck[I] : I in Len+1..Deck.length] =>
|
||||
Len = min(N,Deck.length),
|
||||
Cards = [Deck[I] : I in 1..Len].
|
||||
|
||||
% Print deck
|
||||
print_deck(Deck) =>
|
||||
println("Deck:"),
|
||||
foreach({Card,I} in zip(Deck,1..Deck.len))
|
||||
printf("%w ", Card),
|
||||
if I mod 10 == 0 then
|
||||
nl
|
||||
end
|
||||
end,
|
||||
nl.
|
||||
13
Task/Playing-cards/PicoLisp/playing-cards.l
Normal file
13
Task/Playing-cards/PicoLisp/playing-cards.l
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
(de *Suits
|
||||
Club Diamond Heart Spade )
|
||||
|
||||
(de *Pips
|
||||
Ace 2 3 4 5 6 7 8 9 10 Jack Queen King )
|
||||
|
||||
(de mkDeck ()
|
||||
(mapcan
|
||||
'((Pip) (mapcar cons *Suits (circ Pip)))
|
||||
*Pips ) )
|
||||
|
||||
(de shuffle (Lst)
|
||||
(by '(NIL (rand)) sort Lst) )
|
||||
480
Task/Playing-cards/PowerShell/playing-cards.psh
Normal file
480
Task/Playing-cards/PowerShell/playing-cards.psh
Normal file
|
|
@ -0,0 +1,480 @@
|
|||
<#
|
||||
.Synopsis
|
||||
Creates a new "Deck" which is a hashtable converted to a dictionary
|
||||
This is only used for creating the deck, to view/list the deck contents use Get-Deck.
|
||||
.DESCRIPTION
|
||||
Casts the string value that the user inputs to the name of the variable that holds the "deck"
|
||||
Creates a global variable, allowing you to use the name you choose in other functions and allows you to create
|
||||
multiple decks under different names.
|
||||
.EXAMPLE
|
||||
PS C:\WINDOWS\system32> New-Deck
|
||||
cmdlet New-Deck at command pipeline position 1
|
||||
Supply values for the following parameters:
|
||||
YourDeckName: Deck1
|
||||
|
||||
PS C:\WINDOWS\system32>
|
||||
|
||||
.EXAMPLE
|
||||
PS C:\WINDOWS\system32> New-Deck -YourDeckName Deck2
|
||||
|
||||
PS C:\WINDOWS\system32>
|
||||
|
||||
.EXAMPLE
|
||||
PS C:\WINDOWS\system32> New-Deck -YourDeckName Deck2
|
||||
|
||||
PS C:\WINDOWS\system32> New-Deck -YourDeckName Deck3
|
||||
|
||||
PS C:\WINDOWS\system32>
|
||||
#>
|
||||
function New-Deck
|
||||
{
|
||||
[CmdletBinding()]
|
||||
[OutputType([int])]
|
||||
Param
|
||||
(
|
||||
# Name your Deck, this will be the name of the variable that holds your deck
|
||||
[Parameter(Mandatory=$true,
|
||||
ValueFromPipelineByPropertyName=$False,
|
||||
Position=0)]$YourDeckName
|
||||
)
|
||||
|
||||
Begin
|
||||
{
|
||||
$Suit = @(' of Hearts', ' of Spades', ' of Diamonds', ' of Clubs')
|
||||
$Pip = @('Ace', 'King', 'Queen', 'Jack', '10', '9', '8', '7', '6', '5', '4', '3', '2')
|
||||
|
||||
#Creates the hash table that will hold the suit and pip variables
|
||||
$Deck = @{}
|
||||
|
||||
#creates counters for the loop below to make 52 total cards with 13 cards per suit
|
||||
[int]$SuitCounter = 0
|
||||
[int]$PipCounter = 0
|
||||
[int]$CardValue = 0
|
||||
|
||||
}
|
||||
Process
|
||||
{
|
||||
#Creates the initial deck
|
||||
do
|
||||
{
|
||||
#card2add is the rows in the hashtable
|
||||
$Card2Add = ($Pip[$PipCounter]+$Suit[$SuitCounter])
|
||||
|
||||
#Addes the row to the hashtable
|
||||
$Deck.Add($CardValue, $Card2Add)
|
||||
|
||||
#Used to create the Keys
|
||||
$CardValue++
|
||||
|
||||
#Counts the amount of cards per suit
|
||||
$PipCounter ++
|
||||
if ($PipCounter -eq 13)
|
||||
{
|
||||
#once reached the suit is changed
|
||||
$SuitCounter++
|
||||
#and the per-suit counter is reset
|
||||
$PipCounter = 0
|
||||
}
|
||||
else
|
||||
{
|
||||
continue
|
||||
}
|
||||
}
|
||||
#52 cards in a deck
|
||||
until ($Deck.count -eq 52)
|
||||
|
||||
}
|
||||
End
|
||||
{
|
||||
#sets the name of a variable that is unknown
|
||||
#Then converts the hash table to a dictionary and pipes it to the Get-Random cmdlet with the arguments to randomize the contents
|
||||
Set-Variable -Name "$YourDeckName" -Value ($Deck.GetEnumerator() | Get-Random -Count ([int]::MaxValue)) -Scope Global
|
||||
}
|
||||
}
|
||||
|
||||
<#
|
||||
.Synopsis
|
||||
Lists the cards in your selected deck
|
||||
.DESCRIPTION
|
||||
Contains a Try-Catch-Finally block in case the deck requested has not been created
|
||||
.EXAMPLE
|
||||
PS C:\WINDOWS\system32> Get-Deck -YourDeckName deck1
|
||||
|
||||
8 of Clubs
|
||||
5 of Hearts
|
||||
--Shortened--
|
||||
King of Clubs
|
||||
Jack of Diamonds
|
||||
|
||||
PS C:\WINDOWS\system32>
|
||||
|
||||
|
||||
.EXAMPLE
|
||||
PS C:\WINDOWS\system32> Get-Deck -YourDeckName deck2
|
||||
|
||||
deck2 does not exist...
|
||||
Creating Deck deck2...
|
||||
|
||||
Ace of Spades
|
||||
10 of Hearts
|
||||
--Shortened--
|
||||
5 of Clubs
|
||||
4 of Clubs
|
||||
|
||||
PS C:\WINDOWS\system32>
|
||||
|
||||
.EXAMPLE
|
||||
PS C:\WINDOWS\system32> deck deck2
|
||||
|
||||
Ace of Spades
|
||||
10 of Hearts
|
||||
--Shortened--
|
||||
4 of Spades
|
||||
6 of Spades
|
||||
Queen of Spades
|
||||
|
||||
|
||||
PS C:\WINDOWS\system32>
|
||||
#>
|
||||
function Get-Deck
|
||||
{
|
||||
[CmdletBinding()]
|
||||
[Alias('Deck')]
|
||||
[OutputType([int])]
|
||||
Param
|
||||
(
|
||||
#Brings the Vairiable in from Get-Deck
|
||||
[Parameter(Mandatory=$true,
|
||||
ValueFromPipelineByPropertyName=$true,
|
||||
Position=0)]$YourDeckName
|
||||
)
|
||||
|
||||
Begin
|
||||
{
|
||||
|
||||
}
|
||||
Process
|
||||
{
|
||||
# Will return a terminal error if the deck has not been created
|
||||
try
|
||||
{
|
||||
$temp = Get-Variable -name "$YourDeckName" -ValueOnly -ErrorAction stop
|
||||
}
|
||||
catch
|
||||
{
|
||||
Write-Host
|
||||
Write-Host "$YourDeckName does not exist..."
|
||||
Write-Host "Creating Deck $YourDeckName..."
|
||||
New-Deck -YourDeckName $YourDeckName
|
||||
$temp = Get-Variable -name "$YourDeckName" -ValueOnly
|
||||
}
|
||||
|
||||
finally
|
||||
{
|
||||
$temp | select Value | ft -HideTableHeaders
|
||||
}
|
||||
|
||||
}
|
||||
End
|
||||
{
|
||||
Write-Verbose "End of show-deck function"
|
||||
}
|
||||
}
|
||||
|
||||
<#
|
||||
.Synopsis
|
||||
Shuffles a deck of your selection with Get-Random
|
||||
.DESCRIPTION
|
||||
This function can be used to Shuffle any deck that has been created.
|
||||
This can be used on a deck that has less than 52 cards
|
||||
Contains a Try-Catch-Finally block in case the deck requested has not been created
|
||||
Does NOT output the value of the deck being shuffled (You wouldn't look at the cards you shuffled, would you?)
|
||||
|
||||
.EXAMPLE
|
||||
PS C:\WINDOWS\system32> Shuffle-Deck -YourDeckName Deck1
|
||||
Your Deck was shuffled
|
||||
|
||||
PS C:\WINDOWS\system32>
|
||||
|
||||
.EXAMPLE
|
||||
PS C:\WINDOWS\system32> Shuffle NotMadeYet
|
||||
|
||||
NotMadeYet does not exist...
|
||||
Creating and shuffling NotMadeYet...
|
||||
Your Deck was shuffled
|
||||
|
||||
PS C:\WINDOWS\system32>
|
||||
#>
|
||||
function Shuffle-Deck
|
||||
{
|
||||
[CmdletBinding()]
|
||||
[Alias('Shuffle')]
|
||||
[OutputType([int])]
|
||||
Param
|
||||
(
|
||||
#The Deck you want to shuffle
|
||||
[Parameter(Mandatory=$true,
|
||||
ValueFromPipelineByPropertyName=$true,
|
||||
Position=0)]$YourDeckName)
|
||||
|
||||
Begin
|
||||
{
|
||||
Write-Verbose 'Shuffles your deck with Get-Random'
|
||||
}
|
||||
Process
|
||||
{
|
||||
# Will return a missing variable error if the deck has net been created
|
||||
try
|
||||
{
|
||||
#These two commands could be on one line using the pipeline, but look cleaner on two
|
||||
$temp1 = Get-Variable -name "$YourDeckName" -ValueOnly -ErrorAction stop
|
||||
$temp1 = $temp1 | Get-Random -Count ([int]::MaxValue)
|
||||
}
|
||||
catch
|
||||
{
|
||||
Write-Host
|
||||
Write-Host "$YourDeckName does not exist..."
|
||||
Write-Host "Creating and shuffling $YourDeckName..."
|
||||
New-Deck -YourDeckName $YourDeckName
|
||||
$temp1 = Get-Variable -name "$YourDeckName" -ValueOnly
|
||||
$temp1 = $temp1 | Get-Random -Count ([int]::MaxValue)
|
||||
|
||||
}
|
||||
|
||||
finally
|
||||
{
|
||||
#Gets the actual value of variable $YourDeckName from the New-Deck function and uses that string value
|
||||
#to set the variables name
|
||||
Set-Variable -Name "$YourDeckName" -value ($temp1) -Scope Global
|
||||
}
|
||||
}
|
||||
End
|
||||
{
|
||||
Write-Host "Your Deck was shuffled"
|
||||
}
|
||||
}
|
||||
|
||||
<#
|
||||
.Synopsis
|
||||
Creates a new "Hand" which is a hashtable converted to a dictionary
|
||||
This is only used for creating the hand, to view/list the deck contents use Get-Hand.
|
||||
.DESCRIPTION
|
||||
Casts the string value that the user inputs to the name of the variable that holds the "hand"
|
||||
Creates a global variable, allowing you to use the name you choose in other functions and allows you to create
|
||||
multiple hands under different names.
|
||||
.EXAMPLE
|
||||
PS C:\WINDOWS\system32> New-Hand -YourHandName JohnDoe
|
||||
|
||||
PS C:\WINDOWS\system32>
|
||||
.EXAMPLE
|
||||
PS C:\WINDOWS\system32> New-Hand JaneDoe
|
||||
|
||||
PS C:\WINDOWS\system32>
|
||||
>
|
||||
#>
|
||||
function New-Hand
|
||||
{
|
||||
[CmdletBinding()]
|
||||
[OutputType([int])]
|
||||
Param
|
||||
(
|
||||
# Name your Deck, this will be the name of the variable that holds your deck
|
||||
[Parameter(Mandatory=$true,
|
||||
ValueFromPipelineByPropertyName=$False,
|
||||
Position=0)]$YourHandName
|
||||
)
|
||||
|
||||
Begin
|
||||
{
|
||||
$Hand = @{}
|
||||
}
|
||||
Process
|
||||
{
|
||||
}
|
||||
|
||||
End
|
||||
{
|
||||
Set-Variable -Name "$YourHandName" -Value ($Hand.GetEnumerator()) -Scope Global
|
||||
}
|
||||
}
|
||||
|
||||
<#
|
||||
.Synopsis
|
||||
Lists the cards in the selected Hand
|
||||
.DESCRIPTION
|
||||
Contains a Try-Catch-Finally block in case the hand requested has not been created
|
||||
.EXAMPLE
|
||||
#create a new hand
|
||||
PS C:\WINDOWS\system32> New-Hand -YourHandName Hand1
|
||||
|
||||
PS C:\WINDOWS\system32> Get-Hand -YourHandName Hand1
|
||||
Hand1's hand contains cards, they are...
|
||||
|
||||
PS C:\WINDOWS\system32>
|
||||
#This hand is empty
|
||||
.EXAMPLE
|
||||
PS C:\WINDOWS\system32> Get-Hand -YourHandName Hand2
|
||||
|
||||
Hand2 does not exist...
|
||||
Creating Hand Hand2...
|
||||
Hand2's hand contains cards, they are...
|
||||
|
||||
PS C:\WINDOWS\system32>
|
||||
|
||||
.EXAMPLE
|
||||
PS C:\WINDOWS\system32> hand hand3
|
||||
hand3's hand contains 4 cards, they are...
|
||||
|
||||
5 of Spades
|
||||
4 of Spades
|
||||
6 of Spades
|
||||
Queen of Diamonds
|
||||
#>
|
||||
function Get-Hand
|
||||
{
|
||||
[CmdletBinding()]
|
||||
[Alias('Hand')]
|
||||
[OutputType([int])]
|
||||
Param
|
||||
(
|
||||
#Brings the Vairiable in from Get-Deck
|
||||
[Parameter(Mandatory=$true,
|
||||
ValueFromPipelineByPropertyName=$true,
|
||||
Position=0)]$YourHandName
|
||||
)
|
||||
Begin
|
||||
{
|
||||
}
|
||||
Process
|
||||
{
|
||||
# Will return a missing variable error if the deck has net been created
|
||||
try
|
||||
{
|
||||
$temp = Get-Variable -name "$YourHandName" -ValueOnly -ErrorAction stop
|
||||
}
|
||||
catch
|
||||
{
|
||||
Write-Host
|
||||
Write-Host "$YourHandName does not exist..."
|
||||
Write-Host "Creating Hand $YourHandName..."
|
||||
New-Hand -YourHandName $YourHandName
|
||||
$temp = Get-Variable -name "$YourHandName" -ValueOnly
|
||||
}
|
||||
|
||||
finally
|
||||
{
|
||||
$count = $temp.count
|
||||
Write-Host "$YourHandName's hand contains $count cards, they are..."
|
||||
$temp | select Value | ft -HideTableHeaders
|
||||
}
|
||||
|
||||
}
|
||||
End
|
||||
{
|
||||
Write-Verbose "End of show-deck function"
|
||||
}
|
||||
}
|
||||
|
||||
<#
|
||||
.Synopsis
|
||||
Draws/returns cards
|
||||
.DESCRIPTION
|
||||
Draws/returns cards from your chosen deck , to your chosen hand
|
||||
Can be used without creating a deck or hand first
|
||||
.EXAMPLE
|
||||
PS C:\WINDOWS\system32> DrawFrom-Deck -YourDeckName Deck1 -YourHandName test1 -HowManyCardsToDraw 10
|
||||
|
||||
|
||||
|
||||
PS C:\WINDOWS\system32>
|
||||
.EXAMPLE
|
||||
DrawFrom-Deck -YourDeckName Deck2 -YourHandName test2 -HowManyCardsToDraw 10
|
||||
Deck2 does not exist...
|
||||
Creating Deck Deck2...
|
||||
|
||||
test2 does not exist...
|
||||
Creating Hand test2...
|
||||
test2's hand contains cards, they are...
|
||||
|
||||
|
||||
|
||||
.EXAMPLE
|
||||
PS C:\WINDOWS\system32> draw -YourDeckName deck1 -YourHandName test1 -HowManyCardsToDraw 5
|
||||
|
||||
|
||||
|
||||
#>
|
||||
function Draw-Deck
|
||||
{
|
||||
[CmdletBinding()]
|
||||
[Alias('Draw')]
|
||||
[OutputType([int])]
|
||||
Param
|
||||
(
|
||||
# The Deck in which you want to draw cards out of
|
||||
[Parameter(Mandatory=$true,
|
||||
ValueFromPipelineByPropertyName=$true,
|
||||
Position=0)]$YourDeckName,
|
||||
|
||||
#The hand in which you want to draw cards to
|
||||
[Parameter(Mandatory=$true,
|
||||
ValueFromPipelineByPropertyName=$true,
|
||||
Position=0)]$YourHandName,
|
||||
|
||||
|
||||
#Quanity of cards being drawn
|
||||
[Parameter(Mandatory=$true,
|
||||
ValueFromPipelineByPropertyName=$true,
|
||||
Position=0)]$HowManyCardsToDraw
|
||||
|
||||
)
|
||||
Begin
|
||||
{
|
||||
Write-Verbose "Draws a chosen amount of cards from the chosen deck"
|
||||
|
||||
#try-catch-finally blocks so the user does not have to use New-Deck and New-Hand beforehand.
|
||||
try
|
||||
{
|
||||
$temp = Get-Variable -name "$YourDeckName" -ValueOnly -ErrorAction Stop
|
||||
}
|
||||
catch
|
||||
{
|
||||
Get-Deck -YourDeckName $YourDeckName
|
||||
$temp = Get-Variable -name "$YourDeckName" -ValueOnly -ErrorAction Stop
|
||||
}
|
||||
finally
|
||||
{
|
||||
Write-Host
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
$temp2 = Get-Variable -name "$YourHandName" -ValueOnly -ErrorAction Stop
|
||||
}
|
||||
catch
|
||||
{
|
||||
Get-Hand -YourHandName $YourHandName
|
||||
$temp2 = Get-Variable -name "$YourHandName" -ValueOnly -ErrorAction Stop
|
||||
}
|
||||
finally
|
||||
{
|
||||
Write-Host
|
||||
}
|
||||
}
|
||||
|
||||
Process
|
||||
{
|
||||
Write-Host "you drew $HowManyCardsToDraw cards, they are..."
|
||||
|
||||
$handValues = Get-Variable -name "$YourDeckName" -ValueOnly
|
||||
$handValues = $handValues[0..(($HowManyCardsToDraw -1))] | select value | ft -HideTableHeaders
|
||||
$handValues
|
||||
|
||||
}
|
||||
End
|
||||
{
|
||||
#sets the new values for the deck and the hand selected
|
||||
Set-Variable -Name "$YourDeckName" -value ($temp[$HowManyCardsToDraw..($temp.count)]) -Scope Global
|
||||
Set-Variable -Name "$YourHandName" -value ($temp2 + $handValues) -Scope Global
|
||||
}
|
||||
}
|
||||
38
Task/Playing-cards/Prolog/playing-cards.pro
Normal file
38
Task/Playing-cards/Prolog/playing-cards.pro
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
/** <module> Cards
|
||||
|
||||
A card is represented by the term "card(Pip, Suit)".
|
||||
A deck is represented internally as a list of cards.
|
||||
|
||||
Usage:
|
||||
new_deck(D0), deck_shuffle(D0, D1), deck_deal(D1, C, D2).
|
||||
*/
|
||||
:- module(cards, [ new_deck/1, % -Deck
|
||||
deck_shuffle/2, % +Deck, -NewDeck
|
||||
deck_deal/3, % +Deck, -Card, -NewDeck
|
||||
print_deck/1 % +Deck
|
||||
]).
|
||||
|
||||
%% new_deck(-Deck)
|
||||
new_deck(Deck) :-
|
||||
Suits = [clubs, hearts, spades, diamonds],
|
||||
Pips = [2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king, ace],
|
||||
setof(card(Pip, Suit), (member(Suit, Suits), member(Pip, Pips)), Deck).
|
||||
|
||||
%% deck_shuffle(+Deck, -NewDeck)
|
||||
deck_shuffle(Deck, NewDeck) :-
|
||||
length(Deck, NumCards),
|
||||
findall(X, (between(1, NumCards, _I), X is random(1000)), Ord),
|
||||
pairs_keys_values(Pairs, Ord, Deck),
|
||||
keysort(Pairs, OrdPairs),
|
||||
pairs_values(OrdPairs, NewDeck).
|
||||
|
||||
%% deck_deal(+Deck, -Card, -NewDeck)
|
||||
deck_deal([Card|Cards], Card, Cards).
|
||||
|
||||
%% print_deck(+Deck)
|
||||
print_deck(Deck) :-
|
||||
maplist(print_card, Deck).
|
||||
|
||||
% print_card(+Card)
|
||||
print_card(card(Pip, Suit)) :-
|
||||
format('~a of ~a~n', [Pip, Suit]).
|
||||
134
Task/Playing-cards/PureBasic/playing-cards.basic
Normal file
134
Task/Playing-cards/PureBasic/playing-cards.basic
Normal file
|
|
@ -0,0 +1,134 @@
|
|||
#MaxCards = 52 ;Max Cards in a deck
|
||||
Structure card
|
||||
pip.s
|
||||
suit.s
|
||||
EndStructure
|
||||
|
||||
Structure _membersDeckClass
|
||||
*vtable.i
|
||||
size.i ;zero based count of cards present
|
||||
cards.card[#MaxCards] ;deck content
|
||||
EndStructure
|
||||
|
||||
Interface deckObject
|
||||
Init()
|
||||
shuffle()
|
||||
deal.s(isAbbr = #True)
|
||||
show(isAbbr = #True)
|
||||
EndInterface
|
||||
|
||||
Procedure.s _formatCardInfo(*card.card, isAbbr = #True)
|
||||
;isAbbr determines if the card information is abbrieviated to 2 characters
|
||||
Static pips.s = "2 3 4 5 6 7 8 9 10 Jack Queen King Ace"
|
||||
Static suits.s = "Diamonds Clubs Hearts Spades"
|
||||
Protected c.s
|
||||
|
||||
If isAbbr
|
||||
c = *card\pip + *card\suit
|
||||
Else
|
||||
c = StringField(pips,FindString("23456789TJQKA", *card\pip, 1), " ") + " of "
|
||||
c + StringField(suits,FindString("DCHS", *card\suit, 1)," ")
|
||||
EndIf
|
||||
ProcedureReturn c
|
||||
EndProcedure
|
||||
|
||||
Procedure setInitialValues(*this._membersDeckClass)
|
||||
Protected i, c.s
|
||||
|
||||
Restore cardDat
|
||||
For i = 0 To #MaxCards - 1
|
||||
Read.s c
|
||||
*this\cards[i]\pip = Left(c, 1)
|
||||
*this\cards[i]\suit = Right(c, 1)
|
||||
Next
|
||||
EndProcedure
|
||||
|
||||
Procedure.s dealCard(*this._membersDeckClass, isAbbr)
|
||||
;isAbbr is #True if the card dealt is abbrieviated to 2 characters
|
||||
Protected c.card
|
||||
If *this\size < 0
|
||||
;deck is empty
|
||||
ProcedureReturn ""
|
||||
Else
|
||||
c = *this\cards[*this\size]
|
||||
*this\size - 1
|
||||
ProcedureReturn _formatCardInfo(@c, isAbbr)
|
||||
EndIf
|
||||
EndProcedure
|
||||
|
||||
Procedure showDeck(*this._membersDeckClass, isAbbr)
|
||||
;isAbbr determines if cards are shown with 2 character abbrieviations
|
||||
Protected i
|
||||
|
||||
For i = 0 To *this\size
|
||||
Print(_formatCardInfo(@*this\cards[i], isAbbr))
|
||||
If i <> *this\size: Print(", "): EndIf
|
||||
Next
|
||||
PrintN("")
|
||||
EndProcedure
|
||||
|
||||
Procedure shuffle(*this._membersDeckClass)
|
||||
;works with decks of any size
|
||||
Protected w, i
|
||||
Dim shuffled.card(*this\size)
|
||||
|
||||
For i = *this\size To 0 Step -1
|
||||
w = Random(i)
|
||||
shuffled(i) = *this\cards[w]
|
||||
If w <> i
|
||||
*this\cards[w] = *this\cards[i]
|
||||
EndIf
|
||||
|
||||
Next
|
||||
|
||||
For i = 0 To *this\size
|
||||
*this\cards[i] = shuffled(i)
|
||||
Next
|
||||
EndProcedure
|
||||
|
||||
Procedure newDeck()
|
||||
Protected *newDeck._membersDeckClass = AllocateMemory(SizeOf(_membersDeckClass))
|
||||
If *newDeck
|
||||
*newDeck\vtable = ?vTable_deckClass
|
||||
*newDeck\size = #MaxCards - 1
|
||||
setInitialValues(*newDeck)
|
||||
EndIf
|
||||
ProcedureReturn *newDeck
|
||||
EndProcedure
|
||||
|
||||
DataSection
|
||||
vTable_deckClass:
|
||||
Data.i @setInitialValues()
|
||||
Data.i @shuffle()
|
||||
Data.i @dealCard()
|
||||
Data.i @showDeck()
|
||||
|
||||
cardDat:
|
||||
Data.s "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "TD", "JD", "QD", "KD", "AD"
|
||||
Data.s "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "TC", "JC", "QC", "KC", "AC"
|
||||
Data.s "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "TH", "JH", "QH", "KH", "AH"
|
||||
Data.s "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "TS", "JS", "QS", "KS", "AS"
|
||||
EndDataSection
|
||||
|
||||
If OpenConsole()
|
||||
|
||||
Define deck.deckObject = newDeck()
|
||||
Define deck2.deckObject = newDeck()
|
||||
|
||||
If deck = 0 Or deck2 = 0
|
||||
PrintN("Unable to create decks")
|
||||
End
|
||||
EndIf
|
||||
|
||||
deck\shuffle()
|
||||
PrintN("Dealt: " + deck\deal(#False))
|
||||
PrintN("Dealt: " + deck\deal(#False))
|
||||
PrintN("Dealt: " + deck\deal(#False))
|
||||
PrintN("Dealt: " + deck\deal(#False))
|
||||
deck\show()
|
||||
deck2\show()
|
||||
|
||||
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit")
|
||||
Input()
|
||||
CloseConsole()
|
||||
EndIf
|
||||
26
Task/Playing-cards/Python/playing-cards-1.py
Normal file
26
Task/Playing-cards/Python/playing-cards-1.py
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
import random
|
||||
|
||||
class Card(object):
|
||||
suits = ("Clubs","Hearts","Spades","Diamonds")
|
||||
pips = ("2","3","4","5","6","7","8","9","10","Jack","Queen","King","Ace")
|
||||
|
||||
def __init__(self, pip,suit):
|
||||
self.pip=pip
|
||||
self.suit=suit
|
||||
|
||||
def __str__(self):
|
||||
return "%s %s"%(self.pip,self.suit)
|
||||
|
||||
class Deck(object):
|
||||
def __init__(self):
|
||||
self.deck = [Card(pip,suit) for suit in Card.suits for pip in Card.pips]
|
||||
|
||||
def __str__(self):
|
||||
return "[%s]"%", ".join( (str(card) for card in self.deck))
|
||||
|
||||
def shuffle(self):
|
||||
random.shuffle(self.deck)
|
||||
|
||||
def deal(self):
|
||||
self.shuffle() # Can't tell what is next from self.deck
|
||||
return self.deck.pop(0)
|
||||
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)
|
||||
139
Task/Playing-cards/Quackery/playing-cards.quackery
Normal file
139
Task/Playing-cards/Quackery/playing-cards.quackery
Normal file
|
|
@ -0,0 +1,139 @@
|
|||
[ /mod if 1+ ] is /up ( n n --> n )
|
||||
|
||||
[ [] 52 times [ i^ join ] ] is newpack ( --> [ )
|
||||
|
||||
[ -13 swap join ] is +lowjoker ( [ --> [ )
|
||||
|
||||
[ 64 join ] is +highjoker ( [ --> [ )
|
||||
|
||||
[ +lowjoker +highjoker ] is +jokers ( [ --> [ )
|
||||
|
||||
[ [ table
|
||||
$ 'ace' $ 'two' $ 'three'
|
||||
$ 'four' $ 'five' $ 'six'
|
||||
$ 'seven' $ 'eight' $ 'nine'
|
||||
$ 'ten' $ 'jack' $ 'queen'
|
||||
$ 'king' ] do ] is rank ( n --> $ )
|
||||
|
||||
[ [ table
|
||||
$ 'clubs' $ 'diamonds'
|
||||
$ 'hearts' $ 'spades' ]
|
||||
do ] is suit ( n --> $ )
|
||||
|
||||
[ dup -13 = iff
|
||||
[ drop $ 'low joker' ] done
|
||||
dup 64 = iff
|
||||
[ drop $ 'high joker' ] done
|
||||
13 /mod rank
|
||||
$ ' of ' join
|
||||
swap suit join ] is card ( n --> $ )
|
||||
|
||||
[ [] swap
|
||||
witheach
|
||||
[ card join
|
||||
carriage join ] ] is hand ( [ --> $ )
|
||||
|
||||
[ card echo$ ] is echocard ( n --> )
|
||||
|
||||
[ hand echo$ ] is echohand ( [ --> )
|
||||
|
||||
[ > ] is bysuit ( n n --> b )
|
||||
|
||||
[ 13 /mod 4 * + ] is rankfirst ( n --> n )
|
||||
|
||||
[ rankfirst
|
||||
swap rankfirst < ] is byrank ( n n --> b )
|
||||
|
||||
[ dup [] != while
|
||||
dup size random split
|
||||
swap join ] is cut ( [ --> [ )
|
||||
|
||||
[ dup [] != while
|
||||
dup size 2 * 3 /up
|
||||
random split
|
||||
dup size dup iff
|
||||
[ random split
|
||||
dip swap join ]
|
||||
else drop join ] is scarne ( [ --> [ )
|
||||
|
||||
[ [] nested swap of ] is players ( n --> [ )
|
||||
|
||||
[ temp put
|
||||
over size * times
|
||||
[ over [] = iff
|
||||
[ 1 split swap join ]
|
||||
else
|
||||
[ swap temp share split
|
||||
swap rot behead
|
||||
rot join
|
||||
nested join ] ]
|
||||
temp release ] is dealby ( [ [ n n --> [ [ )
|
||||
|
||||
[ 1 dealby ] is deal ( [ [ n --> [ [ )
|
||||
|
||||
[ 1 swap dealby ] is dealeach ( [ [ n --> [ [ )
|
||||
|
||||
[ over size deal ] is dealall ( [ [ --> [ [ )
|
||||
|
||||
[ [] swap
|
||||
[ behead 1 split
|
||||
dip [ swap dip join ]
|
||||
dup [] = iff
|
||||
drop
|
||||
else
|
||||
[ nested join ]
|
||||
dup [] = until ]
|
||||
drop swap join ] is undeal ( [ [ --> [ )
|
||||
|
||||
[ over size over size
|
||||
tuck /mod
|
||||
rot over -
|
||||
dip [ over 1+ swap of ]
|
||||
rot swap of join
|
||||
swap []
|
||||
2swap
|
||||
witheach
|
||||
[ split unrot nested join
|
||||
swap ]
|
||||
unrot witheach
|
||||
[ dip behead join
|
||||
nested join ] ] is divvy ( [ [ --> [ [ )
|
||||
|
||||
[ newpack
|
||||
4 players divvy
|
||||
witheach
|
||||
[ 1 split 7 split
|
||||
nip join join ]
|
||||
+highjoker ] is euchrepack ( --> [ )
|
||||
|
||||
[ [] swap witheach join
|
||||
swap join ] is gather ( [ [ --> [ )
|
||||
|
||||
[ 2 players divvy undeal ] is faro-out ( [ --> [ )
|
||||
|
||||
[ 2 players divvy
|
||||
1 split swap join undeal ] is faro-in ( [ --> [ )
|
||||
|
||||
[ unrot times
|
||||
[ over i bit & iff
|
||||
faro-in else faro-out ]
|
||||
nip ] is faro ( [ n n --> [ )
|
||||
|
||||
[ players dealall gather ] is piles ( [ n --> [ )
|
||||
|
||||
[ players dealall
|
||||
shuffle gather ] is mixpiles ( [ --> [ )
|
||||
|
||||
[ stack 5 ] is riffskill ( --> [ )
|
||||
|
||||
[ [] swap
|
||||
dup size 2 /up split
|
||||
2 random if swap
|
||||
[ dup [] != while
|
||||
behead nested
|
||||
dip rot join unrot
|
||||
riffskill share 1+ random
|
||||
1 != if swap
|
||||
again ] drop join ] is riffle ( [ --> [ )
|
||||
|
||||
[ times riffle ] is riffles ( [ n --> [ )
|
||||
64
Task/Playing-cards/QuickBASIC/playing-cards.basic
Normal file
64
Task/Playing-cards/QuickBASIC/playing-cards.basic
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
DECLARE SUB setInitialValues (deck() AS STRING * 2)
|
||||
DECLARE SUB shuffle (deck() AS STRING * 2)
|
||||
DECLARE SUB showDeck (deck() AS STRING * 2)
|
||||
DECLARE FUNCTION deal$ (deck() AS STRING * 2)
|
||||
|
||||
DATA "AS", "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "TS", "JS", "QS", "KS"
|
||||
DATA "AH", "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "TH", "JH", "QH", "KH"
|
||||
DATA "AC", "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "TC", "JC", "QC", "KC"
|
||||
DATA "AD", "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "TD", "JD", "QD", "KD"
|
||||
|
||||
RANDOMIZE TIMER
|
||||
|
||||
REDIM cards(51) AS STRING * 2
|
||||
REDIM cards2(51) AS STRING * 2
|
||||
|
||||
setInitialValues cards()
|
||||
setInitialValues cards2()
|
||||
shuffle cards()
|
||||
PRINT "Dealt: "; deal$(cards())
|
||||
PRINT "Dealt: "; deal$(cards())
|
||||
PRINT "Dealt: "; deal$(cards())
|
||||
PRINT "Dealt: "; deal$(cards())
|
||||
showDeck cards()
|
||||
showDeck cards2()
|
||||
|
||||
FUNCTION deal$ (deck() AS STRING * 2)
|
||||
'technically dealing from the BOTTOM of the deck... whatever
|
||||
DIM c AS STRING * 2
|
||||
c = deck(UBOUND(deck))
|
||||
REDIM PRESERVE deck(LBOUND(deck) TO UBOUND(deck) - 1) AS STRING * 2
|
||||
deal$ = c
|
||||
END FUNCTION
|
||||
|
||||
SUB setInitialValues (deck() AS STRING * 2)
|
||||
DIM L0 AS INTEGER
|
||||
|
||||
RESTORE
|
||||
FOR L0 = 0 TO 51
|
||||
READ deck(L0)
|
||||
NEXT
|
||||
END SUB
|
||||
|
||||
SUB showDeck (deck() AS STRING * 2)
|
||||
FOR L% = LBOUND(deck) TO UBOUND(deck)
|
||||
PRINT deck(L%); " ";
|
||||
NEXT
|
||||
PRINT
|
||||
END SUB
|
||||
|
||||
SUB shuffle (deck() AS STRING * 2)
|
||||
DIM w AS INTEGER
|
||||
DIM shuffled(51) AS STRING * 2
|
||||
DIM L0 AS INTEGER
|
||||
|
||||
FOR L0 = 51 TO 0 STEP -1
|
||||
w = INT(RND * (L0 + 1))
|
||||
shuffled(L0) = deck(w)
|
||||
IF w <> L0 THEN deck(w) = deck(L0)
|
||||
NEXT
|
||||
|
||||
FOR L0 = 0 TO 51
|
||||
deck(L0) = shuffled(L0)
|
||||
NEXT
|
||||
END SUB
|
||||
25
Task/Playing-cards/R/playing-cards.r
Normal file
25
Task/Playing-cards/R/playing-cards.r
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
pips <- c("2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace")
|
||||
suit <- c("Clubs", "Diamonds", "Hearts", "Spades")
|
||||
# Create a deck
|
||||
deck <- data.frame(pips=rep(pips, 4), suit=rep(suit, each=13))
|
||||
|
||||
shuffle <- function(deck)
|
||||
{
|
||||
n <- nrow(deck)
|
||||
ord <- sample(seq_len(n), size=n)
|
||||
deck[ord,]
|
||||
}
|
||||
|
||||
deal <- function(deck, fromtop=TRUE)
|
||||
{
|
||||
index <- ifelse(fromtop, 1, nrow(deck))
|
||||
print(paste("Dealt the", deck[index, "pips"], "of", deck[index, "suit"]))
|
||||
deck[-index,]
|
||||
}
|
||||
|
||||
# Usage
|
||||
deck <- shuffle(deck)
|
||||
deck
|
||||
deck <- deal(deck)
|
||||
# While no-one is looking, sneakily deal a card from the bottom of the pack
|
||||
deck <- deal(deck, FALSE)
|
||||
50
Task/Playing-cards/REXX/playing-cards-1.rexx
Normal file
50
Task/Playing-cards/REXX/playing-cards-1.rexx
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
/* REXX ***************************************************************
|
||||
* 1) Build ordered Card deck
|
||||
* 2) Create shuffled stack
|
||||
* 3) Deal 5 cards to 4 players each
|
||||
* 4) show what cards have been dealt and what's left on the stack
|
||||
* 05.07.2012 Walter Pachl
|
||||
**********************************************************************/
|
||||
colors='S H C D'
|
||||
ranks ='A 2 3 4 5 6 7 8 9 T J Q K'
|
||||
i=0
|
||||
cards=''
|
||||
ss=''
|
||||
Do c=1 To 4
|
||||
Do r=1 To 13
|
||||
i=i+1
|
||||
card.i=word(colors,c)word(ranks,r)
|
||||
cards=cards card.i
|
||||
End
|
||||
End
|
||||
n=52 /* number of cards on deck */
|
||||
Do si=1 To 51 /* pick 51 cards */
|
||||
x=random(0,n-1)+1 /* take card x (in 1...n) */
|
||||
s.si=card.x /* card on shuffled stack */
|
||||
ss=ss s.si /* string of shuffled stack */
|
||||
card.x=card.n /* replace card taken */
|
||||
n=n-1 /* decrement nr of cards */
|
||||
End
|
||||
s.52=card.1 /* pick the last card left */
|
||||
ss=ss s.52 /* add it to the string */
|
||||
Say 'Ordered deck:'
|
||||
Say ' 'subword(cards,1,26)
|
||||
Say ' 'subword(cards,27,52)
|
||||
Say 'Shuffled stack:'
|
||||
Say ' 'subword(ss,1,26)
|
||||
Say ' 'subword(ss,27,52)
|
||||
si=52
|
||||
deck.=''
|
||||
Do ci=1 To 5 /* 5 cards each */
|
||||
Do pli=1 To 4 /* 4 players */
|
||||
deck.pli.ci=s.si /* take top of shuffled deck */
|
||||
si=si-1 /* decrement number */
|
||||
deck.pli=deck.pli deck.pli.ci /* pli's cards as string */
|
||||
End
|
||||
End
|
||||
Do pli=1 To 4 /* show the 4 dealt ... */
|
||||
Say pli':' deck.pli
|
||||
End
|
||||
Say 'Left on shuffled stack:'
|
||||
Say ' 'subword(ss,1,26) /* and what's left on stack */
|
||||
Say ' 'subword(ss,27,6)
|
||||
32
Task/Playing-cards/REXX/playing-cards-2.rexx
Normal file
32
Task/Playing-cards/REXX/playing-cards-2.rexx
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
/*REXX pgm shows a method to build/shuffle/deal 5 cards (using a 52─card deck)──►4 hands*/
|
||||
box = build(); say ' box of cards:' box /*a brand new standard box of 52 cards.*/
|
||||
deck= mix(); say 'shuffled deck:' deck /*obtain a randomly shuffled deck. */
|
||||
call deal 5, 4 /* ◄───────────────── 5 cards, 4 hands.*/
|
||||
say; say; say right('[north]' hand.1, 60)
|
||||
say; say ' [west]' hand.4 right('[east]' hand.2, 60)
|
||||
say; say right('[south]' hand.3, 60)
|
||||
say; say; say; say 'remainder of deck: ' deck
|
||||
exit 0 /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
build: _=; ranks= "A 2 3 4 5 6 7 8 9 10 J Q K" /*ranks. */
|
||||
if 5=='f5'x then suits= "h d c s" /*EBCDIC? */
|
||||
else suits= "♥ ♦ ♣ ♠" /*ASCII. */
|
||||
do s=1 for words(suits); $= word(suits, s)
|
||||
do r=1 for words(ranks); _= _ word(ranks, r)$ /*append a suit to rank*/
|
||||
end /*r*/
|
||||
end /*s*/; return _ /*jokers are not used. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
deal: parse arg #cards, hands; hand.= /*initially, nullify all hands. */
|
||||
do #cards /*deal a hand to all the players. */
|
||||
do player=1 for hands /*deal some cards to the players. */
|
||||
hand.player= hand.player word(deck, 1) /*deal the top card to a player. */
|
||||
deck= subword(deck, 2) /*diminish deck, elide one card. */
|
||||
end /*player*/
|
||||
end /*#cards*/; return
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
mix: @=; _=box; #cards= words(_) /*define three REXX variables. */
|
||||
do mixer=1 for #cards /*shuffle all the cards in deck. */
|
||||
?= random(1, #cards + 1 - mixer) /*each shuffle, random# decreases.*/
|
||||
@= @ word(_, ?) /*shuffled deck, 1 card at a time.*/
|
||||
_= delword(_, ?, 1) /*elide just─chosen card from deck*/
|
||||
end /*mixer*/; return @
|
||||
33
Task/Playing-cards/Racket/playing-cards.rkt
Normal file
33
Task/Playing-cards/Racket/playing-cards.rkt
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
#lang racket
|
||||
|
||||
;; suits:
|
||||
(define suits '(club heart diamond spade))
|
||||
|
||||
;; ranks
|
||||
(define ranks '(1 2 3 4 5 6 7 8 9 10 jack queen king))
|
||||
|
||||
;; cards
|
||||
(define cards
|
||||
(for*/list ([suit suits] [rank ranks])
|
||||
(list suit rank)))
|
||||
|
||||
;; a deck is a box containing a list of cards.
|
||||
(define (new-deck)
|
||||
(box cards))
|
||||
|
||||
;; shuffle the cards in a deck
|
||||
(define (deck-shuffle deck)
|
||||
(set-box! deck (shuffle (unbox deck))))
|
||||
|
||||
;; deal a card from the deck:
|
||||
(define (deck-deal deck)
|
||||
(begin0 (first (unbox deck))
|
||||
(set-box! deck (rest (unbox deck)))))
|
||||
|
||||
|
||||
;; TRY IT OUT:
|
||||
(define my-deck (new-deck))
|
||||
(deck-shuffle my-deck)
|
||||
(deck-deal my-deck)
|
||||
(deck-deal my-deck)
|
||||
(length (unbox my-deck))
|
||||
30
Task/Playing-cards/Raku/playing-cards.raku
Normal file
30
Task/Playing-cards/Raku/playing-cards.raku
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
enum Pip <A 2 3 4 5 6 7 8 9 10 J Q K>;
|
||||
enum Suit <♦ ♣ ♥ ♠>;
|
||||
|
||||
class Card {
|
||||
has Pip $.pip;
|
||||
has Suit $.suit;
|
||||
|
||||
method Str { $!pip ~ $!suit }
|
||||
}
|
||||
|
||||
class Deck {
|
||||
has Card @.cards = pick *,
|
||||
map { Card.new(:$^pip, :$^suit) }, flat (Pip.pick(*) X Suit.pick(*));
|
||||
|
||||
method shuffle { @!cards .= pick: * }
|
||||
|
||||
method deal { shift @!cards }
|
||||
|
||||
method Str { ~@!cards }
|
||||
method gist { ~@!cards }
|
||||
}
|
||||
|
||||
my Deck $d = Deck.new;
|
||||
say "Deck: $d";
|
||||
|
||||
my $top = $d.deal;
|
||||
say "Top card: $top";
|
||||
|
||||
$d.shuffle;
|
||||
say "Deck, re-shuffled: ", $d;
|
||||
27
Task/Playing-cards/Red/playing-cards.red
Normal file
27
Task/Playing-cards/Red/playing-cards.red
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
Red [Title: "Playing Cards"]
|
||||
|
||||
pip: ["a" "2" "3" "4" "5" "6" "7" "8" "9" "10" "j" "q" "k"]
|
||||
suit: ["♣" "♦" "♥" "♠"]
|
||||
|
||||
make-deck: function [] [
|
||||
new-deck: make block! 52
|
||||
foreach s suit [foreach p pip [append/only new-deck reduce [p s]]]
|
||||
return new-deck
|
||||
]
|
||||
|
||||
shuffle: function [deck [block!]] [deck: random deck]
|
||||
|
||||
deal: function [other-deck [block!] deck [block!]] [unless empty? deck [append/only other-deck take deck]]
|
||||
|
||||
contents: function [deck [block!]] [
|
||||
line: 0
|
||||
repeat i length? deck [
|
||||
prin [trim/all form deck/:i " "]
|
||||
if (to-integer i / 13) > line [line: line + 1 print ""]
|
||||
]]
|
||||
|
||||
deck: shuffle make-deck
|
||||
print "40 cards from a deck:"
|
||||
loop 5 [ print "" loop 8 [prin [trim/all form take deck " "]]]
|
||||
prin "^/^/remaining: "
|
||||
contents deck
|
||||
245
Task/Playing-cards/Ring/playing-cards.ring
Normal file
245
Task/Playing-cards/Ring/playing-cards.ring
Normal file
|
|
@ -0,0 +1,245 @@
|
|||
Load "guilib.ring"
|
||||
|
||||
nScale = 1
|
||||
|
||||
app1 = new qApp
|
||||
|
||||
mypic = new QPixmap("cards.jpg")
|
||||
|
||||
mypic2 = mypic.copy(0,(124*4)+1,79,124)
|
||||
Player1EatPic = mypic.copy(80,(124*4)+1,79,124)
|
||||
Player2EatPic= mypic.copy(160,(124*4)+1,79,124)
|
||||
|
||||
aMyCards = []
|
||||
aMyValues = []
|
||||
for x1 = 0 to 3
|
||||
for y1 = 0 to 12
|
||||
temppic = mypic.copy((79*y1)+1,(124*x1)+1,79,124)
|
||||
aMyCards + temppic
|
||||
aMyValues + (y1+1)
|
||||
next
|
||||
next
|
||||
|
||||
nPlayer1Score = 0 nPlayer2Score=0
|
||||
|
||||
do
|
||||
Page1 = new Game
|
||||
Page1.Start()
|
||||
again Page1.lnewgame
|
||||
|
||||
mypic.delete()
|
||||
mypic2.delete()
|
||||
Player1EatPic.delete()
|
||||
Player2EatPic.delete()
|
||||
|
||||
for t in aMyCards
|
||||
t.delete()
|
||||
next
|
||||
|
||||
func gui_setbtnpixmap pBtn,pPixmap
|
||||
pBtn {
|
||||
setIcon(new qicon(pPixmap.scaled(width(),height(),0,0)))
|
||||
setIconSize(new QSize(width(),height()))
|
||||
}
|
||||
|
||||
Class Game
|
||||
|
||||
nCardsCount = 10
|
||||
win1 layout1 label1 label2 layout2 layout3 aBtns aBtns2
|
||||
aCards nRole=1 aStatus = list(nCardsCount) aStatus2 = aStatus
|
||||
aValues aStatusValues = aStatus aStatusValues2 = aStatus
|
||||
Player1EatPic Player2EatPic
|
||||
lnewgame = false
|
||||
nDelayEat = 0.5
|
||||
nDelayNewGame = 1
|
||||
|
||||
func start
|
||||
|
||||
win1 = new qWidget() {
|
||||
setwindowtitle("Five")
|
||||
setstylesheet("background-color: White")
|
||||
showfullscreen()
|
||||
}
|
||||
|
||||
layout1 = new qvboxlayout()
|
||||
|
||||
label1 = new qlabel(win1) {
|
||||
settext("Player (1) - Score : " + nPlayer1Score)
|
||||
setalignment(Qt_AlignHCenter | Qt_AlignVCenter)
|
||||
setstylesheet("color: White; background-color: Purple;
|
||||
font-size:20pt")
|
||||
setfixedheight(200)
|
||||
}
|
||||
|
||||
closebtn = new qpushbutton(win1) {
|
||||
settext("Close Application")
|
||||
setstylesheet("font-size: 18px ; color : white ;
|
||||
background-color: black ;")
|
||||
setclickevent("Page1.win1.close()")
|
||||
}
|
||||
|
||||
aCards = aMyCards
|
||||
aValues = aMyValues
|
||||
|
||||
layout2 = new qhboxlayout()
|
||||
|
||||
aBtns = []
|
||||
|
||||
for x = 1 to nCardsCount
|
||||
aBtns + new qpushbutton(win1)
|
||||
aBtns[x].setfixedwidth(79*nScale)
|
||||
aBtns[x].setfixedheight(124*nScale)
|
||||
gui_setbtnpixmap(aBtns[x],mypic2)
|
||||
layout2.addwidget(aBtns[x])
|
||||
aBtns[x].setclickevent("Page1.Player1click("+x+")")
|
||||
next
|
||||
|
||||
layout1.addwidget(label1)
|
||||
layout1.addlayout(layout2)
|
||||
|
||||
label2 = new qlabel(win1) {
|
||||
settext("Player (2) - Score : " + nPlayer2Score)
|
||||
setalignment(Qt_AlignHCenter | Qt_AlignVCenter)
|
||||
setstylesheet("color: white; background-color: red;
|
||||
font-size:20pt")
|
||||
setfixedheight(200)
|
||||
}
|
||||
|
||||
layout3 = new qhboxlayout()
|
||||
|
||||
aBtns2 = []
|
||||
for x = 1 to nCardsCount
|
||||
aBtns2 + new qpushbutton(win1)
|
||||
aBtns2[x].setfixedwidth(79*nScale)
|
||||
aBtns2[x].setfixedheight(124*nScale)
|
||||
gui_setbtnpixmap(aBtns2[x],mypic2)
|
||||
layout3.addwidget(aBtns2[x])
|
||||
aBtns2[x].setclickevent("Page1.Player2click("+x+")")
|
||||
next
|
||||
|
||||
layout1.addwidget(label2)
|
||||
layout1.addlayout(layout3)
|
||||
layout1.addwidget(closebtn)
|
||||
|
||||
win1.setlayout(layout1)
|
||||
|
||||
app1.exec()
|
||||
|
||||
Func Player1Click x
|
||||
if nRole = 1 and aStatus[x] = 0
|
||||
nPos = ((random(100)+clock())%(len(aCards)-1)) + 1
|
||||
gui_setbtnpixmap(aBtns[x],aCards[nPos])
|
||||
del(aCards,nPos)
|
||||
nRole = 2
|
||||
aStatus[x] = 1
|
||||
aStatusValues[x] = aValues[nPos]
|
||||
del(aValues,nPos)
|
||||
Player1Eat(x,aStatusValues[x])
|
||||
checknewgame()
|
||||
ok
|
||||
|
||||
Func Player2Click x
|
||||
if nRole = 2 and aStatus2[x] = 0
|
||||
nPos = ((random(100)+clock())%(len(aCards)-1)) + 1
|
||||
gui_setbtnpixmap(aBtns2[x],aCards[nPos])
|
||||
del(aCards,nPos)
|
||||
nRole = 1
|
||||
aStatus2[x] = 1
|
||||
aStatusValues2[x] = aValues[nPos]
|
||||
del(aValues,nPos)
|
||||
Player2Eat(x,aStatusValues2[x])
|
||||
checknewgame()
|
||||
ok
|
||||
|
||||
Func Player1Eat nPos,nValue
|
||||
|
||||
app1.processEvents()
|
||||
|
||||
delay(nDelayEat)
|
||||
lEat = false
|
||||
for x = 1 to nCardsCount
|
||||
if aStatus2[x] = 1 and (aStatusValues2[x] = nValue or nValue=5)
|
||||
aStatus2[x] = 2
|
||||
gui_setbtnpixmap(aBtns2[x],Player1EatPic)
|
||||
lEat = True
|
||||
nPlayer1Score++
|
||||
ok
|
||||
if (x != nPos) and (aStatus[x] = 1) and
|
||||
(aStatusValues[x] = nValue or nValue=5)
|
||||
aStatus[x] = 2
|
||||
gui_setbtnpixmap(aBtns[x],Player1EatPic)
|
||||
lEat = True
|
||||
nPlayer1Score++
|
||||
ok
|
||||
next
|
||||
if lEat
|
||||
nPlayer1Score++
|
||||
gui_setbtnpixmap(aBtns[nPos],Player1EatPic)
|
||||
aStatus[nPos] = 2
|
||||
label1.settext("Player (1) - Score : " + nPlayer1Score)
|
||||
ok
|
||||
|
||||
Func Player2Eat nPos,nValue
|
||||
|
||||
app1.processEvents()
|
||||
|
||||
delay(nDelayEat)
|
||||
lEat = false
|
||||
for x = 1 to nCardsCount
|
||||
if aStatus[x] = 1 and (aStatusValues[x] = nValue or nValue = 5)
|
||||
aStatus[x] = 2
|
||||
gui_setbtnpixmap(aBtns[x],Player2EatPic)
|
||||
lEat = True
|
||||
nPlayer2Score++
|
||||
ok
|
||||
|
||||
if (x != nPos) and (aStatus2[x] = 1) and
|
||||
(aStatusValues2[x] = nValue or nValue=5 )
|
||||
aStatus2[x] = 2
|
||||
gui_setbtnpixmap(aBtns2[x],Player2EatPic)
|
||||
lEat = True
|
||||
nPlayer2Score++
|
||||
ok
|
||||
next
|
||||
if lEat
|
||||
nPlayer2Score++
|
||||
gui_setbtnpixmap(aBtns2[nPos],Player2EatPic)
|
||||
aStatus2[nPos] = 2
|
||||
label2.settext("Player (2) - Score : " + nPlayer2Score)
|
||||
ok
|
||||
|
||||
Func checknewgame
|
||||
if isnewgame()
|
||||
lnewgame = true
|
||||
|
||||
if nPlayer1Score > nPlayer2Score
|
||||
label1.settext("Player (1) Wins!!!")
|
||||
ok
|
||||
if nPlayer2Score > nPlayer1Score
|
||||
label2.settext("Player (2) Wins!!!")
|
||||
ok
|
||||
|
||||
app1.processEvents()
|
||||
delay(nDelayNewGame)
|
||||
|
||||
win1.delete()
|
||||
app1.quit()
|
||||
ok
|
||||
|
||||
Func isnewgame
|
||||
for t in aStatus
|
||||
if t = 0
|
||||
return false
|
||||
ok
|
||||
next
|
||||
for t in aStatus2
|
||||
if t = 0
|
||||
return false
|
||||
ok
|
||||
next
|
||||
return true
|
||||
|
||||
Func delay x
|
||||
nTime = x * 1000
|
||||
oTest = new qTest
|
||||
oTest.qsleep(nTime)
|
||||
51
Task/Playing-cards/Ruby/playing-cards.rb
Normal file
51
Task/Playing-cards/Ruby/playing-cards.rb
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
class Card
|
||||
# class constants
|
||||
SUITS = %i[ Clubs Hearts Spades Diamonds ]
|
||||
PIPS = %i[ 2 3 4 5 6 7 8 9 10 Jack Queen King Ace ]
|
||||
|
||||
# class variables (private)
|
||||
@@suit_value = Hash[ SUITS.each_with_index.to_a ]
|
||||
@@pip_value = Hash[ PIPS.each_with_index.to_a ]
|
||||
|
||||
attr_reader :pip, :suit
|
||||
|
||||
def initialize(pip,suit)
|
||||
@pip = pip
|
||||
@suit = suit
|
||||
end
|
||||
|
||||
def to_s
|
||||
"#{@pip} #{@suit}"
|
||||
end
|
||||
|
||||
# allow sorting an array of Cards: first by suit, then by value
|
||||
def <=>(other)
|
||||
(@@suit_value[@suit] <=> @@suit_value[other.suit]).nonzero? or
|
||||
@@pip_value[@pip] <=> @@pip_value[other.pip]
|
||||
end
|
||||
end
|
||||
|
||||
class Deck
|
||||
def initialize
|
||||
@deck = Card::SUITS.product(Card::PIPS).map{|suit,pip| Card.new(pip,suit)}
|
||||
end
|
||||
|
||||
def to_s
|
||||
@deck.inspect
|
||||
end
|
||||
|
||||
def shuffle!
|
||||
@deck.shuffle!
|
||||
self
|
||||
end
|
||||
|
||||
def deal(*args)
|
||||
@deck.shift(*args)
|
||||
end
|
||||
end
|
||||
|
||||
deck = Deck.new.shuffle!
|
||||
puts card = deck.deal
|
||||
hand = deck.deal(5)
|
||||
puts hand.join(", ")
|
||||
puts hand.sort.join(", ")
|
||||
25
Task/Playing-cards/Run-BASIC/playing-cards.basic
Normal file
25
Task/Playing-cards/Run-BASIC/playing-cards.basic
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
suite$ = "C,D,H,S" ' Club, Diamond, Heart, Spade
|
||||
card$ = "A,2,3,4,5,6,7,8,9,T,J,Q,K" ' Cards Ace to King
|
||||
|
||||
dim n(55) ' make ordered deck
|
||||
for i = 1 to 52 ' of 52 cards
|
||||
n(i) = i
|
||||
next i
|
||||
|
||||
for i = 1 to 52 * 3 ' shuffle deck 3 times
|
||||
i1 = int(rnd(1)*52) + 1
|
||||
i2 = int(rnd(1)*52) + 1
|
||||
h2 = n(i1)
|
||||
n(i1) = n(i2)
|
||||
n(i2) = h2
|
||||
next i
|
||||
|
||||
for hand = 1 to 4 ' 4 hands
|
||||
for deal = 1 to 13 ' deal each 13 cards
|
||||
card = card + 1 ' next card in deck
|
||||
s = (n(card) mod 4) + 1 ' determine suite
|
||||
c = (n(card) mod 13) + 1 ' determine card
|
||||
print word$(card$,c,",");word$(suite$,s,",");" "; ' show the card
|
||||
next deal
|
||||
print
|
||||
next hand
|
||||
63
Task/Playing-cards/Rust/playing-cards.rust
Normal file
63
Task/Playing-cards/Rust/playing-cards.rust
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
extern crate rand;
|
||||
|
||||
use std::fmt;
|
||||
use rand::Rng;
|
||||
use Pip::*;
|
||||
use Suit::*;
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
enum Pip { Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King }
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
enum Suit { Spades, Hearts, Diamonds, Clubs }
|
||||
|
||||
struct Card {
|
||||
pip: Pip,
|
||||
suit: Suit
|
||||
}
|
||||
|
||||
impl fmt::Display for Card {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{:?} of {:?}", self.pip, self.suit)
|
||||
}
|
||||
}
|
||||
|
||||
struct Deck(Vec<Card>);
|
||||
|
||||
impl Deck {
|
||||
fn new() -> Deck {
|
||||
let mut cards:Vec<Card> = Vec::with_capacity(52);
|
||||
for &suit in &[Spades, Hearts, Diamonds, Clubs] {
|
||||
for &pip in &[Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King] {
|
||||
cards.push( Card{pip: pip, suit: suit} );
|
||||
}
|
||||
}
|
||||
Deck(cards)
|
||||
}
|
||||
|
||||
fn deal(&mut self) -> Option<Card> {
|
||||
self.0.pop()
|
||||
}
|
||||
|
||||
fn shuffle(&mut self) {
|
||||
rand::thread_rng().shuffle(&mut self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Deck {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
for card in self.0.iter() {
|
||||
writeln!(f, "{}", card);
|
||||
}
|
||||
write!(f, "")
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut deck = Deck::new();
|
||||
deck.shuffle();
|
||||
//println!("{}", deck);
|
||||
for _ in 0..5 {
|
||||
println!("{}", deck.deal().unwrap());
|
||||
}
|
||||
}
|
||||
35
Task/Playing-cards/Scala/playing-cards-1.scala
Normal file
35
Task/Playing-cards/Scala/playing-cards-1.scala
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
import scala.annotation.tailrec
|
||||
import scala.util.Random
|
||||
|
||||
object Pip extends Enumeration {
|
||||
type Pip = Value
|
||||
val Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace = Value
|
||||
}
|
||||
|
||||
object Suit extends Enumeration {
|
||||
type Suit = Value
|
||||
val Diamonds, Spades, Hearts, Clubs = Value
|
||||
}
|
||||
|
||||
import Suit._
|
||||
import Pip._
|
||||
case class Card(suit:Suit, value:Pip){
|
||||
override def toString():String=value + " of " + suit
|
||||
}
|
||||
|
||||
class Deck private(val cards:List[Card]) {
|
||||
def this()=this(for {s <- Suit.values.toList; v <- Pip.values} yield Card(s,v))
|
||||
def shuffle:Deck=new Deck(Random.shuffle(cards))
|
||||
|
||||
def deal(n:Int=1):(Seq[Card], Deck)={
|
||||
@tailrec def loop(count:Int, c:Seq[Card], d:Deck):(Seq[Card], Deck)={
|
||||
if(count==0 || d.cards==Nil) (c,d)
|
||||
else {
|
||||
val card :: deck = d.cards
|
||||
loop(count-1, c :+ card, new Deck(deck))
|
||||
}
|
||||
}
|
||||
loop(n, Seq(), this)
|
||||
}
|
||||
override def toString():String="Deck: " + (cards mkString ", ")
|
||||
}
|
||||
15
Task/Playing-cards/Scala/playing-cards-2.scala
Normal file
15
Task/Playing-cards/Scala/playing-cards-2.scala
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
val deck=new Deck()
|
||||
val deckShuffled:Deck=deck.shuffle
|
||||
|
||||
println(deck)
|
||||
println(deckShuffled)
|
||||
|
||||
val (a, rest) = deckShuffled.deal()
|
||||
val (b, rest2) = rest.deal()
|
||||
|
||||
println(a head)
|
||||
println(b head)
|
||||
|
||||
val (cards, rest3) =deckShuffled deal 6
|
||||
println(cards)
|
||||
println(rest3)
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue