[SOLVED] Exit code 201 Snake Game lazarus / 61-Beginners ---------------------------------------------------------------------- DiCri · 2016-06-15 17:39 I don't know why... I only know that it works only on Turbo Pascal but i've choosed Free Pascal 'cause it has more functions and a good compiler.. Here is the source code of the Snake game.. Simple but it tells exit code 201 at the end of the procedure createscore. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program snake;uses crt;var        x,y:array[1..1000] of integer;        xs,ys:integer;        input:char;        i,j:integer;procedure createscore;        begin                 randomize;                xs:=0;                ys:=0;                xs:=random(79)+1;              ys:=random(23)+1;                gotoxy(xs,ys);                write('.');          end;procedure incscore;        begin                      if (x[1]=xs) and (y[1]=ys) then begin                              gotoxy(xs,ys);                                write(' ');                              j:=j+1;                                xs:=0;                                ys:=0;                              gotoxy(1,1);                                writeln(i,',',j,',',xs,',',ys,',',x[1],',',y[1]);                              input:=readkey;                                createscore;                                end;        end; procedure checkdie;      begin              {  for i:=2 to j do begin                      if (x[1]=x[i]) and (y[1]=y[i]) then                                input:='q';                        end;}                end;begin      clrscr;        randomize;        cursoroff;        createscore;      for i:=1 to 3 do begin        x[i]:=random(80)+1;        y[i]:=random(23)+1;        end;        gotoxy(x[1],y[1]);        write('O');        j:=3;        repeat                if keypressed then input:=readkey;                        for i:=1 to j do                              begin                                gotoxy(x[i],y[i]);                                write(' ');                              end;                        for i:=j downto 2 do                                begin                                      x[i]:=x[i-1];                                        y[i]:=y[i-1];                                end;                      if input='w' then y[1]:=y[1]-1;                        if input='s' then y[1]:=y[1]+1;                        if input='a' then x[1]:=x[1]-1;                        if input='d' then x[1]:=x[1]+1;                      for i:=1 to j do                                begin                                gotoxy(x[i],y[i]);                              write('O');                                end;                      incscore;                          delay(50);              until input='q'; end.  Please answer me! Thanks ---------------------------------------------------------------------- skalogryz · 2016-06-15 17:54 are you running it on Windows?, -> if yes, are you compiling it with Lazarus, ---> if yes, check your Project Options -> Config and Target. "Win32 Gui application" must be UNCHECKED. ---> or ---> or you can add the following {$APPTYPE} reference into your source code, like this Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program snake;{APPTYPE CONSOLE}uses crt;.. ---------------------------------------------------------------------- Bart · 2016-06-15 18:13 For me it crashes with a range check error on this line: Code: [Select]                                gotoxy(x[i],y[i]); At that point  i = 1, j = 3, x = 7, y = 256 The screen looks like: Code: [Select]        O        OO        OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   O OO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO    OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO  OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   O OO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO    OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO  OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   O OO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO    OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO  OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   O OO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO    OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO  OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   O OO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO    OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO  OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   O OO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO   OOO    OOO   OOO  I started the program, then pressed "s" once and let it run it's course. Bart ---------------------------------------------------------------------- DiCri · 2016-06-15 18:16 I am using normal Free Pascal and not Lazarus ---------------------------------------------------------------------- rvk · 2016-06-15 18:36 Quote from: Manu12x on June 15, 2016, 07:39:22 pmHere is the source code of the Snake game.. Simple but it tells exit code 217 at the end of the procedure createscore. Not really a clear question. In the question you say exit code 201 and in the text you mention exit code 217. Which is it? Also... you don't say WHEN you get this error. When I run this, I don't get an exit code until I press w and the snake comes to the top of the screen. In that case I get a exit code 201. You don't check for getting to the edge of the screen so if the snake gets to the edge you either get an exit code 201 or the program gives lots of 000. Build in some checks for this and your program should run fine. ---------------------------------------------------------------------- skalogryz · 2016-06-15 18:46 Quote from: Manu12x on June 15, 2016, 08:16:49 pmI am using normal Free Pascal and not Lazarus sure. but please try adding {$APPTYPE CONSOLE} and let us know if it fixed the issue. ---------------------------------------------------------------------- DiCri · 2016-06-15 18:55 I don't want to fix when snake reaches limits of screen.. i know how to do it but it is not the problem i told. When running the game, there is a dot (must be the apple) but when the snake takes it, it tells exit code 201 on free pascal.. Trying to put {$APPTYPE CONSOLE}. I'm italian so i'm wrong with some English words ---------------------------------------------------------------------- skalogryz · 2016-06-15 19:03 the problem is that your x,y arrays are uninitialized. Thus then you take an apple and you j increases. The next "clearing" loop Code: [Select]    for i:=1 to j do  begin       gotoxy(x[i],y[i]);       write(' ');     end; Reads x[ i ], y[ i ] from the new "j" variable. Again, since x and y are not initialized  the value x[ i ] or y[ i ] can be over 255 (which is the limit for gotoxy() procedure), which is causing 201. You can fix the issue by initializing x,y Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  begin  clrscr;  randomize;  for i:=1 to 1000 do begin    x[i]:=1; y[i]:=1;  end;  ... ---------------------------------------------------------------------- Bart · 2016-06-15 19:16 Initializing x and y (which can be done faster using FillChar b.t.w.) does not prevent the range check error in the line I previously mentioned. Make both x and y array[1..1000] of Byte and you wil see where it happens. Bart ---------------------------------------------------------------------- rvk · 2016-06-15 19:27 Quote from: skalogryz on June 15, 2016, 09:03:26 pmReads x[ i ], y[ i ] from the new "j" variable. Again, since x and y are not initialized  the value x[ i ] or y[ i ] can be over 255 (which is the limit for gotoxy() procedure), which is causing 201. Close The x and y array are initialized with all zeros. This is why the x[j] crashes on gotoxy because you can't go to position 0. Initializing both arrays to 1 does fix the 201 after hitting the apple. I would also build in the following lines to make sure the snakes crosses over the edges without triggering a 201 exit. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     if (y[1] = 26) then y[1] := 1    else if (y[1] = 0) then y[1] := 25;    if (x[1] = 80) then x[1] := 1    else if (x[1] = 0) then x[1] := 80; It would also fix the error Bart mentioned. ---------------------------------------------------------------------- skalogryz · 2016-06-15 19:31 Quote from: Bart on June 15, 2016, 09:16:04 pm(which can be done faster using FillChar b.t.w.) well... FillChar didn't exist at TP ---------------------------------------------------------------------- Bart · 2016-06-15 19:34 Quote from: skalogryz on June 15, 2016, 09:31:26 pmwell... FillChar didn't exist at TP Are you sure? I can remember using FillChar in ancient programs of mine. See e.g. http://putka.upm.si/langref/turboPascal/055E.html Anyhow FillChar doesn't do what I intended on an array of integer, stupid me. Bart ---------------------------------------------------------------------- Bart · 2016-06-15 19:37 There's another annoying bit: if the program crashes, the console has no cursor anymore. Bart ---------------------------------------------------------------------- rvk · 2016-06-15 19:40 Quote from: Bart on June 15, 2016, 09:37:59 pmThere's another annoying bit: if the program crashes, the console has no cursor anymore. The program doesn't even need to crash for that. Just pressing q exists the program but the cursor is never reset to the correct default ---------------------------------------------------------------------- Bart · 2016-06-15 20:56 Quote from: rvk on June 15, 2016, 09:40:15 pmThe program doesn't even need to crash for that. Just pressing q exists the program but the cursor is never reset to the correct default Even better  Bart