Created
July 26, 2010 23:55
-
-
Save jcbooth/491477 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xquery version "1.0-ml"; | |
declare variable $deck-counter := map:map(); | |
declare variable $deck := map:map(); | |
declare function local:get-card($num) { | |
let $card := map:get($deck-counter, xs:string($num)) | |
let $_ := xdmp:log(fn:concat("card: ", $card, " $num: ", $num), "debug") | |
return | |
(: call recursively with another rand number, if the | |
card’s been dealt already :) | |
if (fn:empty($card)) then | |
local:get-card(xdmp:random(52)) | |
else ($card, map:delete($deck-counter, xs:string($card))) | |
}; | |
let $suit := ("diamonds", "spades", "clubs", "hearts") let $counter := 0 | |
let $count := | |
for $suit-count at $p in (1 to 4) | |
for $card-num in (1 to 13) | |
let $_ := xdmp:set($counter, ($counter + 1)) let $card := | |
if($card-num eq 1) then | |
fn:concat("ace of ", $suit[$p]) | |
else if ($card-num eq 11) then | |
fn:concat("jack of ", $suit[$p]) | |
else if ($card-num eq 12) then | |
fn:concat("queen of ", $suit[$p]) | |
else if ($card-num eq 13) then | |
fn:concat("king of ", $suit[$p]) | |
else fn:concat($card-num, " of ", $suit[$p]) | |
return map:put($deck, xs:string($counter), $card) | |
let $_load_deck := | |
for $i in (1 to 52) | |
return map:put($deck-counter, xs:string($i), $i) | |
let $cards-to-deal := 2 | |
let $max-players := 3 | |
for $card in (1 to $cards-to-deal) | |
for $player in (1 to $max-players) | |
let $card := | |
local:get-card(xdmp:random(52)) | |
return (fn:concat("player: ", $player, " card: ", map:get($deck, xs:string($card)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment