Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.30 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import os, time
  5. from Tkinter import *
  6. from random import randrange
  7.  
  8. board = [[0,0,0],[0,0,0],[0,0,0]] # 3*3
  9. coo1, coo2, coo3, coo4, coo5, coo6, coo7, coo8, coo9 = (50,50), (150,50), (250,50), (50,150), (150,150), (250,150), (50,250), (150,250), (250,250)
  10. liste_coo = [coo1,coo2,coo3,coo4,coo5,coo6,coo7,coo8,coo9] # Coordonnees centre des 9 cases
  11.  
  12. # Trace 4 lignes dessiant le board
  13. def init_board() :
  14.         draw_line(0,100,300,100)
  15.         draw_line(0,200,300,200)
  16.         draw_line(100,0,100,300)
  17.         draw_line(200,0,200,300)
  18.  
  19. # Trace une ligne de (x1,y1) a (x2,y2)
  20. def draw_line(x1,y1,x2,y2):
  21.         canvas1.create_line(x1,y1,x2,y2,width=5,fill='black')
  22.  
  23. # Dessine une Croix
  24. def draw_cross(x1,y1):
  25.         x1 = int(x1)
  26.         y1 = int(y1)
  27.         draw_line(x1-30,y1-30,x1+30,y1+30)
  28.         draw_line(x1+30,y1-30,x1-30,y1+30)
  29.  
  30. # Dessine un Rond
  31. def draw_cercle(x1,y1):
  32.         canvas1.create_oval(x1-36, y1-36, x1+36, y1+36, width = 4)
  33.  
  34. # i necessaire pour eviter que le programme redessine indéfiniment le board. Avec i en flag on actualise uniquement si un coup legal est joue
  35. def actualise(liste):
  36.         global i
  37.         j = 0
  38.         if i == 1 :
  39.                 for ligne in liste:
  40.                         for colonne in ligne: # Redessine tout le board
  41.                                 if colonne == 1 : draw_cross(liste_coo[j][0],liste_coo[j][1])
  42.                                 elif colonne == 2 : draw_cercle(liste_coo[j][0],liste_coo[j][1])
  43.                                 j+=1
  44.         i = 0
  45.  
  46. def pointeur(event):
  47.         u = 0
  48.         global i
  49.         global player
  50.  
  51.         # U est un flag. Si on a bien changer le board et que le coup est valide il passe a 1.
  52.         # player represent le numero de joueur pour alterner les tours. U est necessaire pour alterner le joueur si seulement le coup est legal.
  53.         # Si on detecte un click dans une case (une case = 100*100px) et que celle ci est vide. On affecte une croix ou un rond en fonction du joueur.
  54.         if (event.x <= 100)   and (event.y <= 100) and board[0][0] == 0 : board[0][0] = player ; i = 1 ; u = 1
  55.         elif (event.x <= 100) and (event.y > 100)  and (event.y <= 200) and board[1][0] == 0 : board[1][0] = player ; i = 1 ; u = 1
  56.         elif (event.x <= 100) and (event.y > 200)  and (event.y <= 300) and board[2][0] == 0 : board[2][0] = player ; i = 1 ; u = 1
  57.         elif (event.x > 100)  and (event.x <= 200) and (event.y <= 100) and board[0][1] == 0 : board[0][1] = player ; i = 1 ; u = 1
  58.         elif (event.x > 100)  and (event.x <= 200) and (event.y > 100) and (event.y <= 200) and board [1][1] == 0 : board[1][1]=player ; i = 1 ; u = 1
  59.         elif (event.x > 100)  and (event.x <= 200) and (event.y > 200) and board [2][1] == 0 : board[2][1] = player ; i = 1 ; u = 1
  60.         elif (event.x > 200)  and (event.y <= 100) and board[0][2] == 0 : board[0][2] = player ; i = 1 ; u = 1
  61.         elif (event.x > 200)  and (event.y > 100)  and (event.y <= 200) and board[1][2] == 0 : board[1][2] = player ; i = 1 ; u = 1
  62.         elif (event.x > 200)  and (event.y > 200)  and board [2][2] == 0 : board[2][2] = player ; i = 1 ; u = 1
  63.  
  64.         if u == 1 :
  65.                 if player == 1 : player = 2
  66.                 else: player = 1
  67.  
  68. def fini(board): # return gagne. 1 = Victoire j1 ; 2 = Victoire j2 ; 3 = match nul ; 0 = pas fini
  69.         gagne = 0 ; flag = 0
  70.         if (board[0][0] == 1 and board[0][1] == 1 and board[0][2] == 1)  : gagne = 1
  71.         elif (board[0][0] == 1 and board[1][0] == 1 and board[2][0] == 1)  : gagne = 1
  72.         elif (board[0][0] == 1 and board[1][1] == 1 and board[2][2] == 1)  : gagne = 1
  73.         elif (board[0][1] == 1 and board[1][1] == 1 and board[2][1] == 1)  : gagne = 1
  74.         elif (board[0][2] == 1 and board[1][2] == 1 and board[2][2] == 1)  : gagne = 1
  75.         elif (board[2][0] == 1 and board[2][1] == 1 and board[2][2] == 1)  : gagne = 1
  76.         elif (board[1][0] == 1 and board[1][1] == 1 and board[1][2] == 1)  : gagne = 1
  77.         elif (board[2][0] == 1 and board[1][1] == 1 and board[0][2] == 1)  : gagne = 1
  78.         elif (board[0][0] == 2 and board[0][1] == 2 and board[0][2] == 2)  : gagne = 2
  79.         elif (board[0][0] == 2 and board[1][0] == 2 and board[2][0] == 2)  : gagne = 2
  80.         elif (board[0][0] == 2 and board[1][1] == 2 and board[2][2] == 2)  : gagne = 2
  81.         elif (board[0][1] == 2 and board[1][1] == 2 and board[2][1] == 2)  : gagne = 2
  82.         elif (board[0][2] == 2 and board[1][2] == 2 and board[2][2] == 2)  : gagne = 2
  83.         elif (board[2][0] == 2 and board[2][1] == 2 and board[2][2] == 2)  : gagne = 2
  84.         elif (board[1][0] == 2 and board[1][1] == 2 and board[1][2] == 2)  : gagne = 2
  85.         elif (board[2][0] == 2 and board[1][1] == 2 and board[0][2] == 2)  : gagne = 2
  86.         elif gagne == 0:
  87.                 for ligne in board:
  88.                         for colonne in ligne :
  89.                                 if colonne != 0 : flag += 1
  90.                 if flag == 9 : gagne = 3
  91.         return gagne
  92.  
  93.  
  94. player = 1 ; i = 0
  95.  
  96.  
  97. #Creation de la fenetre et initialisation du board
  98. fenetre1 = Tk()
  99. canvas1 = Canvas(fenetre1,bg='white',height=300,width=300)
  100. canvas1.pack()
  101. init_board()
  102.  
  103.  
  104. while fini(board) == False:
  105.         canvas1.bind("<Button-1>", pointeur)
  106.         actualise(board)
  107.         fenetre1.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement