Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. # Exercise 27 - Tic Tac Toe Draw
  2.  
  3. def end_of_game ( game ):
  4. full_set = set()
  5. for i in range(len(game)):
  6. full_set.update(game[i])
  7. return full_set == {"X", "O"}
  8.  
  9. def ask_move():
  10. move_str = input("Player "+str(player)+", please enter Your move (row,col): ")
  11. move = move_str.split(",")
  12. moveX = int(move[0].strip())-1
  13. moveY = int(move[1].strip())-1
  14. return [moveX,moveY]
  15.  
  16. def play ( game, symbol, X, Y ):
  17. if game[X][Y] == 0:
  18. game[X][Y] = symbol
  19. else:
  20. print("This move has been played already.")
  21. move = ask_move()
  22. play(game,symbol,move[0],move[1])
  23.  
  24. def next_player ( player ):
  25. list_of_players = list(players)
  26. current = list_of_players.index(player)
  27. if len(list_of_players[current:]) == 1:
  28. return list_of_players[0]
  29. else:
  30. return list_of_players[current+1]
  31.  
  32.  
  33. if __name__ == "__main__":
  34. game = [[0, 0, 0],
  35. [0, 0, 0],
  36. [0, 0, 0]]
  37. players = {1: "X", 2: "O"}
  38. player = 1
  39. while not end_of_game(game):
  40. move = ask_move()
  41. play(game,players[player],move[0],move[1])
  42. print(game)
  43. player = next_player(player)
  44. print("End of game.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement