Created
March 18, 2020 18:55
-
-
Save Aviortheking/2e50e7440406a81a7e0f8f69aa0112b4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print("Bienvenu dans ce programme de Bataille Navale") | |
print("Vous avez 5 bateaux") | |
bateaux = {"porte-avions":5,"croiseur":4, "sous-marin":3,"contre-torpilleur":3 ,"torpilleur":2} | |
print("placez vos bateaux, les cases vont de A à J et de 1 à 10, écrivez les cases une par une") | |
print("Le joueur A va tout d'abord choisir l'emplacement de ses bateaux") | |
GrilleJoueurA=[ | |
["0","0","0","0","0","0","0","0","0","0"], | |
["0","0","0","0","0","0","0","0","0","0"], | |
["0","0","0","0","0","0","0","0","0","0"], | |
["0","0","0","0","0","0","0","0","0","0"], | |
["0","0","0","0","0","0","0","0","0","0"], | |
["0","0","0","0","0","0","0","0","0","0"], | |
["0","0","0","0","0","0","0","0","0","0"], | |
["0","0","0","0","0","0","0","0","0","0"], | |
["0","0","0","0","0","0","0","0","0","0"], | |
["0","0","0","0","0","0","0","0","0","0"] | |
] | |
GrilleJoueurB=[ | |
["0","0","0","0","0","0","0","0","0","0"], | |
["0","0","0","0","0","0","0","0","0","0"], | |
["0","0","0","0","0","0","0","0","0","0"], | |
["0","0","0","0","0","0","0","0","0","0"], | |
["0","0","0","0","0","0","0","0","0","0"], | |
["0","0","0","0","0","0","0","0","0","0"], | |
["0","0","0","0","0","0","0","0","0","0"], | |
["0","0","0","0","0","0","0","0","0","0"], | |
["0","0","0","0","0","0","0","0","0","0"], | |
["0","0","0","0","0","0","0","0","0","0"] | |
] | |
def ChoixDeLaCaseASelec(): | |
reponse = "non" | |
while reponse != "oui": | |
print("choisissez une case, d'abord la lettre puis le chiffre") | |
Lettre = str(input("Lettre: ")) | |
Chiffre = int(input("Chiffre: ")) | |
print("vous etes sur de vouloir choisir la case ", Lettre, Chiffre," ?") | |
reponse = input() | |
return LettreVersNombre(Lettre),int(Chiffre) | |
def LettreVersNombre(lettre): | |
return ord(lettre.lower()) - 96 | |
def caseAUnBateau(grille, x, y): | |
return grille[x][y] != "0" | |
def bateauPeutEtrePlace(bateau, orientation, grille, case): | |
for i in range(0, bateaux[bateau]): | |
tmp = [0, 0] | |
if orientation[0] > 0: | |
tmp[0] = case[0]+orientation[0]+i | |
else: | |
tmp[1] = case[0]+orientation[1]+i | |
if caseAUnBateau(grille, tmp[0]-1, tmp[1]-1): | |
return False | |
return True | |
def placerUnBateau(bateau, grille): | |
print("Placement du bateaux ", bateau) | |
orientation = (0,0) | |
while True: | |
print("Selectionner l'orientation du beateau") | |
print("h = vars le haut") | |
print("b = vars le bas") | |
print("d = vars la droite") | |
print("g = vars la gauche") | |
tmp = input() | |
if ["h","b","d","g"].index(tmp): | |
if tmp == "h": | |
orientation = (-1, 0) | |
elif tmp == "b": | |
orientation = (1, 0) | |
elif tmp == "d": | |
orientation = (0, 1) | |
elif tmp == "g": | |
orientation = (0, -1) | |
break | |
case = ChoixDeLaCaseASelec() | |
if not bateauPeutEtrePlace(bateau, orientation, grille, case): | |
print("Bateau non placeable, recommencer") | |
placerUnBateau(bateau, grille) | |
return | |
for i in range(0, bateaux[bateau]): | |
tmp = [case[0]-1, case[1]-1] | |
if orientation[0] > 0: | |
tmp[0] += i | |
else: | |
tmp[1] += i | |
grille[tmp[0]][tmp[1]] = "1" | |
for bateau in bateaux: | |
placerUnBateau(bateau, GrilleJoueurA) | |
for bateau in bateaux: | |
placerUnBateau(bateau, GrilleJoueurB) | |
print(GrilleJoueurA) | |
print(GrilleJoueurB) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment