-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnakeAndLadder
More file actions
185 lines (151 loc) · 4.18 KB
/
SnakeAndLadder
File metadata and controls
185 lines (151 loc) · 4.18 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
Problem Description:
/*
Design Snake and ladder Game That will take Board size, number of snakes, number of ladders, number of players as an input and return player who reaches the end its as output.
Game will continue until last player will left and returns their names in the order they wins the game.
Board can be of any size.
There can be snake and ladder at any position. Position of snakes and ladders will be given as an input.
*/
Solution:
#include<bits/stdc++.h>
using namespace std;
class Board{
public:
int size;
int start;
int end;
void setBoard(int size){
this->start = 1;
this->end = start+size-1;
this->size = size;
}
};
class Dice{
public:
int minValue;
int maxValue;
void setDice(int min,int max){
minValue = min;
maxValue = max;
}
int roll(){
return minValue+(rand()%maxValue);
}
};
class Ladder{
public:
int start;
int end;
};
class Player{
public:
string name;
int position;
bool won;
Player(string name){
this->name = name;
this->position = 0;
this->won = false;
}
};
class Snake{
public:
int head;
int tail;
};
class Game{
public:
int numberOfLadders;
int numberOfSnakes;
queue<Player> players;
list<Snake> snakes;
list<Ladder> ladders;
Board board;
Dice dice;
Game(int numberOfLadders, int numberOfSnakes,int size){
this->numberOfLadders = numberOfLadders;
this->numberOfSnakes = numberOfSnakes;
queue<Player> q;
this->players = q;
list<Snake> s(numberOfSnakes);
snakes = s;
list<Ladder> l(numberOfLadders);
ladders = l;
board.setBoard(size);
dice.setDice(1,6);
initBoard();
}
void initBoard(){
for(int i=0;i<numberOfSnakes;i++){
int snakeStart;
int snakeEnd;
cin>>snakeStart>>snakeEnd;
snakes.push_back({snakeStart,snakeEnd});
}
for(int i=0;i<numberOfLadders;i++){
int ladderStart;
int ladderEnd;
cin>>ladderStart>>ladderEnd;
ladders.push_back({ladderStart,ladderEnd});
}
}
void addPlayer(Player player){
players.push(player);
}
void PlayGame(){
int cnt = 1;
while(true){
Player player = players.front();
players.pop();
int value = dice.roll();
int newPosition = player.position + value;
if(newPosition > board.end){
player.position = newPosition-value;
players.push(player);
}
else{
player.position = getNewPosition(newPosition);
if(player.position == board.end){
player.won = true;
cout<<"Player secured "<<cnt++<<" position "<<player.name<<endl;
//break;
}
else{
players.push(player);
}
}
if(players.size()<2)
break;
}
}
int getNewPosition(int newPosition){
for(Snake snake:snakes){
if(snake.head == newPosition){
return snake.tail;
}
}
for(Ladder ladder:ladders){
if(ladder.start == newPosition){
return ladder.end;
}
}
return newPosition;
}
};
int main(){
int boardSize;
cin>>boardSize;
int numberOfPlayers;
cin>>numberOfPlayers;
int numSnakes;
cin>>numSnakes;
int numLadders;
cin>>numLadders;
Game game(numLadders, numSnakes, boardSize);
for(int i=0;i<numberOfPlayers;i++){
string name;
cin>>name;
Player player(name);
game.addPlayer(player);
}
game.PlayGame();
}