From 1b188c8cfd9cd5ed776a97356a084313c08a82af Mon Sep 17 00:00:00 2001 From: "Glenn Y. Rolland" Date: Sun, 7 Aug 2016 17:02:57 +0200 Subject: [PATCH] It compiles ! --- coders-strike-back/code6.ml | 164 ++++++++++++++++++++++++------------ 1 file changed, 112 insertions(+), 52 deletions(-) diff --git a/coders-strike-back/code6.ml b/coders-strike-back/code6.ml index 27a6344..1d17c3d 100644 --- a/coders-strike-back/code6.ml +++ b/coders-strike-back/code6.ml @@ -51,21 +51,36 @@ let module Vector = struct end in let module Pod = struct + type orientation_t = + | Behind + | Aside + | WideAligned + | NearAligned + | Aligned + + type distance_t = + | Touching + | Near + | Anywhere + | Far + type t = { - mutable id : int ; - mutable pos : Vector.t ; - mutable dir : Vector.t ; - mutable has_boost : bool ; - mutable checkpoint_pos : Vector.t ; - mutable checkpoint_dist : int ; + mutable id : int ; + mutable pos : Vector.t ; + mutable old_pos : Vector.t ; + mutable dir : Vector.t ; + mutable has_boost : bool ; + mutable checkpoint_pos : Vector.t ; + mutable checkpoint_dist : int ; mutable checkpoint_angle : int ; - mutable output_str : string + mutable output_str : string } let create () = { id = 0 ; pos = Vector.create 0 0 ; + old_pos = Vector.create 0 0 ; dir = Vector.create 0 0 ; has_boost = true ; checkpoint_pos = Vector.create 0 0 ; @@ -84,6 +99,45 @@ let module Pod = struct pod.checkpoint_angle <- checkpoint_angle ; pod + let movement pod = + Vector.diff pod.pos pod.old_pos + + let is_aligned pod = + pod.checkpoint_angle > -2 && + pod.checkpoint_angle < 2 + + let is_near_aligned pod = + (pod.checkpoint_angle > -15 && pod.checkpoint_angle <= -2) || + (pod.checkpoint_angle >= 2 && pod.checkpoint_angle < 15) + + let is_wide_aligned pod = + (pod.checkpoint_angle > -45 && pod.checkpoint_angle <= -15) || + (pod.checkpoint_angle >= 15 && pod.checkpoint_angle < 45) + + let is_aside pod = + (pod.checkpoint_angle > -85 && pod.checkpoint_angle <= -45) || + (pod.checkpoint_angle >= 45 && pod.checkpoint_angle < 85) + + let is_behind pod = + pod.checkpoint_angle >= 85 || pod.checkpoint_angle <= -85 + + let is_checkpoint_far pod = + Vector.is_far pod.pos pod.checkpoint_pos + + let target_orientation pod = + if is_aligned pod then Aligned + else if is_near_aligned pod then NearAligned + else if is_wide_aligned pod then WideAligned + else if is_aside pod then Aside + else Behind + + let target_distance pod = + let dist = Vector.distance pod.pos pod.checkpoint_pos in + if dist <= (checkpoint_ray) then Touching + else if dist <= (checkpoint_ray * 3) then Near + else if dist > (checkpoint_ray * 2 * 3) then Far + else Anywhere + end in @@ -123,57 +177,61 @@ end in let module Game = struct type t = { - pods : Pod.t array ; - opponents : Pod.t array ; - ticks : int ; + pods : Pod.t array ; + opponents : Pod.t array ; + mutable turn : int ; } + + let create () = + { + pods = Array.make 2 (Pod.create ()) ; + opponents = Array.make 2 (Pod.create ()) ; + turn = 0 + } + + let is_first_turn game = game.turn < 2 + + let save_pods game = + (* old_pod := pod ; *) + ignore game + + let tick game = + game.turn <- game.turn + 1 + end in let module Strategy = struct - let apply game pod = - if !first_turn then begin - old_pod := pod ; - first_turn := false + let apply game pod = + if Game.is_first_turn game then begin + Game.save_pods game ; end ; - let movement = Vector.diff pod !old_pod in + let movement = Pod.movement in + (* if (CheckpointList.inc !checkpoint_list checkpoint) then begin prerr_endline "List already includes this checkpoint" ; end else begin checkpoint_list := (CheckpointList.add !checkpoint_list checkpoint) ; fprintf stderr "New checkpoint : %s\n%!" (Vector.to_string checkpoint); end ; + *) - (* output "x y thrust" : the target position + the power *) - let is_aligned = checkpoint_angle > -2 && checkpoint_angle < 2 in - let is_near_aligned = - (checkpoint_angle > -15 && checkpoint_angle <= -2) || - (checkpoint_angle >= 2 && checkpoint_angle < 15) - in - let is_wide_aligned = - (checkpoint_angle > -45 && checkpoint_angle <= -15) || - (checkpoint_angle >= 15 && checkpoint_angle < 45) - in - let is_aside = - (checkpoint_angle > -85 && checkpoint_angle <= -45) || - (checkpoint_angle >= 45 && checkpoint_angle < 85) - in - let is_behind = checkpoint_angle >= 85 || checkpoint_angle <= -85 in let use_boost = - (!tick > 100) && is_aligned && (Vector.is_far pod checkpoint) + (game.turn > 100) && + (Pod.is_aligned pod) && + (Pod.is_checkpoint_far pod) in - debug "angle = %d\n%!" checkpoint_angle ; - let thrust = (* droit devant *) - if is_behind && (Vector.is_touching pod opponent) then 20 - else if is_aside && (Vector.is_touching pod opponent) then 20 - else if is_behind then 6 - else if is_aside && (Vector.is_near checkpoint pod) then 10 - else if is_wide_aligned && (Vector.is_near checkpoint pod) then 20 - else if is_near_aligned && (Vector.is_near checkpoint pod) then 40 - else 100 + match (Pod.target_orientation pod, Pod.target_distance pod) with + (* | (Behind, TouchingOpponent) -> 20 + | (Aside, TouchingOpponent) -> 20 *) + | (Behind, _) -> 6 + | (Aside, Near) -> 10 + | (WideAligned, Near) -> 20 + | (NearAligned, Near) -> 40 + | (_, _) -> 100 in let power_str = match use_boost with @@ -181,7 +239,10 @@ let module Strategy = struct | true -> "BOOST" in - let target = Vector.add checkpoint (Vector.mult movement (-3)) in + let target = + Vector.mult (Pod.movement pod) (-3) + |> Vector.add pod.checkpoint_pos + in printf "%d %d %s\n%!" target.x target.y power_str ; @@ -230,26 +291,25 @@ let parse_init () = done in -let pods = Array.make 2 (Pod.create ()) in let game = Game.create () in (* game loop *) while true do (* read pod1 line & update game data *) - parse_pod_line () |> Pod.update game.pods.(0) ; - parse_pod_line () |> Pod.update game.pods.(1) ; - parse_pod_line () |> Pod.update game.opponents.(0) ; - parse_pod_line () |> Pod.update game.opponents.(1) ; + parse_pod_line () |> Pod.update game.pods.(0) |> ignore ; + parse_pod_line () |> Pod.update game.pods.(1) |> ignore ; + parse_pod_line () |> Pod.update game.opponents.(0) |> ignore ; + parse_pod_line () |> Pod.update game.opponents.(1) |> ignore ; for i = 0 to 1 do game.pods.(i) - |> Pod.update pod1_data - |> Strategy.apply - |> fun pod -> print_endline pod.output_str ; - done + (* |> Strategy.apply *) + |> (fun (pod : Pod.t) -> pod.output_str) + |> print_endline + ; + done ; - tick := !tick + 1 ; - old_pod := pod ; + Game.tick game ; done;