Commit 66b82a3f authored by Trogno Lucas's avatar Trogno Lucas
Browse files

Algorithme modifié

parent 674d45c6
#
# Hello World server in Python # Hello World server in Python
# Binds REP socket to tcp://*:5555 # Binds REP socket to tcp://*:5555
# Expects b"Hello" from client, replies with b"World" # Expects b"Hello" from client, replies with b"World"
...@@ -9,13 +6,13 @@ ...@@ -9,13 +6,13 @@
import time import time
import zmq import zmq
import numpy as np import numpy as np
import builtsin import builtins
def f(c): def f(c):
return ord(c)-ord("a")+1 return ord(c)-ord("A")+1
class Bateau: class Bateau:
"définition de la classe bateau" "definition de la classe bateau"
porte_avion=Bateau() porte_avion=Bateau()
porte_avion.longueur=5 porte_avion.longueur=5
...@@ -40,7 +37,7 @@ torpilleur.nom="torpilleur" ...@@ -40,7 +37,7 @@ torpilleur.nom="torpilleur"
bateaux=[porte_avion, croiseur, contre_torpilleur, sous_marin, torpilleur] bateaux=[porte_avion, croiseur, contre_torpilleur, sous_marin, torpilleur]
class Joueur: class Joueur:
"définition d'un joueur" "definition d'un joueur"
J1=Joueur() J1=Joueur()
M1=np.zeros((10,10)) M1=np.zeros((10,10))
...@@ -50,58 +47,94 @@ M2=np.zeros((10,10)) ...@@ -50,58 +47,94 @@ M2=np.zeros((10,10))
J2.grille=M2 J2.grille=M2
context = zmq.Context() context = zmq.Context()
socket = context.socket(zmq.REP) socket = context.socket(zmq.ROUTER)
socket.bind("tcp://*:5555") socket.bind("tcp://*:5555")
while True: while True:
# Wait for next request from client # Wait for next request from client
message = socket.recv() addr,empty,message=socket.recv_multipart()
print(message) print(message.decode())
if message.decode() =="PLAY": if message.decode() =="PLAY":
socket.send(b"VS J2 or C?") socket.send_multipart(b"VS J2 or C?")
vs=socket.recv() addr,empty,msg=socket.recv_multipart()
print(vs.decode()) print(vs.decode())
socket.send(b"Comment vous appelez-vous?") socket.send_multipart(b"Comment vous appelez-vous?")
nom=socket.recv() addr,empty,msg=socket.recv_multipart()
print(nom.decode()) print(nom.decode())
J1.nom=nom.decode J1.nom=nom.decode
#constituer sa grille
for elem in bateaux: for elem in bateaux:
question="Ou voulez-vous mettre la tete de " + elem.nom + "?" question="Ou voulez-vous mettre la tete de " + elem.nom + "?"
socket.send(question.encode()) question=question.encode()
tete=socket.recv() socket.send_multipart[addr,question]
k=f(tete[0]) tete=addr,empty,msg=socket.recv_multipart()
l=int(tete[1]) tete=tete.decode()
assert M1[k][l]==0 k=f(tete[0])-1
possibilites=[] l=int(tete[1])-1
for possibility in range(4): if M1[k][l]!=0:
socket.send_multipart(b"Cette case est deja occupee")
bateaux.append(elem)
else:
possibilites=[]
a,b,c,d=0,0,0,0 a,b,c,d=0,0,0,0
for case in range(0,elem.longueur): for case in range(0,elem.longueur):
if k-elem.longueur<0 or M1[k-case][l]!=0: #horizontal a gauche
if k-elem.longueur<-1 or M1[k-case][l]!=0:
a+=1 a+=1
if k+elem.longueur>10 and M1[k+case][l]!=0: #horizontal a droite
if k+elem.longueur>10 or M1[k+case][l]!=0:
b+=1 b+=1
if l-elem.longueur<0 and M1[k][l-case]!=0: #vertical en haut
if l-elem.longueur<-1 or M1[k][l-case]!=0:
c+=1 c+=1
if k-elem.longueur>10 and M1[k][l+case]!=0: #vertical en bas
if l+elem.longueur>10 or M1[k][l+case]!=0:
d+=1 d+=1
if a==0: if a==0:
possibilites.append(chr(96+k-elem.longueur)+'l') possibilites.append(chr(64+k-elem.longueur+2)+ str(l+1))
if b==0: if b==0:
possibilites.append(chr(96+k+elem.longueur)+'l') possibilites.append(chr(64+k+elem.longueur)+str(l+1))
if c==0: if c==0:
possibilites.append('k'+chr(96+l-elem.longueur)) possibilites.append(chr(k+65) +str(l-elem.longueur+2))
if d==0: if d==0:
possibilites.append('k'+chr(96+l+elem.longueur)) possibilites.append(chr(k+65) +str(l+elem.longueur))
poss=''
question="Ou voulez-vous mettre la queue de " + elem.nom + " parmis les possibilités?" for possi in possibilites:
print(possibilites) poss+=possi+', '
socket.send(question.encode()) poss=poss[:-2]
queue=socket.recv() print('possibilites = ' + poss)
m=f(tete[0]) question="Ou voulez-vous mettre la queue de " + elem.nom + " parmi les possibilites : " + poss + " ?"
n=int(tete[1]) socket.send_multipart(question.encode())
queue=addr,empty,msg=socket.recv_multipart()
queue=queue.decode()
m=f(queue[0])-1
n=int(queue[1])-1
if k==m:
for i in range(min(l,n),max(l,n)+1):
M1[k][i]=1
if l==n:
for i in range(min(k,m),max(k,m)+1):
M1[i][l]=1
print(M1)
#frapper l'autre grille
socket.send_multipart(b"Quelle case frapper?")
case=addr,empty,msg=socket.recv_multipart()
case=case.decode()
print(case)
k=f(case[0])-1
l=int(case[1])-1
while M2[k][l]==1:
M2[k][l]=2
socket.send_multipart(b"Touche! Quelle case frapper ?")
case=addr,empty,msg=socket.recv_multipart()
case=case.decode()
k=f(case[0])-1
l=int(case[1])-1
socket.send_multipart(b"Manque!")
# Do some 'work' # Do some 'work'
time.sleep(1) time.sleep(1)
# Send reply back to client # Send reply back to client
socket.send(b"World") socket.send(b"World")
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment