Search code examples
pascallazarus

Pascal Battleships Loadgame


The following code uses a game saved in a text file in the form 0/8B1/8B1/8B1/1P6B1/1P8/7S2/7S2/1AAAAA1S2/5DDD2

(for example) and puts the correct pieces in a board. The numbers represent a series of consecutive spaces and / means a new row. The letters represent the ship in that cell of the board ( a two dimensional array).

When I run it it come sup with EXTERNAL SIGSEGV and shows me assembly code saying 00403D61 833a00 cmpl $0x0,(%edx)

Does anyone know what is wrong with it and how to fix it?

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;                        

Solution

  • You haven't initialised the variables row and column before you use them. You must do that, because since they are local variables (on the stack), they will contain random values when LoadGame is called. See changes/suggestions below. I haven't debugged your code, that's for you to do.

    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;