1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
############################################################################
#
# Name: deal.icn
#
# Title: Deal bridge hands
#
# Author: Ralph E. Griswold
#
# Date: June 10, 1988
#
############################################################################
#
# This program shuffles, deals, and displays hands in the game
# of bridge. An example of the output of deal is
# ---------------------------------
#
# S: KQ987
# H: 52
# D: T94
# C: T82
#
# S: 3 S: JT4
# H: T7 H: J9863
# D: AKQ762 D: J85
# C: QJ94 C: K7
#
# S: A652
# H: AKQ4
# D: 3
# C: A653
#
# ---------------------------------
#
# Options: The following options are available:
#
# -h n Produce n hands. The default is 1.
#
# -s n Set the seed for random generation to n. Different
# seeds give different hands. The default seed is 0.
#
############################################################################
#
# Links: options, post, shuffle
#
############################################################################
link options, post, shuffle
global deck, deckimage, handsize, suitsize, denom, rank, blanker
procedure main(args)
local hands, opts
Init__("deal")
deck := deckimage := string(&letters) # initialize global variables
handsize := suitsize := *deck / 4
rank := "AKQJT98765432"
blanker := repl(" ",suitsize)
denom := &lcase[1+:suitsize]
opts := options(args,"h+s+")
hands := \opts["h"] | 1
&random := \opts["s"]
every 1 to hands do
display()
Term__()
end
# Display the hands
#
procedure display()
local layout, i
static bar, offset
initial {
bar := "\n" || repl("-",33)
offset := repl(" ",10)
}
deck := shuffle(deck)
layout := []
every push(layout,show(deck[(0 to 3) * handsize + 1 +: handsize]))
write()
every write(offset,!layout[1])
write()
every i := 1 to 4 do
write(left(layout[4][i],20),layout[2][i])
write()
every write(offset,!layout[3])
write(bar)
end
# Put the hands in a form to display
#
procedure show(hand)
static clubmap, diamondmap, heartmap, spademap
initial {
clubmap := denom || repl(blanker,3)
diamondmap := blanker || denom || repl(blanker,2)
heartmap := repl(blanker,2) || denom || blanker
spademap := repl(blanker,3) || denom
}
return [
"S: " || arrange(hand,spademap),
"H: " || arrange(hand,heartmap),
"D: " || arrange(hand,diamondmap),
"C: " || arrange(hand,clubmap)
]
end
# Arrange hands for presentation
#
procedure arrange(hand,suit)
return map(map(hand,deckimage,suit) -- ' ',denom,rank)
end
|