Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- # http://pastebin.com/fNnk1Yen
- #
- import random
- # ----- functions -----
- def draw_map(moves):
- print "+-------+-------+-------+"
- print "|1 |2 |3 |"
- print "| {} | {} | {} |".format(*moves[0:3])
- print "| | | |"
- print "+-------+-------+-------+"
- print "|4 |5 |6 |"
- print "| {} | {} | {} |".format(*moves[3:6])
- print "| | | |"
- print "+-------+-------+-------+"
- print "|7 |8 |9 |"
- print "| {} | {} | {} |".format(*moves[6:9])
- print "| | | |"
- print "+-------+-------+-------+"
- # ----- constants ----- (UPPER_CASE)
- # winner moves
- WINNER_MOVES = [
- {1,2,3},
- {4,5,6},
- {7,8,9},
- {1,5,9},
- {2,5,8},
- {3,6,9},
- {1,4,7},
- {3,5,7},
- ]
- # ----- main -----
- moves = [" "] * 10
- slots = range(1, 10)
- player_record = []
- AI_record = []
- running = True
- while True: # or: while running:
- # --- player ---
- # repeat till correct move
- while True:
- draw_map(moves)
- try:
- player_move = int(raw_input("MOVE :"))
- except ValueError:
- continue
- if player_move in slots:
- break
- slots.remove(player_move)
- moves[player_move-1] = "X"
- player_record.append(player_move)
- for check in WINNER_MOVES:
- if set(player_record).intersection(check) == check:
- draw_map(moves)
- running = False
- print "YOU WIN !"
- break
- # ----
- if not running: # skip AI
- break
- # --- AI ---
- AI_move = random.choice(slots)
- slots.remove(AI_move)
- moves[AI_move-1] = "O"
- AI_record.append(AI_move)
- for check in WINNER_MOVES:
- if set(AI_record).intersection(check) == check:
- draw_map(moves)
- running = False
- print "AI WIN !"
- break
- # ----
- if not running: # skip Player
- break
- # ---- the end ---
- raw_input("Press any key to exit!")
Add Comment
Please, Sign In to add comment