-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhand.lua
145 lines (124 loc) · 4.03 KB
/
hand.lua
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
local tu = require("typist.lib.tblutils")
local hu = require("typist.handutils")
local M = {}
M.left5 = function()
local cards = {}
for i, card in ipairs(G.hand.cards) do
if i > 5 then break end
table.insert(cards, card)
end
hu.highlight_hand(cards)
end
M.right5 = function()
local cards = {}
for i, card in tu.reversed_ipairs(G.hand.cards) do
if i > 5 then break end
table.insert(cards, card)
end
hu.highlight_hand(cards)
end
-- given a hand rank (jack=11, queen=12, king=13, ace=14)
-- select all cards in your hand with that rank
M.by_rank = function(rank)
local cards = {}
for _, card in pairs(G.hand.cards) do
if hu.get_visible_rank(card) == rank and not tu.contains(G.hand.highlighted, card) then
table.insert(cards, card)
end
end
hu.highlight_hand(cards)
end
M.invert_selection = function()
local unselected = tu.list_diff(G.hand.cards, G.hand.highlighted)
table.sort(unselected, function(x, y)
local action = hu.Action.select_discard_if_possible()
return hu.card_importance(x, action) < hu.card_importance(y, action)
end)
hu.highlight_hand(tu.list_take(unselected, 5))
end
M.best_high_card = function()
local best_card, _ = hu.high_card()
hu.highlight_hand(hu.top_up_with_stones { best_card })
end
M.worst_high_card = function()
local _, worst_card = hu.high_card()
hu.highlight_hand(hu.top_up_with_stones { worst_card })
end
M.best_flush_suit = function()
local best_score = -math.huge
local best_suit
local smeared_joker = next(find_joker("Smeared Joker"))
local four_fingers = next(find_joker("Four Fingers"))
local cards_by_suit = (
smeared_joker
and {
Hearts = hu.select_cards_of_suit("Hearts", "Diamonds"),
Clubs = hu.select_cards_of_suit("Spades", "Clubs"),
}
)
or {
Hearts = hu.select_cards_of_suit("Hearts"),
Clubs = hu.select_cards_of_suit("Clubs"),
Diamonds = hu.select_cards_of_suit("Diamonds"),
Spades = hu.select_cards_of_suit("Spades"),
}
for suit, sorted_flush in pairs(cards_by_suit) do
local base_score = 1000 * #sorted_flush
local hands_to_check = {}
if #sorted_flush >= 5 then table.insert(hands_to_check, tu.list_take(sorted_flush, 5)) end
if four_fingers and #sorted_flush >= 4 then
table.insert(hands_to_check, tu.list_take(sorted_flush, 4))
end
if #hands_to_check == 0 then
table.insert(sorted_flush, { flipped = true })
table.insert(hands_to_check, sorted_flush)
end
for _, hand in pairs(hands_to_check) do
local score = base_score + hu.hand_importance(hand)
if score > best_score then
best_score = score
best_suit = suit
end
end
end
return assert(best_suit, "there are no cards in your hand, you're so cooked")
end
M.flush = function(target_suit)
local four_fingers = next(find_joker("Four Fingers"))
local smeared_joker = next(find_joker("Smeared Joker"))
local cards = {}
if smeared_joker then
if target_suit == "Spades" or target_suit == "Clubs" then
cards = hu.select_cards_of_suit("Spades", "Clubs")
end
if target_suit == "Hearts" or target_suit == "Diamonds" then
cards = hu.select_cards_of_suit("Hearts", "Diamonds")
end
else
cards = hu.select_cards_of_suit(target_suit)
end
if #cards >= 5 then
cards = tu.list_take(cards, 5)
elseif four_fingers and #cards >= 4 then
cards = tu.list_take(cards, 4)
else
cards = {}
for _, card in tu.reversed_ipairs(G.hand.cards) do
local suit = hu.get_visible_suit(card)
if not (suit == target_suit or suit == hu.SuitNullReason.WILD) then
table.insert(cards, card)
end
end
table.sort(cards, function(x, y)
local action = hu.Action.select_discard_if_possible()
return hu.card_importance(x, action) > hu.card_importance(y, action)
end)
cards = tu.list_take(cards, 5)
end
hu.highlight_hand(cards)
end
M.best_hand = function()
local possible_hands = hu.ranked_hands(G.hand.cards)
hu.highlight_hand(hu.next_best_hand(possible_hands, G.hand.highlighted))
end
return M