diff --git a/libs/filha.c b/libs/filha.c index 841081d1646162662f6adda58d0c8edbf2f54ff2..29a66732fbc7e4f64db3e882729aa3e0eeb7bd45 100644 --- a/libs/filha.c +++ b/libs/filha.c @@ -26,6 +26,7 @@ No primeiroNoFilha(Filha f) { } No n = f->primeiro; f->primeiro = getSucessorNo(f->primeiro); + --f->tamanho; setSucessorNo(n, NULL); setAntecessorNo(n, NULL); return n; @@ -37,6 +38,7 @@ No ultimoNoFilha(Filha f) { } No n = f->ultimo; f->ultimo = getAntecessorNo(f->ultimo); + --f->tamanho; setSucessorNo(n, NULL); setAntecessorNo(n, NULL); return n; @@ -48,7 +50,14 @@ No insereFilha(void *conteudo, Filha f) { if(!novo) return NULL; setConteudo(novo, conteudo); + + if(f->tamanho == 0) { + f->primeiro = novo; + } + setAntecessorNo(novo, f->ultimo); + setSucessorNo(f->ultimo, novo); + ++f->tamanho; return f->ultimo = novo; } diff --git a/libs/grafo.c b/libs/grafo.c index 160a15e72592ec60f0b6cee30045a2dd6b0b9201..79bbc3a08078a4341690d250feec26c06e46c275 100644 --- a/libs/grafo.c +++ b/libs/grafo.c @@ -127,6 +127,36 @@ void floodFill(Tblr t, Vertice v, int i, int j){ return; } +#include "filha.h" +void calculaAltura(Grafo g, Lista raiz) { + for(No n = primeiroNoLista(g->vertices); n; n = getSucessorNo(n)) { + Vertice v = (Vertice) getConteudo(n); + v->altura = -1; + } + + Filha fila = constroiFilha(); + for(No n = primeiroNoLista(raiz); n; n = getSucessorNo(n)) { + Vertice v = (Vertice) getConteudo(n); + v->altura = 0; + insereFilha(v, fila); + } + + while(tamanhoFilha(fila) > 0) { + No n = primeiroNoFilha(fila); + if(!n) { + continue; + }; + Vertice v = (Vertice) getConteudo(n); + for(No m = primeiroNoLista(v->filhos); m; m = getSucessorNo(m)) { + Vertice filho = (Vertice) getConteudo(m); + if(filho->altura == -1) { + filho->altura = v->altura+1; + insereFilha(filho, fila); + } + } + } +} + void destroiGrafo(Grafo g) { destroiLista(g->vertices, destroiVertice); free(g); @@ -144,10 +174,10 @@ void grafoParaDot(Grafo g, Lista grupo, FILE* fp) { // Imprime o grafo for(No n = primeiroNoLista(g->vertices); n; n = getSucessorNo(n)) { Vertice pai = (Vertice) getConteudo(n); - fprintf(fp, "\t\"%p\" [label=\"cor=%d,peso=%d\"];\n", pai, pai->cor, pai->peso); + fprintf(fp, "\t\"%p\" [label=\"cor=%d\npeso=%d\naltura=%d\"];\n", pai, pai->cor, pai->peso, pai->altura); for(No m = primeiroNoLista(pai->filhos); m; m = getSucessorNo(m)) { Vertice filho = (Vertice) getConteudo(m); - fprintf(fp, "\t\"%p\" [label=\"cor=%d,peso=%d\"];\n", filho, filho->cor, filho->peso); + fprintf(fp, "\t\"%p\" [label=\"cor=%d\npeso=%d\naltura=%d\"];\n", filho, filho->cor, filho->peso, filho->altura); fprintf(fp, "\t\"%p\" -- \"%p\";\n", pai, filho); } } diff --git a/libs/grafo.h b/libs/grafo.h index ad9a4a20469a93c6ba8db563d1b51a0828aae804..847f6f714f2b7230809ee8f1d7917671df975efd 100644 --- a/libs/grafo.h +++ b/libs/grafo.h @@ -19,6 +19,8 @@ void tabuleiroParaGrafo(Tblr t, Grafo g); void floodFill(Tblr t, Vertice v, int i, int j); +void calculaAltura(Grafo g, Lista raiz); + void destroiGrafo(Grafo g); void grafoParaDot(Grafo g, Lista grupo, FILE* fp); diff --git a/libs/jogador.c b/libs/jogador.c index ba91ac2f0d0075d2bf57a643893860bf4f0398c0..310b94ec628a36e711591cda8dec763f58945a9d 100644 --- a/libs/jogador.c +++ b/libs/jogador.c @@ -29,8 +29,12 @@ Lista Joga(Grafo g, Lista grupo){ for(No n = primeiroNoLista(coresFilhos); n; n = getSucessorNo(n)) { Vertice v = (Vertice) getConteudo(n); // TODO: tratar empates! - if((v->peso + v->bonus) > (maior->peso + maior->bonus)) { + if((v->bonus) > (maior->bonus)) { maior = v; + } else if(v->bonus == maior->bonus) { + if(v->peso > maior->peso) { + maior = v; + } } } insereLista(maior->cor, jogadas); @@ -46,16 +50,17 @@ Lista Joga(Grafo g, Lista grupo){ // Limpa as coisas destroiLista(filhos, NULL); destroiLista(coresFilhos, destroiVertice); - - // // PARA DEBUG!! Imprime as últimas 10 jogadas em um arquivo - // char str[32]; - // sprintf(str, "./jogada%d.out", counter ); - // FILE* debug = fopen(str, "w+"); - // if(debug) { - // grafoParaDot(g, grupo, debug); - // } - // fclose(debug); - // ++counter; + + calculaAltura(g, grupo); + // PARA DEBUG!! Imprime as últimas 10 jogadas em um arquivo + char str[32]; + sprintf(str, "./jogada%d.out", counter ); + FILE* debug = fopen(str, "w+"); + if(debug) { + grafoParaDot(g, grupo, debug); + } + fclose(debug); + ++counter; } while(tamanhoLista(grupo) < tamanhoLista(g->vertices)); return jogadas; diff --git a/libs/no.c b/libs/no.c index b88cfce6e0a4b6e7d03fc8691f67014756a513b4..7627f4ec3c43d61d65bec4f348b6d7df6fc4ef7e 100644 --- a/libs/no.c +++ b/libs/no.c @@ -15,18 +15,22 @@ No criaNo() { } No getSucessorNo(No n) { + if(!n) return NULL; return n->proximo; } void setSucessorNo(No n, No p) { + if(!n) return; n->proximo = p; } No getAntecessorNo(No n) { + if(!n) return NULL; return n->anterior; } void setAntecessorNo(No n, No p) { + if(!n) return; n->anterior = p; } diff --git a/libs/vertice.c b/libs/vertice.c index 35fb0e53c4f912260563da858843b7767ced071b..2dd459044f81ae554a843ee91254e31e48493383 100644 --- a/libs/vertice.c +++ b/libs/vertice.c @@ -9,6 +9,7 @@ Vertice criaVertice() { v->cor = -1; v->peso = 0; v->bonus = 0; + v->altura = -1; v->grupo = false; v->pais = constroiLista(); v->filhos = constroiLista(); diff --git a/libs/vertice.h b/libs/vertice.h index 9ee344e46b561b35965563d85fdb2fa74ccc535c..c740ffc9d38be94c3d019445db9baa06bf2e1774 100644 --- a/libs/vertice.h +++ b/libs/vertice.h @@ -7,6 +7,7 @@ struct Vertice { int cor; int peso; int bonus; + int altura; bool grupo; Lista pais; Lista filhos; diff --git a/main.c b/main.c index 8d59c2b9591fc38e6e2616d8fd68c092533d8286..64059a996b32095e07962f62e899695f145e5cba 100644 --- a/main.c +++ b/main.c @@ -17,7 +17,7 @@ int main(int argc, char *argv[]) { puts("Erro na leitura do tabuleiro"); return -1; } - + // Se colocar UMA e APENAS UMA coisa depois do main if(argc == 2) { // Calcula e imprime o número mÃnimo e máximo de jogadas