This commit is contained in:
Alexandre 2024-11-11 19:02:00 +01:00
parent 10830229e0
commit 77f6ec5c5e
10 changed files with 109 additions and 64 deletions

View File

@ -1,27 +1,24 @@
59.33000000000001 182.62000000000037
0 2
13 21 13 21
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 0 3 3 0 2 2 2 2 2 2 2 2 2 2 0 5 5 5 5 1 1 3 3 3 3 3 3 3 3 3 2 0 0 5 5 5 5 5 5 5 1
1 0 1 3 1 2 1 2 1 2 1 2 1 2 1 2 1 5 1 5 1 1 3 1 3 1 3 1 0 1 0 1 2 1 0 1 5 1 5 1 5 1
1 3 3 3 0 2 2 2 2 2 2 2 2 0 2 0 0 5 5 5 1 1 3 3 3 3 3 3 3 2 3 2 2 2 2 5 5 5 5 5 5 1
1 3 1 3 1 2 1 2 1 2 1 2 1 2 1 2 1 0 1 5 1 1 3 1 3 1 3 1 3 1 3 1 2 1 2 1 5 1 5 1 5 1
1 3 3 3 0 2 2 2 2 2 2 2 2 2 2 0 0 2 2 0 1 1 3 0 3 3 3 3 3 3 3 3 3 0 2 0 5 5 5 5 5 1
1 0 1 3 1 2 1 2 1 2 1 0 1 2 1 2 1 2 1 0 1 1 0 1 0 1 3 1 3 1 3 1 4 1 0 1 5 1 5 1 2 1
1 0 3 3 3 0 0 2 0 2 2 2 2 2 2 2 2 2 0 4 1 1 0 0 6 6 3 3 3 3 3 0 4 0 5 5 5 5 5 4 4 1
1 6 1 6 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 4 1 1 0 1 6 1 6 1 3 1 3 1 4 1 4 1 5 1 4 1 4 1
1 6 6 6 0 0 2 2 0 2 2 2 0 2 0 0 2 0 0 4 1 1 6 6 6 6 6 6 4 4 3 4 4 4 4 4 4 4 4 5 4 1
1 6 1 6 1 2 1 0 1 2 1 2 1 2 1 2 1 4 1 4 1 1 6 1 6 1 6 1 6 1 3 1 4 1 4 1 5 1 4 1 4 1
1 6 6 6 6 0 2 2 2 2 2 2 2 2 2 2 0 4 4 4 1 1 6 6 6 6 6 6 6 0 3 4 4 4 4 4 4 4 4 4 4 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
3 2
4 17 3 59.5 6 11 2 185.56136399999963
9 17 1 60.5 9 13 4 186.85000000000056
5 4 2 62.400000000000006 2
4 8 13 1 2 3 4 1 3
6 3 0 2 2 2 0 1 7 15 2 2 3 2 1 1
8 19 1 0 1 1 1 0
4 19 2 0 2 3 0 0
5 3 3 0 2 4 0 1
1 1
6 19 2 2 9 1

View File

@ -379,4 +379,4 @@ def simulation(strategies):
return return
from importlib import import_module from importlib import import_module
simulation(["./main", "./main","./main", "./main"]) simulation(["./main", "./main", "./main", "./main"])

BIN
main

Binary file not shown.

BIN
main.cmi

Binary file not shown.

BIN
main.cmo

Binary file not shown.

119
main.ml
View File

@ -476,6 +476,53 @@ let has_a_safe_path_origin (cx0 : int) (cy0 : int) (lines : int) (cols : int) (s
with with
| ReturnInt b -> b ;; | ReturnInt b -> b ;;
let has_a_safe_path_origin_2 (cx0 : int) (cy0 : int) (lines : int) (cols : int) (simt : float) (interval : float) (gd : game_data) (dgs : danger array array) (white_dgs : danger array) (white_gd : int array) (black_dgs : danger array) (black_gd : int array) (maxdepth : int) =
(*
core function
performs a BFS starting at (cx0, cy0), avoiding anything in blacklists and halting upon stepping on a whitelisted element
*)
let visited = Hashtbl.create 100 in
let q = Queue.create () in
Hashtbl.add visited (cx0, cy0) 1 ;
Queue.add (cx0+1, cy0, simt +. interval, 2) q ;
Queue.add (cx0-1, cy0, simt +. interval, 0) q ;
Queue.add (cx0, cy0+1, simt +. interval, 1) q ;
Queue.add (cx0, cy0-1, simt +. interval, 3) q ;
if debug_all then Printf.fprintf stderr "[escape] Attempt 1/1...\n" ;
try
while not (Queue.is_empty q) do
let (cx, cy, cur_t, direct) = Queue.pop q in
if (Hashtbl.find_opt visited (cx, cy) = None && is_valid cx cy lines cols && level_of_danger dgs.(cx).(cy) >= cur_t) then begin
Hashtbl.add visited (cx, cy) 1 ;
if cur_t > simt +. (float_of_int maxdepth) *. interval then (* too deep *)
raise (ReturnInt 4)
else if Array.mem dgs.(cx).(cy) white_dgs then
raise (ReturnInt direct)
else if Array.mem gd.laby.(cx).(cy) white_gd then
raise (ReturnInt direct)
else if Array.mem dgs.(cx).(cy) black_dgs then
()
else if Array.mem gd.laby.(cx).(cy) black_gd then
()
else begin
for dir = 0 to 3 do
let newx = cx + fst order.(dir)
and newy = cy + snd order.(dir)
and newt = cur_t +. interval in
Queue.add (newx, newy, newt, direct) q
done
end
end
done;
4
with
| ReturnInt b -> b ;;
(* ---------------------------------------------------------------------------------------------------------------------------------------------------- *) (* ---------------------------------------------------------------------------------------------------------------------------------------------------- *)
(* ---------------------------------------------------------------------------------------------------------------------------------------------------- *) (* ---------------------------------------------------------------------------------------------------------------------------------------------------- *)
@ -504,14 +551,15 @@ let move_safe (gd : game_data) (dgs : danger array array) =
end; end;
(*let result = has_a_safe_path (cx) (cy) gd.dt in*) (*let result = has_a_safe_path (cx) (cy) gd.dt in*)
let result = has_a_safe_path_origin cx cy lines cols gd.dt interval gd dgs [|Bonus|] [||] [|Blocked|] [||] 20 in let result = has_a_safe_path_origin_2 cx cy lines cols gd.dt interval gd dgs [|Bonus|] [||] [|Blocked|] [||] 20 in
if result <> 4 then result else begin if result <> 4 then result else begin
let result2 = has_a_safe_path_origin cx cy lines cols gd.dt interval gd dgs [|Safe|] [||] [|Blocked|] [||] 80 in let result2 = has_a_safe_path_origin_2 cx cy lines cols gd.dt interval gd dgs [|Safe|] [||] [|Blocked|] [||] 80 in
if result2 <> 4 then result2 if result2 <> 4 then result2
else begin else begin
(* you're probably dead if the code reaches here... *) (* you're probably dead if the code reaches here... *)
if debug_all then Printf.fprintf stderr "[escape] Attempt F...\n"; if debug_all then Printf.fprintf stderr "[escape] Attempt F...\n";
Printf.fprintf stderr "well shit\n" ;
4 4
end end
end end
@ -521,6 +569,34 @@ let move_safe (gd : game_data) (dgs : danger array array) =
(* ---------------------------------------------------------------------------------------------------------------------------------------------------- *) (* ---------------------------------------------------------------------------------------------------------------------------------------------------- *)
(* ---------------------------------------------------------------------------------------------------------------------------------------------------- *) (* ---------------------------------------------------------------------------------------------------------------------------------------------------- *)
let is_a_crate_nearby (gd : game_data) (dgs : danger array array) =
let pid = gd.player_id
and lines = Array.length gd.laby
and cols = Array.length gd.laby.(0) in
try
let halt = ref false in
let res = ref false in
for dir = 0 to 3 do
for o = 1 to gd.players.(pid).bomb_radius do
if not !halt then begin
let nx = gd.players.(pid).xy.x + o * (fst order.(dir))
and ny = gd.players.(pid).xy.y + o * (snd order.(dir)) in
if is_valid nx ny lines cols then begin
if gd.laby.(nx).(ny) = 2 then
res := true
else if gd.laby.(nx).(ny) = 1 then
halt := true
else if dgs.(nx).(ny) = Bonus then
raise (ReturnBool false)
end
end
done;
halt := false ;
done;
!res
with
| ReturnBool b -> b ;;
let move_explore (gd: game_data) (dgs : danger array array) = let move_explore (gd: game_data) (dgs : danger array array) =
(* destroy crates *) (* destroy crates *)
let pid = gd.player_id in let pid = gd.player_id in
@ -534,13 +610,13 @@ let move_explore (gd: game_data) (dgs : danger array array) =
let (cxi, cyi) = (gd.players.(pid).xy.x, gd.players.(pid).xy.y) in let (cxi, cyi) = (gd.players.(pid).xy.x, gd.players.(pid).xy.y) in
let move_with_caution (exit : bool) = let move_with_caution (exit : bool) =
let res = has_a_safe_path_origin cxi cyi lines cols gd.dt interval gd dgs [|Bonus|] [||] [|Blocked|] [||] 5 in let res = has_a_safe_path_origin_2 cxi cyi lines cols gd.dt interval gd dgs [|Bonus|] [||] [|Blocked|] [||] 5 in
if res <> 4 then begin if res <> 4 then begin
if debug_all then Printf.fprintf stderr "[crates] success 1/2!\n" ; if debug_all then Printf.fprintf stderr "[crates] success 1/2!\n" ;
res res
end end
else begin else begin
let res2 = has_a_safe_path_origin cxi cyi lines cols gd.dt interval gd dgs [||] [|2|] [|Blocked|] [||] 80 in let res2 = has_a_safe_path_origin_2 cxi cyi lines cols gd.dt interval gd dgs [||] [|2|] [|Blocked|] [||] 80 in
if res2 <> 4 then begin if res2 <> 4 then begin
if debug_all then Printf.fprintf stderr "[crates] success 2/2!\n" ; if debug_all then Printf.fprintf stderr "[crates] success 2/2!\n" ;
res2 res2
@ -560,7 +636,7 @@ let move_explore (gd: game_data) (dgs : danger array array) =
let saved_dgs = Hashtbl.create (4 * (bsize +1)) in let saved_dgs = Hashtbl.create (4 * (bsize +1)) in
for dir = 0 to 3 do for dir = 0 to 3 do
for w = 0 to bsize do for w = 0 to bsize do
Hashtbl.add bomb_hash (bx + w*(fst order.(dir)), by + w*(snd order.(dir))) (Danger 5.5) ; Hashtbl.add bomb_hash (bx + w*(fst order.(dir)), by + w*(snd order.(dir))) (Danger (gd.dt +. 5.5)) ;
done done
done; done;
Hashtbl.iter Hashtbl.iter
@ -577,40 +653,13 @@ let move_explore (gd: game_data) (dgs : danger array array) =
in in
(* check if there's a crate next to the player, and if upon placing a bomb it won't softlock the player, and if you can place a bomb *) (* check if there's a crate next to the player, and if upon placing a bomb it won't softlock the player, and if you can place a bomb *)
if is_valid (cxi+1) (cyi) lines cols && gd.laby.(cxi+1).(cyi) = 2 && (safe_path_with_bomb cxi cyi gd.players.(pid).bomb_radius <> 4) then begin (* Crate at South *) if is_a_crate_nearby gd dgs && (safe_path_with_bomb cxi cyi gd.players.(pid).bomb_radius <> 4) then begin (* Crate at South *)
if gd.players.(pid).nbomb_atonce > 0 then begin if dgs.(cxi).(cyi) = Safe && gd.players.(pid).nbomb_atonce > 0 then begin
current_status := EscapeDeath ; current_status := EscapeDeath ;
action := 1; action := 1;
if debug_all then Printf.fprintf stderr "Fire in the hole!\n" ; if debug_all then Printf.fprintf stderr "Fire in the hole!\n" ;
end;
raise (ReturnInt 4) ; raise (ReturnInt 4) ;
end; end;
if is_valid (cxi-1) (cyi) lines cols && gd.laby.(cxi-1).(cyi) = 2 && (safe_path_with_bomb cxi cyi gd.players.(pid).bomb_radius <> 4) then begin (* Crate at North *)
if gd.players.(pid).nbomb_atonce > 0 then begin
current_status := EscapeDeath ;
action := 1;
if debug_all then Printf.fprintf stderr "Fire in the hole!\n" ;
end;
raise (ReturnInt 4) ;
end;
if is_valid (cxi) (cyi+1) lines cols && gd.laby.(cxi).(cyi+1) = 2 && (safe_path_with_bomb cxi cyi gd.players.(pid).bomb_radius <> 4) then begin (* Crate at East *)
if gd.players.(pid).nbomb_atonce > 0 then begin
current_status := EscapeDeath ;
action := 1;
if debug_all then Printf.fprintf stderr "Fire in the hole!\n" ;
end;
raise (ReturnInt 4) ;
end;
if is_valid (cxi) (cyi-1) lines cols && gd.laby.(cxi).(cyi-1) = 2 && (safe_path_with_bomb cxi cyi gd.players.(pid).bomb_radius <> 4) then begin (* Crate at West *)
if gd.players.(pid).nbomb_atonce > 0 then begin
current_status := EscapeDeath ;
action := 1;
if debug_all then Printf.fprintf stderr "Fire in the hole!\n" ;
end;
raise (ReturnInt 4) ;
end; end;
if debug_all then Printf.fprintf stderr "[crates] Cannot bomb now, searching for a crate...\n"; if debug_all then Printf.fprintf stderr "[crates] Cannot bomb now, searching for a crate...\n";
@ -625,7 +674,7 @@ let move_explore (gd: game_data) (dgs : danger array array) =
let update_strat (gd : game_data) (dgs : danger array array) = match dgs.(gd.players.(gd.player_id).xy.x).(gd.players.(gd.player_id).xy.y) with let update_strat (gd : game_data) (dgs : danger array array) = match dgs.(gd.players.(gd.player_id).xy.x).(gd.players.(gd.player_id).xy.y) with
| Safe -> () | Safe -> ()
| Danger k -> () | Danger k -> current_status := EscapeDeath
| Bonus -> () | Bonus -> ()
| Fatal k -> (* should not happen *) current_status := EscapeDeath | Fatal k -> (* should not happen *) current_status := EscapeDeath
| Blocked -> failwith "did you just suffocate the player ?" ;; | Blocked -> failwith "did you just suffocate the player ?" ;;

View File

@ -1 +1 @@
1 0

View File

@ -1 +1 @@
1 0

View File

@ -1 +1 @@
1 0

View File

@ -1 +0,0 @@
4 0