-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnakes.cpp
113 lines (113 loc) · 3.35 KB
/
Snakes.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include<bits/stdc++.h>
using namespace std;
int main()
{
class Player
{
private:
string name;
public:
int position;
void assig(string a,int b)
{
position=b;
name=a;
}
void play()
{
cout<<name<<", enter a number: ";
int faltu,i;
string alpha;
cin>>alpha;
faltu=1+rand()%6;
if(faltu==6)
faltu=faltu+1+rand()%6;
if(faltu==12)
faltu=faltu+1+rand()%6;
int num1,num2;
num1=faltu/6;
num2=faltu%6;
if(num1==3)
cout<<"You threw 3 sixes in a row.You don't get to move.\n";
else
{
position=position+faltu;
if(position>100)
{position=position-faltu;cout<<"Sorry, You can't move\n";}
else
{
cout<<"You threw a ";
for(i=0;i<num1;i++)
cout<<"6, ";
cout<<num2<<"\nYou have advanced to "<<position<<"\n";
}
}
if(find(ladder_start.begin(),ladder_start.end(),position)!=ladder_start.end())
{
cout<<"Congratulations!! You climbed a Ladder\n";
position=ladder_end[find(ladder_start.begin(),ladder_start.end(),position)-ladder_start.begin()];
cout<<"You have advanced to "<<position<<"\n";
cout<<"Let's Celebrate.You get another chance\n";
play();
}
else if(find(snake_start.begin(),snake_start.end(),position)!=snake_start.end())
{
cout<<"Alas! You were bitten by a snake\n";
position=snake_end[find(snake_start.begin(),snake_start.end(),position)-snake_start.begin()];
cout<<"You have receded to "<<position<<"\n";
}
}
void give_name()
{cout<<name;}
int win()
{
if(position==100)
return 1;
else return 0;
}
vector<int> snake_start={30,55,67,62,87,99};
vector<int> snake_end={15,21,23,50,13,5};
vector<int> ladder_start={3,10,25,41,56,70,84,};
vector<int> ladder_end={22,43,69,81,77,95,93};
};
srand(time(0));
cout<<"Welcome to Snakes and Ladders \n";
cout<<"Enter the number of players \n";
int j,n,k;
string b;
cin>>n;
Player p[n];
for(j=0;j<n;j++)
{
cout<<"Player "<<j+1<<" Enter your name: \n";
cin>>b;
p[j].assig(b,1);
}
j=0;
while(1)
{
p[j].play();
if(p[j].win())
{
cout<<"Yippee!! ";
p[j].give_name();
cout<<" wins.\n";
cout<<"Good Game. Cheers";
return 0;
}
for(k=0;k<n;k++)
{
if((p[j].position==p[k].position)&&(j!=k))
{
cout<<"Sorry ";
p[k].give_name();
cout<<" ";
p[j].give_name();
p[k].position=1;
cout<<" stepped on you. You are being sent to Postion 1.\n";
}
}
j=(j+1)%n;
}
return 0;
}