帕斯卡战舰加载游戏



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

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

当我运行它时,它会弹出外部 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 时它们将包含随机值。 请参阅下面的更改/建议。 我还没有调试你的代码,这是由你做的。

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;                        

最新更新