-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_agents.py
executable file
·82 lines (59 loc) · 2.08 KB
/
test_agents.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import pytest
from agents import InOrder, Random, OneStep, OneStepBlocking, Minimax, TabularQLearning
from board import Board
@pytest.fixture()
def almost_win_board():
board = Board(num_rows=3, num_cols=3, num_to_win=3)
board.make_move((1, 1))
board.make_move((0, 1))
board.make_move((0, 0))
board.make_move((1, 0))
return board
@pytest.fixture()
def almost_lose_board():
board = Board(num_rows=3, num_cols=3, num_to_win=3)
board.make_move((0, 0))
board.make_move((2, 0))
board.make_move((1, 2))
board.make_move((0, 2))
return board
def test_in_order_valid(almost_win_board):
ai = InOrder(almost_win_board, player_idx=0)
move = ai.move()
assert almost_win_board.is_move_free(move)
def test_random_valid(almost_win_board):
ai = Random(almost_win_board, player_idx=0)
move = ai.move()
assert almost_win_board.is_move_free(move)
def test_one_step_win(almost_win_board):
ai = OneStep(almost_win_board, player_idx=0)
move = ai.move()
assert move == (2, 2)
def test_one_step_blocking_win(almost_win_board):
ai = OneStepBlocking(almost_win_board, player_idx=0)
move = ai.move()
assert move == (2, 2)
def test_one_step_blocking_lose(almost_lose_board):
ai = OneStepBlocking(almost_lose_board, player_idx=0)
move = ai.move()
assert move == (1, 1)
def test_minimax_win(almost_win_board):
ai = Minimax(almost_win_board, player_idx=0)
move = ai.move()
assert move == (2, 2)
def test_minimax_lose(almost_lose_board):
ai = Minimax(almost_lose_board, player_idx=0)
move = ai.move()
assert move == (1, 1)
def test_minimax_sequence():
board = Board(num_rows=3, num_cols=3, num_to_win=3)
player0 = Minimax(board, player_idx=0)
player1 = Minimax(board, player_idx=1)
board.make_move(player0.move())
board.make_move(player1.move())
board.make_move(player0.move())
assert len(board.history) == 3
def test_q_learning_valid(almost_win_board):
ai = TabularQLearning(almost_win_board, player_idx=0)
move = ai.move()
assert almost_win_board.is_move_free(move)