-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.c
152 lines (140 loc) · 3.73 KB
/
functions.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "functions.h"
#define RED "\x1b[31m"
#define GREEN "\x1b[32m"
#define YELLOW "\x1b[33m"
#define BLUE "\x1b[34m"
#define MAGENTA "\x1b[35m"
#define CYAN "\x1b[36m"
#define RESET "\x1b[0m"
#define FILAS "ABCDE"
void tablero(){
/* Declaraciones*/
int c,f;
char filas[] = FILAS;
mkdir("J1",0777);
mkdir("J2",0777);
/*Creacion de directorios */
for (f = 0; f < 5; ++f){ //Filas
for (c= 1; c < 6; ++c){ //Columnas
char j1[25], j2[25];
snprintf(j1, sizeof(j1), "./J1/%c_%d", filas[f],c);
snprintf(j2, sizeof(j2), "./J2/%c_%d", filas[f],c);
mkdir(j1, 0777);
mkdir(j2, 0777);
}
}
}
int letra(char a){
if(a=='A'){
return 0;
}
else if (a=='B'){
return 1;
}
else if (a=='C'){
return 2;
}
else if (a=='D'){
return 3;
}
else if (a=='E'){
return 4;
}
}
int casteo(char c){
int i= (int)c - '0';
return i-1;
}
void instrucciones(int j){
printf(RED "Bienvenido Capitan %d\n", j);
printf(YELLOW "Las coordenadas del juego se componen de una Columna (letra mayuscula) seguida de una Fila (numero), ej B1\n");
printf("Filas: 1 - 2 - 3 - 4 - 5\n");
printf("Columnas: A - B - C - D - E\n" RESET);
}
int comprobar(char coord1, char coord2){
char columnas[] = FILAS;
int i;
int l=0;
int n=0;
for (i = 0; i < 5; ++i){
if (coord1 == columnas[i]){
l=1;
}
if (casteo(coord2)==i){
n=1;
}
}
if (n==0 || l==0){
return 0;
}
else{
return 1;
}
}
int existe(int j, char coord1, char coord2){
struct stat st = {0};
if (j == 1){
char ruta1[19];
snprintf(ruta1, sizeof(ruta1), "./J1/%c_%c/barco.txt", coord1, coord2);
if (stat(ruta1, &st) == -1){
return 0;
}
else{
return 1;
}
}
else if (j == 2){
char ruta2[19];
snprintf(ruta2, sizeof(ruta2), "./J2/%c_%c/barco.txt", coord1, coord2);
if (stat(ruta2, &st) == -1){
return 0;
}
else{
return 1;
}
}
}
void crearbarco(int j, char coord1, char coord2){
if (j == 1){
char ruta1[19];
snprintf(ruta1, sizeof(ruta1), "./J1/%c_%c/barco.txt", coord1, coord2);
FILE *fp;
fp=fopen(ruta1,"w");
fclose(fp);
}
else if (j == 2){
char ruta2[19];
snprintf(ruta2, sizeof(ruta2), "./J2/%c_%c/barco.txt", coord1, coord2);
FILE *fp;
fp=fopen(ruta2,"w");
fclose(fp);
}
}
void panel(int w, int arr[5][5]){
int i;
if (w==1){
printf(YELLOW "REPRESENTACIÓN DE VALORES\n0:Casilla no atacada\n1:Casilla atacada sin éxito\n3:Casilla atacada con éxito\n");
printf(GREEN "| Panel de Combate Jugador 1 |\n");
printf("| N° | A | B | C | D | E |\n");
for (i = 0; i < 5; ++i){
printf("| %d | %d | %d | %d | %d | %d |\n", i+1, arr[0][i], arr[1][i], arr[2][i], arr[3][i], arr[4][i]);
}
printf("\n" RESET);
}
else if (w==2){
printf(YELLOW "REPRESENTACIÓN DE VALORES\n0:Casilla no atacada\n1:Casilla atacada sin éxito\n3:Casilla atacada con éxito\n");
printf(CYAN "| Panel de Combate Jugador 2 |\n");
printf("| N° | A | B | C | D | E |\n");
for (i = 0; i < 5; ++i){
printf("| %d | %d | %d | %d | %d | %d |\n", i+1, arr[0][i], arr[1][i], arr[2][i], arr[3][i], arr[4][i]);
}
printf("\n" RESET);
}
}