Pascal Battleships Loadgame

时间:2016-06-04 16:08:58

标签: pascal lazarus

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

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

当我运行它时,它带有EXTERNAL SIGSEGV并显示汇编代码00403D61 833a00 cmpl $ 0x0,(%edx)

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

<table id="myTable">
  <tr>
    <td contentEditable="true"></td>
    <td contentEditable="true"></td>
    <td contentEditable="true"></td>
    <td contentEditable="true"></td>
  </tr>
  <tr>
    <td contentEditable="true"></td>
    <td contentEditable="true"></td>
    <td contentEditable="true"></td>
    <td contentEditable="true"></td>
  </tr>
  <tr>
    <td contentEditable="true"></td>
    <td contentEditable="true"></td>
    <td contentEditable="true"></td>
    <td contentEditable="true"></td>
  </tr>
  <tr>
    <td contentEditable="true"></td>
    <td contentEditable="true"></td>
    <td contentEditable="true"></td>
    <td contentEditable="true"></td>
  </tr>
</table>

1 个答案:

答案 0 :(得分:1)

在使用变量之前,您尚未初始化变量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;