#include <limits.h>
#include <stdio.h>
#include <string.h>

#include "prologin.h"
#include "enumere.h"
#include "eval.h"
#include "utils.h"

void partie_init(void)
{
}

void jouer_tour(void)
{
    for(agent_simul = 0; agent_simul < NB_AGENTS; agent_simul++)
    {
        calc_score_pos();

    /*for(int i = 0; i < TAILLE_BANQUISE; i++)
    {
        for(int j = 0; j < TAILLE_BANQUISE; j++)
        {
            printf("%d %d -> %.0lf\n", i, j, score_pos[i][j]);
        }
    }*/

        position posagent = position_agent(moi(), agent_simul);
        if(alien_sur_case(posagent))
        {
            for(int i = NORD; i < OUEST; i++)
            {
                struct position pdir;
                pdir.ligne   = posagent.ligne   + DEPLACEMENT[i].ligne;
                pdir.colonne = posagent.colonne + DEPLACEMENT[i].colonne;

                struct position pinv;
                pinv.ligne   = posagent.ligne   - DEPLACEMENT[i].ligne;
                pinv.colonne = posagent.colonne - DEPLACEMENT[i].colonne;
                if(agent_sur_case(pdir) == adversaire()
                && agent_sur_case(pinv) == -1
                && type_case(pinv) == LIBRE)
                    if(pousser(agent_simul, i) != OK)
                        printf("POUSSER PB\n");
            }
        }
        else
        {
            act_prof = -1;
            best_score = -1e10;
            best_prof = -1;

            enumere_coups();

            for(int i = 0; i < best_prof; i++)
            {
                struct action_hist* iact = best_stack + i;

                switch(iact->atype)
                {
                    case ACTION_DEPLACER:
                        if(deplacer(iact->id_agent, iact->dir) != OK)
                        {
                        printf("P%d DPL %d %d\n", moi(), iact->id_agent, iact->dir);
                        }
                        break;

                    case ACTION_GLISSER:
                        if(glisser(iact->id_agent, iact->dir) != OK)
                        {
                        printf("P%d GLS %d %d\n", moi(), iact->id_agent, iact->dir);
                        }

                        break;
                    case ACTION_POUSSER:
                        if(pousser(iact->id_agent, iact->dir) != OK)
                        {
                        printf("P%d PSS %d %d\n", moi(), iact->id_agent, iact->dir);
                        }

                        break;
                }
            }
        }
    }
    puts("FIN TOUR");
}

void partie_fin(void)
{
}

