2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,7 +1,13 @@
|
|||
Create a data structure and the associated methods to define and manipulate a deck of [[wp:Playing-cards#Anglo-American-French|playing cards]].
|
||||
;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, and print the current contents of a deck.
|
||||
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.
|
||||
<br><br>
|
||||
|
|
|
|||
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 ####
|
||||
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
|
||||
|
|
@ -1,6 +1,3 @@
|
|||
#ifndef CARDS_H_INC
|
||||
#define CARDS_H_INC
|
||||
|
||||
#include <deque>
|
||||
#include <algorithm>
|
||||
#include <ostream>
|
||||
|
|
@ -8,86 +5,72 @@
|
|||
|
||||
namespace cards
|
||||
{
|
||||
class card
|
||||
{
|
||||
public:
|
||||
class card
|
||||
{
|
||||
public:
|
||||
enum pip_type { two, three, four, five, six, seven, eight, nine, ten,
|
||||
jack, queen, king, ace };
|
||||
enum suite_type { hearts, spades, diamonds, clubs };
|
||||
jack, queen, king, ace, pip_count };
|
||||
enum suite_type { hearts, spades, diamonds, clubs, suite_count };
|
||||
enum { unique_count = pip_count * suite_count };
|
||||
|
||||
// construct a card of a given suite and pip
|
||||
card(suite_type s, pip_type p): value(s + 4*p) {}
|
||||
card(suite_type s, pip_type p): value(s + suite_count * p) {}
|
||||
|
||||
// construct a card directly from its value
|
||||
card(unsigned char v = 0): value(v) {}
|
||||
explicit card(unsigned char v = 0): value(v) {}
|
||||
|
||||
// return the pip of the card
|
||||
pip_type pip() { return pip_type(value/4); }
|
||||
pip_type pip() { return pip_type(value / suite_count); }
|
||||
|
||||
// return the suit of the card
|
||||
suite_type suite() { return suite_type(value%4); }
|
||||
suite_type suite() { return suite_type(value % suite_count); }
|
||||
|
||||
private:
|
||||
// there are only 52 cards, therefore unsigned char suffices
|
||||
private:
|
||||
unsigned char value;
|
||||
};
|
||||
};
|
||||
|
||||
char const* const pip_names[] =
|
||||
const char* const pip_names[] =
|
||||
{ "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
|
||||
"jack", "queen", "king", "ace" };
|
||||
|
||||
// output a pip
|
||||
std::ostream& operator<<(std::ostream& os, card::pip_type pip)
|
||||
{
|
||||
std::ostream& operator<<(std::ostream& os, card::pip_type pip)
|
||||
{
|
||||
return os << pip_names[pip];
|
||||
};
|
||||
|
||||
char const* const suite_names[] =
|
||||
{ "hearts", "spades", "diamonds", "clubs" };
|
||||
|
||||
// output a suite
|
||||
std::ostream& operator<<(std::ostream& os, card::suite_type suite)
|
||||
{
|
||||
return os << suite_names[suite];
|
||||
}
|
||||
|
||||
// output a card
|
||||
std::ostream& operator<<(std::ostream& os, card c)
|
||||
{
|
||||
return os << c.pip() << " of " << c.suite();
|
||||
}
|
||||
|
||||
class deck
|
||||
{
|
||||
public:
|
||||
// default constructor: construct a default-ordered deck
|
||||
deck()
|
||||
{
|
||||
for (int i = 0; i < 52; ++i)
|
||||
cards.push_back(card(i));
|
||||
}
|
||||
|
||||
// shuffle the deck
|
||||
void shuffle() { std::random_shuffle(cards.begin(), cards.end()); }
|
||||
|
||||
// deal a card from the top
|
||||
card deal() { card c = cards.front(); cards.pop_front(); return c; }
|
||||
|
||||
// iterators (only reading access is allowed)
|
||||
typedef std::deque<card>::const_iterator const_iterator;
|
||||
const_iterator begin() const { return cards.begin(); }
|
||||
const_iterator end() const { return cards.end(); }
|
||||
private:
|
||||
// the cards
|
||||
std::deque<card> cards;
|
||||
};
|
||||
|
||||
// output the deck
|
||||
inline std::ostream& operator<<(std::ostream& os, deck const& d)
|
||||
{
|
||||
std::copy(d.begin(), d.end(), std::ostream_iterator<card>(os, "\n"));
|
||||
return os;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
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.
|
||||
|
|
@ -1,21 +1,12 @@
|
|||
(defrecord Card [pip suit]
|
||||
Object
|
||||
(toString [this] (str pip " of " suit)))
|
||||
(def suits [:club :diamond :heart :spade])
|
||||
(def pips [:ace 2 3 4 5 6 7 8 9 10 :jack :queen :king])
|
||||
|
||||
(defprotocol pDeck
|
||||
(deal [this n])
|
||||
(shuffle [this])
|
||||
(newDeck [this])
|
||||
(print [this]))
|
||||
(defn deck [] (for [s suits p pips] [s p]))
|
||||
|
||||
(deftype Deck [cards]
|
||||
pDeck
|
||||
(deal [this n] [(take n cards) (Deck. (drop n cards))])
|
||||
(shuffle [this] (Deck. (shuffle cards)))
|
||||
(newDeck [this] (Deck. (for [suit ["Clubs" "Hearts" "Spades" "Diamonds"]
|
||||
pip ["2" "3" "4" "5" "6" "7" "8" "9" "10" "Jack" "Queen" "King" "Ace"]]
|
||||
(Card. pip suit))))
|
||||
(print [this] (dorun (map (comp println str) cards)) this))
|
||||
|
||||
(defn new-deck []
|
||||
(.newDeck (Deck. nil)))
|
||||
(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)))))
|
||||
|
|
|
|||
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
|
||||
|
|
@ -10,7 +10,7 @@ class Card {
|
|||
|
||||
class Deck {
|
||||
has Card @.cards = pick *,
|
||||
map { Card.new(:$^pip, :$^suit) }, (Pip.pick(*) X Suit.pick(*));
|
||||
map { Card.new(:$^pip, :$^suit) }, flat (Pip.pick(*) X Suit.pick(*));
|
||||
|
||||
method shuffle { @!cards .= pick: * }
|
||||
|
||||
|
|
|
|||
|
|
@ -1,36 +1,36 @@
|
|||
/*REXX pgm shows a method to build/shuffle/deal a standard 52─card deck.*/
|
||||
box = build(); say ' box of cards:' box /*a new box of 52─cards.*/
|
||||
deck=shuffle(); say 'shuffled deck:' deck /*randomly shuffled deck*/
|
||||
call deal 5, 4 /* ◄═════════════════════════════════ 5 cards, 4 hands*/
|
||||
/*REXX program demonstrates a method to build/shuffle/deal a standard 52─card deck. */
|
||||
box = build(); say ' box of cards:' box /*a brand new standard box of 52 cards.*/
|
||||
deck=shuffle(); say 'shuffled deck:' deck /*obtain a randomly shuffled deck. */
|
||||
call deal 5, 4 /* ◄═════════════════════════════════════════════════ 5 cards, 4 hands*/
|
||||
say; say; say right('[north]' hand.1,50)
|
||||
say; say '[west]' hand.4 right('[east]' hand.2,60)
|
||||
say; say right('[south]' hand.3,50)
|
||||
say; say; say; say 'remainder of deck: ' deck
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────BUILD subroutine────────────────────*/
|
||||
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. */
|
||||
#ranks=words(ranks); do s=1 for words(suits); @=word(suits,s)
|
||||
do r=1 for #ranks
|
||||
_=_ word(ranks,r)@
|
||||
end /*s*/
|
||||
end /*r*/
|
||||
return _
|
||||
/*──────────────────────────────────SHUFFLE subroutine──────────────────*/
|
||||
shuffle: y=; _=box; #cards=words(_) /*define REXX vars.*/
|
||||
do shuffler=1 for #cards /*shuffle all the cards in deck. */
|
||||
?=random(1,#cards+1-shuffler) /*each shuffle, random# decreases*/
|
||||
y=y word(_, ?) /*shuffled deck, 1 card at─a─time*/
|
||||
_=delword(_, ?, 1) /*delete the just─chosen card. */
|
||||
end /*shuffler*/
|
||||
return y
|
||||
/*──────────────────────────────────DEAL subroutine─────────────────────*/
|
||||
deal: parse arg #cards, hands; hand.=
|
||||
do #cards /*deal the hand to the players. */
|
||||
do player=1 for hands /*deal some cards to the players.*/
|
||||
hand.player=hand.player word(deck, 1) /*deal top card.*/
|
||||
deck=subword(deck, 2) /*diminish deck, remove one card.*/
|
||||
end /*player*/
|
||||
end /*#cards*/
|
||||
return
|
||||
exit /*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. */
|
||||
#ranks=words(ranks); do s=1 for words(suits); @= word(suits, s)
|
||||
do r=1 for #ranks; _=_ word(ranks, r)@
|
||||
end /*s*/
|
||||
end /*r*/
|
||||
|
||||
return _ /*this build skips the jokers. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
shuffle: y=; _=box; #cards=words(_) /*define three REXX variables. */
|
||||
do shuffler=1 for #cards /*shuffle all the cards in deck. */
|
||||
?=random(1, #cards + 1 - shuffler) /*each shuffle, random# decreases*/
|
||||
y=y word(_, ?) /*shuffled deck, 1 card at─a─time*/
|
||||
_=delword(_, ?, 1) /*delete the just─chosen card. */
|
||||
end /*shuffler*/
|
||||
return y
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
deal: parse arg #cards, hands; hand.=
|
||||
do #cards /*deal the hand to the players. */
|
||||
do player=1 for hands /*deal some cards to the players.*/
|
||||
hand.player=hand.player word(deck, 1) /*deal the top card.*/
|
||||
deck=subword(deck, 2) /*diminish deck, remove one card.*/
|
||||
end /*player*/
|
||||
end /*#cards*/
|
||||
return
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ class Deck
|
|||
end
|
||||
|
||||
def to_s
|
||||
"[#{@deck.join(", ")}]"
|
||||
@deck.inspect
|
||||
end
|
||||
|
||||
def shuffle!
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue