Partage
  • Partager sur Facebook
  • Partager sur Twitter

Aide pour détection Morpion tk

projet d'isn

    19 mai 2015 à 9:05:44

    bonjour, je voudrais introduire la détection d'une égalite ou d'une victoire sur mon programme mais je ne sais pas comment faire

    Voici mon code:

    # -*- coding: utf8 -*-
    
    import random
    from Tkinter import *
    
    joueur1=raw_input("Joueur1 :") # Demande à l'utilisateur d'entrer son nom
    joueur2=raw_input("Joueur2 :") 
    
    def recommencer():
        "Permet de recommencer au debut"
        grille.delete(ALL)
        grille.create_image(230, 260, image=photo)
        plateau(grille)
        grid[:] = [[ None for col in range(NB_CASES)] for row in range(NB_CASES)]
        
    def Nom():
        joueur1=input("Joueur1 :")
        joueur2=input("Joueur2 :")
    def Option():
        "Les option pour le jeu"
        button_restart = Button(fenetre, text='recommencer', command=recommencer)
        button_restart.pack()    
        button_Joueur = Button(fenetre, text='Nom_Joueur', command=Nom)
        button_Joueur.pack()
    
    	
    def from_screen_to_grid(x, y):
        """Retourne la position dans la grille du point donné à l'écran.
        Si le point est en dehors de la grille, retourne None.
        """
        pos_x = (x - MARGIN_X) // TAILLE_CASE
        if pos_x < 0 or pos_x >= NB_CASES:
            pos_x = None
        pos_y = (y - MARGIN_Y) // TAILLE_CASE
        if pos_y < 0 or pos_y >= NB_CASES:
            pos_y = None
        return pos_x, pos_y
         
         
    def on_click(evenement):
        "Traite le click de souris sur la grille."
        grid_x, grid_y = from_screen_to_grid(evenement.x, evenement.y)
         
        if grid_x is None or grid_y is None:
            return
         
        if grid[grid_y][grid_x] is not None:
            return
             
        global player_turn
        if player_turn == 0:
            grid[grid_y][grid_x] = "CROIX"
            dessine_croix(grid_x, grid_y)
            player_turn = 1
        else: # player_turn == 1
            grid[grid_y][grid_x] = "ROND"
            dessine_rond(grid_x, grid_y)
            player_turn = 0
     
     
    def plateau(grille):
        "Dessine le plateau de jeu"
        for i in range(NB_CASES + 1):
            grille.create_line(MARGIN_X + TAILLE_CASE * i,
                               MARGIN_Y,
                               MARGIN_X + TAILLE_CASE * i,
                               MARGIN_Y + NB_CASES * TAILLE_CASE,
                               fill="black")
            grille.create_line(MARGIN_X,
                               MARGIN_Y + TAILLE_CASE * i,
                               MARGIN_X + NB_CASES * TAILLE_CASE,
                               MARGIN_Y + TAILLE_CASE * i,
                               fill="black")
      
     
    def dessine_croix(grid_x, grid_y):
        "Permet de tracer une croix dans le canvas aux coordonnées de la grille grid_x, grid_y"
        x = grid_x * TAILLE_CASE + MARGIN_X
        y = grid_y * TAILLE_CASE + MARGIN_Y
        TOP_LEFT = TAILLE_CASE * 0.8
        BOTTOM_RIGHT = TAILLE_CASE * 0.2
        grille.create_line(x + TOP_LEFT, y + TOP_LEFT,
                           x + BOTTOM_RIGHT, y + BOTTOM_RIGHT,
                           fill="blue", width=5)
        grille.create_line(x + BOTTOM_RIGHT, y + TOP_LEFT,
                           x + TOP_LEFT, y + BOTTOM_RIGHT,
                           fill="blue", width=5)
    def gagant(tab):
    	for i in range(NB_CASES + 1):
    	
    	
        
    def dessine_rond(grid_x, grid_y):
        "Permet de créer le rond dans le canvas aux coordonnées de la grille grid_x, grid_y"
        x = grid_x * TAILLE_CASE + MARGIN_X
        y = grid_y * TAILLE_CASE + MARGIN_Y
        TOP_LEFT = TAILLE_CASE * 0.8
        BOTTOM_RIGHT = TAILLE_CASE * 0.2
        grille.create_oval(x + BOTTOM_RIGHT, y + TOP_LEFT,
                           x + TOP_LEFT , y + BOTTOM_RIGHT,
                           fill="red", width=5)
     
     
     
     
    # Constantes :
    NB_CASES = 3
    TAILLE_CASE = 140
    MARGIN_X, MARGIN_Y = 10, 10
     
    player_turn = 0 # 0 for player 1, 1 for player 2
    grid = [[ None for col in range(NB_CASES)] for row in range(NB_CASES)]
     
    fenetre = Tk()
    fenetre.wm_title("Morpion")
    fenetre.focus_set()
    
    photo = PhotoImage(file="ma_photo.gif")
    grille = Canvas(fenetre, height=500, width=500, bg="white" )
    grille.create_image(230, 260, image=photo)
    grille.bind("<Button-1>", on_click)
    grille.pack(side=LEFT)
    plateau(grille)
    
    button_restart = Button(fenetre, text='recommencer', command=recommencer)
    button_restart.pack()    
    button_Joueur = Button(fenetre, text='Nom_Joueur', command=Nom)
    button_Joueur.pack()
    boutonQuitter = Button(fenetre, text = 'Quitter', command = fenetre.quit)
    boutonQuitter.pack()
    
    fenetre.mainloop()
    

    Merci de votre aide :)

    • Partager sur Facebook
    • Partager sur Twitter
      19 mai 2015 à 9:17:52

      Salut,

      Je dirai que pour réaliser ton test, il faudrait qu'a chaque coup d'un joueur tu appelles une méthode vérifiant toutes les cases, et pour chaque case qui vérifie toutes les possibilités de victoire. Si une condition est trouvée alors le joueur * a gagné, sinon on continue la partie

      • Partager sur Facebook
      • Partager sur Twitter
        19 mai 2015 à 9:21:45

        merci de ta réponce,il faut donc une fontion qui definisse celà?
        • Partager sur Facebook
        • Partager sur Twitter
          19 mai 2015 à 9:23:33

          Ce serait ta fonction gagant ( que tu voulais appeler gagnant je pense :p )  que tu appellerai a chaque coup d'un joueur je pense
          • Partager sur Facebook
          • Partager sur Twitter
            19 mai 2015 à 9:27:19

            d'accord je vais réflechir à comment faire cela:)
            • Partager sur Facebook
            • Partager sur Twitter
              19 mai 2015 à 9:36:00

              Ca marche, hésites pas a revenir par ici si tu as des questions ;)
              • Partager sur Facebook
              • Partager sur Twitter
                19 mai 2015 à 17:11:28

                re,alors j'ai deja mit cela mais apres je ne sais pas comment faire:

                def gagnant(tab):
                	for i in range(NB_CASES + 1):1



                • Partager sur Facebook
                • Partager sur Twitter

                Aide pour détection Morpion tk

                × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
                × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
                • Editeur
                • Markdown