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 :

Programmation bataille navale


Sujet :

Programmation multimédia/Jeux Python

  1. #1
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2015
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2015
    Messages : 7
    Points : 5
    Points
    5
    Par défaut Programmation bataille navale
    Bonjour,

    dans ma prépa, on nous a demandé de programmer un jeu de bataille navale sur Python.

    Par contre, le prof n'a pas été très gourmand en indication, se contentant de nous donner seulement les noms de fonctions:

    • fonction debut() : propose une partie.
    • fonction place_o() : permet à l'ordinateur de se choisir aléatoirement la position de ses navires.
    • fonction place_j() : permet au joueur de se choisir la position de ses navires avec affichage sur la grille joueur.
    • fonction tir_j() : le joueur énonce la case visée (lettre+chiffre), l'ordinateur annonce si c'est « à l'eau »,
    « touché » ou « coulé ». Le plateau adversaire est modifié pour faire apparaître le résultat.
    • fonction tir_o() : l'ordinateur énonce la case visée (lettre+chiffre), puis le plateau joueur est modifié pour faire
    apparaître le résultat. Le joueur n'intervient pas.
    • Fonction reste() : indique les bateaux restant pour chaque joueur.
    • fonction result() : lorsque tous les bateaux du joueur ou de l'ordinateur sont coulés, le gagnant est déclaré et
    félicité.
    Cependant, je n'arrive pas à trouver comment devrait fonctionner le code, j'arrive à faire un certain pseudo code pour certains fonctions, mais ça ne va pas plus loin.

    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
    def tir_j(x,y):
        if: #Si la case contient un bateau
            if: #Si il a détruit le bateau
                print('Coulé')
                #Supprimer bateau de la liste des restes
            else: #Si il ne l'a pas encore détruit
                print('Touché')
                #Faire passer la case colorié en bleu vers rouge
        else: #Si il n'a pas touché de bateau
            print("Dans l'eau")
     
    from random import
    def tir_o(x,y):
        #génère x,y aléatoire
        if: #Si la case contient un bateau
            if: #Si il a détruit le bateau
                print('Coulé')
                #Supprimer bateau de la liste des restes
            else: #Si il ne l'a pas encore détruit
                print('Touché')
                #Faire passer la case colorié en bleu vers rouge
        else: #Si il n'a pas touché de bateau
            print("Dans l'eau")
    Je voudrais donc savoir comment faire un lien entre les différentes fonctions, et quelques aides pour mieux comprendre comment définir les différentes fonctions.

    Merci d'avance.

  2. #2
    Expert éminent sénior
    Homme Profil pro
    Architecte technique retraité
    Inscrit en
    Juin 2008
    Messages
    21 276
    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 276
    Points : 36 761
    Points
    36 761
    Par défaut
    Salut,

    Citation Envoyé par zo-68 Voir le message
    Je voudrais donc savoir comment faire un lien entre les différentes fonctions, et quelques aides pour mieux comprendre comment définir les différentes fonctions.
    Il vous faut fabriquer une grille reflète l'emplacement des bateaux pour tout x, y du domaine de définition.
    "grille" pourrait être une liste de liste ou un dictionnaire (*).
    A vous de tester comment çà fonctionne dans les deux cas et choisir ce qui vous parait le mieux.
    (*) Il y a plein d'autres options mais quand on débute, pas la peine de se disperser.

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

  3. #3
    Expert éminent

    Avatar de deusyss
    Homme Profil pro
    Expert Python
    Inscrit en
    Mars 2010
    Messages
    1 659
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Expert Python
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2010
    Messages : 1 659
    Points : 8 442
    Points
    8 442
    Par défaut
    Salut,

    un Linux Pratique avait abordé la question pour info: http://boutique.ed-diamond.com/linux...14-lphs23.html
    "La connaissance appartient à tout le monde" (Film Antitrust)

    Tout le nécessaire pour Python:
    *News/Accueil *Cours/tutoriels *FAQ
    *Forums *Outils dédiés *Mon espace personnel avec mes Articles, Cours et Tutoriels

  4. #4
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2015
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2015
    Messages : 7
    Points : 5
    Points
    5
    Par défaut
    L'idée de la liste de la liste a été très intéressante. Par contre, dans mon code, j'ai quelques choses de très curieux.

    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
    from random import randint
     
    L=[["0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0"]]
     
    taillenavires=[2,3,3,4,5]
    nomdesnavires=["T","S","D","C","P"]
    nT=2
    nS=3
    nD=3
    nC=4
    nP=5
     
    def placementJ():
         L=[["0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0"]]
        i=1
        l=taillenavires[i]
        p=0
        while p==0:
            x=input("Choisir la ligne entre 1 et 9:")
            y=input("Choisir la colonne entre 1 et 9:")
            if L[x][y]=="0":
                p=1
        L[x][y]=nomdesnavires[i]
        k=0
        while k==0:
            u=input("Entre 0 pour placer à gauche, 1 à droite, 2 en haut, 3 en bas")
            if u==0 and x<10-l and L[x+1][y]=="0" and L[x+2][y]=="0":
                L[x+1][y]=nomdesnavires[i]
                L[x+2][y]=nomdesnavires[i]
                k=1
            if u==1 and x>l-1 and L[x-1][y]=="0" and L[x-2][y]=="0" :
                L[x-1][y]=nomdesnavires[i]
                L[x-2][y]=nomdesnavires[i]
                k=1
            if u==2 and y<10-l and L[x][y+1]=="0" and L[x][y+2]=="0": 
                L[x][y+1]=nomdesnavires[i]
                L[x][y+2]=nomdesnavires[i]
                k=1
            if u==3 and y>l-1 and L[x][y-1]=="0" and L[x][y-2]=="0":
                L[x][y-1]=nomdesnavires[i]
                L[x][y-2]=nomdesnavires[i]
                k=1
     
     
     
     
        i=2
        l=taillenavires[i]
        p=0
        while p==0:
            x=input("Choisir la ligne entre 1 et 9:")
            y=input("Choisir la colonne entre 1 et 9:")
            if L[x][y]=="0":
                p=1
        L[x][y]=nomdesnavires[i]
        k=0
        while k==0:
            u=input("Entre 0 pour placer à gauche, 1 à droite, 2 en haut, 3 en bas")
            if u==0 and x<10-l and L[x+1][y]=="0" and L[x+2][y]=="0":
                L[x+1][y]=nomdesnavires[i]
                L[x+2][y]=nomdesnavires[i]
                k=1
            if u==1 and x>l-1 and L[x-1][y]=="0" and L[x-2][y]=="0" :
                L[x-1][y]=nomdesnavires[i]
                L[x-2][y]=nomdesnavires[i]
                k=1
            if u==2 and y<10-l and L[x][y+1]=="0" and L[x][y+2]=="0": 
                L[x][y+1]=nomdesnavires[i]
                L[x][y+2]=nomdesnavires[i]
                k=1
            if u==3 and y>l-1 and L[x][y-1]=="0" and L[x][y-2]=="0":
                L[x][y-1]=nomdesnavires[i]
                L[x][y-2]=nomdesnavires[i]
                k=1
     
     
     
        i=3
        l=taillenavires[i]
        p=0
        while p==0:
            x=input("Choisir la ligne entre 1 et 9:")
            y=input("Choisir la colonne entre 1 et 9:")
            if L[x][y]=="0":
                p=1
        L[x][y]=nomdesnavires[i]
        k=0
        while k==0:
            u=input("Entre 0 pour placer à gauche, 1 à droite, 2 en haut, 3 en bas")
            if u==0 and x<10-l and L[x+1][y]=="0" and L[x+2][y]=="0" and L[x+3][y]=="0":
                L[x+1][y]=nomdesnavires[i]
                L[x+2][y]=nomdesnavires[i]
                L[x+3][y]=nomdesnavires[i]
                k=1
            if u==1 and x>l-1 and L[x-1][y]=="0" and L[x-2][y]=="0" and L[x-3][y]=="0":
                L[x-1][y]=nomdesnavires[i]
                L[x-2][y]=nomdesnavires[i]
                L[x-3][y]=nomdesnavires[i]
                k=1
            if u==2 and y<10-l and L[x][y+1]=="0" and L[x][y+2]=="0" and L[x][y+3]=="0": 
                L[x][y+1]=nomdesnavires[i]
                L[x][y+2]=nomdesnavires[i]
                L[x][y+3]=nomdesnavires[i]
                k=1
            if u==3 and y>l-1 and L[x][y-1]=="0" and L[x][y-2]=="0" and L[x][y-3]=="0":
                L[x][y-1]=nomdesnavires[i]
                L[x][y-2]=nomdesnavires[i]
                L[x][y-3]=nomdesnavires[i]
                k=1
     
     
        i=4
        l=taillenavires[i]
        p=0
        while p==0:
            x=input("Choisir la ligne entre 1 et 9:")
            y=input("Choisir la colonne entre 1 et 9:")
            if L[x][y]=="0":
                p=1
        L[x][y]=nomdesnavires[i]
        k=0
        while k==0:
            u=input("Entre 0 pour placer à gauche, 1 à droite, 2 en haut, 3 en bas")
            if u==0 and x<10-l and L[x+1][y]=="0" and L[x+2][y]=="0" and L[x+3][y]=="0" and L[x+4][y]=="0":
                L[x+1][y]=nomdesnavires[i]
                L[x+2][y]=nomdesnavires[i]
                L[x+3][y]=nomdesnavires[i]
                L[x+4][y]=nomdesnavires[i]
                k=1
            if u==1 and x>l-1 and L[x-1][y]=="0" and L[x-2][y]=="0" and L[x-3][y]=="0" and L[x-4][y]=="0":
                L[x-1][y]=nomdesnavires[i]
                L[x-2][y]=nomdesnavires[i]
                L[x-3][y]=nomdesnavires[i]
                L[x-4][y]=nomdesnavires[i]
                k=1
            if u==2 and y<10-l and L[x][y+1]=="0" and L[x][y+2]=="0" and L[x][y+3]=="0" and L[x][y+4]=="0": 
                L[x][y+1]=nomdesnavires[i]
                L[x][y+2]=nomdesnavires[i]
                L[x][y+3]=nomdesnavires[i]
                L[x][y+4]=nomdesnavires[i]
                k=1
            if u==3 and y>l-1 and L[x][y-1]=="0" and L[x][y-2]=="0" and L[x][y-3]=="0" and L[x][y-4]=="0":
                L[x][y-1]=nomdesnavires[i]
                L[x][y-2]=nomdesnavires[i]
                L[x][y-3]=nomdesnavires[i]
                L[x][y-4]=nomdesnavires[i]
                k=1
        return(L)
    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
    from random import randint
     
    taillenavires=[2,3,3,4,5]
    nomdesnavires=["T","S","D","C","P"]
    nT=2
    nS=3
    nD=3
    nC=4
    nP=5
     
    def placementIA ():
        L=[["0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0"]]
        i=0
        l=taillenavires[i]
        x=randint(0,9)
        y=randint(0,9)
        L[x][y]=nomdesnavires[i]
        k=0
        while k==0:
            l=randint(0,3)
            if l==0 and x!=0:
                L[x-1][y]=nomdesnavires[i]
                k=1
            if l==1 and x!=9:
                L[x+1][y]=nomdesnavires[i]
                k=1
            if l==2 and y!=0:
                L[x][y-1]=nomdesnavires[i]
                k=1
            if l==3 and y!=9:
                L[x][y+1]=nomdesnavires[i]
                k=1
     
     
        i=1
        l=taillenavires[i]
        p=0
        while p==0:
            x=randint(0,9)
            y=randint(0,9)
            if L[x][y]=="0":
                p=1
        L[x][y]=nomdesnavires[i]
        k=0
        while k==0:
            u=randint(0,3)
            if u==0 and x<10-l and L[x+1][y]=="0" and L[x+2][y]=="0":
                L[x+1][y]=nomdesnavires[i]
                L[x+2][y]=nomdesnavires[i]
                k=1
            if u==1 and x>l-1 and L[x-1][y]=="0" and L[x-2][y]=="0" :
                L[x-1][y]=nomdesnavires[i]
                L[x-2][y]=nomdesnavires[i]
                k=1
            if u==2 and y<10-l and L[x][y+1]=="0" and L[x][y+2]=="0": 
                L[x][y+1]=nomdesnavires[i]
                L[x][y+2]=nomdesnavires[i]
                k=1
            if u==3 and y>l-1 and L[x][y-1]=="0" and L[x][y-2]=="0":
                L[x][y-1]=nomdesnavires[i]
                L[x][y-2]=nomdesnavires[i]
                k=1
     
     
     
     
        i=2
        l=taillenavires[i]
        p=0
        while p==0:
            x=randint(0,9)
            y=randint(0,9)
            if L[x][y]=="0":
                p=1
        L[x][y]=nomdesnavires[i]
        k=0
        while k==0:
            u=randint(0,3)
            if u==0 and x<10-l and L[x+1][y]=="0" and L[x+2][y]=="0":
                L[x+1][y]=nomdesnavires[i]
                L[x+2][y]=nomdesnavires[i]
                k=1
            if u==1 and x>l-1 and L[x-1][y]=="0" and L[x-2][y]=="0" :
                L[x-1][y]=nomdesnavires[i]
                L[x-2][y]=nomdesnavires[i]
                k=1
            if u==2 and y<10-l and L[x][y+1]=="0" and L[x][y+2]=="0": 
                L[x][y+1]=nomdesnavires[i]
                L[x][y+2]=nomdesnavires[i]
                k=1
            if u==3 and y>l-1 and L[x][y-1]=="0" and L[x][y-2]=="0":
                L[x][y-1]=nomdesnavires[i]
                L[x][y-2]=nomdesnavires[i]
                k=1
     
     
     
        i=3
        l=taillenavires[i]
        p=0
        while p==0:
            x=randint(0,9)
            y=randint(0,9)
            if L[x][y]=="0":
                p=1
        L[x][y]=nomdesnavires[i]
        k=0
        while k==0:
            u=randint(0,3)
            if u==0 and x<10-l and L[x+1][y]=="0" and L[x+2][y]=="0" and L[x+3][y]=="0":
                L[x+1][y]=nomdesnavires[i]
                L[x+2][y]=nomdesnavires[i]
                L[x+3][y]=nomdesnavires[i]
                k=1
            if u==1 and x>l-1 and L[x-1][y]=="0" and L[x-2][y]=="0" and L[x-3][y]=="0":
                L[x-1][y]=nomdesnavires[i]
                L[x-2][y]=nomdesnavires[i]
                L[x-3][y]=nomdesnavires[i]
                k=1
            if u==2 and y<10-l and L[x][y+1]=="0" and L[x][y+2]=="0" and L[x][y+3]=="0": 
                L[x][y+1]=nomdesnavires[i]
                L[x][y+2]=nomdesnavires[i]
                L[x][y+3]=nomdesnavires[i]
                k=1
            if u==3 and y>l-1 and L[x][y-1]=="0" and L[x][y-2]=="0" and L[x][y-3]=="0":
                L[x][y-1]=nomdesnavires[i]
                L[x][y-2]=nomdesnavires[i]
                L[x][y-3]=nomdesnavires[i]
                k=1
     
     
        i=4
        l=taillenavires[i]
        p=0
        while p==0:
            x=randint(0,9)
            y=randint(0,9)
            if L[x][y]=="0":
                p=1
        L[x][y]=nomdesnavires[i]
        k=0
        while k==0:
            u=randint(0,3)
            if u==0 and x<10-l and L[x+1][y]=="0" and L[x+2][y]=="0" and L[x+3][y]=="0" and L[x+4][y]=="0":
                L[x+1][y]=nomdesnavires[i]
                L[x+2][y]=nomdesnavires[i]
                L[x+3][y]=nomdesnavires[i]
                L[x+4][y]=nomdesnavires[i]
                k=1
            if u==1 and x>l-1 and L[x-1][y]=="0" and L[x-2][y]=="0" and L[x-3][y]=="0" and L[x-4][y]=="0":
                L[x-1][y]=nomdesnavires[i]
                L[x-2][y]=nomdesnavires[i]
                L[x-3][y]=nomdesnavires[i]
                L[x-4][y]=nomdesnavires[i]
                k=1
            if u==2 and y<10-l and L[x][y+1]=="0" and L[x][y+2]=="0" and L[x][y+3]=="0" and L[x][y+4]=="0": 
                L[x][y+1]=nomdesnavires[i]
                L[x][y+2]=nomdesnavires[i]
                L[x][y+3]=nomdesnavires[i]
                L[x][y+4]=nomdesnavires[i]
                k=1
            if u==3 and y>l-1 and L[x][y-1]=="0" and L[x][y-2]=="0" and L[x][y-3]=="0" and L[x][y-4]=="0":
                L[x][y-1]=nomdesnavires[i]
                L[x][y-2]=nomdesnavires[i]
                L[x][y-3]=nomdesnavires[i]
                L[x][y-4]=nomdesnavires[i]
                k=1
     
    def tourJ():
        L=placementIA()
        T=2
        S=3
        D=3
        C=4
        P=5
        k=5
        while k!=0:
            x=input("Entrer la ligne de la case à bombarder:")
            y=input("Entrer la colonne de la case à bombarder:")
            l=L[x][y]
            if l=="0":
                L[x][y]=1
                print ("raté")
     
            if l=="T":
                 L[x][y]=2
                 print("vous avez touché un torpilleur")
                 T=T-1
                 if T==0:
                     print("Torpilleur coulé")
                     k=k-1
     
            if l=="S":
                 L[x][y]=2
                 print("vous avez touché un sous-marin")
                 S=S-1
                 if S==0:
                     print("sous-marin coulé")
                     k=k-1 
     
            if l=="D":
                 L[x][y]=2
                 print("vous avez touché un contre-torpilleur")
                 D=D-1
                 if D==0:
                     print("contre-Torpilleur coulé")
                     k=k-1
     
     
            if l=="C":
                 L[x][y]=2
                 print("vous avez touché un croiseur")
                 C=C-1
                 if C==0:
                     print("croiseur coulé")
                     k=k-1
     
            if l=="P":
                 L[x][y]=2
                 print("vous avez touché un porte-avion")
                 P=P-1
                 if P==0:
                     print("porte-avion coulé")
                     k=k-1
        print (L)
    Alors que j'avais utilisé des codes similaires pour la programmation IA(qui a fonctionné sans problème, j'ai un gros problème avec la programmation joueur.

    Pour la fonction PlacementJ, pour une raison qui m'échappe, il y a un message d'erreur qui s'affiche sur les lignes où j'affecte une valeur à i. J'ai ça:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    File "E:\Bataille Navale\placement joueur.py", line 15
        i=1
          ^
    IndentationError: unindent does not match any outer indentation level
    Pour la fonction tourJ, c'est la ligne l=L[x][y] qui ne marche pas, et je ne comprends pas la raison car j'ai écrit exactement la même ligne sur la fonction TourIA, et ça marchait
    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
    In [32]: tourJ()
    Entrer la ligne de la case à bombarder:4
    Entrer la colonne de la case à bombarder:3
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-32-3300c49ea706> in <module>()
    ----> 1 tourJ()
     
    E:\Bataille Navale\tour joueur.py in tourJ()
        178         x=input("Entrer la ligne de la case à bombarder:")
        179         y=input("Entrer la colonne de la case à bombarder:")
    --> 180         l=L[x][y]
        181         if l=="0":
        182             L[x][y]=1
     
    TypeError: 'NoneType' object is not subscriptable

  5. #5
    Expert éminent sénior
    Homme Profil pro
    Architecte technique retraité
    Inscrit en
    Juin 2008
    Messages
    21 276
    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 276
    Points : 36 761
    Points
    36 761
    Par défaut
    Salut,

    Pour:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    File "E:\Bataille Navale\placement joueur.py", line 15
        i=1
          ^
    IndentationError: unindent does not match any outer indentation level
    Vous avez écrit:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    def placementJ():
         L=[["0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0"]]
        i=1
        l=taillenavires[i]
        p=0
    Visuellement, vous voyez bien que le "i=1" n'est pas aligné avec "L = ..."

    Pour le "TypeError: 'NoneType' object is not subscriptable", le L est retourné par PlacementIA() et je ne vois pas de return qqc. dans cette fonction.

    Comme vous apprenez à coder, vous ne savez pas encore factoriser votre code et... pas grand chose explose côté nombre de lignes et rend la chose pas facile à lire. Si en plus vous n'avez pas encore acquis les base côté indentation, fonctions, ... çà va être galère d'arriver au bout.
    Essayez en même temps que vous vous prenez la tête sur ce code de lire un tuto. çà va vous donner des idées pour "structurer" et "coder" çà.

    Dernier point. Le plus difficile étant de "tester", pourquoi ne pas définir quelques grilles de placement pré-remplies? Cela mettrait de côté la fonction placement, et éviterait perdre votre temps à remplir la grille avant de pouvoir tester les fonctions du "jeu".
    Si vous représentez une grille vide comme:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
         L=[
                ["0","0","0","0","0","0","0","0","0","0"],
                ["0","0","0","0","0","0","0","0","0","0"],
                ["0","0","0","0","0","0","0","0","0","0"],
                ["0","0","0","0","0","0","0","0","0","0"],
                ["0","0","0","0","0","0","0","0","0","0"],
                ["0","0","0","0","0","0","0","0","0","0"],
                ["0","0","0","0","0","0","0","0","0","0"],
                ["0","0","0","0","0","0","0","0","0","0"],
                ["0","0","0","0","0","0","0","0","0","0"],
                ["0","0","0","0","0","0","0","0","0","0"],
           ]
    Vous voyez mieux "visuellement" comment la remplir.
    Vous pourriez aussi compacter L sous la forme de "lignes"
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    S = '''00000000
    00000000
    00000000
    00000000
    00000000
    00000000
    00000000
    00000000'''
    - W
    Architectures post-modernes.
    Python sur DVP c'est aussi des FAQs, des cours et tutoriels

  6. #6
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2015
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2015
    Messages : 7
    Points : 5
    Points
    5
    Par défaut
    J'avais pas vu le problème d'identation merci.

    Par contre, je ne comprends pas le problème avec ma deuxième erreur...

  7. #7
    Expert éminent sénior
    Homme Profil pro
    Architecte technique retraité
    Inscrit en
    Juin 2008
    Messages
    21 276
    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 276
    Points : 36 761
    Points
    36 761
    Par défaut
    Citation Envoyé par zo-68 Voir le message
    Par contre, je ne comprends pas le problème avec ma deuxième erreur...
    Si vous écrivez:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    def tourJ():
        L=placementIA()
        ...
    Il faut que la fonction placementIA retourne une liste de liste pour que L[x][y] fonctionne.
    Et comme je ne vois même pas l'ombre d'un "return" dans cette fonction là, normal que L soit None.

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

  8. #8
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2015
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2015
    Messages : 7
    Points : 5
    Points
    5
    Par défaut
    J'avais pas fait attention, j'étais sur de l'avoir fait pourtant. Merci.

    Mais du coup, j'ai un problème sur la même ligne avec l'autre code, une fois le problème d'indentation corrigé.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
         19         x=input("Choisir la ligne entre 1 et 9:")
         20         y=input("Choisir la colonne entre 1 et 9:")
    ---> 21         if L[x][y]=="0":
         22             p=1
         23     L[x][y]=nomdesnavires[i]
     
    TypeError: list indices must be integers, not str

  9. #9
    Expert éminent sénior
    Homme Profil pro
    Architecte technique retraité
    Inscrit en
    Juin 2008
    Messages
    21 276
    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 276
    Points : 36 761
    Points
    36 761
    Par défaut
    Salut,

    Pour comprendre et corriger votre erreur, je vous suggère la lecture du cours Python : ce chapitre du Swinnen

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

Discussions similaires

  1. programmation bataille navale
    Par adamorane dans le forum Programmation multimédia/Jeux
    Réponses: 1
    Dernier message: 14/12/2013, 13h09
  2. aide pour jeu de la bataille navale
    Par Jeannot Alpin dans le forum Delphi
    Réponses: 17
    Dernier message: 19/11/2006, 20h33
  3. bataille navale
    Par keenurives dans le forum C
    Réponses: 7
    Dernier message: 21/11/2005, 12h15
  4. [LG]Programme Bataille Navale en Pascal
    Par RaFaL dans le forum Langage
    Réponses: 21
    Dernier message: 10/06/2003, 21h22

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