Skip to content

Instantly share code, notes, and snippets.

@jcbooth
Created July 26, 2010 23:11
Show Gist options
  • Save jcbooth/491423 to your computer and use it in GitHub Desktop.
Save jcbooth/491423 to your computer and use it in GitHub Desktop.
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))
return
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