1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| #!/usr/bin/env python
# -*- coding: UTF-8 -*-
J = True
T = 3
H = 60
M = [T * [False] for x in range(T)]
def joue(event) :
global J
w = event.widget
if M[w.R][w.C] :
return
if J :
fais_x(w)
else :
fais_o(w)
s = symbole(J)
M[w.R][w.C] = s
if gagnant(s) :
info['text'] = 'joueur %s gagne' % s
J = not J
def fais_x(w) :
w.create_polygon(10, 10, 20, 10, 55, 45, 55, 55, 45, 55, 10, 20, fill = 'red')
w.create_polygon(10, 55, 10, 45, 45, 10, 55, 10, 55, 20, 20, 55, fill = 'red')
def fais_o(w) : w.create_oval(10, 10, 55, 55, width = 10)
def symbole(j) : return ['O', 'X'][j]
def gagnant(s) :
global info
for x in range(T) :
if [M[x][0], M[x][1], M[x][2]].count(s) is T :
return True
if [M[0][x], M[1][x], M[2][x]].count(s) is T :
return True
if [M[0][0], M[1][1], M[2][2]].count(s) is T :
return True
if [M[0][2], M[1][1], M[2][0]].count(s) is T :
return True
if False not in M[0] + M[1] + M[2] :
info ['text'] = 'GAME OVER'
from Tkinter import *
morpion = Tk()
morpion.title('MORPION 1.0')
grille = Frame(morpion)
for R in range(T) :
for C in range(T) :
Cell = Canvas(grille, bg = 'light grey', width = H, height = H)
Cell.bind("<Button-1>", joue)
Cell.grid(row = R, column = C)
Cell.R, Cell.C = R, C
grille.pack()
stop = Button(morpion, text='ASSEZ', command = morpion.destroy)
stop.pack()
info = Label(morpion)
info.pack()
morpion.mainloop() |
Partager