-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsingle.py
More file actions
28 lines (23 loc) · 816 Bytes
/
single.py
File metadata and controls
28 lines (23 loc) · 816 Bytes
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
"""
Sample Game between players 'X' and 'O' on a board that only has a single row of three squares.
This generates all possible board states
"""
board = [None, None, None]
def is_full():
"""Board is full if a mark in each location."""
return board[0] and board[1] and board[2]
def play(mark):
"""Play from mark and continue until won or drawn."""
for r in range(3):
if not board[r]:
board[r] = mark # Place mark
if is_full():
print(','.join(board))
else:
play(opponent(mark)) # Recursively play opponent
board[r] = None # Undo and try next one
def opponent(mark):
"""Return opponent for given mark."""
return 'O' if mark == 'X' else 'X'
if __name__ == "__main__":
play('X')