Exemplo n.º 1
0
def test_play():
    from twenty import state_twenty

    s = state_twenty(1)
    level = 4
    player, opponent = 1, 2

    print('start!')

    step = 1
    while not (s.is_win(player) or s.is_win(opponent)):
        move, _, _ = bestmove(s, level, player, opponent)

        print(f'Step {step}: {move}')

        s.do_move(move)

        if s.is_win(player):
            print(f'Player {player} win!')
            break
        print(s.is_win(player) or s.is_win(opponent))
        step += 1
        player, opponent = opponent, player
        if step > 20:
            print('error')
            break
    print('the end')
Exemplo n.º 2
0
def test_bestmove():
    from twenty import state_twenty

    # начальное состояние (пустое)
    s = state_twenty(1)
    # на два хода (на четыре полухода) вперед
    level = 4
    # первым ходит "X", вторым - "0"
    player, opponent = 1, state_twenty.opponent[1]

    # получаем лучший ход
    move, _, nodes = bestmove(s, level, player, opponent)

    print(f"Best move is: {move}")
    print(f"Node count {nodes}")
Exemplo n.º 3
0
def test_time(level):
    ''' Тестирование времени расчета
		level - количество полуходов, максимальная
				глубина дерева
	'''
    from twenty import state_twenty
    from timeit import Timer

    s = state_twenty(1)
    player, opponent = 1, state_twenty.opponent[1]

    # lambda-функция расчета количества узлов
    f = lambda: calc_nodes(s, level, player, opponent)

    # расчет времени выполнения
    t = Timer(f)
    print("Time = ", t.timeit(number=1))
Exemplo n.º 4
0
def test_plot():
    from twenty import state_twenty
    import numpy as np
    import matplotlib.pyplot as plt
    s = state_twenty(1)
    player, opponent = 1, state_twenty.opponent[1]

    # правая граница для графика
    depth = 6
    x = np.arange(1, depth, 1)
    y = np.array([calc_nodes(s, level, player, opponent) for level in x])
    _ = plt.figure()
    plt.plot(x, y)
    plt.title('Count of nodes')
    plt.ylabel('nodes')
    plt.xlabel('alpha_beta')
    plt.grid(True)
    plt.show()
Exemplo n.º 5
0
from twenty import state_twenty
from minimax import bestmove

s = state_twenty(1)

level = 5
player, opponent = 1, state_twenty.opponent[1]
move, _, nodes = bestmove(s, level, player, opponent)
print(f"Best move is: ", move)

def calc_nodes(state, level, player, opponent):
    ''' Расчет количества сгенерированных узлов
        - state - начальное состояние
        - level - максимальная глубина рекрсии (количество полуходов)
        - player - игрок
        - opponent - оппонент
    '''
    _, _, nodes = bestmove(state, level, player, opponent)
    return nodes

def test_time(level):
	''' Тестирование времени расчета
		level - количество полуходов, максимальная
				глубина дерева
	'''

	from twenty import state_twenty
	from timeit import Timer

	s = state_twenty(1)
	player, opponent = 1, state_twenty.opponent[1]