【发布时间】:2021-05-09 10:46:44
【问题描述】:
所以我必须创建一个“熄灯”游戏。我必须创建两个函数。一个功能“翻转”
val 翻转 : bool array array -> int -> int -> bool array array =
表示给定一个bool矩阵和两个整数i,j。它否定了价值观 (真→假,假→真)在矩阵中的位置i,j,以及否定 (最多)4 个水平/垂直相邻元素的值。
这是我的代码:
`
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;; `
我认为是正确的。但我还必须做另一个功能:
val print_matrix : bool array array -> unit =
给定一个布尔矩阵,它将它打印在屏幕上(真→“T”,假→“F”)。
这是我的代码:
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;
这将是正确的输出:
# flip matrix 1 4;;
# print_matrix matrix;;
FTFT
TFFF
FFTT
我知道第二个函数不正确。我非常感谢 Ocaml 的一些帮助和建议。
【问题讨论】:
标签: ocaml imperative