-3

I have tried to make a simple snake game with Free Pascal, when I started the programme, it drew the map exactly what I want but after that, I pressed the button that I have set to control the snake and it exited with exit code 201.

I don't know much about that exit code, could you explain me the problems of the programme? This is the longest program I have ever made with Pascal.

Here is the code:

    uses crt;

    type 
      ran=record
        x:byte;
        y:byte;
      end;

    var 
      f:ran;
      s:array[1..1000] of ran;
      i,j:longint;
      st,l:byte;

    function getkey:integer;
    var 
      k:integer;
    begin
      k:=ord(readkey);
      if k=0 then k:=-ord(readkey);
      getkey:=k;
    end;

    procedure fa;
    begin
      randomize;
      f.x:=random(98)+1;
      f.y:=random(23)+1;
      gotoxy(f.x,f.y);
      writeln('o');
    end;

    procedure draw;
    begin
      gotoxy(1,1);
      st:=1;
      for i:=1 to 25 do begin
        for j:=1 to 100 do write('X');
        writeln
      end;
      gotoxy(st+1,st+1);
      for i:=1 to 23 do begin
        for j:=1 to 98 do write(' ');
        gotoxy(st+1,i+2);
      end;
    end;

    procedure sts;
    begin
      s[1].x:=19;
      s[1].y:=6;
      gotoxy(s[1].x,s[1].y);
      writeln('@');
    end;

    procedure fa1;
    begin
      f.x:=29;
      f.y:=5;
      gotoxy(f.x,f.y);
      writeln('o');
    end;

    procedure eat;
    begin
      if (s[1].x=f.x) and (s[1].y=f.y) then begin
        l:=l+1;
        fa;
      end;
    end;

    function die:boolean;
    begin
      die:=false;
      if (s[1].x=1) or (s[1].x=100) or (s[1].y=1) or (s[1].y=25) then 
        die:=true;
      if l>=5 then
        for i:=5 to l do
          if (s[1].x=s[i].x) and (s[1].y=s[i].y) then 
            die:=true;
    end;

    procedure up;
    begin
      for i:=l downto 2 do begin
        s[i].y:=s[i-1].y;
        gotoxy(s[i].x,s[i].y);
        writeln('+');
      end;
      gotoxy(s[l].x,s[l].y+1);
      writeln(' ');
      s[1].y:=s[1].y-1;
      gotoxy(s[1].x,s[1].y);
      writeln('@');
    end;

    procedure down;
    begin
      for i:=l downto 2 do begin
        s[i].y:=s[i-1].y;
        gotoxy(s[i].x,s[i].y);
        writeln('+');
      end;
      gotoxy(s[l].x,s[l].y-1);
      writeln(' ');
      s[1].y:=s[1].y+1;
      gotoxy(s[1].x,s[1].y);
      writeln('@');
    end;

    procedure left;
    begin
      for i:=l downto 2 do begin
        s[i].x:=s[i-1].x;
        gotoxy(s[i].x,s[i].y);
        writeln('+');
      end;
      gotoxy(s[l].x+1,s[l].y);
      writeln(' ');
      s[1].x:=s[1].x-1;
      gotoxy(s[1].x,s[1].y);
      writeln('@');
    end;

    procedure right;
    begin
      for i:=l downto 2 do begin
        s[i].x:=s[i-1].x;
        gotoxy(s[i].x,s[i].y);
        writeln('+');
      end;
      gotoxy(s[l].x-1,s[l].y);
      writeln(' ');
      s[1].x:=s[1].x+1;
      gotoxy(s[1].x,s[1].y);
      writeln('@');
    end;

    procedure auto(k:integer);
    begin
      case k of
        -72:up;
        -80:down;
        -75:left;
        -77:right;
        119:up;
        115:down;
        97:left;
        100:right;
      end;
    end;

    procedure ingame(t:integer);
    var 
      d,e:boolean;
    begin
      repeat
        auto(t);
        d:=die;
        if d=true then exit;
        eat;
      until (keypressed);
      if keypressed then t:=getkey;
      case t of
        -72:up;
        -80:down;
        -75:left;
        -77:right;
        119:up;
        115:down;
         97:left;
        100:right;
      end;
      eat;
      d:=die;
      if d=true then exit;
    end;

    procedure first;
    var
      k:integer;
    begin
      draw;
      fa1;
      sts;
      if keypressed then k:=getkey;
      ingame(k);
    end;

    BEGIN
      clrscr;
      first;
      readln
    END.
halfer
  • 19,824
  • 17
  • 99
  • 186
Nam Anh
  • 1
  • 1
  • 1

2 Answers2

1

I googled this: 201 : range error, so you probably go out of array bounds. The only array s in indexed by variables that depend on l value (weird name, BTW). But I see a single place where you do changing (increment) this variable and don't see any l initialization. So you are using arbitrary starting value (here perhaps zero because l is global).

Note that you could discover this bug (and perhaps others) with simple debugging.

MBo
  • 77,366
  • 5
  • 53
  • 86
0

The code 201 seems to be explained for example here: Runtime Error 201 at fpc

Exactly why this happens in your code, I don't know.

Arndt Jonasson
  • 856
  • 1
  • 6
  • 14