-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
189 lines (175 loc) · 5.86 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#ifdef WINDOWS
#include <Windows.h>
#else
#include <unistd.h> // for usleep
void Sleep(int milliseconds){ // cross-platform sleep function
if (milliseconds >= 1000)
sleep(milliseconds / 1000);
usleep((milliseconds % 1000) * 1000);
};
#endif
void ImprimeGato(int** tablero);
int** IngresaGato(int** tablero, int turno);
int ChecaFin(int** tablero);
void titulo();
////////////////////////////////////////////////////////////////////////////////////////////////////////
class Juego {
private:
int fin = 0, turno = 0, i, j;
int** tablero = (int**) malloc(3*sizeof(int*));
public:
Juego() {
for (i = 0; i < 3; i++)
{
tablero[i] = (int*) malloc(3*sizeof(int));
}
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
tablero[i][j] = 0;
}
}
};
void acompletate() {
do
{
fin = ChecaFin(tablero);
ImprimeGato(tablero);
if(!fin)
{
tablero = IngresaGato(tablero, turno);
turno = !turno;
}
} while (!fin);
}
int quienGano() {
return fin;
}
};
////////////////////////////////////////////////////////////////////////////////////////////////
int main()
{
system("cls");
titulo();
Juego juego;
juego.acompletate();
int ganador = juego.quienGano();
// anuncia el ganador
if (ganador != 3) printf("\nHa ganado el tablero %d.", ganador);
else printf("\nEmpate.");
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// //
// Funciones //
// //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void titulo() {
printf(" ____ _____ ____\n");
printf("/ %c /%c | / %c\n", 92, 92, 92);
printf("| _ /__%c | | |\n", 92);
printf("| %c / %c | | |\n", 92, 92);
printf("%c____/ / %c | %c____/\n\n\n", 92, 92, 92);
}
void ImprimeGato(int** tablero)
{
int i, j;
for(i = 0; i < 15; i++)
{
for(j = 0; j < 3; j++)
{
if (tablero[i/5][j] == 1)
{
if ((i + 1) % 5 == 1)
{printf(" ___ ");}
else if ((i + 1) % 5 == 2)
{printf(" / %c ", 92);}
else if ((i + 1) % 5 == 3)
{printf(" | | ");}
else if ((i + 1) % 5 == 4)
{printf(" %c___/ ", 92);}
}
else if(tablero[i/5][j] == 2)
{
if ((i + 1) % 5 == 1)
{printf(" ");}
else if ((i + 1) % 5 == 2)
{printf(" %c / ", 92);}
else if ((i + 1) % 5 == 3)
{printf(" X ");}
else if ((i + 1) % 5 == 4)
{printf(" / %c ", 92);}
}
else
{
if ((i + 1) % 5)
printf(" ");
}
if((i + 1) % 5 == 0 && i != 14)
{printf("_______");}
if(i == 14) printf(" ");
if(j != 2) printf("|");
}
printf("\n");
}
}
int** IngresaGato(int** tablero, int turno)
{
int valor;
printf("\n\n");
printf("Turno del tablero %d.\n", turno + 1);
printf("tablero %d, ingresa el n%cmero de acuerdo al siguiente ejemplo:\n\n", turno + 1, 163);
printf("1|2|3\n");
printf("%c|%c|%c\n", 196, 196, 196);
printf("4|5|6\n");
printf("%c|%c|%c\n", 196, 196, 196);
printf("7|8|9\n\n");
do
{
printf("Ingresa el valor: ");
do
{
scanf("%d", &valor);
if(valor < 1 || 9 < valor) printf("Valor invalido: El numero debe estar entre 1 y 9.\n");
} while (valor < 1 || 9 < valor);
if(tablero[(valor-1)/3][(valor-1)%3]) printf("Valor invalido: Elige un lugar no ocupado.\n");
} while(tablero[(valor-1)/3][(valor-1)%3]);
if (turno == 0)
{tablero[(valor-1)/3][(valor-1)%3] = 1;}
else {tablero[(valor-1)/3][(valor-1)%3] = 2;}
return tablero;
}
int ChecaFin(int** tablero)
{
int fin, k, i, j;
for(i = 0, fin = 3; i < 3; i++)//Checa si es empate
{
for (j = 0; j < 3; j++)
{
if (!tablero[i][j]) fin = 0;
}
}
for(i = 0; i < 3; i++)
{
if(tablero[i][0]==tablero[i][1]&&tablero[i][1]==tablero[i][2]&&tablero[i][0]==1)//Checa horizontal jugador 1
fin = 1;
if(tablero[i][0]==tablero[i][1]&&tablero[i][1]==tablero[i][2]&&tablero[i][0]==2)//Checa horizontal jugador 2
fin = 2;
if(tablero[0][i]==tablero[1][i]&&tablero[1][i]==tablero[2][i]&&tablero[0][i]==1)//Checa vertical jugador 1
fin = 1;
if(tablero[0][i]==tablero[1][i]&&tablero[1][i]==tablero[2][i]&&tablero[0][i]==2)//Checa vertical jugador 2
fin = 2;
}
if(tablero[0][0]==tablero[1][1]&&tablero[1][1]==tablero[2][2]&&tablero[0][0]==1)//Checa diagonal (\) jugador 1
fin = 1;
if(tablero[0][0]==tablero[1][1]&&tablero[1][1]==tablero[2][2]&&tablero[0][0]==2)//Checa diagonal (\) jugador 2
fin = 2;
if(tablero[2][0]==tablero[1][1]&&tablero[1][1]==tablero[0][2]&&tablero[2][0]==1)//Checa diagonal (/) jugador 1
fin = 1;
if(tablero[2][0]==tablero[1][1]&&tablero[1][1]==tablero[0][2]&&tablero[2][0]==2)//Checa diagonal (/) jugador 2
fin = 2;
return fin;
}