@@ -33,80 +33,48 @@ local simulateDrop=require'assets.ai.util'.simulateDrop
33
33
local lockPiece = require ' assets.ai.util' .lockPiece
34
34
local clearLine = require ' assets.ai.util' .clearLine
35
35
local getBoundCount = paperArtist .getBoundCount
36
- local directions = {' 0' ,' R' ,' F' ,' L' }
37
36
38
37
--- @param F Mat<boolean> Field (read )
39
38
--- @param shape Mat<boolean>
40
- --- @return number x , number y , string dir
41
- function paperArtist .findPosition (F ,shape )
42
- local best = {
43
- score =- 1e99 ,
44
- x = 4 ,y = 4 ,dir = ' 0' ,
45
- }
46
- local w = F [1 ] and # F [1 ] or 10
47
- for d = 1 ,4 do
48
- for cx = 1 ,w -# shape [1 ]+ 1 do
49
- local F1 = TABLE .copy (F ,1 )
50
- local cy ,colH = simulateDrop (F1 ,shape ,cx )
51
-
52
- lockPiece (F1 ,shape ,cx ,cy )
53
- local clear = clearLine (F1 ,cy ,cy +# shape - 1 )
54
- local score
55
- if # F1 == 0 then
56
- -- Nobody can refuse AC
57
- score = 1e99
58
- else
59
- local HBC ,VBC = getBoundCount (F1 )
60
- score = clear * 2 - HBC * 3 - VBC * 2 - cy
61
- end
62
-
63
- local minH = math.min (unpack (colH ))
64
- for i = 1 ,# colH do
65
- score = score - (colH [i ]- minH )* 1.26
66
- end
67
-
68
- if score > best .score then
69
- best .x ,best .y ,best .dir = cx ,cy ,directions [d ]
70
- best .score = score
71
- end
72
- end
73
- if d < 4 then shape = TABLE .rotate (shape ,' R' ) end
39
+ --- @param X number
40
+ function paperArtist .getSimScore (F ,shape ,X )
41
+ F = TABLE .copy (F ,1 )
42
+ local cy ,colH = simulateDrop (F ,shape ,X )
43
+ lockPiece (F ,shape ,X ,cy )
44
+ local linesClear = clearLine (F ,cy ,cy +# shape - 1 )
45
+ local score
46
+ if # F == 0 then
47
+ -- Nobody can refuse AC
48
+ score = 1e99
49
+ else
50
+ local HBC ,VBC = getBoundCount (F )
51
+ local minH = math.min (unpack (colH ))
52
+ local totalH = MATH .sum (colH )
53
+ local shadowCreated = totalH - minH *# colH
54
+ score = 2 * linesClear - cy - shadowCreated * 1.26 - HBC * 3 - VBC * 2
74
55
end
75
- return best . x , best . y , best . dir
56
+ return score , cy
76
57
end
77
58
59
+ local rotate = TABLE .rotate
60
+ local getSimScore = paperArtist .getSimScore
61
+ local directions = {' 0' ,' R' ,' F' ,' L' }
62
+
78
63
local floor = math.floor
79
64
local ins ,rem = table.insert ,table.remove
80
65
81
66
--- @param F Mat<boolean> Field (read )
82
67
--- @param shape Mat<boolean>
83
68
--- @return { x : number , y : number , dir : string } []
84
- function paperArtist .findBestPositions (F ,shape ,counts )
69
+ function paperArtist .search (F ,shape ,dirCount , bestCount )
85
70
local posList = {{
86
71
score =- 1e99 ,
87
72
x = 4 ,y = 4 ,dir = ' 0' ,
88
73
}}
89
74
local w = F [1 ] and # F [1 ] or 10
90
- for d = 1 ,4 do
75
+ for d = 1 ,dirCount do
91
76
for cx = 1 ,w -# shape [1 ]+ 1 do
92
- local F1 = TABLE .copy (F ,1 )
93
- local cy ,colH = simulateDrop (F1 ,shape ,cx )
94
-
95
- lockPiece (F1 ,shape ,cx ,cy )
96
- local clear = clearLine (F1 ,cy ,cy +# shape - 1 )
97
- local score
98
- if # F1 == 0 then
99
- -- Nobody can refuse AC
100
- score = 1e99
101
- else
102
- local HBC ,VBC = getBoundCount (F1 )
103
- score = clear * 2 - HBC * 3 - VBC * 2 - cy
104
- end
105
-
106
- local minH = math.min (unpack (colH ))
107
- for i = 1 ,# colH do
108
- score = score - (colH [i ]- minH )* 1.26
109
- end
77
+ local score ,cy = getSimScore (F ,shape ,cx )
110
78
111
79
local pos = {
112
80
score = score ,
@@ -122,13 +90,21 @@ function paperArtist.findBestPositions(F,shape,counts)
122
90
end
123
91
end
124
92
ins (posList ,i ,pos )
125
- if # posList > counts then
93
+ if # posList > bestCount then
126
94
rem (posList )
127
95
end
128
96
end
129
- if d < 4 then shape = TABLE . rotate (shape ,' R' ) end
97
+ if d < 4 then shape = rotate (shape ,' R' ) end
130
98
end
131
99
return posList
132
100
end
133
101
102
+ --- @param F Mat<boolean> Field (read )
103
+ --- @param shape Mat<boolean>
104
+ --- @return number x , number y , string dir
105
+ function paperArtist .getBestPosition (F ,shape )
106
+ local best = paperArtist .search (F ,shape ,4 ,1 )[1 ]
107
+ return best .x ,best .y ,best .dir
108
+ end
109
+
134
110
return paperArtist
0 commit comments