Skip to content
Browse files

Allow swapping out of computer and human players

  • Loading branch information...
1 parent 4a2cdee commit b17a3c128752551956f95949f5b0648723bebb3e @kevinclark committed May 12, 2012
Showing with 21 additions and 4 deletions.
  1. +21 −4 tic-tac-toe-2/tictactoe2.py
View
25 tic-tac-toe-2/tictactoe2.py
@@ -38,12 +38,12 @@ def prompt_for_space():
return space
-def human_players(mark):
+def human_player(mark):
print "It's %s's turn - where would you like to play, %s?" % (mark, mark)
return prompt_for_space()
-def play(mark='X', takeTurn=human_players):
+def play(mark='X', takeTurn=human_player):
print "\nThe board looks like this:"
print_board()
print
@@ -59,6 +59,23 @@ def play(mark='X', takeTurn=human_players):
elif not [space for space in board if space is None]:
print "It's a draw!"
else:
- play('O' if mark == 'X' else 'X')
+ play('O' if mark == 'X' else 'X', takeTurn)
-play()
+
+def withAComputerPlayer(mark):
+ if mark == 'X':
+ return human_player(mark)
+ else:
+ space = computer_player(mark)
+ print "The computer picks space %s" % space
+ return space
+
+
+def computer_player(mark):
+ spaceNum = 0
+ for space in board:
+ if space is None:
+ return spaceNum
+ spaceNum += 1
+
+play(takeTurn=withAComputerPlayer)

0 comments on commit b17a3c1

Please sign in to comment.
Something went wrong with that request. Please try again.