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
| #Importation des modules
from random import*
from pprint import *
import random
g=[[0 for i in range(10)]for j in range (10)] #Création de la liste des bateaux
l=[] # création de liste de couple
g_4=[0,1,2,3]
reset_g_4=0
#Définition de la liste avec des couple de 0,0 à 9,9
for j in range (10):
for w in range (10):
l.append((str(j)+" "+str(w)))
def check(x,y):
L_check=(str(x)+' ' +str(y))
return L_check in l
def atk_2 (x,y):
global l
L_check=(str(x)+' ' +str(y))
print(L_check,"= lcheck")
l.remove(L_check)
def atk(x):
L=(x).split() #Séparation de liste en deux élement
l.remove(x) #Exclusion du couple de la liste l
x,y=int(L[0]),int(L[1])
return x,y
def Attack():
Un_couple=random.choice(l) #Choisi un couple aléatoire dans la liste l
x,y=atk(Un_couple)
return x,y # retour de x et y
def retir(chance):
global g_4
global Etat
if Etat=='oui':
if chance == 2 or chance == 3:X1,X2=0,1
else :X1,X2=2,3
verification_1= X1 in g_4
verification_2= X2 in g_4
for i in range (4):
if chance == i:
if verification_1 == True :
g_4.remove(X1)
if verification_2 == True :
g_4.remove(X2)
def Attack_4(x,y):
global reset_g_4
global Etat
global g_4 #intérargir avec la liste g_4
global chance
reset_g_4=0
chance=random.choice(g_4) # Choisie au hasard dans la liste g_4
mx,my=0,0 # Définition des valeur de base
if chance == 0 and check(x,y+1) == True : #Vérification des ses possiblité selon x et y dans la liste l return true or false
my = 1
atk_2(x,y+1)
elif chance == 1 and check(x,y-1) == True:
my = -1
atk_2(x,y-1)
elif chance == 2 and check(x+1,y) == True:
mx = 1
atk_2(x+1,y)
elif chance == 3 and check(x-1,y) == True:
mx = -1
atk_2(x-1,y)
else:
g_4.remove(chance)
Attack_4(x,y)
if y+my==10 or x+mx==10 or y+my ==-1 or x+mx==-1:
g_4.remove(chance)
Attack_4(x,y)
print("chance=",chance)
return x+mx,y+my
def verif():
global g_4
global reset_g_4
global Etat
for i in range (10):
reset_g_4+= 2 in g [i]
print("reset_g_4=",reset_g_4)
if reset_g_4 == False :
g_4=[0,1,2,3]
print(g_4)
print("************************************************plus de bateau***********************************************************")
Etat='non'
def Reponse(x,y): # Réponse de la grille g
global g
global x_1
global y_1
global Etat
global chance
print("x,y=",x,y)
# Vérification
if g[x][y]==2: #Si il y a un bateau
g[x][y]=3 #Le bateau devient toucher
retir(chance)
if Etat == 'non': # a revoir le placement
x_1=x
y_1=y
Etat='oui'
verif()
return "toucher",x,y # retour de l'information toucher
else: #Si il n'y a pas de bateau
g[x][y]=1 #La case devient missile a l'eau
if Etat =='oui':
g_4.remove(chance) #problem a regler
x,y=x_1,y_1
return "rater",x,y # retour de l'information rater
return 'rater',x,y
Etat='non'
def jouer_1_echange(): # phase de jeu
global Etat
global x
global y
if Etat== "non" :
x,y = Attack() #Récupération des coordonnées de l'attaque
status,x,y = Reponse(x,y) # Vérifiation de l'attaque
elif Etat =='oui':
x,y = Attack_4(x,y) #Récupération des coordonnées de l'attaque
status,x,y = Reponse(x,y)# Vérifiation de l'attaque
print("x et y apres=",x,y)
#########test########################################################
chance=0 # de base
x,y = randint(0,9),randint(0,7)
g[x][y]=2
g[x][y+1]=2
g[x][y+2]=2
pprint(g)
def a():
jouer_1_echange()
pprint(g)
for i in range (100):
a()
print(Etat)
print(g_4)
print("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") |
Partager