Skip to content

Commit 6be495c

Browse files
committed
简化查表法lua代码
1 parent f9a99cb commit 6be495c

File tree

3 files changed

+45
-136
lines changed

3 files changed

+45
-136
lines changed

mjlib_lua/base_table/hulib.lua

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,7 @@
11
local table_mgr = require "table_mgr"
22
local M = {}
33

4-
function M.check_7dui(hand_cards, waves)
5-
if #waves > 0 then return false end
6-
7-
for _,c in ipairs(hand_cards) do
8-
if c % 2 ~= 0 then
9-
return false
10-
end
11-
end
12-
13-
return true
14-
end
15-
16-
function M.check_pengpeng()
17-
18-
end
19-
20-
function M.get_hu_info(hand_cards, waves, cur_card)
4+
function M.get_hu_info(hand_cards, cur_card)
215
local cards = {}
226
for i,v in ipairs(hand_cards) do
237
cards[i] = v
@@ -32,7 +16,7 @@ function M.get_hu_info(hand_cards, waves, cur_card)
3216
}
3317

3418
-- 字牌(东西南北中发白)
35-
if not M.check_color(cards, 28, 34, first_info) then
19+
if not M.check_color(cards, first_info) then
3620
return false
3721
--
3822
elseif not M.check_color_chi(cards, 1, 9, first_info) then
@@ -48,7 +32,7 @@ function M.get_hu_info(hand_cards, waves, cur_card)
4832
end
4933

5034
function M.check_color(cards, min, max, info)
51-
for i = min, max do
35+
for i = 28, 34 do
5236
local count = cards[i]
5337

5438
if count == 1 or count == 4 then
@@ -73,7 +57,9 @@ function M.check_color_chi(cards, min, max, info)
7357
local c = cards[i]
7458
if c > 0 then
7559
key = key * 10 + c
76-
elseif key > 0 then
60+
end
61+
62+
if key > 0 and (c == 0 or i == max) then
7763
local eye = (key%3 == 2)
7864
if info.eye and eye then
7965
return false

mjlib_lua/gui_table/hulib.lua

Lines changed: 38 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,8 @@
1-
local mjlib = require "mjlib"
21
local table_mgr = require "table_mgr"
32

4-
local split_table = {
5-
{min = 1, max = 9, chi = true},
6-
{min = 10, max = 18, chi = true},
7-
{min = 19, max = 27, chi = true},
8-
{min = 28, max = 34, chi = false}
9-
}
10-
113
local M = {}
12-
function M.check_7dui(hand_cards, waves)
13-
if #waves > 0 then return false end
14-
15-
for _,c in ipairs(hand_cards) do
16-
if c % 2 ~= 0 then
17-
return false
18-
end
19-
end
20-
21-
return true
22-
end
23-
24-
function M.check_pengpeng()
254

26-
end
27-
28-
function M.get_hu_info(cards, waves, gui_index)
5+
function M.get_hu_info(cards, gui_index)
296
local hand_cards = {}
307
for i,v in ipairs(cards) do
318
hand_cards[i] = v
@@ -37,107 +14,53 @@ function M.get_hu_info(cards, waves, gui_index)
3714
hand_cards[gui_index] = 0
3815
end
3916

40-
local splited_tbl = M.split_info(hand_cards, gui_num)
41-
if not splited_tbl then
42-
return false
43-
end
17+
return M.check(hand_cards, gui_num)
18+
end
4419

45-
return M.check_probability(splited_tbl, gui_num)
20+
function M.check(cards, gui_num)
21+
local total_need_gui = 0
22+
local eye_num = 0
23+
for i=0,3 do
24+
local from = i*9 + 1
25+
local to = from + 8
26+
if i == 3 then
27+
to = from + 6
28+
end
29+
30+
local need_gui, eye = M.get_need_gui(cards, from, to, i==3, gui_num)
31+
if need_gui then
32+
total_need_gui = total_need_gui + need_gui
33+
end
34+
if eye then
35+
eye_num = eye_num + 1
36+
end
37+
end
38+
39+
if eye_num == 0 then
40+
return total_need_gui + 2 <= gui_num
41+
elseif eye_num == 1 then
42+
return total_need_gui <= gui_num
43+
elseif
44+
return total_need_gui + eye_num - 1 <= gui_num
45+
end
4646
end
4747

48-
function M.list_probability(gui_num, num, key, chi)
49-
local find = false
50-
local t = {}
48+
function M.get_need_gui(cards, from, to, chi, gui_num)
49+
local num = 0
50+
local key = 0
51+
for i=from,to do
52+
key = key * 10 + cards[i]
53+
end
54+
5155
for i=0, gui_num do
5256
local yu = (num + i)%3
5357
if yu ~= 1 then
5458
local eye = (yu == 2)
55-
if find or table_mgr:check(key, i, eye, chi) then
56-
table.insert(t, {eye = eye, gui_num = i})
57-
find = true
58-
end
59-
end
60-
end
61-
62-
return t
63-
end
64-
65-
-- 根据花色切分
66-
function M.split_info(t, gui_num)
67-
local ret = {}
68-
for _,v in ipairs(split_table) do
69-
local key = 0
70-
local num = 0
71-
for i=v.min,v.max do
72-
key = key*10 + t[i]
73-
num = num + t[i]
74-
end
75-
if num > 0 then
76-
local t = M.list_probability(gui_num, num, key, v.chi)
77-
if #t == 0 then
78-
return false
79-
end
80-
table.insert(ret, t)
81-
end
82-
end
83-
return ret
84-
end
85-
86-
function M.check_probability_sub(splited_table, eye, gui_num, level, max_level)
87-
for _,v in ipairs(splited_table[level]) do
88-
repeat
89-
if eye and v.eye then
90-
break
91-
end
92-
93-
if gui_num < v.gui_num then
94-
break
95-
end
96-
97-
if level < max_level then
98-
if M.check_probability_sub(
99-
splited_table,
100-
eye or v.eye,
101-
gui_num - v.gui_num,
102-
level + 1,
103-
max_level) then
104-
return true
105-
end
106-
break
107-
end
108-
109-
if not eye and not v.eye and gui_num < 2 + v.gui_num then
110-
break
59+
if table_mgr:check(key, i, eye, chi) then
60+
return i, eye
11161
end
112-
113-
return true
114-
until(true)
115-
end
116-
117-
return false
118-
end
119-
120-
function M.check_probability(splited_table, gui_num)
121-
local c = #splited_table
122-
-- 全是鬼牌
123-
if c == 0 then
124-
return true
125-
end
126-
127-
-- 只有一种花色的牌和鬼牌
128-
if c == 1 then
129-
return true
130-
end
131-
132-
-- 组合花色间的组合,如果能满足组合条件,则胡
133-
for i,v in ipairs(splited_table[1]) do
134-
local ret = M.check_probability_sub(splited_table, v.eye, gui_num - v.gui_num, 2, c)
135-
if ret then
136-
return true
13762
end
13863
end
139-
140-
return false
14164
end
14265

14366
return M

mjlib_lua/gui_table/test.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ local function test_one()
99
0,0,0, 3,0,0, 0,0,0,
1010
0,0,0, 2,2,2, 0,0,0,
1111
0,0,0,0, 0,2,0}
12-
if not hulib.get_hu_info(t, nil, 0) then
12+
if not hulib.get_hu_info(t, 0) then
1313
print("测试失败")
1414
end
1515
end

0 commit comments

Comments
 (0)