def test_play(): from nim import state_nim s = state_nim('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} (cuttent heaps: {s.heaps} Player: {player}') move, _, _ = bestmove(s, level, player, opponent) s.do_move(move) print(f'Step {step}: {move} (cuttent heaps: {s.heaps} Player: {player}') if s.is_win(opponent): print(f'Player {player} win!') break step += 1 player, opponent = opponent, player print('the end')
def test_bestmove(): from nim import state_nim # начальное состояние (пустое) s = state_nim("1") # на два хода (на четыре полухода) вперед level = 4 # первым ходит "X", вторым - "0" player, opponent = "1", state_nim.opponent["1"] # получаем лучший ход move, _, nodes = bestmove(s, level, player, opponent) print(f"Best move is: {move}") print(f"Node count {nodes}")
def test_plot(): from nim import state_nim import numpy as np import matplotlib.pyplot as plt s = state_nim("1") player, opponent = "1", state_nim.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('negmax') plt.grid(True) plt.show()
def test_time(level): ''' Тестирование времени расчета level - количество полуходов, максимальная глубина дерева ''' from nim import state_nim from timeit import Timer s = state_nim("1") player, opponent = "1", state_nim.opponent["1"] # lambda-функция расчета количества узлов f = lambda: calc_nodes(s, level, player, opponent) # расчет времени выполнения t = Timer(f) print("Time = ", t.timeit(number=1))
def test_count(): from nim import state_nim import numpy as np import matplotlib.pyplot as plt s = state_nim('1') player, opponent = "1", "2" # <<<<<<< HEAD lag = 1 x = np.arange(1, 8, lag) 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()
from nim import state_nim from minimax import bestmove s = state_nim("1") level = 5 player, opponent = "1", state_nim.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 nim import state_nim from timeit import Timer