-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cpp
275 lines (229 loc) · 5.88 KB
/
Main.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include <bits/stdc++.h>
using namespace std;
class Snake{
int start;
int end;
public:
Snake(int start,int end)
{
this->start=start;
this->end=end;
}
int getStart(){
return start;
}
int getEnd(){
return end;
}
};
class Ladder{
int start;
int end;
public:
Ladder(int start,int end)
{
this->start=start;
this->end=end;
}
int getStart(){
return start;
}
int getEnd(){
return end;
}
};
class Player{
string name;
// string id;
public:
Player (string name){
this->name = name;
// this->id = rand(0,10);
}
string getName(){
return name;
}
};
class SnakeAndLadderBoard{
int size;
vector<Snake> snakes;
vector<Ladder> ladders;
map<string,int> playerpieces;
public:
SnakeAndLadderBoard(){
this->size=100;
}
int getSize(){
return size;
}
vector<Snake> getSnakes(){
return snakes;
}
void setSnakes(vector<Snake> snakes){
this->snakes = snakes;
}
vector<Ladder> getLadder(){
return ladders;
}
void setLadder(vector<Ladder> ladders){
this->ladders = ladders;
}
map<string,int> getplayerpieces(){
return playerpieces;
}
void setPlayerpieces(map<string,int> playerpieces){
this->playerpieces = playerpieces;
}
};
class DiceService{
public :
int roll(){
return rand()%6+1;
}
};
class SnakeAndLadderService {
SnakeAndLadderBoard snakeandladderboard;
int intialplayer;
queue<Player> p;
public:
bool isCompleted = false;
SnakeAndLadderService(){
this->snakeandladderboard = SnakeAndLadderBoard();
}
void setPlayers(vector<Player> players){
this->intialplayer = players.size();
map<string,int> m;
for(Player player : players){
p.push(player);
m[player.getName()] = 0;
}
snakeandladderboard.setPlayerpieces(m);
}
void setSnakes(vector<Snake> snakes){
vector<Snake> s ;
for(Snake snake : s)
cout<<snake.getStart()<<" "<<snake.getEnd()<<"\n";
snakeandladderboard.setSnakes(snakes);
}
void setLadders(vector<Ladder> ladders){
snakeandladderboard.setLadder(ladders);
}
int getnewposition(int newpos){
int prevpos;
do{
prevpos=newpos;
for(Snake snake : snakeandladderboard.getSnakes())
{
if(snake.getStart() == newpos)
{
newpos= snake.getEnd();
}
}
for(Ladder ladder : snakeandladderboard.getLadder())
{
if(ladder.getStart() == newpos)
{
newpos= ladder.getEnd();
}
}
}
while(newpos!=prevpos);
return newpos;
}
void movePlayer(Player player,int positions){
map<string,int> m = snakeandladderboard.getplayerpieces();
int oldpos = m[player.getName()];
// cout<<oldpos<<"\n";
int newpos = oldpos+positions;
// cout<<newpos<<"\n";
int boardSize = snakeandladderboard.getSize();
if(newpos>boardSize)
{
newpos = oldpos;
}
else
{
newpos = getnewposition(newpos);
}
// cout<<newpos<<"\n";
m[player.getName()]=newpos;
// snakeandladderboard.getplayerpieces().insert({player.getName(),newpos});
// cout<<player.getName()<<" "<<newpos<<"\n";
snakeandladderboard.setPlayerpieces(m);
cout<<player.getName() <<" rolled a "<<positions<<" and moved from "<<oldpos<<" to "<<newpos<<"\n";
}
int getnewvalue(){
DiceService d1;
return d1.roll();
}
bool hasplayerwon(Player player){
map<string,int> m ;
m=snakeandladderboard.getplayerpieces();
int playerpos = m[player.getName()];
int winpos = snakeandladderboard.getSize();
return playerpos==winpos;
}
bool isGameCompleted(){
int cur_players = p.size();
return cur_players<intialplayer;
}
void startGame(){
int first = 1;
int count=0;
while(first || p.size()>=2){
// cout<<"Game start"<<"\n";
first=0;
int totalDicevalue = getnewvalue();
cout<<totalDicevalue<<"\n";
Player current_player = p.front();
p.pop();
movePlayer(current_player,totalDicevalue);
if(hasplayerwon(current_player))
{
count++;
cout<<current_player.getName()<<" won " <<count<<" position ################################"<<"\n";
map<string,int> m;
m.erase(current_player.getName());
}
else
{
p.push(current_player);
}
}
}
};
int main() {
int s;
cin>>s;
vector<Snake> snakes;
for(int i=0;i<s;i++)
{
int x,y;
cin>>x>>y;
snakes.push_back(Snake(x,y));
}
int l;
cin>>l;
vector<Ladder> ladders;
for(int i=0;i<l;i++)
{
int x,y;
cin>>x>>y;
ladders.push_back(Ladder(x,y));
}
int p;
cin>>p;
vector<Player> players;
for(int i=0;i<p;i++)
{
string x;
cin>>x;
// cout<<Player(x).getName()<<"\n";
players.push_back(Player(x));
}
SnakeAndLadderService s1 ;
s1.setPlayers(players);
s1.setLadders(ladders);
s1.setSnakes(snakes);
s1.startGame();
}