-
Notifications
You must be signed in to change notification settings - Fork 0
/
juego_online.cpp
316 lines (270 loc) · 8.32 KB
/
juego_online.cpp
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ncurses.h>
#include <string.h>
#include <stdio_ext.h>
//Libreriras para la conexion online
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include "comunicacion.h"
#define N 30 //cantidad de celdas del mapa
#define EXIT 27
#define DERECHA 100 //la d
#define IZQUIERDA 97 //la a
#define ARRIBA 119 //la w
#define ABAJO 115 //la s
#define DISPARAR1 48 //numero 0
#define DISPARAR2 32 //barra espaciadora
#define MAXAMO 2 //municion maxima que se puede disparar
#define J1 0 //se refiere al jugador 1
#define J2 1
#define NJUGADORES 2
#define COORDENADAS 2
#define FILA 0
#define COLUMNA 1
#define CARACTERJ1 '0'
#define CARACTERJ2 '5'
#define CMUNICION '*'
#define ARMA '='
enum Direccion{norte = 1, sur, este, oeste};
enum Colores{VV = 1, AV, MV, NV, BV, RR};
struct TArma {
Direccion direccion;
int col, fila;
bool se_mueve;
};
void error(const char *mensaje){
endwin(); //Quitar ncurses
perror(mensaje); //Imprimir error
exit(EXIT_FAILURE);
}
void comprobacion(){
int respuesta;
if( LINES < N || COLS < N ){
while( (LINES < N || COLS < N) && respuesta != EXIT){
erase();
mvprintw( LINES/2, COLS/2 - 15, "LINES %d >= %d -- COLS %d >= %d",
LINES, N, COLS, N );
respuesta = getch();
refresh();
}
clear();
}
if (respuesta == EXIT){
endwin();
exit(EXIT_FAILURE);
}
}
void conexion(int *socket_fd, const char* direccion, int puerto){
struct sockaddr_in servidor;
struct hostent *he;
if( (*socket_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
error("Socket");
servidor.sin_family = AF_INET;
servidor.sin_port = htons(puerto);
if( (he = gethostbyname(direccion)) == NULL )
error("DNS");
servidor.sin_addr = *((struct in_addr *) he->h_addr);
bzero(&servidor.sin_zero, sizeof(servidor.sin_zero));
if( connect(*socket_fd, (struct sockaddr *) &servidor, sizeof(struct sockaddr)) == -1)
error("Conexion");
}
void esperar_jugadores(int socket_fd){
char buffer[1000];
recibir(socket_fd, buffer);
if(!strcmp(buffer, "wait")){
printw("Esperando mas usuarios...\n");
refresh();
}
while(!strcmp(buffer, "wait"))
recibir(socket_fd, buffer);
}
void enviar_datos(int fd, int respuesta){
char buffer[100];
sprintf(buffer, "%d", respuesta);
enviar(fd, buffer);
}
void recibir_datos(int fd, int pos_jugadores[][COORDENADAS], Direccion apunta[], bool jugador_caido[], TArma bala[][MAXAMO], int puntuacion[NJUGADORES]){
char buffer[5];
//Actualiza a los jugadores
for(int jugador=0; jugador<NJUGADORES; jugador++){
for(int pos=0; pos<COORDENADAS; pos++){
recibir(fd, buffer);
pos_jugadores[jugador][pos] = atoi(buffer);
}
recibir(fd, buffer);
apunta[jugador] = (Direccion) atoi(buffer);
recibir(fd, buffer);
jugador_caido[jugador] = (bool) atoi(buffer);
recibir(fd, buffer);
puntuacion[jugador] = atoi(buffer);
}
//Actualiza las balas
for(int jugador=0; jugador<NJUGADORES; jugador++)
for(int n_bala=0; n_bala<MAXAMO; n_bala++){
recibir(fd, buffer);
bala[jugador][n_bala].se_mueve = atoi(buffer);
if(bala[jugador][n_bala].se_mueve){
recibir(fd, buffer);
bala[jugador][n_bala].col = atoi(buffer);
recibir(fd, buffer);
bala[jugador][n_bala].fila = atoi(buffer);
}
}
}
void vaciar_tablero(char tablero[N][N], int pos_jugadores[NJUGADORES][COORDENADAS],
Direccion apunta[NJUGADORES], TArma bala[NJUGADORES][MAXAMO]){
//Limpia a los jugadores
for(int jugador=0; jugador<NJUGADORES; jugador++)
tablero[pos_jugadores[jugador][0]][pos_jugadores[jugador][1]] = ' ';
//Limpia el TArma
for(int pintar_arma=0; pintar_arma<2; pintar_arma++)
switch(apunta[pintar_arma]){
case este:
tablero[pos_jugadores[pintar_arma][0]][pos_jugadores[pintar_arma][1]+1] = ' ';
break;
case oeste:
tablero[pos_jugadores[pintar_arma][0]][pos_jugadores[pintar_arma][1]-1] = ' ';
break;
case norte:
tablero[pos_jugadores[pintar_arma][0]-1][pos_jugadores[pintar_arma][1]] = ' ';
break;
case sur:
tablero[pos_jugadores[pintar_arma][0]+1][pos_jugadores[pintar_arma][1]] = ' ';
break;
}
//Limpia las balas
for(int jugador=0; jugador<NJUGADORES; jugador++)
for(int n_bala=0; n_bala<MAXAMO; n_bala++)
if (bala[jugador][n_bala].se_mueve)
tablero[bala[jugador][n_bala].fila][bala[jugador][n_bala].col] = ' ';
}
void rellenar_tablero(char tablero[N][N], int pos_jugadores[NJUGADORES][COORDENADAS],
Direccion apunta[NJUGADORES], TArma bala[NJUGADORES][MAXAMO]){
//Posiciona a los jugadores
tablero[pos_jugadores[1][0]][pos_jugadores[1][1]] = '5';
tablero[pos_jugadores[0][0]][pos_jugadores[0][1]] = '0';
//Posiciona el TArma
for(int pintar_arma=0; pintar_arma<2; pintar_arma++)
switch(apunta[pintar_arma]){
case este:
tablero[pos_jugadores[pintar_arma][0]][pos_jugadores[pintar_arma][1]+1] = '=';
break;
case oeste:
tablero[pos_jugadores[pintar_arma][0]][pos_jugadores[pintar_arma][1]-1] = '=';
break;
case norte:
tablero[pos_jugadores[pintar_arma][0]-1][pos_jugadores[pintar_arma][1]] = '=';
break;
case sur:
tablero[pos_jugadores[pintar_arma][0]+1][pos_jugadores[pintar_arma][1]] = '=';
break;
}
//Posiciona las balas
for(int jugador=0; jugador<NJUGADORES; jugador++)
for(int n_bala=0; n_bala<MAXAMO; n_bala++)
if (bala[jugador][n_bala].se_mueve)
tablero[bala[jugador][n_bala].fila][bala[jugador][n_bala].col] = '*';
}
void pintar_tablero(char tablero[][N], int puntuacion[2]){
init_pair(VV, COLOR_GREEN, COLOR_GREEN);//asigna el color que se le dara
init_pair(AV, COLOR_BLUE, COLOR_GREEN);
init_pair(MV, COLOR_MAGENTA, COLOR_GREEN);
init_pair(NV, COLOR_BLACK, COLOR_GREEN);
init_pair(BV, COLOR_WHITE, COLOR_GREEN);
init_pair(RR, COLOR_RED, COLOR_RED);
erase();
for(int x=0; x<N; x++){
for(int y=0; y<N; y++){
switch(tablero[x][y]){
case ' ':
attron(COLOR_PAIR(VV));
printw("%c", tablero[x][y]);
attroff(COLOR_PAIR(VV));
break;
case 'H':
attron(COLOR_PAIR(RR));
printw("%c", tablero[x][y]);
attroff(COLOR_PAIR(RR));
break;
case CMUNICION:
attron(COLOR_PAIR(NV));
printw("%c", tablero[x][y]);
attroff(COLOR_PAIR(NV));
break;
case CARACTERJ1:
attron(COLOR_PAIR(AV));
printw("%c", tablero[x][y]);
attroff(COLOR_PAIR(AV));
break;
case CARACTERJ2:
attron(COLOR_PAIR(MV));
printw("%c", tablero[x][y]);
attroff(COLOR_PAIR(MV));
break;
case ARMA:
attron(COLOR_PAIR(BV));
printw("%c", tablero[x][y]);
attroff(COLOR_PAIR(BV));
break;
}
}
printw("\n");
}
printw("J1-%i J2-%i\n", puntuacion[J1], puntuacion[J2]);
refresh();
}
void bucle_juego(int socket_fd){
int respuesta,
balas_disparadas[NJUGADORES],//cantidad de balas que se han disparado
puntuacion[NJUGADORES];
int pos_jugadores[NJUGADORES][COORDENADAS];
char tablero[N][N];
Direccion apunta[NJUGADORES];
TArma bala[NJUGADORES][MAXAMO];//balas de cada jugador
bool jugador_caido[NJUGADORES];
do{
for(int x=0; x<N; x++)
for(int y=0; y<N; y++)
if(x == 0 || y == 0 || x == N-1 || y == N-1)
tablero[x][y] = 'H';
else
tablero[x][y] = ' ';
recibir_datos(socket_fd, pos_jugadores, apunta, jugador_caido, bala, puntuacion);
rellenar_tablero(tablero, pos_jugadores, apunta, bala);
pintar_tablero(tablero, puntuacion);
do{
timeout( 1 );
pintar_tablero(tablero, puntuacion);
respuesta = getch();
__fpurge(stdin);
enviar_datos(socket_fd, respuesta);
vaciar_tablero(tablero, pos_jugadores, apunta, bala);
recibir_datos(socket_fd, pos_jugadores, apunta, jugador_caido, bala, puntuacion);
rellenar_tablero(tablero, pos_jugadores, apunta, bala);
}while(respuesta != EXIT && jugador_caido[J1] != true && jugador_caido[J2] != true);
}while(respuesta != EXIT);
}
int main(int argc, char *argv[]){
int socket_fd;
if(argc < 3){
fprintf(stderr, "%s <ip> <puerto>\n", argv[0]);
return EXIT_FAILURE;
}
initscr();
raw();
keypad(stdscr, TRUE);
noecho();
start_color();
comprobacion();
conexion(&socket_fd, argv[1], atoi(argv[2]));
esperar_jugadores(socket_fd);
bucle_juego(socket_fd);
close(socket_fd);
endwin();
return EXIT_SUCCESS;
}