-
Notifications
You must be signed in to change notification settings - Fork 513
Expand file tree
/
Copy pathmain.cpp
More file actions
51 lines (36 loc) · 1.21 KB
/
Copy pathmain.cpp
File metadata and controls
51 lines (36 loc) · 1.21 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
#include <iostream>
#include <map>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;
int rollDice() {
return rand() % 6 + 1;
}
int main() {
srand(time(0));
map<int, int> snakes = {{14, 7}, {31, 12}, {37, 3}, {73, 56}};
map<int, int> ladders = {{2, 23}, {8, 34}, {20, 77}, {32, 68}};
vector<int> playerPositions = {0, 0}; // 2 players
bool won = false;
int turn = 0;
while (!won) {
int player = turn % playerPositions.size();
cout << "Player " << player + 1 << "'s turn. Press Enter to roll dice.";
cin.ignore();
int dice = rollDice();
cout << "Rolled a " << dice << endl;
playerPositions[player] += dice;
if (snakes[playerPositions[player]])
playerPositions[player] = snakes[playerPositions[player]];
if (ladders[playerPositions[player]])
playerPositions[player] = ladders[playerPositions[player]];
cout << "Player " << player + 1 << " is now at position " << playerPositions[player] << endl;
if (playerPositions[player] >= 100) {
cout << "Player " << player + 1 << " wins!" << endl;
won = true;
}
turn++;
}
return 0;
}