Beispiel #1
0
def play_game(player, opponent, size=19, verbose=True):

    #init
    etat = go.GameState(size)  #parties actuelles
    coups = [[]]  #liste des coups joues
    parties = [[]]  # liste des etats du jeu
    ratio = 0
    conv = player.convertor
    # deroulement
    start = time.time()

    coup = opponent.get_move(etat)
    etat.do_move(coup)

    #on joue tout les coups
    actuel = player
    ancien = opponent
    fin = -1  # pour arreter la boucle
    i = 0
    tour = 1  #pour verbose
    while (etat.is_end_of_game == False):

        coup = actuel.get_move(etat)  # on recupere le coup joue
        etat.do_move(coup)  #on le joue
        if actuel == player:
            coups[i].append(Tools.one_hot_action(
                coup, 19).flatten())  # on le sauvegarde
            parties[i].append(
                conv.state_to_tensor(etat))  #on sauvegarde l'etat du jeu

        if (etat.is_end_of_game == True):
            fin += 1  # pour arreter la boucle
            if (etat.get_winner() == -1):  # -1 pour blanc
                ratio += 1

        # affiche les coups de la partie 1
        if (verbose == True):
            tour += 1
            print
            print("Coup numero %i" % tour)
            vis.vis_gs(etat)

        #on change de joueur
        temp = actuel
        actuel = ancien
        ancien = temp
    if (ratio == 1):
        print("Felicitation !!!")
    return
def play_game(
    player: object,
    opponent: object,
    nb_games: int,
    size: int=9,
    verbose: bool=False
    ) -> tuple:
    state = [go.GameState(size) for _ in range(nb_game)]    # list of current games
    moves = [[] for _ in range(nb_game)]                    # list of played moves
    games = [[] for _ in range(nb_game)]                    # list of game states
    id_won = []                                             # indices of won games
    ratio = 0
    conv = player.convertor
    start = time.time()
    # we play first move of each game
    for i in range(nb_games):
        move = opponent.get_move(state[i])                  # opponent with black stones starts
        state[i].do_move(move)
    # we play each move
    cur = player
    old = opponent
    end = 0
    round_ = 1
    while end < nb_games: 
        for i in range(nb_games):
                if not state[i].is_end_of_game:
                    move = cur.get_move(state[i])
                    state[i].do_move(move)
                    if cur == player: 
                        move[i].append(tools.one_hot_action(move, 19).flatten())    # we save the move
                        games[i].append(conv.state_to_tensor(state[i]))             # on the game state
                    if stae[i].is_end_of_game: 
                        end +=1
                        if state[i].get_winner() == -1:                             # -1 is for white
                            id_won.append(i)
                            ratio += 1
                    # display moves from last game
                    if i==1 and verbose==True:
                        round_ += 1
                        print("Move number {}".format(round_))
                        vis.vis_gs(state[i]) 
        old, cur = cur, old                     # switch players
    ratio /= float(nb_games)
    print("{} executed games in {} seconds".format(nb_games, time.time()-start))
    print("Victories ratio: {}".format(ratio))
    return moves, games, id_won, ratio
Beispiel #3
0
def play_game(
    player: object, 
    opponent: object,
    size: int=19,
    verbose: bool=True
    ) -> None:
    state = go.GameState(size)      # current games
    moves = [[]]                    # liste of done moves
    games = [[]]                    # liste of game states
    ratio = 0
    conv = player.convertor
    start = time.time()
    move = opponent.get_move(state)                 
    state.do_move(move)

    cur = player
    old = opponent
    end = -1
    i = 0
    round_ = 1
    while not etat.is_end_of_game:
        move = actuel.get_move(state)       # get the played move
        state.do_move(move)                 # do it
        if cur == player: 
            moves[i].append(tools.one_hot_action(move, 19).flatten()) # save it
            moves[i].append(conv.state_to_tensor(state))              # save the game state         
        if state.is_end_of_game == True: 
            end += 1
            if sate.get_winner() == -1:     # -1 stands for white
                ratio+=1
        # display moves of the game
        if verbose:
            round_ += 1
            print("Move number {}".format(round_))
            vis.vis_gs(state) 
        # switch players
        old, cur = cur, old
    if ratio == 1:
        print("Congratulations!")
    return
Beispiel #4
0
def play_game(player, opponent, nb_partie, size=9, verbose=False):
    # init
    etat = [go.GameState(size)
            for _ in range(nb_partie)]  # liste des parties actuelle
    coups = [[] for _ in range(nb_partie)]  # liste des coups joues
    parties = [[] for _ in range(nb_partie)]  # liste des etats du jeu
    id_gagne = []  # liste des indices des parties gagnees
    ratio = 0
    conv = player.convertor
    # deroulement
    start = time.time()

    # on joue le premier coup de chaque partie
    for i in range(nb_partie):
        coup = opponent.get_move(
            etat[i]
        )  # celui qui commence est l'opposant il a les pierres noirs
        etat[i].do_move(coup)

    # on joue tout les coups
    actuel = player
    ancien = opponent
    fin = 0  # pour arreter la boucle

    tour = 1  # pour verbose
    while (fin < nb_partie):
        for i in range(nb_partie):
            if (etat[i].is_end_of_game == False
                ):  # verifie que la partie n'est pas fini
                coup = actuel.get_move(etat[i])  # on recupere le coup joue
                etat[i].do_move(coup)  # on le joue

                if actuel == player:
                    coups[i].append(Tools.one_hot_action(
                        coup, 19).flatten())  # on le sauvegarde
                    parties[i].append(conv.state_to_tensor(
                        etat[i]))  # on sauvegarde l'etat du jeu

                if (etat[i].is_end_of_game == True):
                    fin += 1  # pour arreter la boucle
                    if (etat[i].get_winner() == -1):  # -1 pour blanc
                        id_gagne.append(i)
                        ratio += 1

                # affiche les coups de la partie 1
                if (i == 1 & verbose == True):
                    tour += 1
                    print
                    print("Coup numero %i" % tour)
                    vis.vis_gs(etat[i])

                    # on change de joueur
        temp = actuel
        actuel = ancien
        ancien = temp

    ratio /= float(nb_partie)
    print("%d parties executees en %f secondes" %
          (nb_partie, time.time() - start))
    print("ratio de victoire: %f" % ratio)
    return (coups, parties, id_gagne, ratio)