帕斯卡战舰加载游戏

Pascal Battleships Loadgame

以下代码使用以 0/8B1/8B1/8B1/1P6B1/1P8/7S2/7S2/1AAAAA1S2/5DDD2 格式保存在文本文件中的游戏

(例如)并将正确的棋子放入棋盘中。数字代表一系列连续的空格,/ 表示新的一行。这些字母代表板的那个单元格中的船(二维数组)。

当我 运行 它与 EXTERNAL SIGSEGV 一起出现并向我显示汇编代码时说 00403D61 833a00 cmpl $0x0,(%edx)

有谁知道它出了什么问题以及如何解决?

Procedure LoadGame(FileName : String; Var Board : TBoard);
Var
  Line : String;
  CurrentFile : Text;
  Row , count : Integer;
  column, counter: Integer;
Begin
  AssignFile(CurrentFile, FileName);
  Reset(CurrentFile);
  Readln(CurrentFile, Line);
  for counter := 1 to length(line) do
  begin
    if (Line[counter] in ['A'..'Z','m','h']) then
    begin
      board[row,column]:=line[counter];
      column:=column+1;
    end
    else
      if line[counter]='0' then
      begin
        for column := 0 to 9 do
        begin
          board[row,column]:='-';
        end;
      end
      else
        If line[counter]='/' then
        begin
          row :=row+1;
          column:=0;
        end
        else 
          for count := 0 to (strtoint(line[counter])-1) do
          begin
            Board[row,column+count] :='-';
            column:=column+1;
          end;
  end;
  CloseFile(CurrentFile);
End;                        

您在使用变量 rowcolumn 之前没有初始化它们。您必须这样做,因为它们是局部变量(在堆栈上),因此在调用 LoadGame 时它们将包含随机值。请参阅下面的 changes/suggestions。我没有调试你的代码,那是你要做的。

Procedure LoadGame(FileName : String; Var Board : TBoard);
Var
  Line : String;
  CurrentFile : Text;
  Row , count : Integer;
  column, counter: Integer;
Begin
  // Ideally, you should initialise each cell of the board with some value not used in the following so you can easily verify the effect of the loading operation

  AssignFile(CurrentFile, FileName);
  Reset(CurrentFile);
  Readln(CurrentFile, Line);

  // initialise Row and Column
  Row := 0;     // assuming the cells are zero-based
  Column := 0;  // ditto

  for counter := 1 to length(line) do
    begin
      if (Line[counter] in ['A'..'Z','m','h']) then
      begin
        board[row,column]:=line[counter];
        column:=column+1;
      end
      else
      if line[counter]='0' then
      begin
        for column := 0 to 9 do
          begin
            board[row,column]:='-';
          end;
      end
      else If line[counter]='/' then
      begin
        row :=row+1;
        column:=0;
      end

      else for count := 0 to (strtoint(line[counter])-1) do
      begin
        Board[row,column+count] :='-';
        column:=column+1;
      end;
  end;

  CloseFile(CurrentFile);
End;