You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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=0 or j=0), trying to access i-1 or j-1 will throw an out-of-bounds error. You need to validate indices before modifying them.
  • Incorrect column length: You set n1 to Array.length matrix (the number of rows) instead of Array.length matrix.(i) (the number of columns in the current row).
  • Function structure: The final matrix reference 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_flip helper 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 bool values directly: print_string expects a string, but you're passing a boolean value—this will throw a type error. You need to convert true to "T" and false to "F".
  • Incorrect newline escape: You used /n instead of \n—/n is just a literal string, not a newline character, so your output won't break into rows.
  • Wrong column length calculation: You set n1 to Array.length matrix (number of rows) instead of Array.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 \n escape 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 16:27:44