It is a rather simple snake game. It just gets hanged. When I try to execute it, it runs for a while and then hangs in some moments. Please Tell me what to do? This is turbo C++ version 3.0 and please do not tell me to use new version. And don't call me outdated, I know that.
#include<conio.h>
#include<iostream.h>
#include<dos.h>
#include<graphics.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int x,y,a,b,flag,i,discx,discy,sc,maxx,maxy,same;
short int arr[100][2];
char ch,check;
void shift();
int introscreen();
void condition();
void gameover();
void score();
void randomapple();
int introscreen()
{
outtextxy(maxx/2,30,"SNAKE");
outtextxy((maxx/2)-10,50,"INSTRUCTIONS");
outtextxy(5,70,"W A S D represent Top Right Bottom Left respectively.");
outtextxy(5,90,"Press Spacebar to quit any moment");
outtextxy(5,110,"To play Press Space else anything else");
char take=getch();
if(take==' ')
return 1;
else
return 0;
}
void randomapple()
{same=0;
while(1)
{
a=random(maxx-5);
b=random(maxy-5);
for(i=0;i<flag;i++)
{if((abs(a-arr[i][0]<=5)&&abs(b-arr[i][1]<=5))||a<=5||b<=5)
same=1;
}
if(same==0) break;
}
setcolor(RED);
outtextxy(a,b,"0");
setcolor(WHITE);
}
void shift()
{discx=arr[flag-1][0];
discy=arr[flag-1][1];
for(i=flag-1;i>0;i--)
{
arr[i][0]=arr[i-1][0];
arr[i][1]=arr[i-1][1];
}
arr[0][0]=x;
arr[0][1]=y;
}
void printsnake()
{
setcolor(BLACK);
outtextxy(discx,discy,"X");
setcolor(GREEN);
outtextxy(x,y,"X");
setcolor(RED);
outtextxy(a,b,"0");
rectangle(0,0,maxx,maxy);
delay(5);
}
void condition()
{
switch(ch)
{
case'd':x++;
break;
case'a':x--;
break;
case'w':y--;
break;
case's':y++;
break;
case' ':exit(1);
}
}
void score()
{if((abs(x-a)<=5)&&(abs(y-b)<=5))
{sc++;
if(flag<500)
flag=flag+10;
setcolor(BLACK);
outtextxy(a,b,"0");
randomapple();
}
if(sc==100)
{
clrscr();
cout<<"You are the champion tonight!!!!";
getch();
exit(1);
}
}
void collision()
{
if(x<=5||y<=5||maxx-x<=5||maxy-y<=5)
gameover();
for(i=1;i<flag;i++)
{
if(arr[0][0]==arr[i][0]&&arr[0][1]==arr[i][1])
gameover();
}
}
void gameover()
{textcolor(BLACK);
clrscr();
textcolor(WHITE);
cout<<"YOU LOSE!\n";
cout<<"GAME OVER\n";
cout<<"Final Score = "<<sc;
getch();
getch();
getch();
getch();
exit(1);
}
void main()
{
randomize();
int gdriver = DETECT, gmode, errorcode;
/* initialize graphics mode */
initgraph(&gdriver, &gmode, "");
maxx=500;
maxy=400;
x=maxx/2;
y=maxy/2;
ch='d';
sc=0;
if(introscreen())
{
textcolor(BLACK);
clrscr();
rectangle(0,0,maxx,maxy);
flag=20;
setcolor(GREEN);
for(i=19;i>=0;i--)
{arr[i][0]=x-i;
arr[i][1]=y-i;
outtextxy(x-i,y-i,"X");
}
setcolor(WHITE);
randomapple();
while(1)
{
if(kbhit())
{
check=getch();
switch(check)
{
case 'a':if(ch!='d') ch=check;
break;
case 'd':if(ch!='a') ch=check;
break;
case 'w':if(ch!='s') ch=check;
break;
case 's':if(ch!='w') ch=check;
break;
case ' ':ch=check;
break;
}
}
condition();
shift();
printsnake();
collision();
score();
}
}
}
From what I see, the problem lies in randomapple function. If you look closer on it, if you set same to 1 then it stays 1 forever. You should reset it every iteration of that while.