forked from kyclark/tiny_python_projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunit.py
More file actions
executable file
·68 lines (51 loc) · 1.56 KB
/
unit.py
File metadata and controls
executable file
·68 lines (51 loc) · 1.56 KB
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
from tictactoe import format_board, find_winner
import random
# --------------------------------------------------
def test_board_no_board():
"""makes default board"""
board = """
-------------
| 1 | 2 | 3 |
-------------
| 4 | 5 | 6 |
-------------
| 7 | 8 | 9 |
-------------
""".strip()
assert format_board('.' * 9) == board
# --------------------------------------------------
def test_board_with_board():
"""makes board"""
board = """
-------------
| 1 | 2 | 3 |
-------------
| O | X | X |
-------------
| 7 | 8 | 9 |
-------------
""".strip()
assert format_board('...OXX...') == board
# --------------------------------------------------
def test_winning():
"""test winning boards"""
wins = [('PPP......'), ('...PPP...'), ('......PPP'), ('P..P..P..'),
('.P..P..P.'), ('..P..P..P'), ('P...P...P'), ('..P.P.P..')]
for player in 'XO':
other_player = 'O' if player == 'X' else 'X'
for board in wins:
board = board.replace('P', player)
dots = [i for i in range(len(board)) if board[i] == '.']
mut = random.sample(dots, k=2)
test_board = ''.join([
other_player if i in mut else board[i]
for i in range(len(board))
])
assert find_winner(test_board) == player
# --------------------------------------------------
def test_losing():
"""test losing boards"""
losing_board = list('XXOO.....')
for _ in range(10):
random.shuffle(losing_board)
assert find_winner(''.join(losing_board)) is None