Function doesn't print correctly

195 views Asked by At

So I have to create a "lights out" game. I have to create two functions. One function "flip"

val flip : bool array array -> int -> int -> bool array array = <fun>

that given a bool matrix and two integers i, j. it negates the values (true→false, false→true) at location i, j in the matrix, as well negating the values on the (up to) 4 horizontally/vertically adjacent elements.

This is my code about it:

let matrixz = [|
  [|true; true; false; false|];
  [|false; false; true; true|];
  [|true; false; true; false|];
  [|true; false; false; true|]
|];;
    
let flip_matrix matrix a b =
  let n = Array.length matrix in
  for i = 1 to n do 
    let n1 = Array.length matrix in
    for j = 1 to n1 do
      if i = a && j = b then begin 
        matrix.(i).(j) <- not matrix.(i).(j);
        matrix.(i+1).(j) <- not matrix.(i+1).(j);
        matrix.(i).(j+1) <- not matrix.(i).(j+1); 
        matrix.(i).(j-1) <- not matrix.(i).(j-1);
        matrix.(i-1).(j) <- not matrix.(i-1).(j);
      end;
    done;
  done;
  matrix;; 

Which I think is correct. But also I have to make another function:

val print_matrix : bool array array -> unit = <fun>

Which given a bool matrix, it prints it on screen (true →”T”, false→”F”).

This is my code about it:

let print_s matrix =
  let n = Array.length matrix in
  for i = 0 to n-1 do 
    let n1 = Array.length matrix in
    for j = 0 to n1-1 do
      print_string matrix.(i).(j);
    done; 
    print_string "/n";
  done;

This would be the correct output:

# flip matrix 1 4;;
# print_matrix matrix;;
FTFT
TFFF
FFTT

I know the second function is incorrect.

1

There are 1 answers

0
Chris On

First off, your code will be easier to figure out if it's nicely formatted. Spaces are cheap, so don't hesitate to use them.

let matrixz =         
  [|[|true; true; false; false|];
    [|false; false; true; true|];
    [|true; false; true; false|];
    [|true; false; false; true|]|]

let flip_matrix matrix a b =
  let n = Array.length matrix in
  for i = 1 to n do 
    let n1 = Array.length matrix in
    for j = 1 to n1 do
      if i = a && j = b then 
      begin 
        matrix.(i).(j) <- not matrix.(i).(j);
        matrix.(i + 1).(j) <- not matrix.(i+1).(j);
        matrix.(i).(j + 1) <- not matrix.(i).(j + 1); 
        matrix.(i).(j - 1) <- not matrix.(i).(j - 1);
        matrix.(i - 1).(j) <- not matrix.(i - 1).(j);
      end;
    done;
  done;
  matrix

The same applies to your print_s function.

let print_s matrix =
  let n = Array.length matrix in
  for i = 0 to n - 1 do 
    let n1 = Array.length matrix in
    for j = 0 to n1 - 1 do
      print_string matrix.(i).(j);
    done; 
    print_string "/n";
  done

This would work, but for two issues. print_string matrix.(i).(j) expects matrix.(i).(j) to be a string, but it's a boolean. You have to convert this to a string. OCaml is very strongly typed, so it won't do this kind of conversion implicitly for you.

The second one is really simple and when you got the type issues figured out. Printing /n won't give you a newline. Printing \n will.

Also, you may wish to investigate the iter function in the Array module to achieve printing.