forked from workattech/machine-coding-feedback
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake_ladder.cpp
More file actions
113 lines (104 loc) · 2.2 KB
/
snake_ladder.cpp
File metadata and controls
113 lines (104 loc) · 2.2 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
#include <iostream>
#include <unordered_map>
using namespace std;
class snake
{
public:
unordered_map <int, int > snake_table;
void fill_table(int key, int data)
{
snake_table[key] = data;
}
};
class ladder
{
public:
unordered_map<int, int> ladder_table;
void fill_table(int key, int data)
{
ladder_table[key] = data;
}
};
class player
{
public:
int cur_pos;
string name;
static int won;
player(string s)
{
name = s;
cur_pos = 0;
}
void move(unordered_map <int, int > snake_table, unordered_map<int, int> ladder_table)
{
int dice_value = (rand() % 6) + 1;
int next = cur_pos + dice_value;
if (next > 100)
next = cur_pos;
unordered_map<int, int>::iterator sl = snake_table.find(next);
unordered_map<int, int>::iterator ld = ladder_table.find(next);
while (sl != snake_table.end() || ld != ladder_table.end())
{
if (sl != snake_table.end())
{
next = sl->second;
}
if (ld != ladder_table.end())
{
next = ld->second;
}
sl = snake_table.find(next);
ld = ladder_table.find(next);
}
if (next == 100)
{
won = 1;
}
cout << name << " rolled a "<< dice_value << " and moved from " << cur_pos << " to " << next << endl;
cur_pos = next;
}
};
int player::won = 0;
int main() {
snake s1;
ladder l1;
int snake_no = 0;
srand(time(0));
cin >> snake_no;
for (int i = 0;i<snake_no;i++)
{
int key = 0, data = 0;
cin >> key >> data;
s1.fill_table(key, data);
}
int ladder_no = 0;
cin >> ladder_no;
for (int i = 0; i<ladder_no;i++)
{
int key = 0, data = 0;
cin >> key >> data;
l1.fill_table(key, data);
}
int player_no = 0;
cin >> player_no;
player *p_list[player_no];
for (int i = 0; i<player_no; i++)
{
string player_name;
cin >> player_name;
p_list[i] = new player(player_name);
}
int i = 0;
while(1)
{
p_list[i]->move(s1.snake_table, l1.ladder_table);
if (player::won == 1)
{
cout << p_list[i]->name << " wins the game" << endl;
break;
}
i = (i+1) % player_no;
}
return 0;
}