Quantcast
Channel: Active questions tagged ocaml - Stack Overflow
Viewing all articles
Browse latest Browse all 527

Insert function for a matrix in OCaml is not working

$
0
0
class virtual ['a] matrix_game m pf =  object     val matrix : 'a array array = m    val rows = Array.length m    val cols = Array.length m.(0)    val print_function : 'a -> unit = pf    method print =      for i = (-1) to cols do         print_char '-'      done;      print_char '\n';      Array.iter         (fun x ->           print_char '|';           Array.iter (fun y -> print_function y) x;           print_endline "|")         matrix;      for i = (-1) to cols do         print_char '-'      done;      print_char '\n'    method private is_out_of_bounds (y, x) =       y >= rows || y < 0 || x >= cols || x < 0    method virtual private is_legal : int * int -> 'a -> bool    method virtual insert : int * int -> 'a -> unit  endclass sudoku =  object(self)    inherit ['a] matrix_game (Array.make_matrix 9 9 0) (fun x -> print_char '') as parent   val m = Array.make_matrix 9 9 0 (*override first def of matrix*)    method private is_out_of_bounds (y, x) =      y >= 9 || y < 0 || x >= 9 || x < 0    method private check_value v = v > 0 && v < 10    method private check_existence (y, x) v =      let empty_value = 0 in      let row_length = Array.length m in      let col_length = if row_length > 0 then Array.length m.(0) else 0 in      if y >= 0 && y < row_length && x >= 0 && x < col_length then        m.(y).(x) <> empty_value      else        false    method private is_legal (y, x) v =      not (self#is_out_of_bounds (y, x)) &&       self#check_value v &&       not (self#check_existence (y, x) v)    method insert (y, x) v =      if self#is_legal (y, x) v then        m.(y).(x) <- v      else        print_endline "Illegal move!"    method print : unit =      parent#print  end

methods insert and print are not working.

let s1 = new sudoku ;; s1#insert (3,3) 5 ;; s1#print;; 

it should insert 5 to (3,3) in the matrix and print it. I'm stuck on this part. Sorry if this is dumb but any help is appreciated.


Viewing all articles
Browse latest Browse all 527

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>