September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
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] *)
|
||||
|
||||
(* ****** ****** *)
|
||||
|
|
@ -1,30 +0,0 @@
|
|||
Loop, 52
|
||||
{
|
||||
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
|
||||
|
|
@ -1,44 +0,0 @@
|
|||
Deck:=new Deck
|
||||
Deck.Shuffle()
|
||||
msgbox % Deck.Show()
|
||||
loop, 2
|
||||
msgbox % Deck.Deal().Show()
|
||||
msgbox % Deck.Show()
|
||||
Return ;-------------------------------------------------------------------
|
||||
|
||||
class Card
|
||||
{
|
||||
__New(Pip, Suit) {
|
||||
this.Pip:=Pip, this.Suit:=Suit
|
||||
}
|
||||
Show() {
|
||||
Return this.Pip . this.Suit
|
||||
}
|
||||
}
|
||||
|
||||
class Deck
|
||||
{
|
||||
Suits:={1:"♣",2:"♦",3:"♠",4:"♥"}
|
||||
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"}
|
||||
|
||||
__New() {
|
||||
this.Deck:=[]
|
||||
For i, Pip in this.Pips
|
||||
For j, Suit in this.Suits
|
||||
this.Deck.Insert(new Card(Pip,Suit))
|
||||
}
|
||||
Shuffle() { ; Knuth Shuffle from http://rosettacode.org/wiki/Knuth_Shuffle#AutoHotkey
|
||||
Loop % this.Deck.MaxIndex()-1 {
|
||||
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
|
||||
}
|
||||
}
|
||||
Deal() {
|
||||
Return this.Deck.Remove() ; to deal from bottom, use Remove(1)
|
||||
}
|
||||
Show() {
|
||||
For i, Card in this.Deck
|
||||
s .= Card.Show() " "
|
||||
Return s
|
||||
}
|
||||
}
|
||||
51
Task/Playing-cards/Kotlin/playing-cards.kotlin
Normal file
51
Task/Playing-cards/Kotlin/playing-cards.kotlin
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
// version 1.1.2
|
||||
|
||||
import java.util.Random
|
||||
|
||||
const val FACES = "23456789tjqka"
|
||||
const val SUITS = "shdc"
|
||||
|
||||
val r = Random()
|
||||
|
||||
fun createDeck(): List<String> {
|
||||
val cards = mutableListOf<String>()
|
||||
for (suit in SUITS) {
|
||||
for (face in FACES) cards.add("$face$suit")
|
||||
}
|
||||
return cards
|
||||
}
|
||||
|
||||
fun shuffleDeck(deck: List<String>): List<String> {
|
||||
val shuffled = mutableListOf<String>()
|
||||
do {
|
||||
val card = deck[r.nextInt(52)]
|
||||
if (card !in shuffled) shuffled.add(card)
|
||||
} while (shuffled.size < 52)
|
||||
return shuffled
|
||||
}
|
||||
|
||||
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 0 until deck.size) {
|
||||
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 = shuffleDeck(deck)
|
||||
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)
|
||||
}
|
||||
|
|
@ -2,19 +2,13 @@ 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))
|
||||
|
||||
(* Fisher-Yates shuffle *)
|
||||
let shuffle deck =
|
||||
for i = Array.length deck - 1 downto 1 do
|
||||
let j = Random.int (i+1) in
|
||||
(* swap deck.(i) and deck.(j) *)
|
||||
let temp = deck.(i) in
|
||||
deck.(i) <- deck.(j);
|
||||
deck.(j) <- temp
|
||||
|
|
|
|||
74
Task/Playing-cards/Phix/playing-cards.phix
Normal file
74
Task/Playing-cards/Phix/playing-cards.phix
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
--
|
||||
-- demo\rosetta\Playing_cards.exw
|
||||
--
|
||||
function deal(sequence deck, integer nhands, integer ncards)
|
||||
sequence hands = repeat({},nhands)
|
||||
for n=1 to ncards do
|
||||
for h=1 to nhands do
|
||||
hands[h] &= deck[1]
|
||||
deck = deck[2..$]
|
||||
end for
|
||||
end for
|
||||
return {deck,hands}
|
||||
end function
|
||||
|
||||
--console:
|
||||
procedure show_cards(sequence s)
|
||||
for i=1 to length(s) do
|
||||
integer c = s[i]-1
|
||||
string sep = iff(mod(i,13)=0 or i=length(s)?"\n":" ")
|
||||
puts(1,"23456789TJQKA"[mod(c,13)+1]&"SHDC"[floor(c/13)+1]&sep)
|
||||
end for
|
||||
end procedure
|
||||
|
||||
sequence deck, hands
|
||||
|
||||
procedure console_show()
|
||||
for i=1 to length(hands) do
|
||||
printf(1,"hand%d:\n",{i})
|
||||
show_cards(sort(hands[i]))
|
||||
end for
|
||||
printf(1,"remaining cards(%d):\n",{length(deck)})
|
||||
show_cards(deck)
|
||||
end procedure
|
||||
|
||||
--GUI:
|
||||
function cards_to_utf8(sequence s)
|
||||
sequence utf32 = {}
|
||||
for i=1 to length(s) do
|
||||
integer c = s[i]
|
||||
integer pip = mod(c,13)
|
||||
utf32 &= 0x1F0A1 + pip+(pip>10) + floor((c-1)/13)*#10
|
||||
end for
|
||||
return utf32_to_utf8(utf32)
|
||||
end function
|
||||
|
||||
include pGUI.e
|
||||
|
||||
constant FONT = sprintf("FONT=\"Arial, %d\"",{92})
|
||||
|
||||
procedure gui_show()
|
||||
IupOpen()
|
||||
IupSetGlobal("UTF8MODE","YES")
|
||||
|
||||
Ihandles lh = {}
|
||||
for i=1 to length(hands) do
|
||||
Ihandle l = IupLabel(sprintf("hand%d:",{i}))
|
||||
Ihandle h = IupLabel(cards_to_utf8(sort(hands[i])),FONT)
|
||||
lh &= l&h
|
||||
end for
|
||||
lh &= IupLabel("remaining cards:")
|
||||
lh &= IupLabel(cards_to_utf8(deck),FONT)
|
||||
|
||||
Ihandle dlg = IupDialog(IupVbox(lh))
|
||||
IupShow(dlg)
|
||||
IupMainLoop()
|
||||
IupClose()
|
||||
end procedure
|
||||
|
||||
constant DECKSIZE=52
|
||||
deck = shuffle(tagset(DECKSIZE))
|
||||
show_cards(deck)
|
||||
{deck,hands} = deal(deck,4,7)
|
||||
console_show()
|
||||
gui_show()
|
||||
25
Task/Playing-cards/Zkl/playing-cards-1.zkl
Normal file
25
Task/Playing-cards/Zkl/playing-cards-1.zkl
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
const Diamonds=1, Spades=3, Clubs=0, Hearts=2, Ace=1; // informational
|
||||
var suits=T(0x1F0D1,0x1F0C1,0x1F0B1,0x1F0A1); //unicode 🃑,🃁,🂱,🂡
|
||||
|
||||
class Card{
|
||||
fcn init(pip,suit){ // or 0..51
|
||||
reg p,s;
|
||||
if(vm.numArgs==1){ s=pip/13; p=pip%13; } else { p=pip; s=suit }
|
||||
var [const] _pip=p, _suit=s;
|
||||
}
|
||||
fcn toString{
|
||||
p:=_pip + (_pip>=11);
|
||||
(suits[_suit]+p).toString(8); // int-->UTF-8
|
||||
}
|
||||
}
|
||||
|
||||
class Deck{ //--> 52 shuffled Cards
|
||||
var [const] deck=L();
|
||||
fcn init{
|
||||
(0).pump(52,deck.clear().write,Card);
|
||||
shuffle();
|
||||
}
|
||||
fcn shuffle{ deck.shuffle() }
|
||||
fcn deal(cards=5){ deck.pop(0,cards); }
|
||||
fcn toString{ deck.pump(String,"toString"); }
|
||||
}
|
||||
4
Task/Playing-cards/Zkl/playing-cards-2.zkl
Normal file
4
Task/Playing-cards/Zkl/playing-cards-2.zkl
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
d:=Deck();
|
||||
d.println(d.deck.len());
|
||||
d.deal().println();
|
||||
d.println();
|
||||
Loading…
Add table
Add a link
Reference in a new issue