Advertisement
Guest User

Untitled

a guest
Jan 14th, 2013
512
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.45 KB | None | 0 0
  1. #! /usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. #####################################
  5. ######## JEU DE MORPION V1.0 ########
  6. #####################################
  7.  
  8. J = True                                # joeur booléen
  9. T = 3                                   # taille de la grille
  10. H = 60                                  # hauteur des cases
  11. M = [T * [False] for x in range(T)] # représentation de la grille
  12. empty_boxes = T*T                       # nombre de cases vides
  13. winner = 0                              # un gagnant ?
  14. PVP = 0                                 # Player VS Player or VS Comp ?
  15.  
  16. ## tour de chaque joueur ##
  17. def joue(event):
  18.     global J, winner   
  19.     while not winner:
  20.         w = event.widget
  21.         if M[w.R][w.C]:
  22.             return
  23.         if J:
  24.             fais_x(w)
  25.         else:
  26.             fais_o(w)
  27.         s = symbole(J)
  28.         M[w.R][w.C] = s
  29.         if gagnant(s):
  30.             info['text'] = 'joueur %s gagne' % s
  31.             winner = 1
  32.         J = not J
  33.         if not PVP :
  34.             move = iaJoue(w)
  35.             playOn(move)
  36.  
  37. def iaJoue(w):
  38.     ##### est-ce que l'IA peut gagner ? Win !
  39.     # une ligne
  40.     for x in range(T):
  41.         if M[x].count('O') is T-1 and False in M[x] : return(x, M[x].index(False))
  42.     # une colonne
  43.     for c in range(T):
  44.         B = []
  45.         for r in range(T):
  46.             B.append(M[r][c])
  47.         if B.count('O') is T-1 and False in B : return(B.index(False), c)
  48.     # diagonale l-r
  49.     B = []
  50.     for x in range(T):
  51.         B.append(M[x][x])
  52.     if B.count('O') is T-1 and False in B : return(B.index(False), B.index(False))
  53.     # diagonale r-l
  54.     B = []
  55.     c = T-1
  56.     for r in range(T):
  57.         B.append(M[r][c])
  58.         c = c-1
  59.     if B.count('O') is T-1 and False in B : return(B.index(False), B.index(False))
  60.     ###### est-ce que le joueur peut gagner ? Block !
  61.     # une ligne
  62.     for x in range(T):
  63.         if M[x].count('X') is T-1 and False in M[x] : return(x, M[x].index(False))
  64.     # une colonne
  65.     for c in range(T):
  66.         B = []
  67.         for r in range(T):
  68.             B.append(M[r][c])
  69.         if B.count('X') is T-1 and False in B : return(B.index(False), c)
  70.     # diagonale l-r
  71.     B = []
  72.     for x in range(T):
  73.         B.append(M[x][x])
  74.     if B.count('X') is T-1 and False in B : return(B.index(False), B.index(False))
  75.     # diagonale r-l
  76.     B = []
  77.     c = T-1
  78.     for r in range(T):
  79.         B.append(M[r][c])
  80.         c = c-1
  81.     if B.count('X') is T-1 and False in B : return(B.index(False), B.index(False))
  82.     ##### est-ce qu'il y a un coin libre ? Move on corner !
  83.     # upper left
  84.     if not M[0][0] : return (0, 0)
  85.     # upper right
  86.     if not M[0][T-1] : return (0, T-1)
  87.     # lower left
  88.     if not M[T-1][0] : return (T-1, 0)
  89.     # lower right
  90.     if not M[T-1][T-1] : return (T-1, T-1)
  91.     ##### est-ce que le centre est libre ? Move on center !
  92.     if T%2:
  93.         if not M[T/2][T/2] : return(M[T/2+1], [T/2+1])
  94.     ##### sinon il y a forcément un côté libre. Move on side !
  95.     for r in range(T):
  96.         for c in range(T):
  97.             if not M[r][c] : return(r, c)
  98.  
  99. ## l'ordinateur joue ##
  100. def playOn(move):
  101.     global J, winner
  102.     raw = move[0]
  103.     column = move[1]
  104.     print Cell.R, Cell.C
  105.     print M
  106.     if J : fais_x(???)
  107.     else : fais_o(???)
  108.     s = symbole(J)
  109.     M[raw][column] = s
  110.     if gagnant(s):
  111.         info['text'] = 'joueur %s gagne' % s
  112.         winner = 1
  113.     J = not J
  114.  
  115. ## dessine symbole X ##
  116. def fais_x(w):
  117.     w.create_polygon(10, 10, 20, 10, 55, 45, 55, 55, 45, 55, 10, 20, fill = 'red')
  118.     w.create_polygon(10, 55, 10, 45, 45, 10, 55, 10, 55, 20, 20, 55, fill = 'red')
  119.  
  120. ## dessine symbole O ##
  121. def fais_o(w) : w.create_oval(10, 10, 55, 55, width = 10)
  122.  
  123. ## retourne symbole joueur actuel ##
  124. def symbole(j) : return ['O', 'X'][j]
  125.  
  126. ## vérifie si coup joué est gagnant ##
  127. def gagnant(s):
  128.     global info, empty_boxes
  129.     # GAME OVER
  130.     empty_boxes = empty_boxes-1
  131.     if empty_boxes == 0 : info ['text'] = 'GAME OVER'
  132.     # raw strike
  133.     for r in range(T):
  134.         B = []
  135.         for c in range(T):
  136.             B.append(M[r][c])
  137.         if B.count(s) is T : return True
  138.     # column strike
  139.     for c in range(T):
  140.         B = []
  141.         for r in range(T):
  142.             B.append(M[r][c])
  143.         if B.count(s) is T : return True
  144.     # diagonal l-r strike
  145.     B = []
  146.     for x in range(T):
  147.         B.append(M[x][x])
  148.     if B.count(s) is T : return True
  149.     # diagonal r-l strike
  150.     B = []
  151.     c = T-1
  152.     for r in range(T):
  153.         B.append(M[r][c])
  154.         c = c-1
  155.     if B.count(s) is T : return True
  156.  
  157. # ~~~~~~~~ PROGRAMME PRINCIPAL ~~~~~~~~ #
  158.  
  159. from Tkinter import *
  160.  
  161. morpion = Tk()
  162. morpion.title('MORPION 1.0')
  163. grille = Frame(morpion)
  164.  
  165. for R in range(T):
  166.     for C in range(T):
  167.         Cell = Canvas(grille, bg = 'light grey', width = H, height = H)
  168.         Cell.bind("<Button-1>", joue)
  169.         Cell.grid(row = R, column = C)
  170.         Cell.R, Cell.C = R, C       # localisation de chaque cellule
  171.  
  172. grille.pack()
  173. stop = Button(morpion, text='ASSEZ!', command = morpion.destroy)
  174. stop.pack()
  175. info = Label(morpion)
  176. info.pack()
  177.  
  178. morpion.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement