board - let newv = Array.make_matrix sz sz false in (for i...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
(* string of board contents, width *) type bd = string * int t (* board, x, y, list of characters followed, array of visited points *) type cursor = bd * int * int * char list * (bool array array) t let load_board f = let ic = open_in f in try let rec readline lines = try let line = input_line ic in readline (line::lines) with End_of_file -> String.concat "" (List.rev lines) in let bd = readline [] in let size = int_of_float (sqrt (float_of_int (String.length bd))) in (close_in ic; (bd, size)) with e -> close_in_noerr ic; raise e let width (_,sz) = sz l let get (bd,sz) x y = bd.[x*sz+y] l let cursor ((_,sz) as bd) x y = let visited = Array.make_matrix sz sz false in visited.(x).(y) <- true; (bd, x, y, [get bd x y], visited) let move_cursor ((_,sz) as bd,x1,y1,wd,v) x2 y2 =
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: let newv = Array.make_matrix sz sz false in (for i = 0 to sz - 1 do for j = 0 to sz - 1 do newv.(i).(j) &lt;- v.(i).(j) done done); newv.(x2).(y2) &lt;- true; (bd, x2, y2, wd @ [get bd x2 y2], newv) let coordinates_of_cursor (_,x,y,_,_) = (x, y) l let get_word (_,_,_,wd,_) = List.fold_left (fun a c -&gt; a ^ (Char.escaped c)) &quot;&quot; wd let is_valid_coordinate (_,sz) x y = x &gt;= 0 &amp;&amp; x &lt; sz &amp;&amp; y &gt;= 0 &amp;&amp; y &lt; sz let get_visited (_,_,_,_,v) x y = v.(x).(y) l let unvisited_neighbors ((bd,x,y,_,_) as c) = let check a (x, y) = if is_valid_coordinate bd x y &amp;&amp; (not (get_visited c x y)) then (x,y)::a else a in List.fold_left check [(x-1,y-1);(x,y-1);(x+1,y-1); (x-1,y);(x+1,y); (x-1,y+1);(x,y+1);(x+1,y+1)];...
View Full Document

Ask a homework question - tutors are online