Snake and Ladder Game in C, Snake Game
C Program, C Game Programming

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
void line_(int n,char ch);
void board();
void scores(char ply1[],char ply2[],int p1, int p2);
void dice(int &score);
void main()
{
int opp1=0,opp2=0,finalpos;
char opp1name[80],opp2name[80];
clrscr();
randomize();
line_(50,'=');
cout<<"\n\n\n\n\t\t ~SNAKE #LADDER GAME\n\n\n\n";
line_(50,'=');
cout<<"\n\n\nPlayer 1 :";
gets(opp1name);
cout<<"\n\n\nPlayer 2 :";
gets(opp2name);
while(opp1<=100 && opp2<=100)
{
board();
scores(opp1name,opp2name,opp1,opp2);
cout<<"\n\n--->" <<opp1name<<" Its your turn >> Enter key to
play ";
getch();
finalpos=opp1;
dice(opp1);
if(opp1<finalpos)
cout<<"\n\a SNAKEEE found !! You are now at postion
"<<opp1<<"\n";
else if(opp1>finalpos+6)
cout<<"\nWELL PLAYED LADDER # .NOW You are at position
"<<opp1;
cout<<"\n\n--->"<<opp2name<<" Its your Turn >> Enter any key
to play ";
getch();
finalpos=opp2;
dice(opp2);
if(opp2<finalpos)
cout<<"\n\n\aSNAKEEE found !! You are now at postion
"<<opp2<<"\n";
else if(opp2>finalpos+6)
cout<<"\n\nSuperb!! Ladder Now you are at position
"<<opp2<<"\n";
getch();
}
clrscr();
cout<<"\n\n\n";
line_(50,'+');
cout<<"\n\n\t\tFINAL RESULT\n\n";
line_(50,'+');
cout<<endl;
scores(opp1name,opp2name,opp1,opp2);
cout<<"\n\n\n";
if(opp1>=opp2)
cout<<opp1name<<" !! You WON\n\n";
else
cout<<opp2name<<" !! You WON\n\n";
line_(50,'+');
getch();
}
void line_(int n,char ch)
{
for(int i=0;i<n;i++)
cout<<ch;
}
void board()
{
clrscr();
cout<<"\n\n";
line_(50,'-');
cout<<"\n\t\tSNAKE AT POSITION\n";
line_(50,'-');
cout<<"\n\tFrom 98 to 28 \n\tFrom 95 to 24\n\tFrom 92 to
51\n\tFrom 83 to 19\n\tFrom 73 to 1\n\tFrom 69 to 33\n\tFrom
64 to 36\n\tFrom 59 to 17\n\tFrom 55 to 7\n\tFrom 52 to
11\n\tFrom 48 to 9\n\tFrom 46 to 5\n\tFrom 44 to 22\n\n";
line_(50,'-');
cout<<"\n\t\t LADDER AT POSITION\n";
line_(50,'-');
cout<<"\n\tFrom 8 to 26\n\tFrom 21 to 82\n\tFrom 43 to 77\n\tFrom
50 to 91\n\tFrom 62 to 96\n\tFrom 66 to 87\n\tFrom 80 to
100\n";
line_(50,'-');
cout<<endl;
}
void scores(char ply1[],char ply2[],int p1, int p2)
{
cout<<"\n";
line_(50,'~');
cout<<"\n\t\tGAME STATUS\n";
line_(50,'~');
cout<<"\n\t--->"<<ply1<<" is at position "<<p1<<endl;
cout<<"\t--->"<<ply2<<" is at position "<<p2<<endl;
line_(50,'_');
cout<<endl;
}
void dice(int &score)
{
int dice;
dice=random(6)+1;
cout<<"\nYou got "<<dice<<" Point !! ";
score=score+dice;
cout<<"Now you are at position "<<score;
switch(score)
{
case 98 :score=28;break;
case 95 :score=24;break;
case 92 :score=51;break;
case 83 :score=19;break;
case 73 :score=1;break;
case 69 :score=33;break;
case 64 :score=36;break;
case 59 :score=17;break;
case 55 :score=7;break;
case 52 :score=11;break;
case 48 :score=9;break;
case 46 :score=5;break;
case 44 :score=22;break;
case 8 :score=26;break;
case 21 :score=82;break;
case 43 :score=77;break;
case 50 :score=91;break;
case 54 :score=93;break;
case 62 :score=96;break;
case 66 :score=87;break;
case 80 :score=100;
}
}
|