def battle_royal(iterations,
                 posPlayers,
                 list_goal,
                 game,
                 strat,
                 affichage=True):
    nbLignes = game.spriteBuilder.rowsize
    nbColonnes = game.spriteBuilder.colsize
    goalStates = [o.get_rowcol() for o in game.layers['ramassable']]
    wallStates = [w.get_rowcol() for w in game.layers['obstacle']]
    players = [o for o in game.layers['joueur']]

    nbPlayers = len(posPlayers)

    liste_gain = np.zeros(nbPlayers)

    for j in range(iterations):

        # ==== Initialisation des positions d'arriver
        chemin = [None] * nbPlayers
        for k in range(nbPlayers):
            list_goal[k] = goalStates[strat[k].get_goal()]
            chemin[k] = a_start(posPlayers[k], list_goal[k], nbLignes,
                                nbColonnes, wallStates)

        while (not fini(chemin)):
            for i in range(len(chemin)):
                if len(chemin[i]) == 0:
                    continue
                next_row, next_col = chemin[i].pop(0)
                players[i].set_rowcol(next_row, next_col)
                game.mainiteration()
                col = next_col
                row = next_row
                posPlayers[i] = (row, col)
                if (row, col) == list_goal[i]:
                    game.mainiteration()
        d = gain(posPlayers, liste_gain, goalStates)
        Strat.repartition(d)

    if affichage:
        for k in range(len(liste_gain)):
            print("resultat joueur", k)
            print("type de stratégie: ", strat[k].get_nom())
            print("score :", liste_gain[k])
            print("score moyen :", liste_gain[k] / iterations)
    return liste_gain
def main():

    #for arg in sys.argv:
    iterations = 20  # default
    if len(sys.argv) == 2:
        iterations = int(sys.argv[1])
    print("Iterations: ")
    print(iterations)

    init()

    #-------------------------------
    # Initialisation
    #-------------------------------
    nbLignes = game.spriteBuilder.rowsize
    nbColonnes = game.spriteBuilder.colsize
    print("lignes", nbLignes)
    print("colonnes", nbColonnes)

    players = [o for o in game.layers['joueur']]
    nbPlayers = len(players)

    # on localise tous les états initiaux (loc du joueur)
    initStates = [o.get_rowcol() for o in game.layers['joueur']]
    print("Init states:", initStates)

    # on localise tous les objets  ramassables (les restaurants)
    goalStates = [o.get_rowcol() for o in game.layers['ramassable']]
    print("Goal states:", goalStates)
    nbRestaus = len(goalStates)

    # on localise tous les murs
    wallStates = [w.get_rowcol() for w in game.layers['obstacle']]
    #print ("Wall states:", wallStates)

    # on liste toutes les positions permises
    allowedStates = [(x,y) for x in range(nbLignes) for y in range(nbColonnes)\
                     if (x,y) not in wallStates or  goalStates]

    #-------------------------------
    # Placement aleatoire des joueurs, en évitant les obstacles
    #-------------------------------

    posPlayers = initStates

    for j in range(nbPlayers):
        x, y = random.choice(allowedStates)
        players[j].set_rowcol(x, y)
        game.mainiteration()
        posPlayers[j] = (x, y)

    #-------------------------------
    # chaque joueur choisit un restaurant
    #-------------------------------

    restau = [0] * nbPlayers
    for j in range(nbPlayers):
        c = random.randint(0, nbRestaus - 1)
        restau[j] = c

    #-------------------------------
    # a* tetu
    #-------------------------------
    from a_start import a_start
    from strat import Strat
    from strat import StratAlea
    from strat import StratTetu
    from strat import StratMoinsRempli
    from strat import StratRestauPlusProche

    from tools import gain, fini, strategie

    from tournoi import battle_royal, tournoi

    # Strat.set_nb_j(nbPlayers)
    Strat.set_list_r(goalStates)

    strat = [None] * nbPlayers
    L = []
    L.append(StratTetu())
    L.append(StratAlea())
    L.append(StratMoinsRempli())
    L.append(StratRestauPlusProche(posPlayers, 0))

    strategie(L, strat)

    list_goal = [None] * nbPlayers

    # ==== debut iteration
    # battle_royal(iterations, posPlayers, list_goal, game, strat)

    nb_essai = 200
    nb_tour = 20
    res = dict()
    names = []
    classement = []
    scores = []
    for i in L:
        res[i.get_nom()] = [0, 0]
    for i in range(nb_essai):
        res1, res2 = tournoi(L, nb_tour, game)
        for j in range(len(res1)):
            res[res1[j][0]][0] = res[res1[j][0]][0] + res1[j][1] / nb_essai
            res[res2[j][0]][1] = res[res2[j][0]][1] + res2[j][1] / nb_essai

    for key, value in res.items():
        names.append(key)
        classement.append(value[0])
        scores.append(value[1])

    plt.figure(figsize=(20, 20))
    plt.gcf().subplots_adjust(left=0.19,
                              bottom=0.23,
                              right=1.0,
                              top=0.94,
                              wspace=0.20,
                              hspace=0)
    plt.subplot(131)
    plt.bar(names, classement)
    plt.xticks(rotation='vertical')
    plt.subplot(132)
    plt.bar(names, scores)
    plt.xticks(rotation='vertical')
    plt.suptitle('Résultat du classement et des scores du tournoi')
    plt.show()