|
| 1 | + |
| 2 | +let maze_3_4 = ['#';'#';'#';'#'; |
| 3 | + '#';'A';'B';'#'; |
| 4 | + '#';'#';'#';'#'] |
| 5 | + |
| 6 | +let rec get_kth_element_from_list list = (fun k -> match k, list with |
| 7 | + | 1, h :: t -> h |
| 8 | + | _, h :: t-> get_kth_element_from_list t (k-1)) |
| 9 | + |
| 10 | +let k_3_4 = get_kth_element_from_list maze_3_4 |
| 11 | + |
| 12 | +let current_3_by_4_view_letters = ref ['A';'#';'#';'#';'B'] (* positions [6; 2; 10; 5; 7] in 3_by_4 maze *) |
| 13 | + |
| 14 | +let current_3_by_4_view_positions = ref [6;2;10;5;7] |
| 15 | + |
| 16 | +let update_3_by_4_view_positions c move = match move with |
| 17 | + | 'U' -> c := [((get_kth_element_from_list !current_3_by_4_view_positions 1)-4); |
| 18 | + ((get_kth_element_from_list !current_3_by_4_view_positions 2)-4); |
| 19 | + ((get_kth_element_from_list !current_3_by_4_view_positions 3)-4); |
| 20 | + ((get_kth_element_from_list !current_3_by_4_view_positions 4)-4); |
| 21 | + ((get_kth_element_from_list !current_3_by_4_view_positions 5)-4)] |
| 22 | + |
| 23 | + | 'D' -> c := [((get_kth_element_from_list !current_3_by_4_view_positions 1)+4); |
| 24 | + ((get_kth_element_from_list !current_3_by_4_view_positions 2)+4); |
| 25 | + ((get_kth_element_from_list !current_3_by_4_view_positions 3)+4); |
| 26 | + ((get_kth_element_from_list !current_3_by_4_view_positions 4)+4); |
| 27 | + ((get_kth_element_from_list !current_3_by_4_view_positions 5)+4)] |
| 28 | + |
| 29 | + | 'L' -> c := [((get_kth_element_from_list !current_3_by_4_view_positions 1)-1); |
| 30 | + ((get_kth_element_from_list !current_3_by_4_view_positions 2)-1); |
| 31 | + ((get_kth_element_from_list !current_3_by_4_view_positions 3)-1); |
| 32 | + ((get_kth_element_from_list !current_3_by_4_view_positions 4)-1); |
| 33 | + ((get_kth_element_from_list !current_3_by_4_view_positions 5)-1)] |
| 34 | + |
| 35 | + | 'R' -> c := [((get_kth_element_from_list !current_3_by_4_view_positions 1)+1); |
| 36 | + ((get_kth_element_from_list !current_3_by_4_view_positions 2)+1); |
| 37 | + ((get_kth_element_from_list !current_3_by_4_view_positions 3)+1); |
| 38 | + ((get_kth_element_from_list !current_3_by_4_view_positions 4)+1); |
| 39 | + ((get_kth_element_from_list !current_3_by_4_view_positions 5)+1)] |
| 40 | + |
| 41 | +let update_3_by_4_view_letters = (fun c -> |
| 42 | + c := [k_3_4 (get_kth_element_from_list !current_3_by_4_view_positions 1); |
| 43 | + k_3_4 (get_kth_element_from_list !current_3_by_4_view_positions 2); |
| 44 | + k_3_4 (get_kth_element_from_list !current_3_by_4_view_positions 3); |
| 45 | + k_3_4 (get_kth_element_from_list !current_3_by_4_view_positions 4); |
| 46 | + k_3_4 (get_kth_element_from_list !current_3_by_4_view_positions 5); |
| 47 | + ]) |
| 48 | +let rec list_of_chars_to_string clist = |
| 49 | + match clist with |
| 50 | + | [] -> "" |
| 51 | + | h :: t -> (String.make 1 (get_kth_element_from_list clist 1)) ^ (list_of_chars_to_string t);; |
| 52 | + |
| 53 | +let update_view = (fun m -> (update_3_by_4_view_positions current_3_by_4_view_positions m); |
| 54 | + update_3_by_4_view_letters current_3_by_4_view_letters; list_of_chars_to_string !current_3_by_4_view_letters) |
| 55 | + |
| 56 | +let starting_view = "A###B" |
| 57 | +let current_view = list_of_chars_to_string !current_3_by_4_view_letters;; |
| 58 | + |
| 59 | +let rec sort lst = |
| 60 | + match lst with |
| 61 | + [] -> [] |
| 62 | + | head :: tail -> insert head (sort tail) |
| 63 | +and insert elt lst = |
| 64 | + match lst with |
| 65 | + [] -> [elt] |
| 66 | + | head :: tail -> if elt <= head then elt :: lst else head :: insert elt tail |
| 67 | + |
| 68 | +let end_game () = "K" ^ (list_of_chars_to_string (sort(!letters_caught))) |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | +(* to reset *) |
| 73 | +(current_pos := (0,0); pos_visited := [(0,0)]; |
| 74 | +current_3_by_4_view_positions := [6;2;10;5;7]; |
| 75 | +current_3_by_4_view_letters := ['A';'#';'#';'#';'B']; |
| 76 | +letters_caught := []);; |
| 77 | + |
| 78 | + |
| 79 | +let rec lets_play current_view current_pos pos_visited = |
| 80 | + check_if_sitting_on_letter current_view; |
| 81 | + let move = (make_new_move (view_to_tuples current_view) current_pos pos_visited) in |
| 82 | + if move = 'K' then Printf.printf "%s\n%!" (end_game ()) else |
| 83 | + (Printf.printf "%s\n%!" current_view; |
| 84 | + Printf.printf "%c\n%!" move; |
| 85 | + let cp = (update_current_pos current_pos move) in |
| 86 | + let pv = (update_pos_visited pos_visited !cp) in |
| 87 | + lets_play (update_view move) cp pv) |
| 88 | + |
| 89 | +let start_game () = lets_play starting_view current_pos pos_visited |
| 90 | + |
| 91 | + (* to do |
| 92 | + 1. implement general solution algorithm |
| 93 | + 2. create function to sort and return character trophies |
| 94 | + *) |
| 95 | + |
| 96 | +(* testing slightly harder maze *) |
| 97 | + |
| 98 | + |
| 99 | +let maze_6_13 = ['#';'#';'#';'#';'#';'#';'#';'#';'#';'#';'#';'#';'#'; |
| 100 | + '#';' ';' ';' ';'A';' ';' ';' ';'#';' ';' ';' ';'#'; |
| 101 | + '#';' ';'#';'#';' ';'#';'#';' ';'#';' ';'#';' ';'#'; |
| 102 | + '#';' ';'Q';'#';' ';'#';' ';' ';' ';'B';' ';' ';'#'; |
| 103 | + '#';' ';' ';'#';'A';'#';' ';'#';' ';'#';'#';'#';'#'; |
| 104 | + '#';'#';'#';'#';'#';'#';'#';'#';'#';'#';'#';'#';'#';] |
| 105 | + |
| 106 | + |
| 107 | +let k_6_13 = get_kth_element_from_list maze_6_13 |
| 108 | + |
| 109 | +let current_6_by_13_view_letters = ref ['A';'#';' ';' ';' '] (* positions [18; 5; 31; 17; 19;] in 6_by_13 maze *) |
| 110 | + |
| 111 | +let current_6_by_13_view_positions = ref [18;5;31;17;19] |
| 112 | + |
| 113 | +let update_6_by_13_view_positions c move = match move with |
| 114 | + | 'U' -> c := [((get_kth_element_from_list !current_6_by_13_view_positions 1)-13); |
| 115 | + ((get_kth_element_from_list !current_6_by_13_view_positions 2)-13); |
| 116 | + ((get_kth_element_from_list !current_6_by_13_view_positions 3)-13); |
| 117 | + ((get_kth_element_from_list !current_6_by_13_view_positions 4)-13); |
| 118 | + ((get_kth_element_from_list !current_6_by_13_view_positions 5)-13)] |
| 119 | + |
| 120 | + | 'D' -> c := [((get_kth_element_from_list !current_6_by_13_view_positions 1)+13); |
| 121 | + ((get_kth_element_from_list !current_6_by_13_view_positions 2)+13); |
| 122 | + ((get_kth_element_from_list !current_6_by_13_view_positions 3)+13); |
| 123 | + ((get_kth_element_from_list !current_6_by_13_view_positions 4)+13); |
| 124 | + ((get_kth_element_from_list !current_6_by_13_view_positions 5)+13)] |
| 125 | + |
| 126 | + | 'L' -> c := [((get_kth_element_from_list !current_6_by_13_view_positions 1)-1); |
| 127 | + ((get_kth_element_from_list !current_6_by_13_view_positions 2)-1); |
| 128 | + ((get_kth_element_from_list !current_6_by_13_view_positions 3)-1); |
| 129 | + ((get_kth_element_from_list !current_6_by_13_view_positions 4)-1); |
| 130 | + ((get_kth_element_from_list !current_6_by_13_view_positions 5)-1)] |
| 131 | + |
| 132 | + | 'R' -> c := [((get_kth_element_from_list !current_6_by_13_view_positions 1)+1); |
| 133 | + ((get_kth_element_from_list !current_6_by_13_view_positions 2)+1); |
| 134 | + ((get_kth_element_from_list !current_6_by_13_view_positions 3)+1); |
| 135 | + ((get_kth_element_from_list !current_6_by_13_view_positions 4)+1); |
| 136 | + ((get_kth_element_from_list !current_6_by_13_view_positions 5)+1)] |
| 137 | + |
| 138 | +let update_6_by_13_view_letters = (fun c -> |
| 139 | + c := [k_6_13 (get_kth_element_from_list !current_6_by_13_view_positions 1); |
| 140 | + k_6_13 (get_kth_element_from_list !current_6_by_13_view_positions 2); |
| 141 | + k_6_13 (get_kth_element_from_list !current_6_by_13_view_positions 3); |
| 142 | + k_6_13 (get_kth_element_from_list !current_6_by_13_view_positions 4); |
| 143 | + k_6_13 (get_kth_element_from_list !current_6_by_13_view_positions 5); |
| 144 | + ]) |
| 145 | + |
| 146 | +let starting_view_h = "A# " |
| 147 | +let current_view_h = list_of_chars_to_string !current_6_by_13_view_letters;; |
| 148 | + |
| 149 | + (* to reset *) |
| 150 | +(current_pos := (0,0); pos_visited := [(0,0)]; |
| 151 | +current_6_by_13_view_positions := [18;5;31;17;19]; |
| 152 | +current_6_by_13_view_letters := ['A';'#';' ';' ';' ']; |
| 153 | +letters_caught := []);; |
| 154 | + |
| 155 | +let update_view1 = (fun m -> (update_6_by_13_view_positions current_6_by_13_view_positions m); |
| 156 | + update_6_by_13_view_letters current_6_by_13_view_letters; list_of_chars_to_string !current_6_by_13_view_letters) |
| 157 | + |
| 158 | + |
| 159 | +let rec lets_play_harder current_view current_pos pos_visited = |
| 160 | + check_if_sitting_on_letter current_view; |
| 161 | + let move = (make_new_move (view_to_tuples current_view) current_pos pos_visited) in |
| 162 | + if move = 'K' then Printf.printf "%s\n%!" (end_game ()) else |
| 163 | + (Printf.printf "%s\n%!" current_view; |
| 164 | + Printf.printf "%c\n%!" move; |
| 165 | + let cp = (update_current_pos current_pos move) in |
| 166 | + let pv = (update_pos_visited pos_visited !cp) in |
| 167 | + lets_play_harder (update_view1 move) cp pv) |
| 168 | + |
| 169 | +let start_game1 () = lets_play_harder starting_view_h current_pos pos_visited |
0 commit comments