-
Notifications
You must be signed in to change notification settings - Fork 513
Expand file tree
/
Copy pathSnakeNLadder.cpp
More file actions
129 lines (116 loc) · 3.3 KB
/
Copy pathSnakeNLadder.cpp
File metadata and controls
129 lines (116 loc) · 3.3 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
// Snake and ladder Game Implementation
#include <bits/stdc++.h>
using namespace std;
class Players {
public:
string name;
int initial_pos;
int final_pos;
Players(string name) {
this->name = name;
initial_pos = 0;
final_pos = 0;
}
};
class Snake {
public:
vector<pair<int, int> > SnakePos;
Snake(vector<pair<int, int> > SnakePos) { this->SnakePos = SnakePos; }
};
class Ladder {
public:
vector<pair<int, int> > LadderPos;
Ladder(vector<pair<int, int> > LadderPos) { this->LadderPos = LadderPos; }
};
class Game {
public:
vector<Players> players;
Snake snake;
Ladder ladder;
int total_players;
Game(vector<Players> players, Snake snake, Ladder ladder)
: players(players), snake(snake), ladder(ladder) {
this->total_players = players.size();
}
void playGame() {
int i = 0;
while (i < total_players) {
int dice = rand() % 6 + 1;
if (players[i].final_pos + dice <= 100) {
players[i].initial_pos = players[i].final_pos;
players[i].final_pos += dice;
for (int j = 0; j < snake.SnakePos.size(); j++) {
if (players[i].final_pos == snake.SnakePos[j].first) {
players[i].final_pos = snake.SnakePos[j].second;
}
}
for (int j = 0; j < ladder.LadderPos.size(); j++) {
if (players[i].final_pos == ladder.LadderPos[j].first) {
players[i].final_pos = ladder.LadderPos[j].second;
}
}
cout << players[i].name << " rolled a " << dice << " moved from "
<< " from " << players[i].initial_pos << " to "
<< players[i].final_pos << endl;
// Check if player wins
if (players[i].final_pos == 100) {
cout << players[i].name << " won the game!" << endl;
break;
}
}
i = (i + 1) % total_players;
}
}
};
int main() {
srand(time(0));
int s, l, p;
vector<pair<int, int> > SnakePos;
vector<pair<int, int> > LadderPos;
vector<Players> players;
cout << "enter the number of snakes\n";
cin >> s;
cout << "enter the snake positions in the order of head and tail\n";
for (int i = 0; i < s; i++) {
int x, y;
cin >> x >> y;
SnakePos.push_back(make_pair(x, y));
}
cout << "enter the number of ladders\n";
cin >> l;
cout << "enter the ladder positions in the order of start and end\n";
for (int i = 0; i < l; i++) {
int x, y;
cin >> x >> y;
LadderPos.push_back(make_pair(x, y));
}
cout << "enter the number of players\n";
cin >> p;
cout << "enter the player names\n";
for (int i = 0; i < p; i++) {
string name;
cin >> name;
players.push_back(Players(name));
}
Snake snake(SnakePos);
Ladder ladder(LadderPos);
for (auto it = snake.SnakePos.begin(); it != snake.SnakePos.end();) {
if (it->first < it->second) {
cout << "Invalid snake positions at head: " << it->first
<< " and tail:" << it->second << endl;
it = snake.SnakePos.erase(it);
} else
++it;
}
for (auto it = ladder.LadderPos.begin(); it != ladder.LadderPos.end();) {
if (it->first > it->second) {
cout << "Invalid ladder positions at head: " << it->first
<< " and tail:" << it->second << endl;
it = ladder.LadderPos.erase(it);
} else
++it;
}
Game game(players, snake, ladder);
game.playGame();
return 0;
};