Lights Out游戏flip函数实现及print_matrix函数打印异常技术咨询
Let's work through fixing both your flip and print_matrix functions for the Lights Out game. I'll break down the issues in each and provide corrected code with explanations.
1. Fixing the flip Function (Your flip_matrix Code)
First, there are several critical issues in your current flip_matrix implementation that will cause errors or incorrect behavior:
- 0-index vs 1-index confusion: OCaml arrays use 0-based indexing, but your loops start at 1 and run to
n(the number of rows). This means you'll never touch the first row/column, and you'll try to access indices beyond the array bounds (e.g.,matrix.(n)which doesn't exist). - Unchecked neighbor access: When flipping cells at the edge of the matrix (like
i=0orj=0), trying to accessi-1orj-1will throw an out-of-bounds error. You need to validate indices before modifying them. - Incorrect column length: You set
n1toArray.length matrix(the number of rows) instead ofArray.length matrix.(i)(the number of columns in the current row). - Function structure: The final
matrixreference is outside the function body, so the function won't return the modified matrix correctly.
Here's the corrected flip function that fixes all these issues:
let flip (matrix : bool array array) (i : int) (j : int) : bool array array = let rows = Array.length matrix in if rows = 0 then matrix else let cols = Array.length matrix.(0) in (* Helper to safely flip a cell only if it's within the matrix bounds *) let safe_flip x y = if x >= 0 && x < rows && y >= 0 && y < cols then matrix.(x).(y) <- not matrix.(x).(y) in (* Flip the target cell and its four adjacent neighbors *) safe_flip i j; safe_flip (i + 1) j; safe_flip (i - 1) j; safe_flip i (j + 1); safe_flip i (j - 1); matrix
Key improvements:
- Uses a
safe_fliphelper to avoid out-of-bounds errors by checking indices first. - Correctly works with OCaml's 0-indexed arrays.
- Returns the modified matrix properly, and handles empty matrices as an edge case.
Note: Your example mentions calling flip matrix 1 4;;—but your 4x4 matrix has columns indexed 0-3, so j=4 is out of bounds. You probably meant flip matrix 1 3 instead.
2. Fixing the print_matrix Function (Your print_s Code)
Your print_s function has three main issues preventing it from working correctly:
- Trying to print
boolvalues directly:print_stringexpects a string, but you're passing a boolean value—this will throw a type error. You need to converttrueto"T"andfalseto"F". - Incorrect newline escape: You used
/ninstead of\n—/nis just a literal string, not a newline character, so your output won't break into rows. - Wrong column length calculation: You set
n1toArray.length matrix(number of rows) instead ofArray.length matrix.(i)(number of columns in the current row).
Here's the corrected print_matrix function:
let print_matrix (matrix : bool array array) : unit = let rows = Array.length matrix in for i = 0 to rows - 1 do let cols = Array.length matrix.(i) in for j = 0 to cols - 1 do (* Convert boolean to the corresponding string character *) let cell_str = if matrix.(i).(j) then "T" else "F" in print_string cell_str; done; (* Print a newline after each row to match your expected output *) print_string "\n" done
This function will now:
- Convert each boolean value to "T" or "F" before printing.
- Print each row on a new line using the correct
\nescape sequence. - Correctly iterate over all columns in each row.
Testing this with your sample matrix (after a valid flip call) will produce the formatted output you expect.
内容的提问来源于stack exchange,提问作者idkrlly

