about summary refs log tree commit diff stats
path: root/js/games/nluqo.github.io/~bh/v1ch13/poker.lg
blob: 17b642bac50462b4643f8f7dff634dc992165fc5 (plain) (blame)
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
to pokerhand :cards
local [ranks suits rankarray]
poker.init :cards
if fourp [print [four of a kind] stop]
if full.housep [print [full house] stop]
if threep [print [three of a kind] stop]
if pairp [print ifelse paircount = 1 [[one pair]] [[two pairs]] stop]
if ace.highp [print ifelse flushp [[royal flush]] [[straight]] stop]
if straightp [print ifelse flushp [[straight flush]] [[straight]] stop]
if flushp [print [flush] stop]
print [nothing!]
end

to poker.init :cards
make "ranks map [ranknum butlast ?] :cards
make "suits remdup map "last :cards
make "rankarray {0 0 0 0 0 0 0 0 0 0 0 0 0}
foreach :ranks [setitem ? :rankarray (item ? :rankarray)+1]
end

to ranknum :rank
if :rank = "a [output 1]
if :rank = "j [output 11]
if :rank = "q [output 12]
if :rank = "k [output 13]
output :rank
end

to fourp
output memberp 4 :rankarray
end

to threep
output memberp 3 :rankarray
end

to pairp
output memberp 2 :rankarray
end

to full.housep
output and threep pairp
end

to paircount
output count locate 2 1
end

to locate :number :index
if :index > 13 [output []]
if (item :index :rankarray) = :number ~
   [output fput :index (locate :number :index+1)]
output locate :number :index+1
end

to flushp
output emptyp butfirst :suits
end

to straightp
output nogap (reduce "min :ranks) 5
end

to min :a :b
output ifelse :a < :b [:a] [:b]
end

to nogap :smallest :howmany
if :howmany=0 [output "true]
if not equalp (item :smallest :rankarray) 1 [output "false]
output nogap :smallest+1 :howmany-1
end

to ace.highp
if not equalp (item 1 :rankarray) 1 [output "false]
output nogap 10 4
end