FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
aima-python/tests/test_games.py at master · opensourceware/aima-python · GitHub
opensourceware
/
aima-python
Public
forked from
aimacode/aima-python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
aima-python
/
tests
/
test_games.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
73 lines (52 loc) · 2.45 KB
Breadcrumbs
aima-python
/
tests
/
test_games.py
Copy path
File metadata and controls
73 lines (52 loc) · 2.45 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
"""A lightweight test suite for games.py"""
# You can run this test suite by doing: py.test tests/games.py
# Of course you need to have py.test installed to do this.
import
pytest
from
games
import
*
# noqa
# Creating the game instances
f52
=
Fig52Game
()
ttt
=
TicTacToe
()
def
gen_state
(
to_move
=
'X'
,
x_positions
=
[],
o_positions
=
[],
h
=
3
,
v
=
3
,
k
=
3
):
"""Given whose turn it is to move, the positions of X's on the board, the
positions of O's on the board, and, (optionally) number of rows, columns
and how many consecutive X's or O's required to win, return the corresponding
game state"""
moves
=
set
([(
x
,
y
)
for
x
in
range
(
1
,
h
+
1
)
for
y
in
range
(
1
,
v
+
1
)]) \
-
set
(
x_positions
)
-
set
(
o_positions
)
moves
=
list
(
moves
)
board
=
{}
for
pos
in
x_positions
:
board
[
pos
]
=
'X'
for
pos
in
o_positions
:
board
[
pos
]
=
'O'
return
GameState
(
to_move
=
to_move
,
utility
=
0
,
board
=
board
,
moves
=
moves
)
def
test_minimax_decision
():
assert
minimax_decision
(
'A'
,
f52
)
==
'a1'
assert
minimax_decision
(
'B'
,
f52
)
==
'b1'
assert
minimax_decision
(
'C'
,
f52
)
==
'c1'
assert
minimax_decision
(
'D'
,
f52
)
==
'd3'
def
test_alphabeta_full_search
():
assert
alphabeta_full_search
(
'A'
,
f52
)
==
'a1'
assert
alphabeta_full_search
(
'B'
,
f52
)
==
'b1'
assert
alphabeta_full_search
(
'C'
,
f52
)
==
'c1'
assert
alphabeta_full_search
(
'D'
,
f52
)
==
'd3'
state
=
gen_state
(
to_move
=
'X'
,
x_positions
=
[(
1
,
1
), (
3
,
3
)],
o_positions
=
[(
1
,
2
), (
3
,
2
)])
assert
alphabeta_full_search
(
state
,
ttt
)
==
(
2
,
2
)
state
=
gen_state
(
to_move
=
'O'
,
x_positions
=
[(
1
,
1
), (
3
,
1
), (
3
,
3
)],
o_positions
=
[(
1
,
2
), (
3
,
2
)])
assert
alphabeta_full_search
(
state
,
ttt
)
==
(
2
,
2
)
state
=
gen_state
(
to_move
=
'O'
,
x_positions
=
[(
1
,
1
)],
o_positions
=
[])
assert
alphabeta_full_search
(
state
,
ttt
)
==
(
2
,
2
)
state
=
gen_state
(
to_move
=
'X'
,
x_positions
=
[(
1
,
1
), (
3
,
1
)],
o_positions
=
[(
2
,
2
), (
3
,
1
)])
assert
alphabeta_full_search
(
state
,
ttt
)
==
(
1
,
3
)
def
test_random_tests
():
assert
play_game
(
Fig52Game
(),
alphabeta_player
,
alphabeta_player
)
==
3
# The player 'X' (one who plays first) in TicTacToe never loses:
assert
play_game
(
ttt
,
alphabeta_player
,
alphabeta_player
)
>=
0
# The player 'X' (one who plays first) in TicTacToe never loses:
assert
play_game
(
ttt
,
alphabeta_player
,
random_player
)
>=
0
if
__name__
==
'__main__'
:
pytest
.
main
()
Back
|
FazBrowse Home
|
New Git URL