C++ Data strucs Algorithms. Please follow all instructions and give output. Will
ID: 3809493 • Letter: C
Question
C++ Data strucs Algorithms.
Please follow all instructions and give output. Will leave thumbs up as well :)!
Make Snake game with an auto generated maze, different levels of difficulty, multi-player options, and unique graphical user interface.
Please also identify the data structures and algorithms in your program and how they are fundamental to the successful execution of the program. Must also include internal documentation (comments explaining the functioning of the source code) Visual aspects of the program such as “pop up boxes” and GUI’s are recommended..
Explanation / Answer
Snake Game for U
Method 1 -
Features are -
Multiple Fruits For Snake,
3 levels - Difficulty
Options - Pause & Cintinue
Arrow keys control
high Graphics
/*
Data Structure "Snake Game"
*/
#include<conio.h>
#include<ctype.h>
#include<stdio.h>
#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
#include<dos.h>
#include<graphics.h>
class Art
{
public:
Art();
~Art();
void screen() const;
void intro() const;
void sMenu() const;
};
Art :: Art()
{
intro();
screen();
}
Art :: ~Art(){}
void Art :: intro() const
{
int i= 0,w= 0;
while(w++<=50)
{
setfillstyle(11,4); // BACK_GROUND
floodfill(1,1,1);
setcolor(i++);
settextstyle(1,0,4);
outtextxy(305,2,"OOP");
setcolor(i+2);
settextstyle(8,0,8);
outtextxy(65,2,"ASSIGNMENT");
setcolor(i+3);
settextstyle(5,0,6);
outtextxy(300,74,"OF");
setcolor(i+4);
settextstyle(10,0,5);
outtextxy(20,120,"DATA-STRUCTURE");
setcolor(i+5);
settextstyle(1,0,4);
outtextxy(245,205,"SUBMITTED");
setcolor(i+6);
settextstyle(5,0,3);
outtextxy(315,235,"TO");
setcolor(i+7);
settextstyle(4,0,6);
outtextxy(278,260,"SIR.");
setcolor(i+8);
settextstyle(7,0,8);
outtextxy(12,310,"NAZIR_AHMAD");
setcolor(i+9);
settextstyle(1,0,4);
outtextxy(33,420,"BY: " BSSIT.13.19 "");
delay(50);
}
getch();
cleardevice();
}
void Art :: screen() const
{
for(int i=2; i<8; i+=2)
{
setcolor(i+7);
rectangle(1+i,1+i,getmaxx()-i,getmaxy()-40-i);
rectangle(20-i,431+i,getmaxx()-20+i,getmaxy()-0-i);
}
setcolor(6);
rectangle(21,441,164,470);
rectangle(480,441,620,471);
setfillstyle(1,0);
bar(22,442,163,469);
settextstyle(7,0,3);
outtextxy(23,440,"Score 00000");
outtextxy(481,436," Nomi Ch");
outtextxy(290,436," ESC");
settextstyle(11,0,2);
setcolor(4);
outtextxy(504,462,"BSSIT.13.19");
setcolor(5);
outtextxy(170,448,"B--> Break.");
outtextxy(380,448,"Up : Down");
outtextxy(170,458,"C--> Continue");
outtextxy(370,458,"Left : Right");
setcolor(4);
outtextxy(297,462,"To Exit");
}
class Fruit
{
protected:
int x,y;
public:
Fruit(int,int);
virtual ~Fruit()=0;
virtual void draw() const=0;
void remove() const;
const int getX() const;
const int getY() const;
};
Fruit :: Fruit(int px, int py):x(px),y(py){}
Fruit :: ~Fruit(){}
void Fruit :: remove() const
{
setcolor(0);
setfillstyle(1,0);
fillellipse(x,y,10,10);
}
const int Fruit :: getX() const
{
return x;
}
const int Fruit :: getY() const
{
return y;
}
class Apple : public Fruit
{
public:
Apple(int,int);
~Apple();
void draw() const;
};
Apple :: Apple(int x, int y):Fruit(x,y){}
Apple :: ~Apple(){}
void Apple :: draw() const
{
setcolor(4);
setfillstyle(1,4);
fillellipse(x,y+1,7,7);
setcolor(0);
setfillstyle(1,0);
fillellipse(x,y+8,3,1);
fillellipse(x,y-6,3,1);
setcolor(6);
ellipse(x+1,y+1,290,80,3,5);
ellipse(x,y+1,290,80,3,5);
setcolor(2);
ellipse(x+4,y-4,90,180,4,4);
ellipse(x+4,y-5,90,180,4,4);
setfillstyle(1,2);
sector(x-2,y-0,225,45,4,3);
}
class Mango : public Fruit
{
public:
Mango(int,int);
~Mango();
void draw() const;
};
Mango :: Mango(int x, int y):Fruit(x,y){}
Mango :: ~Mango(){}
void Mango :: draw() const
{
setcolor(14);
setfillstyle(1,14);
fillellipse(x,y+2,7,7);
setcolor(0);
setfillstyle(1,0);
fillellipse(x-5,y+4,2,3);
setcolor(14);
setfillstyle(1,14);
fillellipse(x-2,y-1,3,3);
setcolor(2);
setfillstyle(1,2);
sector(x-3,y,60,190,5,4);
setcolor(4);
arc(x+4,y-5,90,180,4);
arc(x+4,y-4,90,180,4);
}
class Melon : public Fruit
{
public:
Melon(int,int);
~Melon();
void draw() const;
};
Melon :: Melon(int x, int y):Fruit(x,y){}
Melon :: ~Melon(){}
void Melon :: draw() const
{
setcolor(14);
setfillstyle(1,14);
fillellipse(x,y,9,7);
setcolor(2);
ellipse(x+1,y,270,90,6,7);
ellipse(x-1,y,270,90,4,7);
ellipse(x+1,y,90,270,6,7);
ellipse(x,y,90,270,4,7);
setcolor(0);
setfillstyle(1,0);
fillellipse(x,y-7,3,1);
}
class Pomegranate : public Fruit
{
public:
Pomegranate(int,int);
~Pomegranate();
void draw() const;
};
Pomegranate :: Pomegranate(int x, int y):Fruit(x,y){}
Pomegranate :: ~Pomegranate(){}
void Pomegranate :: draw() const
{
setcolor(4);
setfillstyle(1,4);
fillellipse(x,y+1,7,7);
setcolor(0);
ellipse(x-2,y+1,270,30,7,6);
setfillstyle(1,0);
fillellipse(x,y-7,4,3);
setcolor(4);
ellipse(x+5,y-5,90,180,3,3);
ellipse(x-5,y-5,0,90,3,3);
ellipse(x,y-5,0,180,2,4);
putpixel(x-1,y-5,14);
putpixel(x+1,y-5,14);
}
char ch= 'R';
class Node
{
private:
int x,y;
Node *next,*prev;
public:
Node(int,int);
~Node();
void setX(const int);
void setY(const int);
const int getX() const;
const int getY() const;
void setNext(Node*);
void setPrev(Node*);
Node* getNext() const;
Node* getPrev() const;
};
Node :: Node(int X=0, int Y=0):x(X),y(Y),next(NULL){}
Node :: ~Node(){}
void Node :: setX(const int px)
{
x= px;
}
void Node :: setY(const int py)
{
y= py;
}
int Node :: getX() const
{
return x;
}
int Node :: getY() const
{
return y;
}
void Node :: setNext(Node *n)
{
next= n;
}
Node* Node :: getNext() const
{
return next;
}
void Node :: setPrev(Node *n)
{
prev= n;
}
Node* Node :: getPrev() const
{
return prev;
}
class Game
{
private:
char *dScore,fruitCh;
int stage;
long score;
void secondStage() const;
void thirdStage() const;
void setScore(const int);
void displayScore() const;
const int isFruitPosValid(const int,const int) const;
public:
Game();
~Game();
const int getScore() const;
void chkStage();
const int getStage() const;
void gameOver() const;
void newFruit(Fruit*) ;
};
Game :: Game():stage(1),score(0)
{
dScore[0]=fruitCh= '';
}
Game :: ~Game(){}
void Game :: gameOver() const
{
cleardevice();
int c,x=getmaxx()/2,y=100;
settextjustify(1,2);
for(int i=1;i<=29;i++)
{
/* angry face */
int p1[]={324,85,320,77,369,60,379,70};//505,85,501,77,550,60,560,70
int p2[]={314,86,317,77,269,60,259,70};//495,86,498,77,450,60,440,70
setfillstyle(11,4);
floodfill(1,1,1);
setcolor(0); /* outer boundry */
setfillstyle(1,0);
fillellipse(x,y,80,60);
fillellipse(x,y-5,80,65);
setcolor(i);
setfillstyle(1,i); //14
fillellipse(x,y,70,50);
fillellipse(x,y-5,70,55);
/* eyes */
setfillstyle(1,4);
fillellipse(x-30,y-10,25,20);
fillellipse(x+30,y-10,25,20);
setfillstyle(1,15);
fillellipse(x-28,y-12,20,15);
fillellipse(x+28,y-12,20,15);
setfillstyle(1,3);
fillellipse(x-26,y-13,19,13);
fillellipse(x+26,y-13,19,13);
setcolor(0);
setfillstyle(1,0);
fillellipse(x-23,y-15,14,10);
fillellipse(x+23,y-15,14,10);
/** eye hairs **/
fillpoly(4,p1);
fillpoly(4,p2);
setfillstyle(1,15);
fillellipse(x-25,y-10,3,4);
fillellipse(x+20,y-12,3,4);
/* nouth */
setcolor(0);
ellipse(x,y+42,30,150,35,20);
ellipse(x,y+42,30,150,35,19);
ellipse(x,y+42,30,150,35,18);
ellipse(x,y+42,30,150,35,17);
ellipse(x,y+42,30,150,35,16);
ellipse(x,y+42,30,150,35,15);
settextstyle(7,0,4);
setcolor((c=1+rand()%15));
sprintf(dScore,"Your Score %d",score);
outtextxy(getmaxx()/2,200,dScore);
setcolor(c+2);
outtextxy(getmaxx()/2,250,"This Game ");
setcolor(c+4);
outtextxy(getmaxx()/2,370,"BSSIT.13.19");
outtextxy(getmaxx()/2,400,"Hi");
setcolor(c+6);
outtextxy(getmaxx()/2,435,"Hey");
setcolor(c+9);
settextstyle(7,0,6);
outtextxy(getmaxx()/2,300,"Nomi_Ch");
delay(50);
}
}
const int Game :: getScore() const
{
return score;
}
void Game ::setScore(const int x)
{
score= x;
}
void Game :: displayScore() const
{
if(score>9999)
{
sprintf(dScore,"%d",score);
}
else if(score>999)
{
sprintf(dScore,"0%d",score);
}
else if(score>99)
{
sprintf(dScore,"00%d",score);
}
else if(score>9)
{
sprintf(dScore,"000%d",score);
}
else
{
sprintf(dScore,"0000%d",score);
}
setfillstyle(1,0);
bar(85,442,163,469);
setcolor(4);
settextstyle(7,0,3);
outtextxy(93,440,dScore);
}
void Game :: thirdStage() const
{
setcolor(13);
setfillstyle(2,13);
bar(159,95,483,105);
bar(159,194,483,204);
bar(159,291,483,301);
bar(159,388,483,398);
}
void Game :: secondStage() const
{
setcolor(9);
setfillstyle(2,9);
bar(99,50,109,390);
bar(533,50,543,390);
}
void Game :: chkStage()
{
if(score>100)
{
stage= 3;
secondStage();
thirdStage();
}
else if(score>70)
{
stage= 2;
secondStage();
}
else
{
stage= 1;
}
}
const int Game :: getStage() const
{
return stage;
}
const int Game :: isFruitPosValid(const int x,const int y) const
{
if( (((x+10) < 99) || ((x-10) > 109)) && (((x+10) < 533) || ((x-10) > 543)) )
if( ((y+10)<95) || ((y-10)>105) && ((y+10)<194) || ((y-10)>204) && ((y+10)<291) || ((y-10)>301) && ((y+10)<388) || ((y-10)>398) )
return 0;
return 1;
}
void Game :: newFruit(Fruit *fp)
{
sound(7000);
if(fruitCh=='M')
{
setScore(getScore()+5);
}
else if(fruitCh=='P')
{
setScore(getScore()+10);
}
else if(fruitCh=='A')
{
setScore(getScore()+15);
}
else
{
setScore(getScore()+20);
}
fp->remove();
delete fp;
fp= NULL;
int x,y;
do
{
x= 20+rand()%600;
y= 20+rand()%402;
}
while(isFruitPosValid(x,y));
switch(1+rand()%4)
{
case 1:
{
fp= new Mango(x,y);
fp->draw();
fruitCh= 'M';
}
break;
case 2:
{
fp= new Pomegranate(x,y);
fp->draw();
fruitCh= 'P';
}
break;
case 3:
{
fp= new Apple(x,y);
fp->draw();
fruitCh= 'A';
}
break;
case 4:
{
fp= new Melon(x,y);
fp->draw();
fruitCh= 'K';
}
break;
}
displayScore();
nosound();
}
class Snake
{
private:
int length,s,life;
Node *head, *tail;
void turnControl();
void secondStageHurdles();
void thirdStageHurdles();
void checkHurdles(Node*,Game);
void drawHead(Node*);
void drawBack(Node*);
public:
Snake(int,int);
~Snake();
const int isAteFruit(Fruit*) const;
void addTail(int,int);
int isLife();
void move(Game);
void die();
};
Snake :: Snake(int pl=5 , int ps=7):length(0),s(ps),life(1),head(NULL),tail(NULL)
{
int s=200;
for(int i=0; i<=pl; i++)
{
addTail(s-=13,240);
}
}
Snake :: ~Snake(){}
void Snake :: secondStageHurdles()
{
const int x= head->getX()+5;
const int y= head->getY()+5;
if( x>99 && y>50 && x<109 && y<390 )
{
life= 0;
}
else if( x>533 && y>50 && x<543 && y<390 )
{
life= 0;
}
}
void Snake :: thirdStageHurdles()
{
const int x= head->getX()+3;
const int y= head->getY()+3;
if( x>159 && y>95 && x<488 && y<105 )
{
life= 0;
}
else if(x>159 && y>194 && x<488 && y<204)
{
life= 0;
}
else if(x>159 && y>291 && x<488 && y<301)
{
life= 0;
}
else if(x>159 && y>388 && x<488 && y<398)
{
life= 0;
}
}
int Snake :: isLife()
{
return life;
}
void Snake :: addTail(int x=0, int y=0)
{
Node *nn= new Node(x,y);
nn->setNext(NULL);
nn->setPrev(NULL);
if(NULL==head)
{
head=tail= nn;
}
else
{
nn->setPrev(tail);
tail->setNext(nn);
tail= nn;
}
}
void Snake :: drawBack(Node *t)
{
setcolor(1);
setfillstyle(1,2);
fillellipse(t->getX(),t->getY(),s,s);
setcolor(15);
setfillstyle(1,4);
if('U'==ch)
{
fillellipse(t->getX(),t->getY(),s-4,s);
}
else if('D'==ch)
{
fillellipse(t->getX(),t->getY(),s-4,s);
}
else if('L'==ch)
{
fillellipse(t->getX(),t->getY(),s,s-4);
}
else if('R'==ch)
{
fillellipse(t->getX(),t->getY(),s,s-4);
}
setcolor(0);
setfillstyle(1,0);
if('U'==ch)
{
fillellipse(t->getX(),t->getY(),s-6,s);
}
else if('D'==ch)
{
fillellipse(t->getX(),t->getY(),s-6,s);
}
else if('L'==ch)
{
fillellipse(t->getX(),t->getY(),s,s-6);
}
else if('R'==ch)
{
fillellipse(t->getX(),t->getY(),s,s-6);
}
}
void Snake :: drawHead(Node *temp)
{
setcolor(4); // FACE
setfillstyle(1,4);
fillellipse(temp->getX(),temp->getY(),s,s);
setcolor(3); // EYES
setfillstyle(1,3);
if('U'==ch)
{
fillellipse(temp->getX()-3,temp->getY(),2,2);
fillellipse(temp->getX()+3,temp->getY(),2,2);
}
else if('D'==ch)
{
fillellipse(temp->getX()-3,temp->getY(),2,2);
fillellipse(temp->getX()+3,temp->getY(),2,2);
}
else if('L'==ch)
{
fillellipse(temp->getX(),temp->getY()+3,2,2);
fillellipse(temp->getX(),temp->getY()-3,2,2);
}
else if('R'==ch)
{
fillellipse(temp->getX(),temp->getY()-3,2,2);
fillellipse(temp->getX(),temp->getY()+3,2,2);
}
setcolor(0);
setfillstyle(1,0);
if('U'==ch)
{
fillellipse(temp->getX()-3,temp->getY(),1,1);
fillellipse(temp->getX()+3,temp->getY(),1,1);
}
else if('D'==ch)
{
fillellipse(temp->getX()-3,temp->getY(),1,1);
fillellipse(temp->getX()+3,temp->getY(),1,1);
}
else if('L'==ch)
{
fillellipse(temp->getX(),temp->getY()+3,1,1);
fillellipse(temp->getX(),temp->getY()-3,1,1);
}
else if('R'==ch)
{
fillellipse(temp->getX(),temp->getY()-3,1,1);
fillellipse(temp->getX(),temp->getY()+3,1,1);
}
// MOUTH
setcolor(13);
setfillstyle(1,13);
if('U'==ch)
{
fillellipse(temp->getX(),temp->getY()-5,1,2);
}
else if('D'==ch)
{
fillellipse(temp->getX(),temp->getY()+5,1,2);
}
else if('L'==ch)
{
fillellipse(temp->getX()-5,temp->getY(),2,1);
}
else if('R'==ch)
{
fillellipse(temp->getX()+5,temp->getY(),2,1);
}
}
void Snake :: turnControl()
{
if('U'==ch)
{
head->setY(head->getY()-(2*s));
}
else if('D'==ch)
{
head->setY(head->getY()+(2*s));
}
else if('L'==ch)
{
head->setX(head->getX()-(2*s));
}
else if('R'==ch)
{
head->setX(head->getX()+(2*s));
}
}
void Snake :: checkHurdles(Node* temp,Game gp)
{
while(temp!=tail)
{
if((head->getX() == temp->getX()) && (head->getY()==temp->getY()))
{
life= 0;
}
temp= temp->getNext();
}
if((head->getX() < 15))
{
life= 0;
}
else if((head->getX() > 622))
{
life= 0;
}
else if((head->getY() < 16))
{
life= 0;
}
else if((head->getY() > 424))
{
life= 0;
}
if(gp.getStage()==2)
{
secondStageHurdles();
}
else if(gp.getStage()==3)
{
secondStageHurdles();
thirdStageHurdles();
}
}
void Snake :: die()
{
Node *temp;
for(int i=1; i<25; i++)
{
sound(5000);
if(i%2)
{
temp=head;
setcolor(0);
setfillstyle(1,0);
while(temp!=NULL)
{
fillellipse(temp->getX(),temp->getY(),s,s);
temp=temp->getNext();
}
}
else
{
temp=tail;
while(temp!=NULL)
{
if(temp==head)
{
drawHead(head);
}
else
{
drawBack(temp);
}
temp=temp->getPrev();
}
}
nosound();
delay(100);
}
}
void Snake :: move(Game gp)
{
Node *temp= tail;
while(temp!=head)
{
temp->setX(temp->getPrev()->getX());
temp->setY(temp->getPrev()->getY());
temp= temp->getPrev();
}
turnControl();
checkHurdles(head->getNext()->getNext(),gp);
temp= head;
while(temp!=tail)
{
if(temp==head)
{
drawHead(head);
}
else
{
drawBack(temp);
}
temp= temp->getNext();
}
setcolor(0);
setfillstyle(1,0);
fillellipse(temp->getX(),temp->getY(),s,s);
delay(150);
}
const int Snake :: isAteFruit(Fruit *fp) const
{
if((head->getX()) > (fp->getX()-13) && (head->getX()) < (fp->getX()+13) && (head->getY()) > (fp->getY()-13) && (head->getY()) < (fp->getY()+13))
{
return 1;
}
return 0;
}
void main()
{
clrscr();
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"..\bgi");
Fruit *f=new Apple(40,100);
Art a;
Game g;
g.chkStage();
g.newFruit(f);
Snake s(4,7);
char menu;
do
{
if(kbhit())
{
menu= getch();
switch(toupper(menu))
{
case 72:
{
if(ch!='D')
ch= 'U';
}
break;
case 75:
{
if(ch!='R')
ch= 'L';
}
break;
case 77:
{
if(ch!='L')
ch= 'R';
}
break;
case 80:
{
if(ch!='U')
ch= 'D';
}
break;
case 'B':
{
do
{
}
while(toupper(getch())!='C');
}
break;
}
}
if(s.isLife())
{
if(s.isAteFruit(f))
{
g.newFruit(f);
s.addTail();
}
s.move(g);
}
else
{
s.die();
g.gameOver();
delay(100);
return;
}
g.chkStage();
}
while(menu!=27);
if(menu==27)
{
g.gameOver();
}
}
Method 2 - No levels - Basic
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
#include<string.h>
class Snake
{
int p1,p2,v1,v2,v3,e1,e2,prev,now,n,colr,dsp,cnt,dly,m;
int stp,egGen;
int xr,yr;
void caught();
public:
long scr;
int strtX,strtY,endX,endY;
int pos[100][2];
void show();
void init();
void egg();
void transpose();
void gnrtCond();
void gnrtUnCond();
void check();
void checkEgg();
void move();
void chngDir();
void sndEt();
void sndCgt();
int test();
void score();
Snake();
Snake(Snake*);
~Snake();
};
Snake::Snake()
{
}
Snake::~Snake()
{
}
void Snake::checkEgg()
{
if((e1 == p1) && (e2 == p2))
{sndEt();
egg();
dly--;
score();
n++;
}
}
void Snake::sndEt()
{nosound();
sound(2500);
delay(2);
nosound();
}
void Snake::sndCgt()
{nosound();
for(int x=1000;x>0;x--)
{sound(x);
delay(1);
}
nosound();
}
void Snake::score()
{ char *p;
ltoa(scr,p,10);
settextstyle(8,0,1);
setcolor(0);
outtextxy(585,40,p);
if(egGen != 1){
scr = scr + dly / 10;
}
ltoa(scr,p,10);
setcolor(10);
outtextxy(585,40,p);
}
void Snake::gnrtCond()
{if(n < 367)
{if(now == 8 && (prev != 8 && prev != 2))
{pos[0][0] = p1;
pos[0][1] = p2 - dsp;
prev = now;
}
if(now == 4 && (prev != 4 && prev != 1))
{pos[0][0] = p1 + dsp;
pos[0][1] = p2;
prev = now;
}
if(now == 2 && (prev != 8 && prev != 2))
{pos[0][0] = p1;
pos[0][1] = p2 + dsp;
prev = now;
}
if(now == 1 && (prev != 1 && prev != 4))
{pos[0][0] = p1 - dsp;
pos[0][1] = p2;
prev = now;
}
}
}
void Snake::gnrtUnCond()
{
if( prev == 8 )
{pos[0][0] = p1;
pos[0][1] = p2 - dsp;
}
if( prev == 4 )
{pos[0][0] = p1 + dsp;
pos[0][1] = p2;
}
if( prev == 2 )
{pos[0][0] = p1;
pos[0][1] = p2 + dsp;
}
if( prev == 1 )
{pos[0][0] = p1 - dsp;
pos[0][1] = p2;
}
p1 = pos[0][0];
p2 = pos[0][1];
}
void Snake::check()
{
if(p1 > endX)
{p1 = strtX;}
else if(p1 < strtX)
{p1 = endX;}
if(p2 > endY)
{p2 = strtY;}
else if(p2 < strtY)
{p2 = endY;}
pos[0][0] = p1;
pos[0][1] = p2;
for(int i = 1;i < n;i++)
{ if(p1 == pos[i][0] && p2 == pos[i][1])
{caught();
break;
}
}
}
void Snake::show()
{
int x = getcolor();
if(egGen != 1)
{
setcolor(getbkcolor());
setfillstyle(1,getbkcolor());
fillellipse(v1,v2,yr,yr);
}
else
egGen = 0;
if(egGen == 2)
egGen--;
setcolor(colr);
setfillstyle(1,9);
if(now == 8 || now == 2)
fillellipse(pos[0][0],pos[0][1],xr,yr);
else if(now == 4 || now == 1)
fillellipse(pos[0][0],pos[0][1],yr,xr);
setcolor(x);
}
void Snake::transpose()
{ int i,j,x,y;
p1 = pos[0][0];
p2 = pos[0][1];
if(!egGen){
v1 = pos[n-1][0];
v2 = pos[n-1][1];
}
else
egGen = 0;
for(i = n-1;i >= 1;i--)
{pos[i][0] = pos[i-1][0];
pos[i][1] = pos[i-1][1];
}
}
void Snake::move()
{ int st = 0;
do{
if(!kbhit())
{checkEgg();
if(!st)
show();
else
st = 0;
delay(dly/4);
transpose();
delay(dly/4);
gnrtUnCond();
delay(dly/4);
check();
delay(dly/4);
}
else if(stp){
chngDir();
gnrtCond();
check();
show();
st = 1;
}
}while(stp);
}
void Snake::init()
{time_t tm;
srand(time(&tm));
dsp = 20;
n = 5;
prev = 4;
for(int i = 4;i >= 0;i--)
{pos[i][0] = 201 + (n - i - 1) * dsp;
pos[i][1] = 301;
}
strtX = 21;
strtY = 21;
endX = 481;
endY = 361;
colr = 14;
now = prev;
dsp = 20;
stp = 111;
cnt = -1;
scr = 0;
dly = 150;
xr = 3;
yr = 9;
egg();
egGen = 1;
score();
int x = getcolor();
setlinestyle(0,1,3);
setcolor(15);
rectangle(strtX-15,strtY-15,endX+15,endY+15);
rectangle(endX+25,strtY-15,getmaxx()-15,endY+15);
rectangle(strtX-15,endY+25,getmaxx()-15,getmaxy()-5);
line(endX+25,strtY+75,getmaxx()-15,strtY+75);
line(endX+25,strtY+200,getmaxx()-15,strtY+200);
line(endX+25,strtY+275,getmaxx()-15,strtY+275);
setlinestyle(0,1,1);
settextstyle(8,0,1);
setcolor(11);
outtextxy(514,40,"SCORE");
setcolor(14);
settextstyle(11,0,5);
outtextxy(524,110," CONTROLS ");
outtextxy(522,135,"p = PAUSE");
outtextxy(522,155,"g = RESUME");
outtextxy(522,175,"e = EXIT");
outtextxy(513,195,"ARROWS");
outtextxy(512,205," -MOVEMENT");
setcolor(14);
settextstyle(4,0,9);
outtextxy(getmaxx()-500,getmaxy()-110,"SNAKE");
settextstyle(8,0,1);
setcolor(x);
}
void Snake::caught()
{
stp = 0;
sndCgt();
for(int i=0;i<=7;i++)
{if(i%2)
{setcolor(10);
outtextxy(512,250,"GAME OVER");
delay(900);
}
else
{setcolor(0);
outtextxy(512,250,"GAME OVER");
delay(500);
}
}
sleep(1);
}
void Snake::chngDir()
{int clr;
fillsettingstype *p;
char x = getch();
if(x == 72)
now = 8;
else if(x == 77)
now = 4;
else if(x == 80)
now = 2;
else if(x == 75)
now = 1;
else if(x == 'e')
caught();
else if(x == 'p')
{//int y = getcolor();
int twnkl = 1;
settextstyle(11,0,9);
while(1)
{if(kbhit())
{int c = getch();
if(c == 'g')
{clr = getcolor();
setcolor(0);
rectangle(endX+40,endY-10,getmaxx()-35,getmaxy()-160);
outtextxy(endX+60,endY-29,"PAUSE");
break;
}
}
else
{if(twnkl%2)
{clr = getcolor();
setcolor(10);
rectangle(endX+40,endY-10,getmaxx()-35,getmaxy()-160);
outtextxy(endX+60,endY-29,"PAUSE");
setcolor(clr);
delay(1000);
}
else
{
clr = getcolor();
setcolor(0);
rectangle(endX+40,endY-10,getmaxx()-35,getmaxy()-160);
outtextxy(endX+60,endY-29,"PAUSE");
delay(1000);
}
}
twnkl++;
}
settextstyle(8,0,1);
}
}
Snake::Snake(Snake *p)
{
*p=NULL;
}
void Snake::egg()
{ do
{e1 = (rand() % 100) * dsp + strtX;
e2 = (rand() % 100) * dsp + strtY;
}while(test());
int x = getcolor();
setcolor(7);
setfillstyle(1,random(15)+1);
fillellipse(e1,e2,xr+2,xr+2);
setcolor(x);
egGen = 2;
}
int Snake::test()
{ for(int i=0;i<n;i++)
{if(e1 == pos[i][0] && e2 == pos[i][1])
break;
if(v1 == e1 && v2 == e2)
break;
if((e1 >= endX+1) || (e2 >= endY+1))
break;
}
if(i != n)
return 1;
else
return 0;
}
void main()
{Snake snk;
int gd=DETECT,gm,i,j,k,x,y;
clrscr();
initgraph(&gd,&gm,"E:Turboc3");
snk.init();
snk.move();
closegraph();
restorecrtmode();
}
Method 3 : No levels - Mini Project
#include<stdlib.h>
#include<ctype.h>
#include<conio.h>
#include<stdio.h>
#include<time.h>
#include<dos.h>
#define ESC 27
#define UPARR 72
#define LEFTARR 75
#define DOWNARR 80
#define RIGHTARR 77
#define SAVE 60
#define LOAD 61
main()
{
void starting(void);
void make_xy(char **,char **);
void getrand(char *,char *,char *,char *,char *,int,char);
char getkey(char,char);
void savegame(char *,char *,int,char);
int loadgame(char *,char *,char *);
void win_message(void);
char *x,*y,pos_x,pos_y,num,out=0,old_ch=0,ch=0,new_ch,new_x,new_y,old_num=0;
int i,length=6;
starting();
make_xy(&x,&y);
getrand(&pos_x,&pos_y,&num,x,y,length,ch);
while(!out){
if((new_ch=getkey(old_ch,ch))==ESC)
out=2;
if(out)
break;
if(new_ch==SAVE)
savegame(x,y,length,old_ch);
else if(new_ch==LOAD){
length=loadgame(x,y,&ch);
getrand(&pos_x,&pos_y,&num,x,y,length,ch);
}
else
ch=new_ch;
new_x=x[0];
new_y=y[0];
if(ch==UPARR)
new_y=y[0]-1;
else if(ch==LEFTARR)
new_x=x[0]-1;
else if(ch==DOWNARR)
new_y=y[0]+1;
else if(ch==RIGHTARR)
new_x=x[0]+1;
old_ch=ch;
if((new_x<2)|(new_y<2)|(new_x>79)|(new_y>22))
out=1; /* HIGHEST POSSIBLE SCORE ÷ (78*21-6)*5 = 8160 ÷ 10,000 */
for(i=1;i<length-!old_num;i++) /* NOT "length": TAIL-END MAY MOVE AWAY! */
if((new_x==x[i])&(new_y==y[i])){
out=1;
break;
}
if((pos_x==new_x)&(pos_y==new_y)){
old_num+=num;
/* x=(char *)realloc(x,(score+6)*sizeof(char));
y=(char *)realloc(y,(score+6)*sizeof(char)); */
/* if((x==0)|(y==0)) */ /* PROBLEM IS NOT HERE */
/* x=x;*//* SOMEHOW realloc ISN'T COPYING PROPERLY */
getrand(&pos_x,&pos_y,&num,x,y,length,ch);
}
if(!old_num){
gotoxy(x[length-1],y[length-1]);
putchar(' ');
}
else{
length++;
if(length==1638){
win_message();
return 0;
}
gotoxy(67,25);
printf("Score = %5d",length-6);
old_num--;
x[i+1]=x[i];
y[i+1]=y[i];
}
for(i=length-1;i>0;i--){
x[i]=x[i-1];
y[i]=y[i-1];
if(i==1){
gotoxy(x[i],y[i]);
putchar('Û');
}
}
x[0]=new_x;
y[0]=new_y;
gotoxy(x[0],y[0]);
printf(""); /* USE THE FUNCTION _setcursortype() */
if(out)
break;
delay(99);
}
if(out==1){
gotoxy(1,24);
printf("The snake collided with the wall or with itself! "
"GAME OVER!! (Press 'q' to terminate...)");
gotoxy(x[0],y[0]);
while(toupper(getch())!='Q');
}
clrscr();
printf("Hope you enjoyed the game Bye! ");
return 0;
}
/*-------------------------------------------------------------------------*/
void starting()
{
char i;
clrscr(); /* FIRST TO DRAW A BOUNDARY for THE GAME */
putchar('É');
for(i=0;i<78;i++)
putchar('Í');
putchar('»');
gotoxy(1,23);
putchar('È');
for(i=0;i<78;i++)
putchar('Í');
putchar('¼');
window(1,2,1,23);
for(i=0;i<21;i++)
cprintf("º");
window(80,2,80,23);
for(i=0;i<21;i++)
cprintf("º"); /* THE BOUNDARY IS DRAWN */
window(1,1,80,25);
gotoxy(38,12);
printf("ÛÛÛÛÛ"); /* THE "SNAKE" IS PUT for THE FIRST TIME */
gotoxy(1,24);
printf("Welcome to the game of SNAKE! (Press any arrow key to start now,"
" Escape to leave at any time...)"); /* WELCOME MESSAGE */
gotoxy(43,12);
while(!kbhit());
gotoxy(30,24);
delline();delline(); /* REMOVING MESSAGE */
cprintf(" ( EAT THE NUMBER !! ) Score = 0");
gotoxy(43,12); /* GO TO THE HEAD OF THE SNAKE */
}
void make_xy(char **px,char **py)
{
char i;
*px=(char *)malloc(1638*sizeof(char)); /*EARLIER IT WAS 6, NOT 1638; BUT*/
*py=(char *)malloc(1638*sizeof(char)); /*realloc IS NOT COPYING PROPERLY*/
for(i=0;i<6;i++){
(*px)[i]=43-i;
(*py)[i]=12;
} /* THE TWO ARRAYS for COORDINATES OF THE SNAKE ARE SIMULATED */
}
void getrand(char *px,char *py,char *pn,char *x,char *y,int length,char ch)
{
int allowed=0,i; /* i AND length MUST BE int */
while(!allowed){
allowed=1;
srand((unsigned)time(0));
*px=rand()%78+2; /* GENERATING RANDOM POSITIONAL COORDINATES for */
srand((unsigned)time(0));
*py=rand()%21+2; /* PUTTING A RANDOM NUMBER */
if(ch==UPARR){
if((*px==x[0])&(*py==y[0]-1))
allowed=0;
}
else if(ch==DOWNARR){
if((*px==x[0])&(*py==y[0]+1))
allowed=0;
}
else if(ch==LEFTARR){
if((*px==x[0]-1)&(*py==y[0]))
allowed=0;
}
else if((ch==RIGHTARR)&(*px==x[0]+1)&(*py==y[0]))
allowed=0;
for(i=0;(i<length)&&allowed;i++)
if((*px==x[i])&(*py==y[i]))
allowed=0;
} /* THE RANDOM NUMBER GENERATED SHOULD NOT BE PUT ON SNAKE'S BODY */
srand((unsigned)time(0));
*pn=rand()%9+1; /* THE NUMBER */
gotoxy(*px,*py);
putchar(*pn+48);
gotoxy(x[0],y[0]);
}
char getkey(char old_ch,char ch)
{
char i;
if(kbhit())
for(i=0;i<5;i++){ /* if i too low, takes too many keystrokes */
while((ch=getch())==0);
if(ch==27){
/* out=2;
i=5;
break;*/return ch;
}
if((ch!=LOAD)&(ch!=SAVE)&(ch!=UPARR)&(ch!=DOWNARR)&
(ch!=LEFTARR)&(ch!=RIGHTARR))
continue;
if((ch!=old_ch)|(!kbhit()))
break;
}
else
for(i=0;(i<12)&(!kbhit());i++)
delay(100);
return ch;
}
void savegame(char *px,char *py,int length,char ch)
{
FILE *fp;
int i;
rename("snake.sav","snake.bak");
fp=fopen("snake.sav","wb");
fprintf(fp,"%d %c",length,ch);
for(i=0;i<length;i++)
fprintf(fp,"%c%c",px[i],py[i]);
fclose(fp);
}
int loadgame(char *px,char *py,char *pch)
{
FILE *fp;
int length,i;
fp=fopen("snake.sav","rb");
if(!fp){
clrscr();
puts("ERROR: no saved game found in current directory!!! "
"Exiting... ");
sleep(3);
exit(1);
}
window(2,2,79,22);
clrscr();
/* fscanf(fp,"%d %c ",&length,pch);*/
fscanf(fp,"%d %c",&length,pch);
for(i=0;i<length;i++){
/* fscanf(fp,"%d %d ",&px[length],&py[length]);*/
fscanf(fp,"%c%c",&px[i],&py[i]);
gotoxy(px[i]-1,py[i]-1);
putchar('Û');
}
window(1,1,80,25);
gotoxy(30,24);
delline();delline(); /* REMOVING MESSAGE */
cprintf(" ( EAT THE NUMBER !! ) Score = %5d",length-6);
gotoxy(px[0],py[0]);
printf("");
fclose(fp);
return length;
}
void win_message()
{
window(1,1,80,25);
gotoxy(1,24);
delline();delline();
textcolor(14);
cprintf("CONGRATULATION!! YOU HAVE COMPLETED THE GAME!! "
"(Press any key to terminate...)");
clrscr();
textcolor(7);
}
//cscanf: WHEN YOU PRESS ENTER, CURSOR MOVES TO BEGINNING OF current LINE!!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.