-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiplay.c
More file actions
291 lines (276 loc) · 10 KB
/
multiplay.c
File metadata and controls
291 lines (276 loc) · 10 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <mf.h>
#include <time.h>
#define endl getCursorXY(&x,&y);gotoxy(x-3,y+1);
#define color(f_c,b_c) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),f_c | b_c<<4);
#define ps printf(" ");
#define def_color SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),0x07);
extern const int source,split,wall,normal; // source: Blue split:Yellow wall: Gray normal:Ajori
extern int x,y,cell_num,walls;
extern const int width,height;
typedef struct{
int energy;
int type;
}block;
typedef struct{
int w,h;
}position;
typedef struct{
position p;
int energy;
}cell_inf;
#include "multiplayer.h"
void print_cell(position p){
if(p.w%2 == 0)
x=p.w * 7 + 3,y=p.h *4 + 2;
else
x=p.w * 7 + 3,y=p.h *4 + 4;
gotoxy(x,y);
printf("%c%c",198,181);
}
void print_opp_cell(position p){
if(p.w%2 == 0)
x=p.w * 7 + 3,y=p.h *4 + 2;
else
x=p.w * 7 + 3,y=p.h *4 + 4;
gotoxy(x,y);
printf("%c%c",181,198);
}
int multi_check_block_empty(int h,int w,cell_inf cell){
if(cell.p.h == h && cell.p.w == w)
return 1;
return 0;
}
void multi_move(block arr_map[height][width],position p,int dir,cell_inf *cell,cell_inf opp){
int q=dir;
remove_cell(p,arr_map);Sleep(500);
if(p.w % 2 == 1)
switch(q){
case 1:
if((cell->p).h-1 >= 0 && arr_map[(cell->p).h-1][(cell->p).w].type!=wall && check_block_empty((cell->p).h-1,(cell->p).w,opp)==0)
(cell->p).h --;
break;
case 2:
if((cell->p).h+1 <height && arr_map[(cell->p).h+1][(cell->p).w].type!=wall && multi_check_block_empty((cell->p).h+1,(cell->p).w,opp)==0)
(cell->p).h ++;
break;
case 3:
if((cell->p).w+1 <width && arr_map[(cell->p).h][(cell->p).w+1].type!=wall && multi_check_block_empty((cell->p).h,(cell->p).w+1,opp)==0)
(cell->p).w ++;
break;
case 4:
if((cell->p).w-1 >=0 && arr_map[(cell->p).h][(cell->p).w-1].type!=wall && multi_check_block_empty((cell->p).h,(cell->p).w-1,opp)==0)
(cell->p).w --;
break;
case 5:
if((cell->p).h+1 <height && (cell->p).w+1 <width && arr_map[(cell->p).h+1][(cell->p).w+1].type!=wall && multi_check_block_empty((cell->p).h+1,(cell->p).w+1,opp)==0)
(cell->p).h ++,(cell->p).w ++;
break;
case 6:
if((cell->p).h+1 <height && (cell->p).w-1 >=0 && arr_map[(cell->p).h+1][(cell->p).w-1].type!=wall && multi_check_block_empty((cell->p).h+1,(cell->p).w-1,opp)==0)
(cell->p).h ++,(cell->p).w --;
break;
}
else
switch(q){
case 1:
if((cell->p).h-1 >= 0 && arr_map[(cell->p).h-1][(cell->p).w].type!=wall && multi_check_block_empty((cell->p).h-1,(cell->p).w,opp)==0)
(cell->p).h --;
break;
case 2:
if((cell->p).h+1 <height && arr_map[(cell->p).h+1][(cell->p).w].type!=wall && multi_check_block_empty((cell->p).h+1,(cell->p).w,opp)==0)
(cell->p).h ++;
break;
case 3:
if((cell->p).h-1 >=0 && (cell->p).w+1 <width && arr_map[(cell->p).h-1][(cell->p).w+1].type!=wall && multi_check_block_empty((cell->p).h-1,(cell->p).w+1,opp)==0)
(cell->p).h --,(cell->p).w ++;
break;
case 4:
if((cell->p).h-1 >=0 && (cell->p).w-1 >=0 && arr_map[(cell->p).h-1][(cell->p).w-1].type!=wall && multi_check_block_empty((cell->p).h-1,(cell->p).w-1,opp)==0)
(cell->p).h --,(cell->p).w --;
break;
case 5:
if((cell->p).w+1 <width && arr_map[(cell->p).h][(cell->p).w+1].type!=wall && multi_check_block_empty((cell->p).h,(cell->p).w+1,opp)==0)
(cell->p).w ++;
break;
case 6:
if((cell->p).w-1 >=0 && arr_map[(cell->p).h][(cell->p).w-1].type!=wall && multi_check_block_empty((cell->p).h,(cell->p).w-1,opp)==0)
(cell->p).w --;
break;
}
}
void multi_boost_energy(block arr_map[height][width],cell_inf *cell,position p){
if(arr_map[p.h][p.w].energy==0)
return;
if(arr_map[p.h][p.w].energy>=15){
if(cell->energy+15>100){
arr_map[p.h][p.w].energy-=100 - cell->energy;
cell->energy=100;
}
else{
arr_map[p.h][p.w].energy-=15;
cell->energy+=15;
}
}
else{
if(cell->energy+arr_map[p.h][p.w].energy>100){
arr_map[p.h][p.w].energy -= 100- cell->energy;
cell->energy=100;
}
else{
cell->energy+=arr_map[p.h][p.w].energy;
arr_map[p.h][p.w].energy=0;
}
}
print_cell(p);
show_block_energy(arr_map);
}
void multiplay(){
/*---------Map load------*/
FILE *f;
char *zxc;
zxc=show_text_box("Enter Map file name(just 'bin' files):");
f=fopen(zxc,"rb");
if(f==NULL){
show_box_end("Cant open file");Sleep(1500);
return;
}
fread(&width,sizeof(int),1,f);
fread(&height,sizeof(int),1,f);
block arr_map[height][width];
map(arr_map,f);
fclose(f);
/*--------------------*/
/*create random position for my cell*/
struct cell_position my_pos,opp_pos;
while(1){
position tu;
tu=random_pos();
if(arr_map[tu.h][tu.w].type!=wall){
my_pos.y=tu.h;
my_pos.x=tu.w;
break;
}
}
/*-----------------------*/
opp_pos=cell_new(my_pos);
cell_inf my,opp;
my.p.w=my_pos.x;
my.p.h=my_pos.y;
my.energy=0;
opp.p.w=opp_pos.x;
opp.p.h=opp_pos.y;
opp.energy=0;
print_cell(my.p);
print_opp_cell(opp.p);
position p;int d,q,dir;
system("cls");
print_map(arr_map);
while(1){
turn: clear_text();
print(3,0,"[1] Select a cell\n[2] Save\n[3] Exit\n");
int s;
char qwerty[50];scanf("%s",qwerty);s=atoi(qwerty);
switch(s){
case 1:
clear_text();
printf("Select one Cell with Coordinate: ");
char qwerty[50];scanf("%s",qwerty);p.w=atoi(qwerty);
scanf("%s",qwerty);p.h=atoi(qwerty);
if(p.h != my_pos.y || p.w != my_pos.x)
goto turn;
p.h=height-p.h-1;
select_cell(p);
clear_text();
/*-----show energy----*/
gotoxy(0,4 * height +2);
print(12,11,"Cell energy is:%d\n",my.energy);
/*-------------------*/
switch(arr_map[p.h][p.w].type){
case 1:
print(3,0,"[1] Move\n");
print(3,0,"[2] Boost energy\n");
char qwerty[50];scanf("%s",qwerty);q=atoi(qwerty);
switch(q){
case 1:
clear_text();
show_move_hint();
char qwerty[50];scanf("%s",qwerty);dir=atoi(qwerty);
multi_move(arr_map,my.p,dir,&my,opp);
print_cell(my.p);
break;
case 2:
multi_boost_energy(arr_map,&my,my.p);
/*-----show energy----*/
gotoxy(0,4 * height +2);
print(12,11,"Cell energy is:%d\n",my.energy);
Sleep(500);
/*-------------------*/
remove_show_energy();
break;
default:
print_cell(p);
}
break;
case 6:
print(3,0,"[1] Move\n");
print(8,0,"[2] Boost energy\n");
scanf("%s",qwerty);q=atoi(qwerty);
switch(q){
case 1:
clear_text();
show_move_hint();
char qwerty[50];scanf("%s",qwerty);dir=atoi(qwerty);
multi_move(arr_map,my.p,dir,&my,opp);
print_cell(my.p);
break;
default:
print_cell(p);
}
break;
}
break;
case 2:
clear_text();
cell_inf cell_arr[2];
cell_arr[0]=my;
cell_arr[1]=opp;
cell_num=2;
print(3,0,"[1] Text\n[2] Binary\n[3] Both above\n");
scanf("%s",qwerty);q=atoi(qwerty);
switch(q){
case 1:
zxc=show_text_box("Enter file name(with format):");
save_text(arr_map,cell_arr,zxc);
break;
case 2:
zxc=show_text_box("Enter file name(with format):"); save_bin(arr_map,cell_arr,zxc);
break;
case 3:
zxc=show_text_box("Enter file name(binary file):"); save_bin(arr_map,cell_arr,zxc);
zxc=show_text_box("Enter file name(text file):");
save_text(arr_map,cell_arr,zxc);
}
system("cls");
print_map(arr_map);
print_cells(cell_arr);
goto turn;
break;
case 3:
system("cls");
return;
break;
}
struct cell_move_data data;
data.cell_energy=my.energy;
data.move_direction=dir;
data=cell_move(data);
opp.energy=data.cell_energy;
multi_move(arr_map,opp.p,data.move_direction,&opp,my);
print_opp_cell(opp.p);
}
}