Playing cards: Difference between revisions
Content deleted Content added
Added Elixir |
|||
Line 165: | Line 165: | ||
<pre>((King OF Clubs), (6 OF Hearts), (7 OF Diamonds), (Ace OF Hearts), (9 OF Spades), (10 OF Clubs), (Ace OF Spades), (8 OF Clubs), (4 OF Spades), (8 OF Hearts), (Jack OF Hearts), (3 OF Clubs), (7 OF Hearts), (10 OF Hearts), (Jack OF Clubs), (Ace OF Clubs), (King OF Spades), (9 OF Clubs), (7 OF Spades), (5 OF Spades), (7 OF Clubs), (Queen OF Clubs), (9 OF Diamonds), (2 OF Spades), (6 OF Diamonds), (Ace OF Diamonds), (Queen OF Diamonds), (5 OF Hearts), (4 OF Clubs), (5 OF Clubs), (4 OF Hearts), (3 OF Diamonds), (4 OF Diamonds), (3 OF Hearts), (King OF Diamonds), (2 OF Clubs), (Jack OF Spades), (2 OF Diamonds), (5 OF Diamonds), (Queen OF Spades), (10 OF Diamonds), (King OF Hearts), (Jack OF Diamonds), (Queen OF Hearts), (8 OF Spades), (2 OF Hearts), (8 OF Diamonds), (10 OF Spades), (9 OF Hearts), (6 OF Clubs), (3 OF Spades), (6 OF Spades))</pre> |
<pre>((King OF Clubs), (6 OF Hearts), (7 OF Diamonds), (Ace OF Hearts), (9 OF Spades), (10 OF Clubs), (Ace OF Spades), (8 OF Clubs), (4 OF Spades), (8 OF Hearts), (Jack OF Hearts), (3 OF Clubs), (7 OF Hearts), (10 OF Hearts), (Jack OF Clubs), (Ace OF Clubs), (King OF Spades), (9 OF Clubs), (7 OF Spades), (5 OF Spades), (7 OF Clubs), (Queen OF Clubs), (9 OF Diamonds), (2 OF Spades), (6 OF Diamonds), (Ace OF Diamonds), (Queen OF Diamonds), (5 OF Hearts), (4 OF Clubs), (5 OF Clubs), (4 OF Hearts), (3 OF Diamonds), (4 OF Diamonds), (3 OF Hearts), (King OF Diamonds), (2 OF Clubs), (Jack OF Spades), (2 OF Diamonds), (5 OF Diamonds), (Queen OF Spades), (10 OF Diamonds), (King OF Hearts), (Jack OF Diamonds), (Queen OF Hearts), (8 OF Spades), (2 OF Hearts), (8 OF Diamonds), (10 OF Spades), (9 OF Hearts), (6 OF Clubs), (3 OF Spades), (6 OF Spades))</pre> |
||
=={{header| |
=={{header|Batch File}}== |
||
The data structure is a simple string with 3 characters per card, one char for the suit, a char for the pip (T) means ten and a last char not printed that would allow to sort a hand. |
|||
===Pseudo-arrays=== |
|||
<lang |
<lang Batch File> |
||
@echo off |
|||
{ |
|||
setlocal enabledelayedexpansion |
|||
Random, card%A_Index%, 1, 52 |
|||
While card%A_Index% |
|||
Random, card%A_Index%, 1, 52 |
|||
card%A_Index% := Mod(card%A_Index%, 12) . " of " . ((card%A_Index% <= 12) |
|||
? "diamonds" : ((card%A_Index%) <= 24) |
|||
? "hearts" : ((card%A_Index% <= 36) |
|||
? "clubs" |
|||
: "spades")) |
|||
allcards .= card%A_Index% . "`n" |
|||
} |
|||
currentcard = 1 |
|||
Gui, Add, Text, vcard w500 |
|||
Gui, Add, Button, w500 gNew, New Deck (Shuffle) |
|||
Gui, Add, Button, w500 gDeal, Deal Next Card |
|||
Gui, Add, Button, w500 gReveal, Reveal Entire Deck |
|||
Gui, Show,, Playing Cards |
|||
Return |
|||
New: |
|||
Reload |
|||
GuiClose: |
|||
ExitApp |
|||
Deal: |
|||
GuiControl,, card, % card%currentcard% |
|||
currentcard++ |
|||
Return |
|||
Reveal: |
|||
GuiControl,, card, % allcards |
|||
Return</lang> |
|||
===Classes=== |
|||
{{works with|AutoHotkey L}} |
|||
<lang ahk>Deck:=new Deck |
|||
Deck.Shuffle() |
|||
msgbox % Deck.Show() |
|||
loop, 2 |
|||
msgbox % Deck.Deal().Show() |
|||
msgbox % Deck.Show() |
|||
Return ;------------------------------------------------------------------- |
|||
call:newdeck deck |
|||
class Card |
|||
echo new deck: |
|||
{ |
|||
echo. |
|||
__New(Pip, Suit) { |
|||
call:showcards deck |
|||
this.Pip:=Pip, this.Suit:=Suit |
|||
echo. |
|||
} |
|||
echo shuffling: |
|||
Show() { |
|||
echo. |
|||
Return this.Pip . this.Suit |
|||
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 |
|||
class Deck |
|||
set "loc1=!%~1!" |
|||
{ |
|||
set "%~2=!%~2!!loc1:~0,3!" |
|||
Suits:={1:"♣",2:"♦",3:"♠",4:"♥"} |
|||
set "%~1=!loc1:~3!" |
|||
Pips:={13:"A",1:"2",2:"3",3:"4",4:"5",5:"6",6:"7",7:"8",8:"9",9:"T",10:"J",11:"Q",12:"K"} |
|||
exit /b |
|||
__New() { |
|||
:deal deck n player1 player2...up to 7 |
|||
this.Deck:=[] |
|||
set "loc=!%~1!" |
|||
For i, Pip in this.Pips |
|||
set "cards=%~2" |
|||
For j, Suit in this.Suits |
|||
set players=%3 %4 %5 %6 %7 %8 %9 |
|||
this.Deck.Insert(new Card(Pip,Suit)) |
|||
for /L %%j in (1,1,!cards!) do ( |
|||
} |
|||
for %%k in (!players!) do call:getcard loc %%k) |
|||
Shuffle() { ; Knuth Shuffle from http://rosettacode.org/wiki/Knuth_Shuffle#AutoHotkey |
|||
set "%~1=!loc!" |
|||
Loop % this.Deck.MaxIndex()-1 { |
|||
exit /b |
|||
Random, i, A_Index, this.Deck.MaxIndex() ; swap item 1,2... with a random item to the right of it |
|||
temp := this.Deck[i], this.Deck[i] := this.Deck[A_Index], this.Deck[A_Index] := temp |
|||
: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 ( |
|||
Deal() { |
|||
for %%j in (20 31 42 53 64 75 86 97 T8 J9 QA KB AC) do set loc=!loc!%%i%%j |
|||
Return this.Deck.Remove() ; to deal from bottom, use Remove(1) |
|||
) |
|||
} |
|||
set "%~1=!loc!" |
|||
Show() { |
|||
exit /b |
|||
For i, Card in this.Deck |
|||
s .= Card.Show() " " |
|||
:showcards [deck] :: prints a deck or a hand |
|||
Return s |
|||
set "loc=!%~1!" |
|||
} |
|||
for /L %%j in (0,39,117) do ( |
|||
}</lang> |
|||
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 |
|||
</lang> |
|||
{{Out}} |
|||
<pre> |
|||
new deck: |
|||
♠2 ♠3 ♠4 ♠5 ♠6 ♠7 ♠8 ♠9 ♠T ♠J ♠Q ♠K ♠A |
|||
♦2 ♦3 ♦4 ♦5 ♦6 ♦7 ♦8 ♦9 ♦T ♦J ♦Q ♦K ♦A |
|||
♥2 ♥3 ♥4 ♥5 ♥6 ♥7 ♥8 ♥9 ♥T ♥J ♥Q ♥K ♥A |
|||
♣2 ♣3 ♣4 ♣5 ♣6 ♣7 ♣8 ♣9 ♣T ♣J ♣Q ♣K ♣A |
|||
shuffling: |
|||
♥8 ♥7 ♦K ♠K ♥2 ♥5 ♥A ♠7 ♣8 ♠A ♦7 ♠9 ♠3 |
|||
♠J ♣J ♦9 ♠8 ♦A ♣Q ♦2 ♦5 ♣K ♥9 ♦4 ♣2 ♣6 |
|||
♦6 ♥Q ♦8 ♥6 ♣5 ♥T ♦3 ♠6 ♣7 ♠5 ♣T ♣3 ♠T |
|||
♠2 ♦Q ♠Q ♦T ♥3 ♥K ♦J ♥4 ♠4 ♣4 ♥J ♣A ♣9 |
|||
dealing 5 cards to 4 players |
|||
player 1 |
|||
♥8 ♥2 ♣8 ♠3 ♠8 |
|||
player 2 |
|||
♥7 ♥5 ♠A ♠J ♦A |
|||
player 3 |
|||
♦K ♥A ♦7 ♣J ♣Q |
|||
player 4 |
|||
♠K ♠7 ♠9 ♦9 ♦2 |
|||
32 cards remaining in the deck |
|||
♦5 ♣K ♥9 ♦4 ♣2 ♣6 ♦6 ♥Q ♦8 ♥6 ♣5 ♥T ♦3 |
|||
♠6 ♣7 ♠5 ♣T ♣3 ♠T ♠2 ♦Q ♠Q ♦T ♥3 ♥K ♦J |
|||
♥4 ♠4 ♣4 ♥J ♣A ♣9 |
|||
</pre> |
|||
=={{header|BASIC}}== |
=={{header|BASIC}}== |