-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserverh.cpp
More file actions
160 lines (150 loc) · 4.57 KB
/
serverh.cpp
File metadata and controls
160 lines (150 loc) · 4.57 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
#include "server.h"
string Game::addPlayer(string playerNo){
string msg = "";
int name = stoi(playerNo)-stoi("0");
if(name == 1){
Player p_obj1(playerNo, 0, 0);
map[0][0] = 1;
p.push_back(p_obj1);
}else if (name == 2){
Player p_obj2(playerNo, 0, 10);
map[0][10] = 2;
p.push_back(p_obj2);
}else if (name == 3){
Player p_obj3(playerNo, 10, 10);
map[10][10] = 3;
p.push_back(p_obj3);
}else if (name == 4){
Player p_obj4(playerNo, 10, 0);
map[10][0] = 4;
p.push_back(p_obj4);
}
for (int i = 0; i < 11; ++i)
{
for (int j = 0; j < 11; ++j)
{
msg += to_string(map[i][j]);
}
}
return msg+"%";
}
string Game::playerMovementHandler(int pn, char move){
string msg = "";
pn -= 1;
if(move == 'w'){
if(p[pn].p_x > 0){
if (map[p[pn].p_x-1][p[pn].p_y] == 0){
map[p[pn].p_x][p[pn].p_y] = 0;
map[p[pn].p_x-1][p[pn].p_y] = pn+1;
p[pn].p_x -= 1;
}else
msg = "Invalid move. Try another one.";
}else
msg = "Invalid move. Try another one.";
}else if(move == 'a'){
if(p[pn].p_y > 0){
if (map[p[pn].p_x][p[pn].p_y-1] == 0){
map[p[pn].p_x][p[pn].p_y] = 0;
map[p[pn].p_x][p[pn].p_y-1] = pn+1;
p[pn].p_y -= 1;
}else
msg = "Invalid move. Try another one.";
}else
msg = "Invalid move. Try another one.";
}else if(move == 's'){
if(p[pn].p_x < 10){
if (map[p[pn].p_x+1][p[pn].p_y] == 0){
map[p[pn].p_x][p[pn].p_y] = 0;
map[p[pn].p_x+1][p[pn].p_y] = pn+1;
p[pn].p_x += 1;
}else
msg = "Invalid move. Try another one.";
}else
msg = "Invalid move. Try another one.";
}else if(move == 'd'){
if(p[pn].p_y < 10){
if(map[p[pn].p_x][p[pn].p_y+1] == 0){
map[p[pn].p_x][p[pn].p_y] = 0;
map[p[pn].p_x][p[pn].p_y+1] = pn+1;
p[pn].p_y += 1;
}else
msg = "Invalid move. Try another one.";
}else
msg = "Invalid move. Try another one.";
}
if(msg != "Invalid move. Try another one."){
for (int i = 0; i < 11; ++i)
{
for (int j = 0; j < 11; ++j)
msg += to_string(map[i][j]);
}
}
if(p[pn].p_y == 5 && p[pn].p_x == 5)
winner_flag = pn+1;
return msg+"%";
}
string Game::renderMap(){
string msg = "";
for (int i = 0; i < 11; ++i)
{
for (int j = 0; j < 11; ++j)
{
msg += to_string(map[i][j]);
}
}
return msg+"%";
}
string Game::playerWallPlacementHandler(int x, int y, int v){
string msg = "";
if (v == 1){
if((x != 0) && (x != 10)){
if( (map[x][y] == 0) && (map[x-1][y] == 0) && (map[x+1][y] == 0)){
if(y == 5){
if(x != 5 && x != 4 && x != 6){
map[x][y] = 8;
map[x+1][y] = 8;
map[x-1][y] = 8;
}
else{
msg = "can't place a wall there!";
}
}else{
map[x][y] = 8;
map[x+1][y] = 8;
map[x-1][y] = 8;
}
}else
msg = "can't place a wall there!";
}else
msg = "can't place a wall there!";
}else {
if((y != 0) && (y != 10)){
if( (map[x][y] == 0) && (map[x][y-1] == 0) && (map[x][y+1] == 0)){
if(x == 5){
if(y != 5 && y != 4 && y != 6){
map[x][y] = 8;
map[x][y+1] = 8;
map[x][y-1] = 8;
}else
msg = "can't place a wall there!";
}else{
map[x][y] = 8;
map[x][y+1] = 8;
map[x][y-1] = 8;
}
}else
msg = "can't place a wall there!";
}else
msg = "can't place a wall there!";
}
if(msg != "can't place a wall there!"){
for (int i = 0; i < 11; ++i)
{
for (int j = 0; j < 11; ++j)
{
msg += to_string(map[i][j]);
}
}
}
return msg+"%";
}