Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env python
- # -*- coding: utf-8 -*-
- #####################################
- ######## JEU DE MORPION V1.0 ########
- #####################################
- J = True # joeur booléen
- T = 3 # taille de la grille
- H = 60 # hauteur des cases
- M = [T * [False] for x in range(T)] # représentation de la grille
- empty_boxes = T*T # nombre de cases vides
- winner = 0 # un gagnant ?
- PVP = 0 # Player VS Player or VS Comp ?
- ## tour de chaque joueur ##
- def joue(event):
- global J, winner
- while not winner:
- 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
- winner = 1
- J = not J
- if not PVP :
- move = iaJoue(w)
- playOn(move)
- def iaJoue(w):
- ##### est-ce que l'IA peut gagner ? Win !
- # une ligne
- for x in range(T):
- if M[x].count('O') is T-1 and False in M[x] : return(x, M[x].index(False))
- # une colonne
- for c in range(T):
- B = []
- for r in range(T):
- B.append(M[r][c])
- if B.count('O') is T-1 and False in B : return(B.index(False), c)
- # diagonale l-r
- B = []
- for x in range(T):
- B.append(M[x][x])
- if B.count('O') is T-1 and False in B : return(B.index(False), B.index(False))
- # diagonale r-l
- B = []
- c = T-1
- for r in range(T):
- B.append(M[r][c])
- c = c-1
- if B.count('O') is T-1 and False in B : return(B.index(False), B.index(False))
- ###### est-ce que le joueur peut gagner ? Block !
- # une ligne
- for x in range(T):
- if M[x].count('X') is T-1 and False in M[x] : return(x, M[x].index(False))
- # une colonne
- for c in range(T):
- B = []
- for r in range(T):
- B.append(M[r][c])
- if B.count('X') is T-1 and False in B : return(B.index(False), c)
- # diagonale l-r
- B = []
- for x in range(T):
- B.append(M[x][x])
- if B.count('X') is T-1 and False in B : return(B.index(False), B.index(False))
- # diagonale r-l
- B = []
- c = T-1
- for r in range(T):
- B.append(M[r][c])
- c = c-1
- if B.count('X') is T-1 and False in B : return(B.index(False), B.index(False))
- ##### est-ce qu'il y a un coin libre ? Move on corner !
- # upper left
- if not M[0][0] : return (0, 0)
- # upper right
- if not M[0][T-1] : return (0, T-1)
- # lower left
- if not M[T-1][0] : return (T-1, 0)
- # lower right
- if not M[T-1][T-1] : return (T-1, T-1)
- ##### est-ce que le centre est libre ? Move on center !
- if T%2:
- if not M[T/2][T/2] : return(M[T/2+1], [T/2+1])
- ##### sinon il y a forcément un côté libre. Move on side !
- for r in range(T):
- for c in range(T):
- if not M[r][c] : return(r, c)
- ## l'ordinateur joue ##
- def playOn(move):
- global J, winner
- raw = move[0]
- column = move[1]
- print Cell.R, Cell.C
- print M
- if J : fais_x(???)
- else : fais_o(???)
- s = symbole(J)
- M[raw][column] = s
- if gagnant(s):
- info['text'] = 'joueur %s gagne' % s
- winner = 1
- J = not J
- ## dessine symbole X ##
- 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')
- ## dessine symbole O ##
- def fais_o(w) : w.create_oval(10, 10, 55, 55, width = 10)
- ## retourne symbole joueur actuel ##
- def symbole(j) : return ['O', 'X'][j]
- ## vérifie si coup joué est gagnant ##
- def gagnant(s):
- global info, empty_boxes
- # GAME OVER
- empty_boxes = empty_boxes-1
- if empty_boxes == 0 : info ['text'] = 'GAME OVER'
- # raw strike
- for r in range(T):
- B = []
- for c in range(T):
- B.append(M[r][c])
- if B.count(s) is T : return True
- # column strike
- for c in range(T):
- B = []
- for r in range(T):
- B.append(M[r][c])
- if B.count(s) is T : return True
- # diagonal l-r strike
- B = []
- for x in range(T):
- B.append(M[x][x])
- if B.count(s) is T : return True
- # diagonal r-l strike
- B = []
- c = T-1
- for r in range(T):
- B.append(M[r][c])
- c = c-1
- if B.count(s) is T : return True
- # ~~~~~~~~ PROGRAMME PRINCIPAL ~~~~~~~~ #
- 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 # localisation de chaque cellule
- grille.pack()
- stop = Button(morpion, text='ASSEZ!', command = morpion.destroy)
- stop.pack()
- info = Label(morpion)
- info.pack()
- morpion.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement