Thursday 5 January 2017
On 01:50 by Vardan Kumar in C tutorial No comments
FUNCTIONALITIES IN THE GAME
- Use of File handling to save the game and resuming it from previously save point.
- Random selection of six different combinations of snakes as well as six different ladders every time the game runs.
- A player gets a green signal only if he gets 1 or 6 as dice value.
- A player gets a consecutive turn if he gets 6 as dice value
- Player is demoted to an end point of the snake he hits(off course if hits at a particular position).
Player hit by snake and ladder - A player can only win if he reaches 100 not more or less,for example if he is at 99 and he gets 5,then he is not allowed to move his piece,he can only win if he gets 1).
- A lot more logical constraints are applied so that the logical feasibility of the game is maintained.
SOURCE CODE
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int ladlower[6]={0,0,0,0,0,0},ladupper[6]={0,0,0,0,0,0},snalow[6]={0,0,0,0,0,0},snaup[6]={0,0,0,0,0,0};
void dissal(int pl,int comp)
{
if(pl<comp)
{
printf("Yipee you got a ladder to %d",comp);
}
else if(pl>comp)
{
printf("oops snake has eaten you to position %d",comp);
}
}
int checkforsal(int player)
{
int i,pos;
for(i=0;i<6;i++)
{
if (player==ladlower[i])
{
pos=ladupper[i];
break;
}
else if(player==snaup[i])
{
pos=snalow[i];
break;
}
else
{
pos=player;
}
}
return pos;
}
void main()
{
int temp,p1=0,p2=0,di=0,fp1=0,fp2=0,fturn1=1,pocket=0,i;
int j=0;
FILE *f;
char ch,get;
int state=0;
while(1)
{
printf("Press 1 to start new game\n");
printf("Press 2 to resume previous game\n");
fflush(stdin);
srand(time(NULL));
get=getchar();
if(get=='1')
{
state=0;
}
else if(get=='2')
{
state=1;
}
else
{
continue;
}
if(state==1)
{
f=fopen("sal.txt","r");
fread(ladupper,sizeof(ladupper),1,f);
fread(ladlower,sizeof(ladlower),1,f);
fread(snaup,sizeof(snaup),1,f);
fread(snalow,sizeof(snalow),1,f);
fread(&p1,sizeof(int),1,f);
fread(&p2,sizeof(int),1,f);
fread(&fp1,sizeof(int),1,f);
fread(&fp2,sizeof(int),1,f);
fclose(f);
}
else if (state==0)
{
for( i = 0 ; i < 6 ; i++ )
{
j=0;
ladlower[i]=(rand()%90)+1;
ladupper[i]=((ladlower[i]+(rand()%100)+1)%99)+1;
snalow[i]=(rand()%90)+1;
snaup[i]=((snalow[i]+(rand()%100)+1)%99)+1;
if(ladlower[i]>ladupper[i])
{
temp=ladlower[i];
ladlower[i]=ladupper[i];
ladupper[i]=temp;
}
if(snalow[i]>snaup[i])
{
temp=snalow[i];
snalow[i]=snaup[i];
snaup[i]=temp;
}
if(ladupper[i]-ladlower[i]<9&&ladupper[i]<90)
{
ladupper[i]=ladupper[i]+10;
}
if(snaup[i]-snalow[i]<9&&snaup[i]<90)
{
snaup[i]=snaup[i]+7;
}
if(ladupper[i]==snalow[i]||ladupper[i]==snaup[i]
||ladlower[i]==snaup[i]||ladlower[i]==snalow[i])
{
i=i-1;
continue;
}
while(j<i)
{
if(ladupper[j]==snaup[i]||ladlower[j]==snalow[i]||ladlower[j]==snaup[i]||
snalow[j]==snaup[i]||snaup[j]==snaup[i]||snalow[j]==snalow[i]||ladlower[j]==ladlower[i]
||ladupper[j]==ladupper[i])
{
i=i-1;
continue;
}
j++;
}
}
}
printf("\n\t Ladders\t\t Snakes\n");
for(i=0;i<6;i++)
{
printf("%d\t%02d--->%02d",i+1,ladlower[i],ladupper[i]);
printf("\t\t%02d--->%02d\n",snaup[i],snalow[i]);
}
while(p1!=100&&p2!=100)
{
if(fturn1==1)
{
printf("Player 1 turn:Please press R to roll the dice");
printf("\nPress a to restart the game ");
printf("\nPress s to save and quit the game");
printf("\nPress q to quit without saving");
fflush(stdin);
ch=getchar();
if(ch=='r')
{
di=(rand()%6)+1;
printf("\nWooo!You got %d ",di);
if(p1==0&&(di==1||di==6))
{
fp1=1;
printf("\nYeah you are good to go....");
}
if(fp1==1&&di==6)
{
fturn1=1;
p1=p1+6;
pocket=checkforsal(p1);
dissal(p1,pocket);
p1=pocket;
}
else if(fp1==1&&di!=6)
{
fturn1=0;
p1=p1+di;
pocket=checkforsal(p1);
dissal(p1,pocket);
p1=pocket;
}
else if(fp1==0)
{
printf("\nSorry,you need 1 or 6 to get a green signal");
fturn1=0;
p1=0;
}
if(p1>100)
{
printf("Sorry you cannot cross 100");
p1=p1-di;
}
printf("\nPlayer 1 at position %02d\n",p1);
printf("Player 2 at position %02d\n",p2);
}
else if(ch=='q')
{
exit(0);
}
else if(ch=='a')
{
state=0;
p1=0;
p2=0;
fp1=0;
fp2=0;
fturn1=1;
break;
}
else if(ch=='s')
{
state=1;
f = fopen("sal.txt", "w");
//printf("values are %d %d",snalow[4],snalow[5]);
if(f==NULL)
printf("Unable to open file\n");
fwrite(ladupper, sizeof(ladupper),1 , f);
fwrite(ladlower, sizeof(ladlower), 1 , f);
fwrite(snaup, sizeof(snaup),1 , f);
fwrite(snalow, sizeof(snalow), 1 , f);
fwrite(&p1,sizeof(int),1,f);
fwrite(&p2,sizeof(int),1,f);
fwrite(&fp1,sizeof(int),1,f);
fwrite(&fp2,sizeof(int),1,f);
fclose(f);
exit(0);
}
else
{
continue;
}
}
else
{
printf("Player 2 turn:Please press R to roll the dice");
fflush(stdin);
if(getchar()=='r')
{
di=(rand()%6)+1;
printf("\nWooo!You got %d ",di);
if(p2==0&&(di==1||di==6))
{
fp2=1;
printf("\nYeah you are good to go....");
}
if(fp2==1&&di==6)
{
fturn1=0;
p2=p2+6;
pocket=checkforsal(p2);
dissal(p2,pocket);
p2=pocket;
}
else if(fp2==1&&di!=6)
{
fturn1=1;
p2=p2+di;
pocket=checkforsal(p2);
dissal(p2,pocket);
p2=pocket;
}
else if(fp2==0)
{
printf("\nSorry,you need 1 or 6 to get a green signal");
fturn1=1;
p2=0;
}
if(p2>100)
{
printf("Sorry you cannot cross 100");
p2=p2-di;
}
printf("\nPlayer 1 at position %02d\n",p1);
printf("Player 2 at position %02d\n",p2);
}
else
{
continue;
}
}
}
if(p1==100)
{
printf("\t\t\tPLayer 1 Wins");
}
else if(p2==100)
{
printf("\t\t\tPlayer 2 Wins");
}
else
{
printf("\nThe game has been restarted\n");
}
}
}
Subscribe to:
Post Comments (Atom)
Search
Popular Posts
-
Troubleshooting Cisco VPN client Before starting troubleshooting, Let us see what VPN is and what it requires to perform its intended f...
-
Evolution-Mobile Phones With the development of portable technology,wireless communication has so evolved that (According to the announce...
-
File Versioning C# File versioning, saving file with unique file name in c# File versioning allows a user to have several versions of ...
-
Text Box Hint in c# Windows Form Application Text Box Hint in c# Windows Form Application While developing a windows form applicat...
-
Unable to set the Freeze Panes property of Window Class C# It is generally easy to resolve the compile time errors because the reason fo...
0 comments:
Post a Comment