-
Notifications
You must be signed in to change notification settings - Fork 1
/
hundir.c
586 lines (568 loc) · 16.4 KB
/
hundir.c
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
//Constante que determina el tamaño del tablero de juego(Matriz DIMxDIM)
#define DIM 11
//Vector que contiene la cantidad de barcos segun su tamaño. El tamaño vendrà dado por la posicion en el vector y la cantidad de
//barcos por el numero que se encuentra en la posicion. P. ej.: barcos[1] --> 4 barcos de 1 casilla.
#define BARCOS {0,4,3,2,1}
//Constante con la cantidad de barcos diferentes que hay mas uno ya que la posicion '0' no se utilizara.
#define DIFBARCOS 5
#define TOTALBARCOS 10
void colocarBarcos();//Declaracion funcion para colocar barcos
int espacioDispo();//Declaracion funcion para comprobar la disponibilidad de la posicion elegida para el barco.
int letraxnum(char letra);//Declaracion funcion para traducir la letra del eje "y" en un numero para movernos por la matriz.
void agua();//Declaracion funcion para dejar libres las casillas alrededor del barco.
int posicionEnTablero();//Declaracion funcion para comprobar que la posicion elegida para el barco esta dentro del tablero.
void crearTablero();//Declaracion funcion para la creacion de los tableros de juego en blanco.
void mostrar();//Declaracion funcion para mostrar tablero por pantalla.
void rellenarAgua();//Declaracion funcion para rellenar resto del tablero con agua
int comprobarBarco();//Declaracion funcion para comprobar disponibilidad del barco.
int tocadoHundido();//Declaracion funcion para comprobar si el barco ha sido hundido o unicamente tocado.
int disparo();//Definicion funcion que comprueba resultado del disparo.
void main()
{
char tablero1A[DIM][DIM], tablero2A[DIM][DIM],tablero1B[DIM][DIM],tablero2B[DIM][DIM];
int posi1, posi2, posj1, posj2, posOk, barcOk;
int x, aux, totbarcosj1, totbarcosj2;
char letra;
int barcosJugador1[DIFBARCOS]=BARCOS, barcosJugador2[DIFBARCOS]=BARCOS;
totbarcosj1=0;
totbarcosj2=0;
crearTablero(tablero1A);
crearTablero(tablero2A);
crearTablero(tablero1B);
crearTablero(tablero2B);
printf("\t\t\t\t\tBIENVENIDO AL JUEGO HUNDIR LA FLOTA.\n");
printf("\t\t\t\t\t\tEtapa colocacion.\n");
printf("\t\t\t\t\t\tInstrucciones:\n");
printf("- Cada jugador colocara sus barcos por turno. Primero el jugador 1 y despues el jugador 2.\n");
printf("- Los barcos a colocar seran los siguientes:\n");
for (x = 1; x < DIFBARCOS; x++){
printf("\tTamano: %d casillas Cantidad: %d barcos\n", x, barcosJugador1[x]);
}
system("pause");
printf("Inicio primer jugador.\n");
while(totbarcosj1!=TOTALBARCOS)
{
do
{
printf("Quieres ver la disposicion del tablero?(s/n)\n");
scanf("%c", &letra);
fflush(stdin);
if(letra=='s'||letra=='S')
{
mostrar(tablero1A);
}
printf("Introduce posicion donde empieza el barco.\n");
scanf("%c%d", &letra, &posj1);
posi1=letraxnum(letra);
fflush(stdin);
printf("Introduce posicion de fin.\n");
scanf("%c%d", &letra, &posj2);
posi2=letraxnum(letra);
fflush(stdin);
if(posi1>posi2)
{
aux=posi1;
posi1=posi2;
posi2=aux;
}
if(posj1>posj2)
{
aux=posj1;
posj1=posj2;
posj2=aux;
}
posOk=posicionEnTablero(posi1, posi2, posj1, posj2);
if(posOk==-1)
{
printf("El barco no puede estar fuera del tablero.\n");
system("pause");
}
else
{
if(posOk==-2)
{
printf("No se pueden colocar barcos en diagonal.\n");
system("pause");
}
else
{
barcOk=comprobarBarco(posi1, posi2, posj1, posj2,barcosJugador1);
if (barcOk==-1)
{
printf("No existen barcos tan grandes.\n");
}
else
{
if(barcOk==-2)
{
printf("No quedan barcos de ese tamano a introducir.\n");
}
else
{
posOk=espacioDispo(posi1, posi2, posj1, posj2, tablero1A);
if(posOk==-1)
{
printf("No se puede colocar el barco en esta posicion. (Recuerda que los barcos no pueden tocarse)\n");
}
}
}
}
}
}
while(posOk!=1 || barcOk!=1);
colocarBarcos(posi1, posi2, posj1, posj2, tablero1A, barcosJugador1);
agua(posi1, posi2, posj1, posj2, tablero1A);
totbarcosj1++;
printf("Barco colocado!\n");
system("pause");
system("cls");
}
rellenarAgua(tablero1A);
printf("Inicio segundo jugador.\n");
while(totbarcosj2!=TOTALBARCOS)
{
do
{
printf("Quieres ver la disposicion del tablero?(s/n)\n");
scanf("%c", &letra);
fflush(stdin);
if(letra=='s'||letra=='S')
{
mostrar(tablero2A);
}
printf("Introduce posicion donde empieza el barco.\n");
scanf("%c%d", &letra, &posj1);
posi1=letraxnum(letra);
fflush(stdin);
printf("Introduce posicion de fin.\n");
scanf("%c%d", &letra, &posj2);
posi2=letraxnum(letra);
fflush(stdin);
if(posi1>posi2)
{
aux=posi1;
posi1=posi2;
posi2=aux;
}
if(posj1>posj2)
{
aux=posj1;
posj1=posj2;
posj2=aux;
}
posOk=posicionEnTablero(posi1, posi2, posj1, posj2);
if(posOk==-1)
{
printf("El barco no puede estar fuera del tablero.\n");
system("pause");
}
else
{
if(posOk==-2)
{
printf("No se pueden colocar barcos en diagonal.\n");
system("pause");
}
else
{
barcOk=comprobarBarco(posi1, posi2, posj1, posj2, barcosJugador2);
if (barcOk==-1)
{
printf("No existen barcos tan grandes.\n");
}
else
{
if(barcOk==-2)
{
printf("No quedan barcos de ese tamano a introducir.\n");
}
else
{
posOk=espacioDispo(posi1, posi2, posj1, posj2, tablero2A);
if(posOk==-1)
{
printf("No se puede colocar el barco en esta posicion. (Recuerda que los barcos no pueden tocarse)\n");
}
}
}
}
}
}
while(posOk!=1 || barcOk!=1);
colocarBarcos(posi1, posi2, posj1, posj2, tablero2A, barcosJugador2);
agua(posi1, posi2, posj1, posj2, tablero2A);
totbarcosj2++;
printf("Barco colocado!\n");
system("pause");
system("cls");
}
rellenarAgua(tablero2A);
system("cls");
aux=0;
printf("\t\t\t\t\t\tEtapa de juego.\n\n");
printf("\t\t\t\t\t\tInstrucciones:\n\n");
printf("- Los jugadores iran diparando por turnos sucesivamente. Primero el jugador 1 y despues el jugador 2.\n\n\n");
printf("\t\t\t\t\t\tQue gane el mejor. Suerte!\n");
system("pause");
while(totbarcosj1!=0 && totbarcosj2!=0)
{
while(aux%2==0)
{
printf("Turno del jugador 1.\n");
printf("Dispara!\n");
mostrar(tablero1B);
scanf("%c%d", &letra, &posj1);
posi1=letraxnum(letra);
fflush(stdin);
posOk=posicionEnTablero(posi1, posi1, posj1, posj1);
if(posOk==1)
{
barcOk=disparo(posi1, posj1, tablero2A, tablero1B);
if(barcOk==-1)
{
printf("Agua!\n");
}
else if(barcOk==1)
{
printf("Tocado!\n");
}
else
{
printf("Tocado y hundido!\n");
totbarcosj2--;
}
mostrar(tablero1B);
aux++;
}
else
{
printf("Introduce una posicion que se encuentre dentro del tablero.\n");
}
}
while(aux%2==1)
{
mostrar(tablero2B);
printf("Turno del jugador 2.\n");
printf("Dispara!\n");
scanf("%c%d", &letra, &posj1);
posi1=letraxnum(letra);
fflush(stdin);
posOk=posicionEnTablero(posi1, posi1, posj1, posj1);
if(posOk==1)
{
barcOk=disparo(posi1, posj1, tablero1A, tablero2B);
if(barcOk==-1)
{
printf("Agua!\n");
}
else if(barcOk==1)
{
printf("Tocado!\n");
}
else
{
printf("Tocado y hundido!\n");
totbarcosj1--;
}
mostrar(tablero2B);
aux++;
}
else
{
printf("Introduce una posicion que se encuentre dentro del tablero.\n");
}
}
}
if(totbarcosj2==0)
{
printf("\t\t\t\tEl jugador 1 ha ganado!\n");
}
else
{
printf("\t\t\t\tEl jugador 2 ha ganado! Felicidades!");
}
}
//Definicion funcion para colocar barcos en tablero
void colocarBarcos(int posi1, int posi2, int posj1, int posj2, char mat[DIM][DIM], int vbarcos[])
{
int i, j, cont;
cont=0;
if(posi1==posi2)
{
i=posi1;
for(j=posj1; j<=posj2; j++)
{
mat[i][j]='O';
cont++;
}
}
else
{
j=posj1;
for(i=posi1; i<=posi2; i++)
{
mat[i][j]='O';
cont++;
}
}
vbarcos[cont]=vbarcos[cont]-1;
}
//Definicion funcion para comprobar la disponibilidad de la posicion elegida para el barco.
int espacioDispo(int posi1, int posi2, int posj1, int posj2, char mat[DIM][DIM])
{
int i, j;
for(i=posi1; i<=posi2; i++)
{
for(j=posj1; j<=posj2; j++)
{
if(mat[i][j]!=' ')
{
return -1;
}
}
}
return 1;
}
//Definicion funcion para traducir la letra del eje "y" en un numero para movernos por la matriz.
int letraxnum(char letra)
{
switch (tolower(letra))
{
case 'a':
return 1;
case 'b':
return 2;
case 'c':
return 3;
case 'd':
return 4;
case 'e':
return 5;
case 'f':
return 6;
case 'g':
return 7;
case 'h':
return 8;
case 'i':
return 9;
case 'j':
return 10;
default:
return -1;
}
}
//Definicion funcion para dejar libres las casillas alrededor del barco. Las rellenaremos con "agua".
//Llamamos a la funcion con la posicion inicial y final del barco junto con la matriz del jugador.
void agua(int posi1, int posi2, int posj1, int posj2, char mat[DIM][DIM])
{
int i,j;
for(i=(posi1-1); i<(posi2+2); i++)
{
if(i>0 && i<DIM)
{
for(j=(posj1-1); j<(posj2+2); j++)
{
if (j>0 && j<DIM)
{
if(posi1==posi2)
{
if(i!=posi1 || j<posj1 || j>posj2)
{
mat[i][j]='~';
}
}
else
{
if(j!=posj1 || i<posi1 || i>posi2)
{
mat[i][j]='~';
}
}
}
}
}
}
}
//Definicion funcion para comprobar que la posicion elegida para el barco esta dentro del tablero.
int posicionEnTablero(int posi1, int posi2, int posj1, int posj2)
{
if(posi1==-1||posi2==-1)
{
return -1;
}
if(posi1!=posi2 && posj1!=posj2)
{
return -2;
}
if(posj1<1||posj1>10||posj2<1||posj2>10)
{
return -1;
}
return 1;
}
//Definicion funcion para la creacion de los tableros de juego en blanco.
void crearTablero(char mat[DIM][DIM])
{
int i,j;
mat[0][0]=' ';
mat[0][1]='1';
mat[0][2]='2';
mat[0][3]='3';
mat[0][4]='4';
mat[0][5]='5';
mat[0][6]='6';
mat[0][7]='7';
mat[0][8]='8';
mat[0][9]='9';
mat[0][10]='0';
mat[1][0]='A';
mat[2][0]='B';
mat[3][0]='C';
mat[4][0]='D';
mat[5][0]='E';
mat[6][0]='F';
mat[7][0]='G';
mat[8][0]='H';
mat[9][0]='I';
mat[10][0]='J';
for (i=1; i<DIM; i++)
{
for (j=1; j<DIM; j++)
{
mat[i][j]=' ';
}
}
}
//Definicion funcion para mostrar tablero por pantalla.
void mostrar(char mat[DIM][DIM])
{
int i, j;
for (i=0; i<DIM; i++)
{
printf("\t\t\t\t");
for (j=0; j<DIM; j++)
{
printf("%c ", mat[i][j]);
}
printf("\n");
}
}
//Definicion funcion para rellenar resto del tablero con agua. Se llama a la funcion una vez colocados todos los barcos.
void rellenarAgua(char mat[DIM][DIM])
{
int i, j;
for(i=1; i<DIM; i++)
{
for(j=1; j<DIM; j++)
{
if(mat[i][j]==' ')
{
mat[i][j]='~';
}
}
}
}
//Definicion funcion para comprobar disponibilidad del barco.
int comprobarBarco(int posi1, int posi2, int posj1, int posj2, int barcos[])
{
int i, j, tamano;
if (posi1 == posi2)
{
tamano = posj2 - posj1 + 1;
}
else
{
tamano = posi2 - posi1 + 1;
}
if (tamano > 5)
{
return (-1);
}
else
{
if (barcos[tamano] != 0)
{
return (1);
}
else
{
return (-2);
}
}
}
//Definicion funcion para comprobar si el barco ha sido hundido o unicamente tocado.
int tocadoHundido(int posi, int posj, char mat[DIM][DIM])
{
int i, j;
i=posi;
j=posj;
while(i>0&&mat[i][j]=='X')
{
i--;
}
if(i>0)
{
if(mat[i][j]=='O')
{
return 1;
}
}
i=posi;
while(i<(DIM-1)&&mat[i][j]=='X')
{
i++;
}
if(i<DIM)
{
if(mat[i][j]=='O')
{
return 1;
}
}
i=posi;
while(j>0&&mat[i][j]=='X')
{
j--;
}
if(j>0)
{
if(mat[i][j]=='O')
{
return 1;
}
}
j=posj;
while(j<(DIM-1)&&mat[i][j]=='X')
{
i++;
}
if(i<DIM)
{
if(mat[i][j]=='O')
{
return 1;
}
}
return 2;
}
//Definicion funcion que comprueba resultado del disparo.
int disparo(int posi, int posj, char mat1[DIM][DIM], char mat2[DIM][DIM])
{
int x;
if(mat1[posi][posj]=='O')
{
mat1[posi][posj]='X';
mat2[posi][posj]='X';
x=tocadoHundido(posi, posj, mat1);
}
else
{
mat2[posi][posj]='~';
x=-1;
}
return x;
}