Advertisement
here2share

# Tk_tic_tac_toe.py

Oct 11th, 2016
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.79 KB | None | 0 0
  1. # Tk_tic_tac_toe.py
  2. # ZZZ just for a quick reference
  3.  
  4. from Tkinter import *
  5.  
  6. import random
  7.  
  8. squares = None
  9. player = None
  10. scores = [0, 0]
  11. O = 10
  12. X = 1
  13.  
  14. solo = True
  15.  
  16. winning_combos = [
  17.     [0, 1, 2], [3, 4, 5], [6, 7, 8],
  18.     [0, 3, 6], [1, 4, 7], [2, 5, 8],
  19.     [0, 4, 8], [2, 4, 6]]
  20.  
  21. winners = ['X won', 'result is draw', 'O won']
  22.  
  23.  
  24. def available_moves():
  25.     #return empty spots
  26.     return [k for k, v in enumerate(squares) if v is None]
  27.  
  28. def complete():
  29.     #return if the game is finish
  30.     if None not in [v for v in squares]:
  31.         return True
  32.     if winner() != None:
  33.         return True
  34.     return False
  35.  
  36. def game_over():
  37.     info_winner = None
  38.     if winner() == O:
  39.         info_winner = 'AI won'  
  40.     elif winner() == X:
  41.         info_winner = 'player won'
  42.     elif winner() == None:
  43.         info_winner = 'nobody won'
  44.     trace_win_lign()
  45.    
  46.    
  47.  
  48. def detect_win_lign():
  49.     for combo in winning_combos:
  50.         k = 0
  51.         for pos in combo:
  52.             print(pos)
  53.             print ("sp", squares[pos])
  54.             if not squares[pos] == None:
  55.                 k += squares[pos]
  56.         if k==3 or k==30:
  57.             print (combo)
  58.             return combo
  59.  
  60. def trace_win_lign():
  61.     lign = detect_win_lign()
  62.     if lign == [0, 1, 2]:
  63.         canvas.create_line(15, 101, 590, 101, width = 12, fill = '#1FA055')
  64.     elif lign == [3, 4, 5]:
  65.         canvas.create_line(15, 302, 590, 302, width = 12, fill = '#1FA055')
  66.     elif lign == [6, 7, 8]:
  67.         canvas.create_line(15, 503, 590, 503, width = 12, fill = '#1FA055')
  68.     elif lign == [0, 3, 6]:
  69.         canvas.create_line(101, 15, 101, 590, width = 12, fill = '#1FA055')
  70.     elif lign == [1, 4, 7]:
  71.         canvas.create_line(302, 15, 302, 590, width = 12, fill = '#1FA055')
  72.     elif lign == [2, 5, 8]:
  73.         canvas.create_line(503, 15, 503, 590, width = 12, fill = '#1FA055')
  74.     elif lign == [0, 4, 8]:
  75.         canvas.create_line(15, 15, 590, 590, width = 12, fill = '#1FA055')
  76.     elif lign == [2, 4, 6]:
  77.         canvas.create_line(590, 15, 15, 590, width = 12, fill = '#1FA055')
  78.  
  79. def ___init___():
  80.     canvas.delete("all")
  81.     board_trace()
  82.     global squares
  83.     squares = [None for i in range(9)]
  84.     global player
  85.     player = random.choice([X, O])
  86.     if player == O and solo == True:
  87.         ai(player)
  88.         player = X
  89.  
  90.  
  91. def X_win():
  92.     return winner() == X
  93.  
  94. def O_win():
  95.     return winner() == O
  96.  
  97. def draw():
  98.     return complete() == True and winner() is None
  99.  
  100. def winner():
  101.     #return who is the winner (if there is one)
  102.     for player in (X, O):
  103.         positions = get_squares(player)
  104.         for combo in winning_combos:
  105.             win = True
  106.             for pos in combo:
  107.                 if pos not in positions:
  108.                     win = False
  109.             if win:
  110.                 return player
  111.     return None
  112.  
  113. def get_squares(player):
  114.     #return squares that belong to a player
  115.     return [k for k, v in enumerate(squares) if v == player]
  116.  
  117. def ai(player):
  118.     print ("- AI START -")
  119.     a = -2
  120.     moves = []
  121.     if len(available_moves()) == 9:
  122.         make_move(random.choice([0, 2, 6, 8, 4]), player) #if ai start, choose center or random corner
  123.         return
  124.     for move in available_moves():
  125.         ghost_move(move, player)
  126.         val = alphabeta(get_enemy(player), -2, 2)
  127.         ghost_move(move, None)
  128.         print ("If move in ", move + 1, ", ", winners[val + 1])
  129.         if val > a:
  130.             a = val
  131.             moves = [move]
  132.         elif val == a:
  133.             moves.append(move)
  134.     print ("usefull moves : ", moves)
  135.     print ("- AI STOP -")
  136.     make_move(random.choice(moves), player)
  137.     return
  138.  
  139. def alphabeta(player, alpha, beta):
  140.     #simple minmax algo with alpha-beta pruning
  141.     if complete():
  142.         if X_win():
  143.             return -1
  144.         elif draw():
  145.             return 0
  146.         elif O_win():
  147.             return 1
  148.     for move in available_moves():
  149.         ghost_move(move, player)
  150.         val = alphabeta(get_enemy(player), alpha, beta)
  151.         ghost_move(move, None)
  152.         if player == O:
  153.             if val > alpha:
  154.                 alpha = val
  155.             if alpha >= beta:
  156.                 return beta
  157.         else:
  158.             if val < beta:
  159.                 beta = val
  160.             if beta <= alpha:
  161.                 return alpha
  162.     if player == O:
  163.         return alpha
  164.     else:
  165.         return beta
  166.  
  167. def ghost_move(position, player):
  168.     #
  169.     squares[position] = player
  170.  
  171. def get_enemy(player):
  172.     #return opposit player
  173.     if player == X:
  174.         return O
  175.     return X
  176.  
  177. def board_trace():
  178.     #drawn the board ligns
  179.     canvas.create_line(202, 2, 202, 603, width = 2, fill = '#F0F0F0')
  180.     canvas.create_line(403, 2, 403, 603, width = 2, fill = '#F0F0F0')
  181.     canvas.create_line(2, 202, 603, 202, width = 2, fill = '#F0F0F0')
  182.     canvas.create_line(2, 403, 603, 403, width = 2, fill = '#F0F0F0')
  183.  
  184. def make_move(position, player):
  185.     """place on the board"""
  186.     if position == 0:
  187.         symbol_trace(101, 101, player)
  188.     elif position == 1:
  189.         symbol_trace(302, 101, player)
  190.     elif position == 2:
  191.         symbol_trace(503, 101, player)
  192.     elif position == 3:
  193.         symbol_trace(101, 302, player)
  194.     elif position == 4:
  195.         symbol_trace(302, 302, player)
  196.     elif position == 5:
  197.         symbol_trace(503, 302, player)
  198.     elif position == 6:
  199.         symbol_trace(101, 503, player)
  200.     elif position == 7:
  201.         symbol_trace(302, 503, player)
  202.     elif position == 8:
  203.         symbol_trace(503, 503, player)
  204.  
  205.     squares[position] = player
  206.  
  207. def symbol_trace(x, y, player):
  208.  
  209.     if player == X:
  210.         canvas.create_line(x-65, y-65, x+65, y+65, width = 30, fill = '#1E7FCB')
  211.         canvas.create_line(x-65, y+65, x+65, y-65, width = 30, fill = '#1E7FCB')
  212.     elif player == O:
  213.         canvas.create_oval(x - 75, y - 75, x + 75, y + 75, outline='#FF0921', fill = '#FF0921')
  214.         canvas.create_oval(x - 50, y - 50, x+50, y+50, outline='#FF0921', fill = '#272822')
  215.  
  216. def click_pos_parsing(event):
  217.     if complete():
  218.         ___init___()
  219.         return
  220.     clickX = event.x
  221.     clickY = event.y
  222.     player_choice = None
  223.  
  224.     if clickY > 0 and clickY <= 205:
  225.         if clickX > 0 and clickX <= 205:
  226.             player_choice = 0
  227.         elif clickX > 205 and clickX <= 405:
  228.             player_choice = 1
  229.         elif clickX > 405 and clickX <= 605:
  230.             player_choice = 2
  231.     elif clickY > 205 and clickY <= 405:
  232.         if clickX > 0 and clickX <= 205:
  233.             player_choice = 3
  234.         elif clickX > 205 and clickX <= 405:
  235.             player_choice = 4
  236.         elif clickX > 405 and clickX <= 605:
  237.             player_choice = 5
  238.     elif clickY > 405 and clickY <= 605:
  239.         if clickX > 0 and clickX <= 205:
  240.             player_choice = 6
  241.         elif clickX > 205 and clickX <= 405:
  242.             player_choice = 7
  243.         elif clickX > 405 and clickX <= 605:
  244.             player_choice = 8
  245.     else :
  246.         player_choice = None
  247.     global player
  248.  
  249.     if not player_choice == None and player_choice in available_moves():
  250.         make_move(player_choice, player)
  251.         player = get_enemy(player)
  252.  
  253. def b_release(event):
  254.     global player
  255.     if solo == True and player == O and not complete():
  256.         ai(player)
  257.         player = get_enemy(player)
  258.     if complete():
  259.         game_over()
  260.         return
  261.  
  262. window = Tk()
  263. window.title('Morpion')
  264. window.attributes("-fullscreen", True)
  265. window.configure(bg="#272822")
  266.  
  267. largeur = 601
  268. hauteur = 601
  269. canvas = Canvas(window, width = largeur, height = hauteur, bg ='#272822')
  270. canvas.pack(padx =10, pady =10)
  271.  
  272. ___init___()
  273.  
  274. canvas.bind('<Button-1>', click_pos_parsing)
  275. canvas.bind('<ButtonRelease-1>', b_release)
  276. def exit_game(event):
  277.     window.destroy()
  278. window.bind('<Escape>', exit_game)
  279.  
  280. window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement