-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnakeandladder.cpp
More file actions
68 lines (51 loc) · 1.59 KB
/
snakeandladder.cpp
File metadata and controls
68 lines (51 loc) · 1.59 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
#include <bits/stdc++.h>
using namespace std;
// Dice function
int rollDice() {
return rand() % 6 + 1;
}
int main() {
srand(time(0));
int player1 = 0, player2 = 0;
int dice, turn = 1;
// Snakes and Ladders using map
map<int, int> snakes = {
{99, 54}, {70, 55}, {52, 42}, {25, 2}, {95, 72}
};
map<int, int> ladders = {
{6, 25}, {11, 40}, {60, 85}, {46, 90}, {17, 69}
};
cout << "===== SNAKE AND LADDER GAME =====\n";
while (player1 < 100 && player2 < 100) {
cout << "\nPlayer " << turn << "'s turn";
cout << "\nPress Enter to roll dice...";
cin.ignore();
dice = rollDice();
cout << "Dice rolled: " << dice << endl;
if (turn == 1) {
if (player1 + dice <= 100)
player1 += dice;
if (snakes[player1])
player1 = snakes[player1];
else if (ladders[player1])
player1 = ladders[player1];
cout << "Player 1 position: " << player1 << endl;
turn = 2;
}
else {
if (player2 + dice <= 100)
player2 += dice;
if (snakes[player2])
player2 = snakes[player2];
else if (ladders[player2])
player2 = ladders[player2];
cout << "Player 2 position: " << player2 << endl;
turn = 1;
}
}
if (player1 == 100)
cout << "\n🎉 Player 1 Wins!\n";
else
cout << "\n🎉 Player 2 Wins!\n";
return 0;
}