bataille_navale_console.py

Created by vgalletramond

Created on March 16, 2025

4.09 KB

pas grand chose


import random

placement_ba=[]
vie_ba=[5,4,3,3,2]
bateaux_coules=[]
plateau=[[0 for i in range(10)] for i in range(10)]
plateau_vierge=[[0 for i in range(10)] for i in range(10)]
nb_tir=[]

class Bateau :
    def __init__(self,N_vie):
        self.vie=N_vie
    def touche (self, n_b):
        self.vie=self.vie-1
        if (self.vie==0):
            print("Le",n_b,"a été coulé.")
            bateaux_coules.append(n_b)
            if (len(bateaux_coules)==5):
                print('Tout les bateaux ont été coulés Bravo!\nVous avez utilisé',len(nb_tir),'tirs pour élimier tout les bateaux.')
    def vie_restante (self):
        return self.vie
    
porte_avion=Bateau(5)
croiseur=Bateau(4)
sous_marin_1=Bateau(3)
sous_marin_2=Bateau(3)
torpilleur=Bateau(2)
coordones_bateaux=[[],[],[],[],[]]

def placement_bateaux():
    co_bateaux=0
    for i in range(5):
        while True:
            test=0
            placement_ba.append(random.randint(0,1))
            if (placement_ba[0]==0):
                placement_ba.append(random.randint(0,9-vie_ba[i]))
                placement_ba.append(random.randint(0,9))
                for a in range(vie_ba[i]):
                    plateau[placement_ba[2]][placement_ba[1]+a]+=1
                    coordones_bateaux[co_bateaux].append([placement_ba[1]+a,placement_ba[2]])
                    if (plateau[placement_ba[2]][placement_ba[1]+a]>1):
                        test=1
                        for b in range(a+1):
                            plateau[placement_ba[2]][placement_ba[1]+a-b]-=1
                        coordones_bateaux[co_bateaux].clear()
                        placement_ba.clear()
                        break
            elif (placement_ba[0]==1):
                placement_ba.append(random.randint(0,9))
                placement_ba.append(random.randint(0,9-vie_ba[i]))
                for a in range(vie_ba[i]):
                    plateau[placement_ba[2]+a][placement_ba[1]]+=1
                    coordones_bateaux[co_bateaux].append([placement_ba[1],placement_ba[2]+a])
                    if (plateau[placement_ba[2]+a][placement_ba[1]]>1):
                        test=1
                        for b in range(a+1):
                            plateau[placement_ba[2]+a-b][placement_ba[1]]-=1
                        coordones_bateaux[co_bateaux].clear()
                        placement_ba.clear()
                        break
            if (test==0):
                placement_ba.clear()
                break     
        co_bateaux+=1

def tir (x,y):
    nb_tir.append([x,y])
    if (plateau[y][x]==1):
        print('Vous avez touché un bateau')
        plateau[y][x]=3
        plateau_vierge[y][x]=2
        for coordo in coordones_bateaux:
            index=coordones_bateaux.index(coordo)
            if [x, y] in coordo:
                index_2=coordo.index([x, y])
                N_bateau=[index]
                if (N_bateau[0]==0):
                    n_b='porte avion'
                    porte_avion.touche(n_b)
                elif (N_bateau[0]==1):
                    n_b='croiseur'
                    croiseur.touche(n_b)
                elif (N_bateau[0]==2):
                    n_b='premier sous marin'
                    sous_marin_1.touche(n_b)
                elif (N_bateau[0]==3):
                    n_b='deuxième sous marin'
                    sous_marin_2.touche(n_b)
                elif (N_bateau[0]==4):
                    n_b='torpilleur'
                    torpilleur.touche(n_b)
    elif (plateau[y][x]==0):
        print('Plouf')
        plateau_vierge[y][x]=1
        plateau[y][x]=4

placement_bateaux()
print("Vous vous retrouver a devoir commander où tirer\n1=plouf 2=touché")
while True:
    print('   0 1 2 3 4 5 6 7 8 9')
    for i in range(len(plateau_vierge)):
        print(str(i)+"|",plateau_vierge[i][0],plateau_vierge[i][1],plateau_vierge[i][2],plateau_vierge[i][3],plateau_vierge[i][4],plateau_vierge[i][5],plateau_vierge[i][6],plateau_vierge[i][7],plateau_vierge[i][8],plateau_vierge[i][9])
    x,y = input("Coordonées ? : ").split(',')
    tir(int(x),int(y))
    if (len(bateaux_coules)==5):
        break

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.