furas

Python - Tic Tac Toe (FB: LearnPython.org)

Oct 21st, 2016
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.58 KB | None | 0 0
  1. #-*- coding: utf-8 -*-
  2.  
  3. #
  4. # https://en.wikipedia.org/wiki/Box-drawing_character#Unicode
  5. #
  6. # http://pastebin.com/fNnk1Yen
  7. # http://pastebin.com/W3qdVbDr
  8. #
  9.  
  10. import random
  11.  
  12. # ----- functions -----
  13.  
  14. def draw_map(moves):
  15.    
  16.     print u"╔═══════╤═══════╤═══════╗"
  17.     print u"║1      │2      │3      ║"
  18.     print u"║   {}   │   {}   │   {}   ║".format(*moves[0:3])
  19.     print u"║       │       │       ║"
  20.     print u"╟───────┼───────┼───────╢"
  21.     print u"║4      │5      │6      ║"
  22.     print u"║   {}   │   {}   │   {}   ║".format(*moves[3:6])
  23.     print u"║       │       │       ║"
  24.     print u"╟───────┼───────┼───────╢"
  25.     print u"║7      │8      │9      ║"
  26.     print u"║   {}   │   {}   │   {}   ║".format(*moves[6:9])
  27.     print u"║       │       │       ║"
  28.     print u"╚═══════╧═══════╧═══════╝"
  29.  
  30. # ----- constants ----- (UPPER_CASE)
  31.  
  32. # winner moves
  33.  
  34. WINNER_MOVES = [
  35.     {1,2,3},
  36.     {4,5,6},
  37.     {7,8,9},
  38.     {1,5,9},
  39.     {2,5,8},
  40.     {3,6,9},
  41.     {1,4,7},
  42.     {3,5,7},
  43. ]
  44.  
  45. # ----- main -----
  46.  
  47. moves = [" "] * 10
  48.  
  49. slots = range(1, 10)
  50.  
  51. player_record = []
  52. AI_record = []
  53.  
  54. running = True
  55.  
  56. while True: # or: while running:
  57.  
  58.     # --- player ---
  59.    
  60.     # repeat till correct move        
  61.     while True:
  62.         draw_map(moves)
  63.  
  64.         try:
  65.            player_move = int(raw_input("MOVE: "))
  66.         except ValueError:
  67.             continue
  68.        
  69.         if player_move in slots:
  70.             break
  71.  
  72.     slots.remove(player_move)
  73.        
  74.     moves[player_move-1] = "X"
  75.     player_record.append(player_move)
  76.  
  77.     for check in WINNER_MOVES:
  78.         if set(player_record).intersection(check) == check:
  79.             draw_map(moves)
  80.             print "YOU WIN !"
  81.  
  82.             running = False
  83.             break
  84.            
  85.     # ----
  86.    
  87.     if not running: # end game
  88.         break
  89.    
  90.     # --- AI ---
  91.    
  92.     AI_move = random.choice(slots)
  93.     slots.remove(AI_move)
  94.        
  95.     moves[AI_move-1] = "O"
  96.     AI_record.append(AI_move)
  97.  
  98.     for check in WINNER_MOVES:
  99.         if set(AI_record).intersection(check) == check:
  100.             draw_map(moves)
  101.             print "AI WIN !"
  102.  
  103.             running = False
  104.             break
  105.  
  106.     # ----
  107.    
  108.     if not running: # end game
  109.         break
  110.    
  111. # ---- the end ---
  112.  
  113. raw_input("Press any key to exit!")
Add Comment
Please, Sign In to add comment