예제 #1
0
def play_game(players, verbose=False):
    field = Field()
    for turn in range(9):
        id_player = turn % 2
        move = players[id_player].move(field.field)
        field.field[0, move] = 1
        status = field.eval()

        if id_player == 1:
            status = -status

        s = field.to_str(id_player)
        if verbose:
            print(s)

        if status[0] > 0:
            if verbose:
                print("Victory of x!")
            return 1
        if status[0] < 0:
            if verbose:
                print("victory of o!")
            return -1
        field.field = -field.field
    return 0
예제 #2
0
def train(players, optimizer):
    players[0].reset()
    players[1].reset()
    field = Field()
    for turn in range(9):
        id_player = turn % 2
        move = players[id_player].move(field.field)
        field.field[0, move] = 1
        status = field.eval()

        if id_player == 1:
            status = -status

        if status[0] != 0:
            loss = players[id_player].loss_victory() + players[
                1 - id_player].loss_defeat()
            return loss
        field.field = -field.field
    return None