Skip to content

Commit 37356f3

Browse files
committed
modified: Game solvers/asciiplanes-player.sf -- some optimizations
1 parent c872d6d commit 37356f3

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

Game solvers/asciiplanes-player.sf

+33-10
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ var simulate = false
4545
var hit_char = HIT
4646
var miss_char = AIR
4747
var head_char = HEAD
48+
var seed = 0
4849

4950
func usage {
5051
print <<"EOT"
@@ -58,6 +59,7 @@ main:
5859
--miss=s : character used when a plane is missed (default: "#{miss_char}")
5960
--colors! : use ANSI colors (requires Term::ANSIColor) (default: #{use_colors})
6061
--simulate! : run a random simulation (default: #{simulate})
62+
--seed=i : run with a given pseudorandom seed value > 0 (default: #{seed})
6163
6264
help:
6365
--help : print this message and exit
@@ -85,6 +87,7 @@ if (ARGV) {
8587
getopt.GetOptions(
8688
'board-size|size=i' => func(_,v) { BOARD_SIZE = Num(v) },
8789
'planes-num=i' => func(_,v) { PLANES_NUM = Num(v) },
90+
'seed=i' => func(_,v) { seed = Num(v) },
8891
'hit-char=s' => func(_,v) { hit_char = Str(v) },
8992
'miss-char=s' => func(_,v) { miss_char = Str(v) },
9093
'wrap!' => func(_,v) { wrap_plane = Bool(v) },
@@ -95,6 +98,11 @@ if (ARGV) {
9598
)
9699
}
97100

101+
if (seed) {
102+
iseed(seed)
103+
Perl.eval("srand(#{seed})")
104+
}
105+
98106
#---------------------------------------------------------------
99107

100108
func pointers(board, x, y, indices) {
@@ -241,24 +249,24 @@ func create_planes(play_board) {
241249
func guess(info_board, play_board, plane_count) {
242250

243251
var count = 0
244-
var sampled = Set()
245252
var max_tries = BOARD_SIZE*BOARD_SIZE
253+
var indices = PAIR_INDICES.shuffle
246254

247255
while (count != (PLANES_NUM - plane_count)) {
248256

249257
#var x = irand(1, BOARD_SIZE)-1
250258
#var y = irand(1, BOARD_SIZE)-1
251259

252-
var (x,y) = (PAIR_INDICES.shuffle.first_by {|idx|
253-
(play_board[idx[0]][idx[1]] == BLANK) && (info_board[idx[0]][idx[1]] == BLANK) && !sampled.has(idx)
254-
} \\ return nil)...
260+
var (x,y) = (indices.pop_rand \\ return nil)...
261+
loop {
262+
(play_board[x][y] == BLANK) && (info_board[x][y] == BLANK) && break
263+
(x,y) = (indices.pop_rand \\ return nil)...
264+
}
255265

256266
if (--max_tries <= 0) {
257267
return nil
258268
}
259269

260-
sampled << [x,y]
261-
262270
var good_directions = DIRECTIONS.grep {|dir|
263271
var plane = pointers(info_board, x, y, dir)
264272
plane && plane.none { *_ == AIR }
@@ -362,10 +370,24 @@ func solve(callback) {
362370
var all_dead = true
363371
var new_info = false
364372

365-
get_head_positions(play_board).shuffle.each_2d {|i,j|
373+
var head_pos = get_head_positions(play_board).shuffle
374+
375+
head_pos = head_pos.grep_2d {|x,y| info_board[x][y] == BLANK }.map_2d {|x,y|
376+
[x, y, DIRECTIONS.first_by {|d|
377+
(pointers(play_board, x, y, d).count_by { *_ == HIT } == 7) &&
378+
(pointers(info_board, x, y, d).none { *_ == AIR })
379+
}]
380+
}
381+
382+
# Prefer the planes with the most hits
383+
head_pos = head_pos.sort_by {|p|
384+
pointers(info_board, p[0], p[1], p[2]).count_by { *_ == HIT }
385+
}.flip
386+
387+
head_pos.each_2d {|i,j|
366388

367-
if (info_board[i][j] == HEAD) {
368-
next # already destroyed
389+
if (info_board[i][j] != BLANK) {
390+
next
369391
}
370392

371393
all_dead = false
@@ -381,10 +403,11 @@ func solve(callback) {
381403
if (score == HEAD) {
382404
new_info = true
383405
boards = make_play_boards(info_board)
406+
next
384407
}
385408
elsif (score == AIR) {
386409
new_info = true
387-
boards = boards.grep { valid_assignment(.head, info_board) }
410+
boards = boards.grep { valid_assignment(.head, info_board) }.flip
388411
}
389412

390413
break

0 commit comments

Comments
 (0)