Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- import os, time
- from Tkinter import *
- from random import randrange
- board = [[0,0,0],[0,0,0],[0,0,0]] # 3*3
- 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)
- liste_coo = [coo1,coo2,coo3,coo4,coo5,coo6,coo7,coo8,coo9] # Coordonnees centre des 9 cases
- # Trace 4 lignes dessiant le board
- def init_board() :
- draw_line(0,100,300,100)
- draw_line(0,200,300,200)
- draw_line(100,0,100,300)
- draw_line(200,0,200,300)
- # Trace une ligne de (x1,y1) a (x2,y2)
- def draw_line(x1,y1,x2,y2):
- canvas1.create_line(x1,y1,x2,y2,width=5,fill='black')
- # Dessine une Croix
- def draw_cross(x1,y1):
- x1 = int(x1)
- y1 = int(y1)
- draw_line(x1-30,y1-30,x1+30,y1+30)
- draw_line(x1+30,y1-30,x1-30,y1+30)
- # Dessine un Rond
- def draw_cercle(x1,y1):
- canvas1.create_oval(x1-36, y1-36, x1+36, y1+36, width = 4)
- # 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
- def actualise(liste):
- global i
- j = 0
- if i == 1 :
- for ligne in liste:
- for colonne in ligne: # Redessine tout le board
- if colonne == 1 : draw_cross(liste_coo[j][0],liste_coo[j][1])
- elif colonne == 2 : draw_cercle(liste_coo[j][0],liste_coo[j][1])
- j+=1
- i = 0
- def pointeur(event):
- u = 0
- global i
- global player
- # U est un flag. Si on a bien changer le board et que le coup est valide il passe a 1.
- # player represent le numero de joueur pour alterner les tours. U est necessaire pour alterner le joueur si seulement le coup est legal.
- # 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.
- if (event.x <= 100) and (event.y <= 100) and board[0][0] == 0 : board[0][0] = player ; i = 1 ; u = 1
- 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
- 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
- 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
- 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
- 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
- elif (event.x > 200) and (event.y <= 100) and board[0][2] == 0 : board[0][2] = player ; i = 1 ; u = 1
- 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
- elif (event.x > 200) and (event.y > 200) and board [2][2] == 0 : board[2][2] = player ; i = 1 ; u = 1
- if u == 1 :
- if player == 1 : player = 2
- else: player = 1
- def fini(board): # return gagne. 1 = Victoire j1 ; 2 = Victoire j2 ; 3 = match nul ; 0 = pas fini
- gagne = 0 ; flag = 0
- if (board[0][0] == 1 and board[0][1] == 1 and board[0][2] == 1) : gagne = 1
- elif (board[0][0] == 1 and board[1][0] == 1 and board[2][0] == 1) : gagne = 1
- elif (board[0][0] == 1 and board[1][1] == 1 and board[2][2] == 1) : gagne = 1
- elif (board[0][1] == 1 and board[1][1] == 1 and board[2][1] == 1) : gagne = 1
- elif (board[0][2] == 1 and board[1][2] == 1 and board[2][2] == 1) : gagne = 1
- elif (board[2][0] == 1 and board[2][1] == 1 and board[2][2] == 1) : gagne = 1
- elif (board[1][0] == 1 and board[1][1] == 1 and board[1][2] == 1) : gagne = 1
- elif (board[2][0] == 1 and board[1][1] == 1 and board[0][2] == 1) : gagne = 1
- elif (board[0][0] == 2 and board[0][1] == 2 and board[0][2] == 2) : gagne = 2
- elif (board[0][0] == 2 and board[1][0] == 2 and board[2][0] == 2) : gagne = 2
- elif (board[0][0] == 2 and board[1][1] == 2 and board[2][2] == 2) : gagne = 2
- elif (board[0][1] == 2 and board[1][1] == 2 and board[2][1] == 2) : gagne = 2
- elif (board[0][2] == 2 and board[1][2] == 2 and board[2][2] == 2) : gagne = 2
- elif (board[2][0] == 2 and board[2][1] == 2 and board[2][2] == 2) : gagne = 2
- elif (board[1][0] == 2 and board[1][1] == 2 and board[1][2] == 2) : gagne = 2
- elif (board[2][0] == 2 and board[1][1] == 2 and board[0][2] == 2) : gagne = 2
- elif gagne == 0:
- for ligne in board:
- for colonne in ligne :
- if colonne != 0 : flag += 1
- if flag == 9 : gagne = 3
- return gagne
- player = 1 ; i = 0
- #Creation de la fenetre et initialisation du board
- fenetre1 = Tk()
- canvas1 = Canvas(fenetre1,bg='white',height=300,width=300)
- canvas1.pack()
- init_board()
- while fini(board) == False:
- canvas1.bind("<Button-1>", pointeur)
- actualise(board)
- fenetre1.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement