IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Programmation multimédia/Jeux Python Discussion :

Le jeu de pente programmation python


Sujet :

Programmation multimédia/Jeux Python

  1. #1
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2020
    Messages
    2
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Maine et Loire (Pays de la Loire)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2020
    Messages : 2
    Par défaut Le jeu de pente programmation python
    Bonjour,
    Je suis en deuxième année de licence maths informatiques et j'ai un projet à réaliser en python.
    Ce projet consiste a programmer un jeu de pente.
    Ce jeu consiste à aligner 5 pions de la même couleur ou bien capturer 5 paire de la même couleur.
    Il faut faire une IA
    j'ai déjà commencé à coder.
    Mon problème est au niveau du placage de pion et plus précisément coder les stratégies
    Voici mon code :

    Player.py
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    # -*- coding: utf-8 -*-
    """
    Created on Mon Apr  6 13:26:32 2020
     
    @author: Nguyen
    """
    from Model import*
    from random import*
     
    class SamplePlayer:
        def __init__(self):
            self.board = []
     
        def initGame(self,color):
            self.color=color
            self.board = []
            for i in range(19):
                self.board.append(19*[None])
            self.capturesPlayer = 0
            self.capturesAdv = 0
            self.turn = 1                   # Numéros du tour        
            self.currentMove = None
     
        def searchMove(self, moves):
            #Mise a jour du plateau du jeu d'après le coup de l'adversaire
            if (moves != []):
                self.apply(moves[0], self.color.next())
     
            if (self.turn == 1 and self.color == Color.White):
                self.currentMove = Move(0,0)
                self.turn += 1
            elif (self.turn == 1):
                found = False
                while (not found):
                    x = randint(-9, 9)
                    y = randint(-9, 9)
                    found = self.board[x+9][y+9] == None
                self.currentMove = Move(x,y)
                self.turn += 1
            elif (self.turn == 2 and self .color == Color.Black):
                found = False 
                while (not found):
                    x = randint(-9,9)
                    y = randint(-9,9)
                    found = (abs(x) > 2 or abs(y) >2) and self.board[x+9][y+9] == None
                self.currentMove = Move(x,y)
                self.turn += 1
    ### ligne rajoutée
     
            elif (self.turn == 3 and self .color == Color.Black):
                found = False 
                while (not found):
                    x = randint(-9,9)
                    y = randint(-9,9)
                    found = (abs(x) > 2 or abs(y) >2) and self.board[x+9][y+9] == None
                self.currentMove = Move(x,y)
                self.turn += 1
            elif (self.turn == 4 and self .color == Color.Black):
                found = False 
                while (not found):
                    x = randint(-9,9)
                    y = randint(-9,9)
                    found = (abs(x) > 2 or abs(y) >2) and self.board[x+9][y+9] == None
                self.currentMove = Move(x,y)
                self.turn += 1
     
            elif (self.turn == 5 and self .color == Color.Black):
                found = False 
                while (not found):
                    x = randint(-9,9)
                    y = randint(-9,9)
                    found = (abs(x) > 2 or abs(y) >2) and self.board[x+9][y+9] == None
                self.currentMove = Move(x,y)
                self.turn += 1
     
            elif (self.turn == 6 and self .color == Color.Black):
                found = False 
                while (not found):
                    x = randint(-9,9)
                    y = randint(-9,9)
                    found = (abs(x) > 2 or abs(y) >2) and self.board[x+9][y+9] == None
                self.currentMove = Move(x,y)
                self.turn += 1
     
            elif (self.turn == 7 and self .color == Color.Black):
                found = False 
                while (not found):
                    x = randint(-9,9)
                    y = randint(-9,9)
                    found = (abs(x) > 2 or abs(y) >2) and self.board[x+9][y+9] == None
                self.currentMove = Move(x,y)
                self.turn += 1
     
            elif (self.turn == 8 and self .color == Color.Black):
                found = False 
                while (not found):
                    x = randint(-9,9)
                    y = randint(-9,9)
                    found = (abs(x) > 2 or abs(y) >2) and self.board[x+9][y+9] == None
                self.currentMove = Move(x,y)
                self.turn += 1
     
     
            else:
                self.currentMove = None      # currentMove = coup à jouer
                self.movesAlign4 = []
                self.movesAlign3 = []
                self.movesAlign2 = []
                found = self.evalBoard(self.board)
                if (not found):
                    if (self.movesAlign4 != []):
                        n = randint(len(self.movesAlign4))
                        self.currentMove = self.movesAlign4[n]
    ##                elif (self.movesAlign3 != []):
    ##                    n= randint(len(self.movesAlign3))
    ##                    self.currentMove = self.movesAlign3[n]
    ##                elif (self.movesAlign2 != []):
    ##                    n= randint(len(self.movesAlign2))
    ##                    self.currentMove = self.movesAlign2[n]
     
                        #print('pass')   # A compléter
            self.apply(self.currentMove, self.color)
     
        def apply(self, move, color):
            i = move.x + 9
            j = move.y + 9
            self.board[i][j] = color
            for d in list(Direction):
                self.checkCapture(i, j, d, color)            
     
        def checkCapture(self, i, j, d, color):
            i1 = i + d.deltaX()
            j1 = j + d.deltaY()
            i2 = i + 2*d.deltaX()
            j2 = j + 2*d.deltaY()
            i3 = i + 3*d.deltaX()
            j3 = j + 3*d.deltaY()
            if (self.inBoard(i3, j3) and self.board[i1][j1] == color.next() and self.board[i2][j2] == color.next() and self.board[i3][j3] == color):
                self.board[i2][j2] = None
                self.board[i1][j1] = None
                if (color == self.color):
                    self.capturesPlayer += 1
                else:
                    self.capturesAdv += 1
     
        def inBoard(self, i, j):
            return 0 <= i < 19 and 0 <= j < 19
     
        def evalBoard(self, t):
            for i in range(19):
                for j in range(19):
                    if (t[i][j] == self.color):
                        d = Direction.E
                        while (d != Direction.W):
                            found = self.evalDir(t, i, j, d)
                            if (found):
                                return True
                            else:
                                d = d.next()
                    elif (t[i][j] == self.color.next()):
                        d = Direction.E
                        while (d != Direction.W):
                            found = self.evalDir(t, i, j, d)
                            if (found):
                                return True
                            else:
                                d = d.next()
                     #print('pass')   # A compléter !
     
     
        def evalDir(self, t, i, j, d):
            i1, j1 = self.nextCoord(i, j, d)
            i2, j2 = self.nextCoord(i1, j1, d)
            i3, j3 = self.nextCoord(i2, j2, d)
            i4, j4 = self.nextCoord(i3, j3, d)
            i_1, j_1 = self.nextCoord(i, j, d.next())
     
            # _ o o o o _
            if (self.inBoard(i3,j3) and t[i3][j3] == t[i2][j2] == t[i1][j1] == self.color):
                if (self.inBoard(i_1, j_1) and t[i_1][j_1] == None):
                    self.currentMove = Move(i_1 - 9, j_1 - 9)
                    return True
                elif (self.inBoard(i4,j4) and t[i4][j4] == None):
                    self.currentMove = Move(i4 - 9, j4 - 9)
                    return True
            # _ o _ o o
            elif (self.inBoard(i3, j3) and t[i3][j3] == t[i2][j2] == self.color and t[i1][j1] == None):
                self.movesAlign4.append(Move(i1-9,j1-9))
            # _ o o _
            elif (self.inBoard(i2, j2) and t[i1][j1] == self.color and t[i2][j2] == None):
                self.movesAlign3.append(Move(i2-9,j2-9))
            # O_O_O
            elif (self.inBoard(i2, j2) and t[i1][j1] == t[i3][j3] ==  self.color and t[i2][j2] == None):
                self.movesAlign3.append(Move(i2-9,j2-9))
     
        def nextCoord(self, i, j, d):
            return i + d.deltaX(), j + d.deltaY()
     
    # Liste de tous les coups possibles 
    #        moves = [(Move(7, 7), 100), Move(4, 3), 50),]
     
    class RandomPlayer:
        def __init__(self):
            self.color=None
            self.freeMove=[]
            self.currentMove=None
     
        def initGame(self,color):
            self.color=color
            self.freeMove=[]
     
            for i in range(-9,10):
                for j in range(-9,10):
                    self.freeMove.append(Move(i,j))
            self.freeMove.remove(Move(0,0))
            self.freeMove.remove(Move(0,3))
            self.freeMove.remove(Move(0,-3))
            shuffle(self.freeMove) #pour modifier l'ordre (aléatoire)
            self.freeMove=[Move(0,0),Move(0,3),Move(0,-3)]+self.freeMove
            self.currentMove=None
     
        def searchMove(self,moves): #pour ne pas mettre 2 fois le même pion dans la liste
            if(moves!=[]):
                self.freeMove.remove(moves[0])
            self.currentMove=self.freeMove[0]
            del self.freeMove[0]
    PenteRun.py
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    from Player import *
    from PenteView import *
     
    view=PenteView()
    player1=RandomPlayer()
    player2=SamplePlayer()
    view.setPlayers(player1,player2)
    view.setDelay(0.2)
    view.mainloop()
    Merci d'avance pour vos réponse

  2. #2
    Expert éminent
    Homme Profil pro
    Architecte technique retraité
    Inscrit en
    Juin 2008
    Messages
    21 714
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Manche (Basse Normandie)

    Informations professionnelles :
    Activité : Architecte technique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2008
    Messages : 21 714
    Par défaut
    Salut,

    Citation Envoyé par EnzoLL Voir le message
    Il faut faire une IA
    j'ai déjà commencé à coder, je vous ai mis le code en piece jointe
    Mon problème est au niveau du placage de pion et plus précisément coder les stratégies
    Une IA, c'est juste un algo.
    La bonne rubrique pour çà est Algorithmique dans laquelle, il y a aussi des forums où poser des questions (mais un algo. c'est pas du code, donc il va falloir décrire un peu ce que vous voulez).

    - W
    Architectures post-modernes.
    Python sur DVP c'est aussi des FAQs, des cours et tutoriels

  3. #3
    Membre Expert

    Homme Profil pro
    Ingénieur calcul scientifique
    Inscrit en
    Mars 2013
    Messages
    1 229
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Ingénieur calcul scientifique

    Informations forums :
    Inscription : Mars 2013
    Messages : 1 229
    Par défaut
    Bonsoir.

    1) Il faut expliquer mieux le jeu de pente, car perso je ne connais pas.
    2) Mettre le code directement dans ton message, entre balise CODE (le bouton # quand tu rédiges le message), en l'ayant au préalable réduit pour qu'il illustre bien ton problème tout en étant le plus petit possible.

  4. #4
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2020
    Messages
    2
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Maine et Loire (Pays de la Loire)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2020
    Messages : 2
    Par défaut
    Le jeu de pente est jeu sur plateau, qui se joue a deux. Pour gagner une partie il y a deux possibilités :
    - en ayant (au minimum) cinq pierres alignées
    - en capturant cinq paires (au minimum)
    Pour commencer le premier doit placer sa pierre au milieu. Puis le second doit placer sa pierre hors d'un cadre déjà prédéfinie.
    Pour capturer une pierre il faut qu'a chaque fois que votre adversaire a deux pierres côte à côte (et deux seulement), elles peuvent être capturées. La paire peut être capturée si l'attaquant positionne une pierre de telle sorte que la paire de l'adversaire soit « ceinturée »
    En ce qui concerne le code j'ai oublié de vous mettre tout le dossier que le prof nous a donner
    Player.py
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
     
    from Model import*
    from random import*
     
    class SamplePlayer:
        def __init__(self):
            self.board = []
     
        def initGame(self,color):
            self.color=color
            self.board = []
            for i in range(19):
                self.board.append(19*[None])
            self.capturesPlayer = 0
            self.capturesAdv = 0
            self.turn = 1                  
            self.currentMove = None
     
        def searchMove(self, moves):
            #Mise a jour du plateau du jeu d'après le coup de l'adversaire
            if (moves != []):
                self.apply(moves[0], self.color.next())
     
            if (self.turn == 1 and self.color == Color.White):
                self.currentMove = Move(0,0)
                self.turn += 1
            elif (self.turn == 1):
                found = False
                while (not found):
                    x = randint(-9, 9)
                    y = randint(-9, 9)
                    found = self.board[x+9][y+9] == None
                self.currentMove = Move(x,y)
                self.turn += 1
            elif (self.turn == 2 and self .color == Color.White):
                found = False 
                while (not found):
                    x = randint(-9,9)
                    y = randint(-9,9)
                    found = (abs(x) > 2 or abs(y) >2) and self.board[x+9][y+9] == None
                self.currentMove = Move(x,y)
                self.turn += 1
            else:
                self.currentMove = None
                self.movesAlign4 = []
                self.movesAlign3 = []
                self.movesAlign2 = []
                found = self.evalBoard(self.board)
                if (not found):
                    if (self.movesAlign4 != []):
                        n = randint(len(self.movesAlign4))
                        self.currentMove = self.movesAlign4[n]
                    elif (self.movesAlign3 != []):
                        print('pass')   # A compléter
            self.apply(self.currentMove, self.color)
     
        def apply(self, move, color):
            i = move.x + 9
            j = move.y + 9
            self.board[i][j] = color
            for d in list(Direction):
                self.checkCapture(i, j, d, color)            
     
        def checkCapture(self, i, j, d, color):
            i1 = i + d.deltaX()
            j1 = j + d.deltaY()
            i2 = i + 2*d.deltaX()
            j2 = j + 2*d.deltaY()
            i3 = i + 3*d.deltaX()
            j3 = j + 3*d.deltaY()
            if (self.inBoard(i3, j3) and self.board[i1][j1] == color.next() and self.board[i2][j2] == color.next() and self.board[i3][j3] == color):
                self.board[i2][j2] = None
                self.board[i1][j1] = None
                if (color == self.color):
                    self.capturesPlayer += 1
                else:
                    self.capturesAdv += 1
     
        def inBoard(self, i, j):
            return 0 <= i < 19 and 0 <= j < 19
     
        def evalBoard(self, t):
            for i in range(19):
                for j in range(19):
                    if (t[i][j] == self.color):
                        d = Direction.E
                        while (d != Direction.W):
                            found = self.evalDir(t, i, j, d)
                            if (found):
                                return True
                            else:
                                d = d.next()
                    elif (t[i][j] == self.color.next()):
                        print('pass')   # A compléter 
     
     
        def evalDir(self, t, i, j, d):
            i1, j1 = self.nextCoord(i, j, d)
            i2, j2 = self.nextCoord(i1, j1, d)
            i3, j3 = self.nextCoord(i2, j2, d)
            i4, j4 = self.nextCoord(i3, j3, d)
            i_1, j_1 = self.nextCoord(i, j, d.next())
     
            # _ o o o o _
            if (self.inBoard(i3,j3) and t[i3][j3] == t[i2][j2] == t[i1][j1] == self.color):
                if (self.inBoard(i_1, j_1) and t[i_1][j_1] == None):
                    self.currentMove = Move(i_1 - 9, j_1 - 9)
                    return True
                elif (self.inBoard(i4,j4) and t[i4][j4] == None):
                    self.currentMove = Move(i4 - 9, j4 - 9)
                    return True
            # _ o _ o o
            elif (self.inBoard(i3, j3) and t[i3][j3] == t[i2][j2] == self.color and t[i1][j1] == None):
                self.movesAlign4.append(Move(i1-9,j1-9))
            # _ o o _
            elif (self.inBoard(i2, j2) and t[i1][j1] == self.color and t[i2][j2] == None):
                self.movesAlign3.append(Move(i2-9,j2-9))
            # .........
     
        def nextCoord(self, i, j, d):
            return i + d.deltaX(), j + d.deltaY()
     
    # Liste de tous les coups possibles 
    #        moves = [(Move(7, 7), 100), Move(4, 3), 50),]
     
    class RandomPlayer:
        def __init__(self):
            self.color=None
            self.freeMove=[]
            self.currentMove=None
     
        def initGame(self,color):
            self.color=color
            self.freeMove=[]
     
            for i in range(-9,10):
                for j in range(-9,10):
                    self.freeMove.append(Move(i,j))
            self.freeMove.remove(Move(0,0))
            self.freeMove.remove(Move(0,3))
            self.freeMove.remove(Move(0,-3))
            shuffle(self.freeMove) #pour modifier l'ordre (aléatoire)
            self.freeMove=[Move(0,0),Move(0,3),Move(0,-3)]+self.freeMove
            self.currentMove=None
     
        def searchMove(self,moves): #pour ne pas mettre 2 fois le même pion dans la liste
            if(moves!=[]):
                self.freeMove.remove(moves[0])
            self.currentMove=self.freeMove[0]
            del self.freeMove[0]
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     from Player import *
    from PenteView import *
     
    view=PenteView()
    player1=RandomPlayer()
    player2=SamplePlayer()
    view.setPlayers(player1,player2)
    view.setDelay(0.2)
    view.mainloop()
    Images attachées Images attachées   

Discussions similaires

  1. Programmation jeu de la vie Python
    Par Benecile dans le forum Général Python
    Réponses: 12
    Dernier message: 29/05/2015, 07h02
  2. Aide programmation Python; Jeu de blackjack
    Par toniobuteau dans le forum Général Python
    Réponses: 1
    Dernier message: 03/10/2012, 06h21
  3. programmation d'un jeu de poker en python
    Par mnspc dans le forum Général Python
    Réponses: 4
    Dernier message: 10/04/2008, 21h33
  4. probleme pour un jeu de dames en python
    Par doudou152 dans le forum Général Python
    Réponses: 7
    Dernier message: 22/04/2005, 14h53
  5. [Lien]erreur dans mon programme python
    Par durnambule dans le forum Général Python
    Réponses: 11
    Dernier message: 29/01/2004, 14h59

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo