The problem is that it is not detecting a draw and just hangs if noone wins. I understand that it isn't a good practice to paste the whole script here and ask for help but I'm all out of ideas. I got this TicTacToe script off of github and am trying to implement random moves between two AI players (both making only random moves).
import random
import time
class Tic(object):
winning_combos = (
[0, 1, 2], [3, 4, 5], [6, 7, 8],
[0, 3, 6], [1, 4, 7], [2, 5, 8],
[0, 4, 8], [2, 4, 6])
winners = ('X-win', 'Draw', 'O-win')
def __init__(self, squares=[]):
if len(squares) == 0:
self.squares = [" " for i in range(9)]
else:
self.squares = squares
def show(self):
for element in [self.squares[i:i + 3] for i in range(0, len(self.squares), 3)]:
print(element)
def available_moves(self):
"""what spots are left empty?"""
return [k for k, v in enumerate(self.squares) if v is " "]
def available_combos(self, player):
"""what combos are available?"""
return self.available_moves() + self.get_squares(player)
def complete(self):
"""is the game over?"""
if " " not in [v for v in self.squares]:
return True
if self.winner() != " ":
return True
return False
def X_won(self):
return self.winner() == 'X'
def O_won(self):
return self.winner() == 'O'
def tied(self):
return self.complete() is True and self.winner() is " "
def winner(self):
for player in ('X', 'O'):
positions = self.get_squares(player)
for combo in self.winning_combos:
win = True
for pos in combo:
if pos not in positions:
win = False
if win:
return player
return " "
def get_squares(self, player):
"""squares that belong to a player"""
return [k for k, v in enumerate(self.squares) if v == player]
def make_move(self, position, player):
"""place on square on the board"""
self.squares[position] = player
def determine(board, player):
a = -2
choices = []
if len(board.available_moves()) == 9:
return 4
for move in board.available_moves():
board.make_move(move, player)
board.make_move(move, " ")
if val > a:
a = val
choices = [move]
elif val == a:
choices.append(move)
return random.choice(choices)
def get_enemy(player):
if player == 'O':
return 'X'
return 'O'
board = Tic()
count = 0
player = 'X'
while not board.complete():
if board.complete():
break
while count == 0:
player_move = int(random.randint(1, 9))
if player_move not in board.available_moves():
continue
board.make_move(player_move, player)
player = get_enemy(player)
count += 1
while count == 1:
computer_move = int(random.randint(1, 9))
if computer_move not in board.available_moves():
continue
board.make_move(computer_move, player)
count -= 1
if board.complete():
break
if board.complete():
print("winner is", board.winner())
final_win = "winner is " + board.winner()
log = open("log_for_orig.txt", "a")
log.write(final_win + "\n" + "\n")