MODULE PATTERN
Module
pattern is a software design pattern which allows the software designer to
break the software in modules and use the as and when required. This helps to
remove redundancy in and create a better design.
EXAMPLE:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
int ladder[6]={8,15,39,43,55,80};
int snakes[6]={14,38,42,79,82,99};
int main()
{
int p[4]={1,1,1,1},dice=0;
int roll_dice();
int add_pos(int,int);
int snake_bite(int);
int climb_ladder(int);
char choice;
int chance=0;
while(p[0] !=100 && p[1] !=100 && p[2] !=100 && p[3] !=100)
{
chance=chance%4;
cout<<"\n \nchance :"<< chance+1;
cout<<"\n Do you wanna roll dice?(y)";
while(choice!='y')
{
cin>>choice;
}
dice=roll_dice();
cout<<"your no. is "<<dice;
p[chance]=add_pos(p[chance],dice);
cout<<"\n Your position is :"<<p[chance];
choice='n';
chance++;
}
cout<<"\n"<<p[0]<<"\n"<<p[1]<<"\n"<<p[2]<<"\n"<<p[3];
return 0;
}
int roll_dice()
{
int a = rand()%7;
if(a==0)
{
a= roll_dice();
}
return a;
}
int snake_bite(int a )
{
if(a==10)
{
a=6;
cout<<"\n Snake bite";
}
else if(a==38)
{
a=28;
cout<<"\n Snake bite";
}
else if(a==42)
{
a=19;
cout<<"\n Snake bite";
}
else if(a==79)
{
a=50;
cout<<"\n Snake bite";
}
else if(a==88)
{
a=30;
cout<<"\n Snake bite";
}
else if(a==99)
{
a=5;
cout<<"\n Snake bite";
}
return a;
}
int climb_ladder(int a)
{
if(a==8)
{
a=25;
cout<<"\n congratulations you climbed ladder";
}
else if(a==15)
{
a=46;
cout<<"\n congratulations you climbed ladder";
}
else if(a==39)
{
a=63;
cout<<"\n congratulations you climbed ladder";
}
else if(a==43)
{
a=81;
cout<<"\n congratulations you climbed ladder";
}
else if(a==55)
{
a=91;
cout<<"\n congratulations you climbed ladder";
}
else if(a==80)
{
a=98;
cout<<"\n congratulations you climbed ladder";
}
return a;
}
int add_pos(int a,int b)
{
int pos=a+b;
if(pos>100)
{
pos=a;
}
for(int i=0;i<6;i++)
{
if(pos==snakes[i])
pos=snake_bite(pos);
}
for(int i=0;i<6;i++)
{
if(pos==ladder[i])
pos=climb_ladder(pos);
}
return pos;
}
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
int ladder[6]={8,15,39,43,55,80};
int snakes[6]={14,38,42,79,82,99};
int main()
{
int p[4]={1,1,1,1},dice=0;
int roll_dice();
int add_pos(int,int);
int snake_bite(int);
int climb_ladder(int);
char choice;
int chance=0;
while(p[0] !=100 && p[1] !=100 && p[2] !=100 && p[3] !=100)
{
chance=chance%4;
cout<<"\n \nchance :"<< chance+1;
cout<<"\n Do you wanna roll dice?(y)";
while(choice!='y')
{
cin>>choice;
}
dice=roll_dice();
cout<<"your no. is "<<dice;
p[chance]=add_pos(p[chance],dice);
cout<<"\n Your position is :"<<p[chance];
choice='n';
chance++;
}
cout<<"\n"<<p[0]<<"\n"<<p[1]<<"\n"<<p[2]<<"\n"<<p[3];
return 0;
}
int roll_dice()
{
int a = rand()%7;
if(a==0)
{
a= roll_dice();
}
return a;
}
int snake_bite(int a )
{
if(a==10)
{
a=6;
cout<<"\n Snake bite";
}
else if(a==38)
{
a=28;
cout<<"\n Snake bite";
}
else if(a==42)
{
a=19;
cout<<"\n Snake bite";
}
else if(a==79)
{
a=50;
cout<<"\n Snake bite";
}
else if(a==88)
{
a=30;
cout<<"\n Snake bite";
}
else if(a==99)
{
a=5;
cout<<"\n Snake bite";
}
return a;
}
int climb_ladder(int a)
{
if(a==8)
{
a=25;
cout<<"\n congratulations you climbed ladder";
}
else if(a==15)
{
a=46;
cout<<"\n congratulations you climbed ladder";
}
else if(a==39)
{
a=63;
cout<<"\n congratulations you climbed ladder";
}
else if(a==43)
{
a=81;
cout<<"\n congratulations you climbed ladder";
}
else if(a==55)
{
a=91;
cout<<"\n congratulations you climbed ladder";
}
else if(a==80)
{
a=98;
cout<<"\n congratulations you climbed ladder";
}
return a;
}
int add_pos(int a,int b)
{
int pos=a+b;
if(pos>100)
{
pos=a;
}
for(int i=0;i<6;i++)
{
if(pos==snakes[i])
pos=snake_bite(pos);
}
for(int i=0;i<6;i++)
{
if(pos==ladder[i])
pos=climb_ladder(pos);
}
return pos;
}
No comments:
Post a Comment