calibrated everything

This commit is contained in:
Alexandre 2024-11-09 17:16:56 +01:00
parent 5994546629
commit 8d46228c2e
14 changed files with 207 additions and 208 deletions

View File

@ -1,2 +1 @@
ocamlc main.ml -o main ocamlc main.ml -o main
./main

View File

@ -1,32 +1,25 @@
88.00000000000014 158.0
0 0
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 3 3 3 3 0 0 2 2 2 2 2 2 0 0 5 5 5 5 5 1 1 3 3 3 0 2 0 2 2 2 2 2 2 0 5 5 5 5 5 5 1
1 3 1 3 1 0 1 0 1 2 1 0 1 0 1 0 1 5 1 5 1 1 3 1 3 1 2 1 0 1 2 1 2 1 5 1 5 1 5 1 5 1
1 3 3 3 3 0 2 2 2 2 0 0 0 2 2 2 0 5 5 5 1 1 3 3 3 0 0 3 3 0 2 2 0 5 5 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 2 1 3 1 0 1 0 1 5 1 5 1 0 1 5 1
1 0 0 3 0 2 2 2 2 2 0 2 2 2 2 0 2 2 0 5 1 1 6 6 6 6 0 0 3 4 4 4 4 5 5 5 4 4 0 0 5 1
1 6 1 0 1 2 1 2 1 2 1 2 1 0 1 2 1 2 1 0 1 1 3 1 0 1 0 1 3 1 4 1 4 1 5 1 4 1 0 1 0 1
1 6 0 0 2 0 2 2 2 2 2 2 2 0 0 2 2 2 2 0 1 1 6 6 6 6 6 6 3 6 4 2 0 0 5 5 5 5 4 0 0 1
1 6 1 0 1 2 1 2 1 2 1 2 1 2 1 2 1 0 1 4 1 1 6 1 6 1 3 1 6 1 0 1 0 1 5 1 4 1 4 1 4 1
1 6 6 6 0 2 2 2 2 2 2 2 0 2 2 2 0 4 4 4 1 1 6 6 6 6 5 5 5 5 5 5 5 5 5 5 4 4 4 4 4 1
1 6 1 6 1 2 1 0 1 2 1 2 1 2 1 0 1 4 1 4 1 1 6 1 6 1 6 1 6 1 5 1 0 1 5 1 4 1 4 1 4 1
1 6 6 6 0 2 2 2 2 2 2 2 2 2 0 4 4 4 4 4 1 1 6 6 6 6 6 6 6 6 5 0 0 5 5 5 5 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 1
11 4 1 89.5 7 9 2 159.00000000000048
3 5 3 89.90000000000012
8 17 3 90.5
4 4
1 6 0 1 1 3 0 1 5 7 0 0 2 4 0 1
9 17 1 0 1 3 1 1 4 7 1 5 2 1 2 2
3 17 2 1 1 2 1 1 9 8 2 4 4 4 1 4
9 3 3 0 1 1 3 1 9 8 3 1 2 3 2 0
6 1
7 19 2 3 8 2
5 18 0
5 4 0
9 4 3
10 15 2
11 14 0

View File

@ -1,2 +1,2 @@
chmod u+x main.ml ocamlc main.ml -o main
./main.ml ./main

View File

@ -7,7 +7,7 @@ Created on Wed Sep 12 13:52:48 2018
TEMPS_BASE = 1 TEMPS_BASE = 1
TEMPS_PROPAGATION = 0.01 TEMPS_PROPAGATION = 0.01
TEMPS_EXPLOSION = 5.5 TEMPS_EXPLOSION = 5.5
TEMPS_PARTIE = 100 TEMPS_PARTIE = 1000
E_INSTANT = 0 E_INSTANT = 0
E_NATURE = 1 E_NATURE = 1
@ -339,7 +339,7 @@ def simulation(strategies):
execute_evenement(evenements, evenement, plateau, plateauCouleur, bombes, joueurs, powerups, pieges) execute_evenement(evenements, evenement, plateau, plateauCouleur, bombes, joueurs, powerups, pieges)
affiche_plateau(canvas, plateau, plateauCouleur, bombes, joueurs, powerups) affiche_plateau(canvas, plateau, plateauCouleur, bombes, joueurs, powerups)
affiche_infos(canvasInfosJoueurs, joueurs, plateauCouleur) affiche_infos(canvasInfosJoueurs, joueurs, plateauCouleur)
temps = int((evenements[0][0]-evenement[0])/3*500) temps = int((evenements[0][0]-evenement[0])/3*100)
if temps != 0: if temps != 0:
fenetre.after(temps, pas_de_jeu) fenetre.after(temps, pas_de_jeu)
else: else:

BIN
main

Binary file not shown.

BIN
main.cmi

Binary file not shown.

BIN
main.cmo

Binary file not shown.

350
main.ml
View File

@ -76,18 +76,20 @@ type game_data = {
mutable boosts : boost array ; mutable boosts : boost array ;
} }
type danger = Safe | Danger of float | Fatal of float | Blocked ;; type danger = Safe | Danger of float | Fatal of float | Blocked | Bonus ;;
let int_of_danger = function let int_of_danger = function
| Safe -> 0 | Safe -> 0
| Danger _ -> 1 | Bonus -> 1
| Fatal _ -> 2 | Danger _ -> 2
| Blocked -> 3 ;; | Fatal _ -> 3
| Blocked -> 4 ;;
let danger_of_int t = function let danger_of_int t = function
| 0 -> Safe | 0 -> Safe
| 1 -> Danger t | 1 -> Bonus
| 2 -> Fatal t | 2 -> Danger t
| 3 -> Fatal t
| _ -> Blocked ;; | _ -> Blocked ;;
type moveType = EscapeDeath | BlowUpCrates | KillPlayers | ClaimLand ;; type moveType = EscapeDeath | BlowUpCrates | KillPlayers | ClaimLand ;;
@ -113,11 +115,11 @@ let is_valid i j len hei =
i >= 0 && j >= 0 && i < len && j < hei ;; i >= 0 && j >= 0 && i < len && j < hei ;;
let print_direction = function let print_direction = function
| 0 -> Printf.printf "NORTH " | 0 -> Printf.fprintf stderr "NORTH "
| 1 -> Printf.printf "EAST " | 1 -> Printf.fprintf stderr "EAST "
| 2 -> Printf.printf "SOUTH " | 2 -> Printf.fprintf stderr "SOUTH "
| 3 -> Printf.printf "WEST " | 3 -> Printf.fprintf stderr "WEST "
| 4 -> Printf.printf "STILL " | 4 -> Printf.fprintf stderr "STILL "
| _-> failwith "ERROR : invalid direction" ;; | _-> failwith "ERROR : invalid direction" ;;
let level_of_danger = function let level_of_danger = function
@ -135,57 +137,63 @@ let overwrite_file (filename : string) =
(* ---------------------------------------------------------------------------------------------------------------------------------------------------- *) (* ---------------------------------------------------------------------------------------------------------------------------------------------------- *)
let print_game_data (gd : game_data) = let print_game_data (gd : game_data) =
Printf.printf "--------------------------------| Board data |--------------------------------\n" ; Printf.fprintf stderr "--------------------------------| Board data |--------------------------------\n" ;
Printf.printf "Time : %f\n" gd.dt ; Printf.fprintf stderr "Time : %f\n" gd.dt ;
Printf.printf "ID : %d\n" gd.player_id ; Printf.fprintf stderr "ID : %d\n" gd.player_id ;
Printf.printf "Laby [of size %d %d]:\n" (Array.length gd.laby) (Array.length gd.laby.(0)); Printf.fprintf stderr "Laby [of size %d %d]:\n" (Array.length gd.laby) (Array.length gd.laby.(0));
for l = 0 to Array.length gd.laby -1 do for l = 0 to Array.length gd.laby -1 do
Printf.printf " " ; Printf.fprintf stderr " " ;
for c = 0 to Array.length gd.laby.(l) -1 do for c = 0 to Array.length gd.laby.(l) -1 do
Printf.printf "%d " gd.laby.(l).(c) ; Printf.fprintf stderr "%d " gd.laby.(l).(c) ;
done; done;
Printf.printf "\n" Printf.fprintf stderr "\n"
done ; done ;
Printf.printf "Bombs (%d) : \n" gd.nbombs ; Printf.fprintf stderr "Bombs (%d) : \n" gd.nbombs ;
for b = 0 to gd.nbombs -1 do for b = 0 to gd.nbombs -1 do
Printf.printf " [Bomb] (at %d %d) (of size %d) (blowing up at %f)\n" gd.bombs.(b).xy.x gd.bombs.(b).xy.y gd.bombs.(b).size gd.bombs.(b).det_time ; Printf.fprintf stderr " [Bomb] (at %d %d) (of size %d) (blowing up at %f)\n" gd.bombs.(b).xy.x gd.bombs.(b).xy.y gd.bombs.(b).size gd.bombs.(b).det_time ;
done; done;
Printf.printf "Players (%d) : \n" gd.nplayers ; Printf.fprintf stderr "Players (%d) : \n" gd.nplayers ;
for b = 0 to gd.nplayers -1 do for b = 0 to gd.nplayers -1 do
Printf.printf " [Player %d] (at %d %d) (holding %d %d %d %d %d)\n" gd.players.(b).id gd.players.(b).xy.x gd.players.(b).xy.y gd.players.(b).nspeed gd.players.(b).nbomb_atonce gd.players.(b).bomb_radius gd.players.(b).ndash gd.players.(b).ntraps ; Printf.fprintf stderr " [Player %d] (at %d %d) (holding %d %d %d %d %d)\n" gd.players.(b).id gd.players.(b).xy.x gd.players.(b).xy.y gd.players.(b).nspeed gd.players.(b).nbomb_atonce gd.players.(b).bomb_radius gd.players.(b).ndash gd.players.(b).ntraps ;
done; done;
Printf.printf "Boosts (%d) : \n" gd.nboosts ; Printf.fprintf stderr "Boosts (%d) : \n" gd.nboosts ;
for b = 0 to gd.nboosts -1 do for b = 0 to gd.nboosts -1 do
Printf.printf " [Boost] (at %d %d) (of type %d)\n" gd.boosts.(b).xy.x gd.boosts.(b).xy.y gd.boosts.(b).spec ; Printf.fprintf stderr " [Boost] (at %d %d) (of type %d)\n" gd.boosts.(b).xy.x gd.boosts.(b).xy.y gd.boosts.(b).spec ;
done;; done;;
let print_danger_levels (map : danger array array) = let print_danger_levels (map : danger array array) =
Printf.printf "--------------------------------| Danger levels |--------------------------------\n" ; Printf.fprintf stderr "--------------------------------| Danger levels |--------------------------------\n" ;
for l = 0 to (Array.length map -1) do for l = 0 to (Array.length map -1) do
for c = 0 to (Array.length map.(l) -1) do for c = 0 to (Array.length map.(l) -1) do
match map.(l).(c) with match map.(l).(c) with
| Blocked -> Printf.printf "@ " | Blocked -> Printf.fprintf stderr "@ "
| Safe -> Printf.printf ". " | Safe -> Printf.fprintf stderr ". "
| Danger x -> Printf.printf "! " | Bonus -> Printf.fprintf stderr "+ "
| Fatal x -> Printf.printf "X " | Danger x -> Printf.fprintf stderr "! "
| Fatal x -> Printf.fprintf stderr "X "
done; done;
Printf.printf "\n" Printf.fprintf stderr "\n"
done ;; done ;;
let print_gain_map (map : int array array) = let print_gain_map (map : int array array) =
Printf.printf "--------------------------------| Gain levels |--------------------------------\n" ; Printf.fprintf stderr "--------------------------------| Gain levels |--------------------------------\n" ;
for l = 0 to (Array.length map -1) do for l = 0 to (Array.length map -1) do
for c = 0 to (Array.length map.(l) -1) do for c = 0 to (Array.length map.(l) -1) do
Printf.printf "%d " map.(l).(c) ; Printf.fprintf stderr "%d " map.(l).(c) ;
done; done;
Printf.printf "\n" Printf.fprintf stderr "\n"
done ;; done ;;
(* ---------------------------------------------------------------------------------------------------------------------------------------------------- *) (* ---------------------------------------------------------------------------------------------------------------------------------------------------- *)
(* ---------------------------------------------------------------------------------------------------------------------------------------------------- *) (* ---------------------------------------------------------------------------------------------------------------------------------------------------- *)
let get_meta_info () = let rec ln_b b = function
let ptr = open_in "main.sav" in | k when k < 0 -> failwith "are you sure about that ?"
| k when k < b -> 0
| k -> 1 + ln_b b (k/b) ;;
let get_meta_info (pid : int) =
let ptr = open_in ("main_"^(string_of_int pid)^".sav") in
match (int_of_string (input_line ptr)) with match (int_of_string (input_line ptr)) with
| 0 -> current_status := EscapeDeath | 0 -> current_status := EscapeDeath
| 1 -> current_status := BlowUpCrates | 1 -> current_status := BlowUpCrates
@ -194,8 +202,8 @@ let get_meta_info () =
| _ -> current_status := EscapeDeath ; | _ -> current_status := EscapeDeath ;
close_in ptr ;; close_in ptr ;;
let set_meta_info () = let set_meta_info (pid : int) =
let ptr = open_out "main.sav" in let ptr = open_out ("main_"^(string_of_int pid)^".sav") in
match !current_status with match !current_status with
| EscapeDeath -> Printf.fprintf ptr "0" | EscapeDeath -> Printf.fprintf ptr "0"
| BlowUpCrates -> Printf.fprintf ptr "1" | BlowUpCrates -> Printf.fprintf ptr "1"
@ -209,6 +217,9 @@ let set_meta_info () =
let int_of_string (str : string) = let int_of_string (str : string) =
String.fold_right (fun ch acc -> let cd = Char.code ch in if cd >= 48 || cd <= 57 then 10*acc + cd - 48 else failwith "not an integer\n") str 0 ;; String.fold_right (fun ch acc -> let cd = Char.code ch in if cd >= 48 || cd <= 57 then 10*acc + cd - 48 else failwith "not an integer\n") str 0 ;;
let string_of_int (k0 : int) =
String.make (1) (Char.chr (k0 + 48)) ;;
let int_n_of_string (str : string) (n : int) (nlast : int ref) = let int_n_of_string (str : string) (n : int) (nlast : int ref) =
let res = Array.make n 0 in let res = Array.make n 0 in
let rec aux idres idstr = match idstr with let rec aux idres idstr = match idstr with
@ -235,15 +246,15 @@ let parse_input (str : string) =
let (res : game_data) = {dt = 0. ; player_id = 0 ; laby = [||] ; nbombs = 0 ; bombs = [||] ; nplayers = 0 ; players = [||] ; nboosts = 0 ; boosts = [||] ;} in let (res : game_data) = {dt = 0. ; player_id = 0 ; laby = [||] ; nbombs = 0 ; bombs = [||] ; nplayers = 0 ; players = [||] ; nboosts = 0 ; boosts = [||] ;} in
try try
(* time *) (* time *)
if debug_all then Printf.printf "Time\n" ; if debug_all then Printf.fprintf stderr "Time\n" ;
res.dt <- Float.of_string (input_line ptr) ; res.dt <- Float.of_string (input_line ptr) ;
(* player_id *) (* player_id *)
if debug_all then Printf.printf "PID\n" ; if debug_all then Printf.fprintf stderr "PID\n" ;
res.player_id <- int_of_string (input_line ptr) ; res.player_id <- int_of_string (input_line ptr) ;
(* maze *) (* maze *)
if debug_all then Printf.printf "Maze\n" ; if debug_all then Printf.fprintf stderr "Maze\n" ;
let msize = int_n_of_string (input_line ptr) 2 useless in let msize = int_n_of_string (input_line ptr) 2 useless in
res.laby <- Array.make msize.(0) [||] ; res.laby <- Array.make msize.(0) [||] ;
@ -253,7 +264,7 @@ let parse_input (str : string) =
done; done;
(* bombs *) (* bombs *)
if debug_all then Printf.printf "Boom\n" ; if debug_all then Printf.fprintf stderr "Boom\n" ;
res.nbombs <- int_of_string (input_line ptr) ; res.nbombs <- int_of_string (input_line ptr) ;
res.bombs <- Array.make res.nbombs default_bomb ; res.bombs <- Array.make res.nbombs default_bomb ;
@ -267,7 +278,7 @@ let parse_input (str : string) =
done; done;
(* players *) (* players *)
if debug_all then Printf.printf "Players\n" ; if debug_all then Printf.fprintf stderr "Players\n" ;
res.nplayers <- int_of_string (input_line ptr) ; res.nplayers <- int_of_string (input_line ptr) ;
res.players <- Array.make res.nplayers default_player ; res.players <- Array.make res.nplayers default_player ;
@ -277,7 +288,7 @@ let parse_input (str : string) =
done; done;
(* boosts *) (* boosts *)
if debug_all then Printf.printf "Boosts\n" ; if debug_all then Printf.fprintf stderr "Boosts\n" ;
res.nboosts <- int_of_string (input_line ptr) ; res.nboosts <- int_of_string (input_line ptr) ;
res.boosts <- Array.make res.nboosts default_boost ; res.boosts <- Array.make res.nboosts default_boost ;
@ -286,7 +297,7 @@ let parse_input (str : string) =
res.boosts.(p) <- {xy = {x = dat.(0) ; y = dat.(1) ;} ; spec = dat.(2)} res.boosts.(p) <- {xy = {x = dat.(0) ; y = dat.(1) ;} ; spec = dat.(2)}
done; done;
if debug_all then Printf.printf "Done!\n" ; if debug_all then Printf.fprintf stderr "Done!\n" ;
close_in ptr ; close_in ptr ;
res res
with with
@ -318,6 +329,11 @@ let evaluate_dangers (gd : game_data) =
done done
done ; done ;
(* add bonuses *)
for b = 0 to gd.nboosts -1 do
res.(gd.boosts.(b).xy.x).(gd.boosts.(b).xy.y) <- danger_priority (res.(gd.boosts.(b).xy.x).(gd.boosts.(b).xy.y)) Bonus
done;
(* sort bombs based on detonation time *) (* sort bombs based on detonation time *)
for b = 0 to gd.nbombs -1 do for b = 0 to gd.nbombs -1 do
let m = ref gd.bombs.(b).det_time let m = ref gd.bombs.(b).det_time
@ -401,8 +417,70 @@ let cell_values (gd : game_data) =
(* ---------------------------------------------------------------------------------------------------------------------------------------------------- *) (* ---------------------------------------------------------------------------------------------------------------------------------------------------- *)
(* ---------------------------------------------------------------------------------------------------------------------------------------------------- *) (* ---------------------------------------------------------------------------------------------------------------------------------------------------- *)
let has_a_safe_path_origin (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 ;
if is_valid (cx0+1) (cy0) lines cols && not ((simt +. interval) > (level_of_danger dgs.(cx0+1).(cy0)) && (simt +. interval) < (level_of_danger dgs.(cx0+1).(cy0)) +. explosion_time) then begin (* South *)
if debug_all then Printf.fprintf stderr "[escape] +South\n" ;
Queue.add (cx0+1, cy0, simt +. interval, 2) q ;
end;
if is_valid (cx0-1) (cy0) lines cols && not ((simt +. interval) > (level_of_danger dgs.(cx0-1).(cy0)) && (simt +. interval) < (level_of_danger dgs.(cx0-1).(cy0)) +. explosion_time) then begin (* North *)
if debug_all then Printf.fprintf stderr "[escape] +North\n" ;
Queue.add (cx0-1, cy0, simt +. interval, 0) q ;
end;
if is_valid (cx0) (cy0+1) lines cols && not ((simt +. interval) > (level_of_danger dgs.(cx0).(cy0+1)) && (simt +. interval) < (level_of_danger dgs.(cx0).(cy0+1)) +. explosion_time) then begin (* East *)
if debug_all then Printf.fprintf stderr "[escape] +East\n" ;
Queue.add (cx0, cy0+1, simt +. interval, 1) q ;
end;
if is_valid (cx0) (cy0-1) lines cols && not ((simt +. interval) > (level_of_danger dgs.(cx0).(cy0-1)) && (simt +. interval) < (level_of_danger dgs.(cx0).(cy0-1)) +. explosion_time) then begin (* West *)
if debug_all then Printf.fprintf stderr "[escape] +West\n" ;
Queue.add (cx0, cy0-1, simt +. interval, 3) q ;
end;
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 then () else 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 (* either danger or fatal *)
let dang_time = level_of_danger dgs.(cx).(cy) in
for dir = 0 to 3 do
let newx = cx + fst order.(dir)
and newy = cy + snd order.(dir)
and newt = cur_t +. interval in
if (is_valid newx newy lines cols) && not (newt > dang_time && newt < dang_time +. explosion_time) then
Queue.add (newx, newy, newt, direct) q
done
end
end
done;
4
with
| ReturnInt b -> b ;;
(* ---------------------------------------------------------------------------------------------------------------------------------------------------- *)
(* ---------------------------------------------------------------------------------------------------------------------------------------------------- *)
let is_safe = function let is_safe = function
| Safe | Danger _ -> true | Safe | Danger _ | Bonus -> true
| Fatal _ | Blocked -> false ;; | Fatal _ | Blocked -> false ;;
let move_safe (gd : game_data) (dgs : danger array array) = let move_safe (gd : game_data) (dgs : danger array array) =
@ -411,7 +489,7 @@ let move_safe (gd : game_data) (dgs : danger array array) =
let pid = gd.player_id in let pid = gd.player_id in
let interval = Float.pow 0.9 (float_of_int gd.players.(pid).nspeed) in let interval = Float.pow 0.9 (float_of_int gd.players.(pid).nspeed) in
if debug_all then Printf.printf "I = %f\n" interval ; if debug_all then Printf.fprintf stderr "I = %f\n" interval ;
let lines = Array.length gd.laby let lines = Array.length gd.laby
and cols = Array.length gd.laby.(0) in and cols = Array.length gd.laby.(0) in
@ -425,64 +503,17 @@ let move_safe (gd : game_data) (dgs : danger array array) =
raise (ReturnInt 4) ; raise (ReturnInt 4) ;
end; end;
let visited = Hashtbl.create 100 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 has_a_safe_path (cx0 : int) (cy0 : int) (simt : float) =
let q = Queue.create () in
Hashtbl.add visited (cx0, cy0) 1 ;
if is_valid (cx0+1) (cy0) lines cols && not ((simt +. interval) > (level_of_danger dgs.(cx0+1).(cy0)) && (simt +. interval) < (level_of_danger dgs.(cx0+1).(cy0)) +. explosion_time) then begin (* South *)
if debug_all then Printf.printf "[escape] +South\n" ;
Queue.add (cx0+1, cy0, simt +. interval, 2) q ;
end;
if is_valid (cx0-1) (cy0) lines cols && not ((simt +. interval) > (level_of_danger dgs.(cx0-1).(cy0)) && (simt +. interval) < (level_of_danger dgs.(cx0-1).(cy0)) +. explosion_time) then begin (* North *)
if debug_all then Printf.printf "[escape] +North\n" ;
Queue.add (cx0-1, cy0, simt +. interval, 0) q ;
end;
if is_valid (cx0) (cy0+1) lines cols && not ((simt +. interval) > (level_of_danger dgs.(cx0).(cy0+1)) && (simt +. interval) < (level_of_danger dgs.(cx0).(cy0+1)) +. explosion_time) then begin (* East *)
if debug_all then Printf.printf "[escape] +East\n" ;
Queue.add (cx0, cy0+1, simt +. interval, 1) q ;
end;
if is_valid (cx0) (cy0-1) lines cols && not ((simt +. interval) > (level_of_danger dgs.(cx0).(cy0-1)) && (simt +. interval) < (level_of_danger dgs.(cx0).(cy0-1)) +. explosion_time) then begin (* West *)
if debug_all then Printf.printf "[escape] +West\n" ;
Queue.add (cx0, cy0-1, simt +. interval, 3) q ;
end;
if debug_all then Printf.printf "[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 then () else begin
Hashtbl.add visited (cx, cy) 1 ;
(*if debug_all then Printf.printf "dealing at (%d, %d) with dir %d\n" cx cy direct ;*)
if dgs.(cx).(cy) = Safe then
raise (ReturnInt direct)
else if dgs.(cx).(cy) = Blocked then
()
else begin (* either danger or fatal *)
let dang_time = level_of_danger dgs.(cx).(cy) in
for dir = 0 to 3 do
let newx = cx + fst order.(dir)
and newy = cy + snd order.(dir)
and newt = cur_t +. interval in
if (is_valid newx newy lines cols) && not (newt > dang_time && newt < dang_time +. explosion_time) then
Queue.add (newx, newy, newt, direct) q
done
end
end
done;
4
with
| ReturnInt b -> b
in
let result = has_a_safe_path (cx) (cy) gd.dt in
if result <> 4 then result else begin if result <> 4 then result else begin
(* you're probably dead if the code reaches here... *) let result2 = has_a_safe_path_origin cx cy lines cols gd.dt interval gd dgs [|Safe|] [||] [|Blocked|] [||] 80 in
if debug_all then Printf.printf "[escape] Attempt F...\n"; if result2 <> 4 then result2
4 else begin
(* you're probably dead if the code reaches here... *)
if debug_all then Printf.fprintf stderr "[escape] Attempt F...\n";
4
end
end end
with with
| ReturnInt k -> k ;; | ReturnInt k -> k ;;
@ -502,67 +533,24 @@ let move_explore (gd: game_data) (dgs : danger array array) =
try try
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 visited = Hashtbl.create 100 in
let has_a_safe_path (cx0 : int) (cy0 : int) (simt : float) =
let q = Queue.create () in
Hashtbl.add visited (cx0, cy0) 1 ;
if is_valid (cx0+1) (cy0) lines cols && not ((simt +. interval) > (level_of_danger dgs.(cx0+1).(cy0)) && (simt +. interval) < (level_of_danger dgs.(cx0+1).(cy0)) +. explosion_time) then begin (* South *)
if debug_all then Printf.printf "[crates] +South\n" ;
Queue.add (cx0+1, cy0, simt +. interval, 2) q ;
end;
if is_valid (cx0-1) (cy0) lines cols && not ((simt +. interval) > (level_of_danger dgs.(cx0-1).(cy0)) && (simt +. interval) < (level_of_danger dgs.(cx0-1).(cy0)) +. explosion_time) then begin (* North *)
if debug_all then Printf.printf "[crates] +North\n" ;
Queue.add (cx0-1, cy0, simt +. interval, 0) q ;
end;
if is_valid (cx0) (cy0+1) lines cols && not ((simt +. interval) > (level_of_danger dgs.(cx0).(cy0+1)) && (simt +. interval) < (level_of_danger dgs.(cx0).(cy0+1)) +. explosion_time) then begin (* East *)
if debug_all then Printf.printf "[crates] +East\n" ;
Queue.add (cx0, cy0+1, simt +. interval, 1) q ;
end;
if is_valid (cx0) (cy0-1) lines cols && not ((simt +. interval) > (level_of_danger dgs.(cx0).(cy0-1)) && (simt +. interval) < (level_of_danger dgs.(cx0).(cy0-1)) +. explosion_time) then begin (* West *)
if debug_all then Printf.printf "[crates] +West\n" ;
Queue.add (cx0, cy0-1, simt +. interval, 3) q ;
end;
if debug_all then Printf.printf "[crates] 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 then () else begin
Hashtbl.add visited (cx, cy) 1 ;
(*if debug_all then Printf.printf "[crates] exploring (%d, %d) at time %f for dir %d\n" cx cy cur_t direct ;*)
if gd.laby.(cx).(cy) = 2 then (* crate *)
raise (ReturnInt direct)
else if dgs.(cx).(cy) = Blocked then
()
else begin (* we need to go deeper *)
let dang_time = level_of_danger dgs.(cx).(cy) in
for dir = 0 to 3 do
let newx = cx + fst order.(dir)
and newy = cy + snd order.(dir)
and newt = cur_t +. interval in
if (is_valid newx newy lines cols) && not (newt > dang_time && newt < dang_time +. explosion_time) then
Queue.add (newx, newy, newt, direct) q
done
end
end
done;
(-1)
with
| ReturnInt k -> k
in
let move_with_caution (exit : bool) = let move_with_caution (exit : bool) =
let res = has_a_safe_path cxi cyi gd.dt in let res = has_a_safe_path_origin cxi cyi lines cols gd.dt interval gd dgs [|Bonus|] [||] [|Blocked|] [||] 5 in
if res <> -1 then begin if res <> 4 then begin
if debug_all then Printf.printf "[crates] success!\n" ; if debug_all then Printf.fprintf stderr "[crates] success 1/2!\n" ;
res res
end end
else begin else begin
if false && exit then let res2 = has_a_safe_path_origin cxi cyi lines cols gd.dt interval gd dgs [||] [|2|] [|Blocked|] [||] 80 in
current_status := ClaimLand ; if res2 <> 4 then begin
4 if debug_all then Printf.fprintf stderr "[crates] success 2/2!\n" ;
res2
end
else begin
if false && exit then (* TODO *)
current_status := ClaimLand ;
Printf.fprintf stderr "Exited.\n" ;
4
end
end end
in in
@ -593,7 +581,7 @@ let move_explore (gd: game_data) (dgs : danger array array) =
if gd.players.(pid).nbomb_atonce > 0 then begin if gd.players.(pid).nbomb_atonce > 0 then begin
current_status := EscapeDeath ; current_status := EscapeDeath ;
action := 1; action := 1;
if debug_all then Printf.printf "Fire in the hole!\n" ; if debug_all then Printf.fprintf stderr "Fire in the hole!\n" ;
end; end;
raise (ReturnInt 4) ; raise (ReturnInt 4) ;
end; end;
@ -602,7 +590,7 @@ let move_explore (gd: game_data) (dgs : danger array array) =
if gd.players.(pid).nbomb_atonce > 0 then begin if gd.players.(pid).nbomb_atonce > 0 then begin
current_status := EscapeDeath ; current_status := EscapeDeath ;
action := 1; action := 1;
if debug_all then Printf.printf "Fire in the hole!\n" ; if debug_all then Printf.fprintf stderr "Fire in the hole!\n" ;
end; end;
raise (ReturnInt 4) ; raise (ReturnInt 4) ;
end; end;
@ -611,7 +599,7 @@ let move_explore (gd: game_data) (dgs : danger array array) =
if gd.players.(pid).nbomb_atonce > 0 then begin if gd.players.(pid).nbomb_atonce > 0 then begin
current_status := EscapeDeath ; current_status := EscapeDeath ;
action := 1; action := 1;
if debug_all then Printf.printf "Fire in the hole!\n" ; if debug_all then Printf.fprintf stderr "Fire in the hole!\n" ;
end; end;
raise (ReturnInt 4) ; raise (ReturnInt 4) ;
end; end;
@ -620,12 +608,12 @@ let move_explore (gd: game_data) (dgs : danger array array) =
if gd.players.(pid).nbomb_atonce > 0 then begin if gd.players.(pid).nbomb_atonce > 0 then begin
current_status := EscapeDeath ; current_status := EscapeDeath ;
action := 1; action := 1;
if debug_all then Printf.printf "Fire in the hole!\n" ; if debug_all then Printf.fprintf stderr "Fire in the hole!\n" ;
end; end;
raise (ReturnInt 4) ; raise (ReturnInt 4) ;
end; end;
if debug_all then Printf.printf "[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";
(* go to one without stepping into a dangerous tile *) (* go to one without stepping into a dangerous tile *)
raise (ReturnInt (move_with_caution true)) ; raise (ReturnInt (move_with_caution true)) ;
@ -638,24 +626,30 @@ 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 -> ()
| 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 ?" ;;
(* ---------------------------------------------------------------------------------------------------------------------------------------------------- *) (* ---------------------------------------------------------------------------------------------------------------------------------------------------- *)
(* ---------------------------------------------------------------------------------------------------------------------------------------------------- *) (* ---------------------------------------------------------------------------------------------------------------------------------------------------- *)
let debug_game_data1 (gd : game_data) =
Printf.fprintf stderr "[player %d started turn]\n" gd.player_id ;;
let debug_game_data (gd : game_data) = let debug_game_data (gd : game_data) =
Printf.fprintf stderr "[player %d]\n" gd.player_id ;; Printf.fprintf stderr "[player %d ended turn]\n" gd.player_id ;;
(* ---------------------------------------------------------------------------------------------------------------------------------------------------- *) (* ---------------------------------------------------------------------------------------------------------------------------------------------------- *)
(* ---------------------------------------------------------------------------------------------------------------------------------------------------- *) (* ---------------------------------------------------------------------------------------------------------------------------------------------------- *)
get_meta_info () ;;
let game_d = parse_input "entrees.txt" ;; let game_d = parse_input "entrees.txt" ;;
let dangers = evaluate_dangers game_d ;; let dangers = evaluate_dangers game_d ;;
let gains = cell_values game_d ;; let gains = cell_values game_d ;;
get_meta_info game_d.player_id ;;
(*debug_game_data1 game_d ;;*)
if debug_data then begin if debug_data then begin
print_game_data game_d ; print_game_data game_d ;
print_danger_levels dangers ; print_danger_levels dangers ;
@ -664,15 +658,21 @@ end ;;
update_strat game_d dangers ;; update_strat game_d dangers ;;
let chosen = ref 0 ;;
let main_actions () = match !current_status with let main_actions () = match !current_status with
| EscapeDeath -> | EscapeDeath ->
Printf.printf "%d " (move_safe game_d dangers) chosen := move_safe game_d dangers ;
Printf.printf "%d " !chosen
| BlowUpCrates -> | BlowUpCrates ->
if dangers.(game_d.players.(game_d.player_id).xy.x).(game_d.players.(game_d.player_id).xy.y) = Safe then if dangers.(game_d.players.(game_d.player_id).xy.x).(game_d.players.(game_d.player_id).xy.y) = Safe then begin
Printf.printf "%d " (move_explore game_d dangers) chosen := move_explore game_d dangers ;
Printf.printf "%d " !chosen
end
else begin else begin
current_status := EscapeDeath ; current_status := EscapeDeath ;
Printf.printf "%d " (move_safe game_d dangers) chosen := move_safe game_d dangers ;
Printf.printf "%d " !chosen
end end
| ClaimLand -> | ClaimLand ->
() ()
@ -682,6 +682,10 @@ let main_actions () = match !current_status with
main_actions () ; main_actions () ;
Printf.printf "%d" !action ; Printf.printf "%d" !action ;
debug_game_data game_d ;; (*debug_game_data game_d ;;*)
set_meta_info () ;; Printf.fprintf stderr "[player %d went at direction " game_d.player_id ;;
print_direction !chosen ;;
Printf.fprintf stderr "with action %d] time at end : %f\n" !action game_d.dt ;;
set_meta_info game_d.player_id ;;

View File

@ -1 +1 @@
0 1

1
main_0.sav Normal file
View File

@ -0,0 +1 @@
1

1
main_1.sav Normal file
View File

@ -0,0 +1 @@
1

1
main_2.sav Normal file
View File

@ -0,0 +1 @@
0

1
main_3.sav Normal file
View File

@ -0,0 +1 @@
0

View File

@ -1 +0,0 @@
4 1