Skip to content

Commit 75c1ea1

Browse files
committed
implemented function 'make_new_move'
1 parent f43777d commit 75c1ea1

File tree

1 file changed

+40
-7
lines changed

1 file changed

+40
-7
lines changed

maze_fun.ml

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,22 @@ let rec maze_robot = ()
1010

1111
let current_pos = ref (0,0)
1212
let update_current_pos p = (fun c -> match c , !p with
13-
| 'U' , (a,b) -> p := (a+1,b); p
14-
| 'D' , (a,b) -> p := (a-1,b); p
15-
| 'R' , (a,b) -> p := (a,b+1); p
16-
| 'L' , (a,b) -> p := (a,b-1); p
13+
| 'U' , (a,b) -> p := (a,b+1); p
14+
| 'D' , (a,b) -> p := (a,b-1); p
15+
| 'R' , (a,b) -> p := (a+1,b); p
16+
| 'L' , (a,b) -> p := (a-1,b); p
1717
)
1818

1919
let pos_visited = ref [(0,0)];;
2020

2121
let update_pos_visited p = (fun cp -> (p := (cp :: !p)); p);;
22-
22+
23+
let rec visited_already pos_list pos = match pos_list with
24+
| [] -> false
25+
| h::t -> if (h = pos) then true else (we_visited_already t pos)
26+
27+
let havent_visited_yet pos_list pos = not (we_visited_already pos_list pos)
28+
2329
let letters_caught = ref []
2430

2531
let remove_first_letter = (fun s -> String.sub s 1 ((String.length s) - 1))
@@ -34,7 +40,25 @@ let rec there_is_a_letter_we_can_move_to = (fun s -> match (string_to_list_of_c
3440
| h :: t -> if (h = '#' || h = ' ') then (there_is_a_letter_we_can_move_to (remove_first_letter s))
3541
else
3642
true)
37-
43+
44+
let rec join l1 l2 = match l1, l2 with
45+
| [], [] -> []
46+
| h1::t1, h2::t2 -> (h1,h2) :: join t1 t2;;
47+
48+
let view_to_tuples current_view =
49+
let l1 = string_to_list_of_chars (remove_first_letter current_view) in
50+
let l2 = ['U';'D';'L';'R'] in
51+
join l1 l2;;
52+
53+
(* method below moves to first new square available --> looks Up, then down, then left then right *)
54+
let rec make_new_move view_tuples current_pos pos_list = match view_tuples, current_pos with
55+
| [], _ -> 'K' (* no new move available *)
56+
| (_,'#') :: t , _ -> make_new_move t current_pos pos_list
57+
| ('U',_) :: t , (x,y) -> if (havent_visited_yet !pos_list (x,y+1)) then 'U' else make_new_move t current_pos pos_list
58+
| ('D',_) :: t , (x,y) -> if (havent_visited_yet !pos_list (x,y-1)) then 'D' else make_new_move t current_pos pos_list
59+
| ('L',_) :: t , (x,y) -> if (havent_visited_yet !pos_list (x-1,y)) then 'L' else make_new_move t current_pos pos_list
60+
| ('R',_) :: t , (x,y) -> if (havent_visited_yet !pos_list (x+1,y)) then 'R' else make_new_move t current_pos pos_list
61+
3862

3963
let check_if_sitting_on_letter = (fun s -> match (String.get s 0) with
4064
| '#' -> (letters_caught := !letters_caught)
@@ -59,7 +83,16 @@ let move_to_first_available_space = (fun s ->
5983
let make_naive_move = (fun s -> (check_if_sitting_on_letter s); if (there_is_a_letter_we_can_move_to s) then (move_to_first_available_letter s)
6084
else
6185
(move_to_first_available_space s))
62-
86+
let make_slightly_better_move current_pos current_view pos_list =
87+
check_if_sitting_on_letter current_view;
88+
if (there_is_a_new_letter current_view current_pos pos_list) then (move_to_first_available_letter s)
89+
else
90+
move_to_first_available_space current_view;;
91+
(*
92+
let make_even_better_move current_pos current_view pos_list =
93+
check_if_sitting_on_letter current_view;
94+
move_to_all_new_locations
95+
*)
6396

6497
let sort_char_list_alphabetically = ()
6598
let concatenate_list = ()

0 commit comments

Comments
 (0)