Recent

Author Topic: [SOLVED] Exit code 201 Snake Game  (Read 14192 times)

DiCri

  • Full Member
  • ***
  • Posts: 151
  • My goal : Build a game
    • http://manueldicriscito.altervista.org/DinoLand.zip
[SOLVED] Exit code 201 Snake Game
« on: June 15, 2016, 07:39:22 pm »
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][+][-]
  1. program snake;
  2. uses crt;
  3. var
  4.         x,y:array[1..1000] of integer;
  5.         xs,ys:integer;
  6.         input:char;
  7.         i,j:integer;
  8. procedure createscore;
  9.         begin
  10.  
  11.                 randomize;
  12.                 xs:=0;
  13.                 ys:=0;
  14.                 xs:=random(79)+1;
  15.                 ys:=random(23)+1;
  16.                 gotoxy(xs,ys);
  17.                 write('.');
  18.  
  19.  
  20.         end;
  21. procedure incscore;
  22.         begin
  23.                         if (x[1]=xs) and (y[1]=ys) then begin
  24.                                 gotoxy(xs,ys);
  25.                                 write(' ');
  26.                                 j:=j+1;
  27.                                 xs:=0;
  28.                                 ys:=0;
  29.                                 gotoxy(1,1);
  30.                                 writeln(i,',',j,',',xs,',',ys,',',x[1],',',y[1]);
  31.                                 input:=readkey;
  32.                                 createscore;
  33.                                 end;
  34.         end;
  35.  
  36. procedure checkdie;
  37.         begin
  38.               {  for i:=2 to j do begin
  39.                         if (x[1]=x[i]) and (y[1]=y[i]) then
  40.                                 input:='q';
  41.                         end;}
  42.                 end;
  43. begin
  44.         clrscr;
  45.         randomize;
  46.         cursoroff;
  47.         createscore;
  48.         for i:=1 to 3 do begin
  49.         x[i]:=random(80)+1;
  50.         y[i]:=random(23)+1;
  51.         end;
  52.         gotoxy(x[1],y[1]);
  53.         write('O');
  54.         j:=3;
  55.         repeat
  56.                 if keypressed then input:=readkey;
  57.                         for i:=1 to j do
  58.                                 begin
  59.                                 gotoxy(x[i],y[i]);
  60.                                 write(' ');
  61.                                 end;
  62.                         for i:=j downto 2 do
  63.                                 begin
  64.                                         x[i]:=x[i-1];
  65.                                         y[i]:=y[i-1];
  66.                                 end;
  67.                         if input='w' then y[1]:=y[1]-1;
  68.                         if input='s' then y[1]:=y[1]+1;
  69.                         if input='a' then x[1]:=x[1]-1;
  70.                         if input='d' then x[1]:=x[1]+1;
  71.                         for i:=1 to j do
  72.                                 begin
  73.                                 gotoxy(x[i],y[i]);
  74.                                 write('O');
  75.                                 end;
  76.                         incscore;
  77.  
  78.  
  79.                         delay(50);
  80.                 until input='q';
  81.  
  82. end.
  83.  
  84.  
Please answer me! Thanks
« Last Edit: June 15, 2016, 09:26:06 pm by Manu12x »
I'm a game developer.. Now studying..
Go download my game:
http://manueldicriscito.altervista.org/DinoLand.zip

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
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][+][-]
  1. program snake;
  2. {APPTYPE CONSOLE}
  3. uses crt;
  4. ..
  5.  

Bart

  • Hero Member
  • *****
  • Posts: 5727
    • Bart en Mariska's Webstek
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

  • Full Member
  • ***
  • Posts: 151
  • My goal : Build a game
    • http://manueldicriscito.altervista.org/DinoLand.zip
I am using normal Free Pascal and not Lazarus
I'm a game developer.. Now studying..
Go download my game:
http://manueldicriscito.altervista.org/DinoLand.zip

rvk

  • Hero Member
  • *****
  • Posts: 7043
Here 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

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
I 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

  • Full Member
  • ***
  • Posts: 151
  • My goal : Build a game
    • http://manueldicriscito.altervista.org/DinoLand.zip
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
I'm a game developer.. Now studying..
Go download my game:
http://manueldicriscito.altervista.org/DinoLand.zip

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
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][+][-]
  1.  
  2. begin
  3.   clrscr;
  4.   randomize;
  5.   for i:=1 to 1000 do begin
  6.     x[i]:=1; y[i]:=1;
  7.   end;
  8.   ...
  9.  


Bart

  • Hero Member
  • *****
  • Posts: 5727
    • Bart en Mariska's Webstek
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
« Last Edit: June 15, 2016, 09:31:17 pm by Bart »

rvk

  • Hero Member
  • *****
  • Posts: 7043
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.
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][+][-]
  1.     if (y[1] = 26) then y[1] := 1
  2.     else if (y[1] = 0) then y[1] := 25;
  3.     if (x[1] = 80) then x[1] := 1
  4.     else if (x[1] = 0) then x[1] := 80;
  5.  
It would also fix the error Bart mentioned.

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
(which can be done faster using FillChar b.t.w.)
well... FillChar didn't exist at TP

Bart

  • Hero Member
  • *****
  • Posts: 5727
    • Bart en Mariska's Webstek
well... 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

  • Hero Member
  • *****
  • Posts: 5727
    • Bart en Mariska's Webstek
Re: [SOLVED] Exit code 201 Snake Game
« Reply #12 on: June 15, 2016, 09:37:59 pm »
There's another annoying bit: if the program crashes, the console has no cursor anymore.

Bart

rvk

  • Hero Member
  • *****
  • Posts: 7043
Re: [SOLVED] Exit code 201 Snake Game
« Reply #13 on: June 15, 2016, 09:40:15 pm »
There'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

  • Hero Member
  • *****
  • Posts: 5727
    • Bart en Mariska's Webstek
Re: [SOLVED] Exit code 201 Snake Game
« Reply #14 on: June 15, 2016, 10:56:11 pm »
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 :)

Even better  >:D

Bart

 

TinyPortal © 2005-2018