# DEAL # # Shuffle and deal bridge hands # # Ralph E. Griswold # # Last modified 7/10/83 # global deckimage procedure main(x) local deck, handsize, Table, hands, bar, i, s deck := deckimage := &lcase || &ucase bar := repl("-",33) handsize := *deck / 4 hands := 1 i := 0 while i < *x do { s := x[i +:= 1] | break case s of { "-h": hands := integer(x[i +:= 1]) | stop("usage: deal [-h n] [-s n]") "-s": &random := integer(x[i +:= 1]) | stop("usage: deal [-h n] [-s n]") default: stop("usage: deal [-h n] [-s n]") } } write(bar,"\n") every 1 to hands do { deck := shuffle(deck) Table := [ show(deck[1+:handsize]), show(deck[handsize + 1+:handsize]), show(deck[2 * handsize + 1+:handsize]), show(deck[3 * handsize + 1+:handsize]) ] every write(repl(" ",10),!Table[1]) write() every i := 1 to 4 do write(left(Table[4][i],20),Table[2][i]) write() every write(repl(" ",10),!Table[3]) write("\n",bar,"\n") } end # shuffle deck # procedure shuffle(deck) local m every m := *deck to 2 by -1 do deck[?m] :=: deck[m] return deck end # display the hands # procedure show(hand) static clubs, diamonds, hearts, spades, denom, blanker, suitsize initial { suitsize := *deckimage / 4 blanker := repl(" ",suitsize) denom := &lcase[1+:suitsize] clubs := denom || repl(blanker,3) diamonds := blanker || denom || repl(blanker,2) hearts := repl(blanker,2) || denom || blanker spades := repl(blanker,3) || denom } # if a hand consists of all 13 cards of one # suit, that suit will be misformatted slightly return [ "S:" || arrange(hand,spades), "H:" || arrange(hand,hearts), "D:" || arrange(hand,diamonds), "C:" || arrange(hand,clubs) ] write("\n") end # arrange hands for presentation # procedure arrange(hand,suit) static denom, rank, suitsize initial { suitsize := *deckimage / 4 denom := &lcase[1+:suitsize] rank := "AKQJT98765432" } return trim(map(cset(map(hand,deckimage,suit)),denom,rank)) end