-
Notifications
You must be signed in to change notification settings - Fork 0
/
PruebaImprimirTablero.c
138 lines (132 loc) · 4.61 KB
/
PruebaImprimirTablero.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
void inicializarTablero(char tablero[6][7]){
for(int i=0; i<6; i++){
for(int j=0; j<7; j++){
tablero[i][j]='-';
}
}
}
/*void decodificarTablero(char tablero[6][7], char *buffer){
char c;
for(int i = 0; i < 6; i++ ){
for(int j = 0; j < 7; j++ ){
c = tablero[i][j];
if (j != 0){
strcat(buffer,",");
}
strcat(buffer,&c);
}
strcat(buffer,";");
strcat(buffer," ");
}
}*/
void decodificarTablero(char tablero[6][7], char *aux){
char c[2];
c[1]='\0';
for(int i = 0; i < 6; i++ ){
for(int j = 0; j < 7; j++ ){
c[0] =tablero[i][j];
if (j != 0){
strcat(aux,",");
}
strcat(aux, c);
}
strcat(aux,";");
}
}
void imprimir(char c[250]){
if(((strncmp(c, "+Ok. Empieza la partida.",24))==0)|| (((strncmp(c, "+Ok. Nuevo tablero.",19)))==0)){
char *token;
token=strtok(c, ".");
printf("%s",token);
token=strtok(NULL, ".");
printf("%s\n",token);
printf("|1|2|3|4|5|6|7|\n|");
token=strtok(NULL, "\n");
for(int l=0; l<(strlen(token)-1); l++){
if(token[l]=='-'){
token[l]=' ';
printf("%c", token[l]);
}
else if(token[l]==','){
token[l]='|';
printf("%c", token[l]);
}
else if(token[l]=='x'){
token[l]='x';
printf("%c", token[l]);
}
else if(token[l]=='o'){
token[l]='o';
printf("%c", token[l]);
}
else if(token[l]==';'){
token[l]='|';
printf("%c\n|", token[l]);
}
}
printf("|\n");
}else{
printf("%s\n",c);
}
}
int comprobarGanador(char tablero[6][7]){
int i,j;
//Comprobamos si hay 4 fichas iguales en horizontal
for(i=0;i<6;i++){
for(j=0;j<7;j++){
if(tablero[i][j]=='o' && tablero[i][j]==tablero[i][j+1] && tablero[i][j]==tablero[i][j+2] && tablero[i][j]==tablero[i][j+3]
|| tablero[i][j]=='x' && tablero[i][j]==tablero[i][j+1] && tablero[i][j]==tablero[i][j+2] && tablero[i][j]==tablero[i][j+3]){
return 1;
}
}
}
//Comprobamos si hay 4 fichas iguales en vertical
for(i=0;i<6;i++){
for(j=0;j<7;j++){
if(tablero[i][j]=='o' && tablero[i][j]==tablero[i+1][j] && tablero[i][j]==tablero[i+2][j] && tablero[i][j]==tablero[i+3][j]
|| tablero[i][j]=='x' && tablero[i][j]==tablero[i+1][j] && tablero[i][j]==tablero[i+2][j] && tablero[i][j]==tablero[i+3][j]){
return 1;
}
}
}
//Comprobamos si hay 4 fichas iguales en diagonal
for(i=0;i<6;i++){
for(j=0;j<7;j++){
if(tablero[i][j]=='o' && tablero[i][j]==tablero[i+1][j+1] && tablero[i][j]==tablero[i+2][j+2] && tablero[i][j]==tablero[i+3][j+3]
|| tablero[i][j]=='x' && tablero[i][j]==tablero[i+1][j+1] && tablero[i][j]==tablero[i+2][j+2] && tablero[i][j]==tablero[i+3][j+3]){
return 1;
}
}
}
//Comprobamos si hay 4 fichas iguales en diagonal inversa
for(i=0;i<6;i++){
for(j=0;j<7;j++){
if(tablero[i][j]=='o' && tablero[i][j]==tablero[i+1][j-1] && tablero[i][j]==tablero[i+2][j-2] && tablero[i][j]==tablero[i+3][j-3]
|| tablero[i][j]=='x' && tablero[i][j]==tablero[i+1][j-1] && tablero[i][j]==tablero[i+2][j-2] && tablero[i][j]==tablero[i+3][j-3]){
return 1;
}
}
}
return 0;
}
int main(){
char buffer[250];
strcpy(buffer, "+Ok. Empieza la partida.");
char tablero[6][7]={{'-','-','-','-','-','-','-'},
{'-','-','-','-','-','-','-'},
{'-','-','-','-','-','-','-'},
{'-','-','-','-','-','-','-'},
{'-','-','-','-','-','-','-'},
{'-','-','-','-','-','-','-'}};
decodificarTablero(tablero,buffer);
printf("RawBuffer=[%s]\n",buffer);
imprimir(buffer);
strcpy(buffer, "+Ok. Nuevo tablero. ");
decodificarTablero(tablero,buffer);
printf("RawBuffer=[%s]\n",buffer);
imprimir(buffer);
}