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.