-
Notifications
You must be signed in to change notification settings - Fork 514
Expand file tree
/
Copy pathmain.cpp
More file actions
189 lines (166 loc) · 5.04 KB
/
Copy pathmain.cpp
File metadata and controls
189 lines (166 loc) · 5.04 KB
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
#include<bits/stdc++.h>
using namespace std;
class Board {
private:
int cells, snakesCount, laddersCount;
unordered_map<int, int> snakes;
unordered_map<int, int> ladders;
void setSnakesValues (vector<pair<int, int>> &snakes) {
this->snakesCount = snakes.size();
for (auto each : snakes)
this->snakes[each.first] = each.second;
}
void setLaddersValues (vector<pair<int, int>> &ladders) {
this->laddersCount = ladders.size();
for (auto each : ladders)
this->ladders[each.first] = each.second;
}
public:
Board (vector<pair<int, int>> &snakes, vector<pair<int, int>> &ladders) {
setSnakesValues(snakes);
setLaddersValues(ladders);
this->cells = 100;
}
Board (vector<pair<int, int>> &snakes, vector<pair<int, int>> &ladders, int cells) {
setSnakesValues(snakes);
setLaddersValues(ladders);
this->cells = cells;
}
int isSnakeSting (int cellNumber) {
if (snakes[cellNumber]) {
return snakes[cellNumber];
}
else {
return -1;
}
}
int isLadderRewarded (int cellNumber) {
if (ladders[cellNumber]) {
return ladders[cellNumber];
}
else {
return -1;
}
}
};
class DiceRoller {
private:
int diceCount;
public:
DiceRoller () {
this->diceCount = 1;
}
DiceRoller (int n) {
this->diceCount = n;
}
int RollTheDice () {
mt19937 generator(std::chrono::system_clock::now().time_since_epoch().count());
uniform_int_distribution<int> distribution(1, 6);
int sum = distribution(generator);
if (sum == 6) {
int secondChance = distribution(generator);
if (sum + secondChance == 12) {
int thirdChance = distribution(generator);
if (sum + secondChance + thirdChance == 18) {
return 0;
} else {
return sum + secondChance + thirdChance;
}
} else {
return sum + secondChance;
}
} else {
return sum;
}
}
};
class Player {
private:
string name;
int cellPosition;
public:
Player (string name) {
this->name = name;
cellPosition = 0;
}
string getName() {
return this->name;
}
int getCellNumber () {
return this->cellPosition;
}
void updateCellPosition (int newCellPosition) {
this->cellPosition = newCellPosition;
}
};
class Game {
public:
Game () {
}
void play () {
int numberOfSnakes;
cin>>numberOfSnakes;
int from , to;
vector<pair<int, int> > snakes(numberOfSnakes);
for (int i =0 ; i < numberOfSnakes; ++i) {
cin>>from>>to;
snakes[i] = { from, to };
}
int numberOfLadders;
cin>>numberOfLadders;
vector<pair<int, int> > ladders(numberOfLadders);
for (int i =0; i < numberOfLadders; ++i) {
cin>>from>>to;
ladders[i] = { from, to };
}
Board * board = new Board(snakes, ladders);
int numberOfPlayers;
cin>>numberOfPlayers;
string name;
vector<Player *> players(numberOfPlayers);
for (int i =0; i < numberOfPlayers; ++i) {
cin>>name;
players[i] = new Player(name);
}
DiceRoller * dice = new DiceRoller();
int playerReached = -1;
while(true) {
for (int i = 0; i < numberOfPlayers; ++i) {
int num = dice->RollTheDice();
int initialPosition = players[i]->getCellNumber();
int newPosition = num + initialPosition;
int snakePosition = board->isSnakeSting(newPosition);
int ladderPosition = board->isLadderRewarded(newPosition);
if (snakePosition != -1) {
newPosition = snakePosition;
} else if (ladderPosition != -1) {
newPosition = ladderPosition;
}
if (newPosition > 100) {
newPosition = initialPosition;
cout<<players[i]->getName()<<" rolled a "<<num<<" which is not valid from "<<initialPosition<<endl;
} else {
players[i]->updateCellPosition(newPosition);
cout<<players[i]->getName()<<" rolled a "<<num<<" and moved from "<<initialPosition<<" to "<<newPosition<<endl;
}
if (newPosition == 100) {
playerReached = i;
break;
}
}
if (playerReached != -1) {
cout<<players[playerReached]->getName()<<" wins the game";
break;
}
}
}
};
int main () {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
Game * game = new Game();
game->play();
return 0;
}