Skip to content

Instantly share code, notes, and snippets.

@breeko
Last active January 28, 2018 02:08
Show Gist options
  • Save breeko/4f9898b14ab8b7b2d36dffb2f727bb12 to your computer and use it in GitHub Desktop.
Save breeko/4f9898b14ab8b7b2d36dffb2f727bb12 to your computer and use it in GitHub Desktop.
Tic-tac-toe interface used with mini-max
def play(game, bot):
human = None
while human is None:
human = input("Select player: {} ".format(game.legal_players)).upper()
if human not in game.legal_players:
print("Invalid option")
human = None
comp = [alt_player for alt_player in game.legal_players if alt_player != human][0]
turn = game.legal_players[0]
while not game.gameover():
if comp == turn:
move = bot.play(game, comp)
else:
print(game)
move = None
while move is None:
move = input("Input move: ")
if move.upper()[:1] == "Q":
print("Quitter...")
return
elif move.upper()[:1] == "H":
print(game.__doc__)
continue
if move.isdigit():
move = int(move)
if move in game.get_moves():
game.move(turn, move)
turn = comp if turn == human else human
else:
print("Illegal move. Q to quit. H for help")
print(game)
if game.winner() == human:
print("You won!")
elif game.winner() == comp:
print("You lost :-(")
else:
print("It's a draw")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment