NO ESTA TERMINADO AUN
Pero este programa genera tantos Arboles como queramos con ayuda de una funcion recursiva "next_moves" y la profuncidad deceada.
Primero se inicializa el tablero inicial con un valor random y apartir de ahi genera las siguientes "Level" Jugadas rellenando un arbol para su posterior evaluacion del siguiente mejor movimiento dado el turno Actual (Esto aun No esta)
Salida del programa (Aleatoria primera Jugada):
Codigo (Espoiler EXCESO DE APUNTADORES Y MEMORIA DINAMICA)
La base para ver MAS jugadas esta en el tercer parametro de la funcion best, el cual si quertemos un arbol de mas profundidad cambiamos el 1 por 2 o 3 o X.... segun el nivel que queramos
Saludos!
Pero este programa genera tantos Arboles como queramos con ayuda de una funcion recursiva "next_moves" y la profuncidad deceada.
Primero se inicializa el tablero inicial con un valor random y apartir de ahi genera las siguientes "Level" Jugadas rellenando un arbol para su posterior evaluacion del siguiente mejor movimiento dado el turno Actual (Esto aun No esta)
Salida del programa (Aleatoria primera Jugada):
Código [Seleccionar]
Tablero Actual:
...
...
...
Turno de X
X Jugo:
...
...
X..
Creando Nodo nuevo de Arbol
O..
...
X..
Creando Nodo nuevo de Arbol
.O.
...
X..
Creando Nodo nuevo de Arbol
..O
...
X..
Creando Nodo nuevo de Arbol
...
O..
X..
Creando Nodo nuevo de Arbol
...
.O.
X..
Creando Nodo nuevo de Arbol
...
..O
X..
Creando Nodo nuevo de Arbol
...
...
XO.
Creando Nodo nuevo de Arbol
...
...
X.O
Codigo (Espoiler EXCESO DE APUNTADORES Y MEMORIA DINAMICA)
Código (c) [Seleccionar]
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<stdbool.h>
struct tablero {
char juego[3][3];
};
struct nodo {
struct tablero *data;
int total;
struct nodo **aristas;
};
struct move {
char x,y;
};
char *jugadores = "OX"; //0 = O , 1 = X
void imprimir_tablero(struct tablero *t);
struct tablero *tablero_nuevo();
struct nodo *nuevo_nodo();
struct nodo* create_nodo_from(struct tablero *t);
void next_moves(struct nodo *n,int turno,int level);
struct move *best(struct tablero *actual,int turno,int level);
int main() {
bool end = false;
char x,y;
struct tablero *inicial = NULL;
srand(time(NULL));
int turno = rand() % 2;
inicial = tablero_nuevo();
while(!end) {
printf("Tablero Actual:\n");
imprimir_tablero(inicial);
printf("Turno de %c\n",jugadores[turno%2]);
x = rand() % 3;
y = rand() % 3;
inicial->juego[x][y] = jugadores[turno%2];
printf("%c Jugo:\n",jugadores[turno%2]);
imprimir_tablero(inicial);
//if(turno == 8) {
end = true;
//}
turno++;
best(inicial,turno,1); //El nivel 1 es solo la siguiente jugada, a mas profundidad del arbol mas tarda, pero mas "dificilta" representa la computadora
}
return 0;
}
struct move *best(struct tablero *actual,int turno,int level) {
struct move *m = malloc(sizeof(struct move));
struct nodo *raiz,*pivote,*aux;
int i = 0;
raiz = nuevo_nodo();
raiz->data = malloc(sizeof(struct tablero));
memcpy(raiz->data,actual,sizeof(struct tablero));
next_moves(raiz,turno,level);
return m;
}
void next_moves(struct nodo *n,int turno,int level) {
int i,j;
struct nodo *temp = NULL;
if(n) {
i = 0;
while(i < 3) {
j =0;
while(j < 3) {
if(n->data->juego[i][j] == 0) {
n->aristas = realloc(n->aristas,sizeof(struct nodo*)*n->total+1);
n->aristas[n->total] = create_nodo_from(n->data);
n->aristas[n->total]->data->juego[i][j] = jugadores[turno%2];
if(level > 0) {
next_moves(n->aristas[n->total],turno+1,level-1);
printf("Creando Nodo nuevo de Arbol\n");
imprimir_tablero(n->aristas[n->total]->data);
}
n->total++;
}
j++;
}
i++;
}
}
}
struct nodo* create_nodo_from(struct tablero *t) {
struct nodo *temp = nuevo_nodo();
temp->data = tablero_nuevo();
memcpy(temp->data,t,sizeof(struct tablero));
return temp;
}
struct nodo *nuevo_nodo() {
struct nodo *n = calloc(1,sizeof(struct nodo));
return n;
}
struct tablero *tablero_nuevo() {
struct tablero *temp = calloc(1,sizeof(struct tablero));
return temp;
}
void imprimir_tablero(struct tablero *t) {
char i,j;
if(t) {
i = 0;
while(i < 3) {
j = 0;
while(j < 3) {
if(t->juego[i][j]) {
printf("%c",t->juego[i][j]);
}
else {
printf(".");
}
j++;
}
printf("\n");
i++;
}
}
}
La base para ver MAS jugadas esta en el tercer parametro de la funcion best, el cual si quertemos un arbol de mas profundidad cambiamos el 1 por 2 o 3 o X.... segun el nivel que queramos
Código (c) [Seleccionar]
best(inicial,turno,1);
Saludos!