/
toe.py
52 lines (43 loc) · 1.56 KB
/
toe.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
# Copyright Michael Kristofik 2011-2012.
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)
"""Computer-only tic-tac-toe game of a naive algorithm vs. a good one."""
import ai
import rules
import sys
naive_char = 'x'
smart_char = 'o'
naive_goes_first = True
# Pass a 2 on the command-line to let the smart algorithm go first.
if len(sys.argv) > 1 and sys.argv[1] == '2':
naive_char, smart_char = smart_char, naive_char
naive_goes_first = False
board = [' '] * 9
winner = None
first_move = True
while not winner and not rules.isFull(board):
# Skip the naive player's first turn if the smart player goes first.
if not first_move or naive_goes_first:
# Naive player's turn.
if first_move:
board[ai.firstMove()] = naive_char
else:
board[ai.tryToBeSmart(board, naive_char)] = naive_char
winner = rules.getWinner(board)
first_move = False
if not winner and not rules.isFull(board):
# Smart player's turn
if first_move:
board[ai.firstMove()] = smart_char
else:
# Don't care what the score was, just take the best move.
_, move = ai.negamax(board, smart_char)
assert move is not None
board[move] = smart_char
winner = rules.getWinner(board)
first_move = False
# Either the game just ended or we're on to the next round.
rules.printBoard(board)
print
rules.printEndGame(winner)