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

Algorithme modifié

parent 674d45c6
#
# Hello World server in Python
# Binds REP socket to tcp://*:5555
# Expects b"Hello" from client, replies with b"World"
......@@ -9,13 +6,13 @@
import time
import zmq
import numpy as np
import builtsin
import builtins
def f(c):
return ord(c)-ord("a")+1
return ord(c)-ord("A")+1
class Bateau:
"définition de la classe bateau"
"definition de la classe bateau"
porte_avion=Bateau()
porte_avion.longueur=5
......@@ -40,7 +37,7 @@ torpilleur.nom="torpilleur"
bateaux=[porte_avion, croiseur, contre_torpilleur, sous_marin, torpilleur]
class Joueur:
"définition d'un joueur"
"definition d'un joueur"
J1=Joueur()
M1=np.zeros((10,10))
......@@ -50,58 +47,94 @@ M2=np.zeros((10,10))
J2.grille=M2
context = zmq.Context()
socket = context.socket(zmq.REP)
socket = context.socket(zmq.ROUTER)
socket.bind("tcp://*:5555")
while True:
# Wait for next request from client
message = socket.recv()
print(message)
addr,empty,message=socket.recv_multipart()
print(message.decode())
if message.decode() =="PLAY":
socket.send(b"VS J2 or C?")
vs=socket.recv()
socket.send_multipart(b"VS J2 or C?")
addr,empty,msg=socket.recv_multipart()
print(vs.decode())
socket.send(b"Comment vous appelez-vous?")
nom=socket.recv()
socket.send_multipart(b"Comment vous appelez-vous?")
addr,empty,msg=socket.recv_multipart()
print(nom.decode())
J1.nom=nom.decode
#constituer sa grille
for elem in bateaux:
question="Ou voulez-vous mettre la tete de " + elem.nom + "?"
socket.send(question.encode())
tete=socket.recv()
k=f(tete[0])
l=int(tete[1])
assert M1[k][l]==0
possibilites=[]
for possibility in range(4):
question=question.encode()
socket.send_multipart[addr,question]
tete=addr,empty,msg=socket.recv_multipart()
tete=tete.decode()
k=f(tete[0])-1
l=int(tete[1])-1
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
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
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
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
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
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:
possibilites.append(chr(96+k+elem.longueur)+'l')
possibilites.append(chr(64+k+elem.longueur)+str(l+1))
if c==0:
possibilites.append('k'+chr(96+l-elem.longueur))
possibilites.append(chr(k+65) +str(l-elem.longueur+2))
if d==0:
possibilites.append('k'+chr(96+l+elem.longueur))
question="Ou voulez-vous mettre la queue de " + elem.nom + " parmis les possibilités?"
print(possibilites)
socket.send(question.encode())
queue=socket.recv()
m=f(tete[0])
n=int(tete[1])
possibilites.append(chr(k+65) +str(l+elem.longueur))
poss=''
for possi in possibilites:
poss+=possi+', '
poss=poss[:-2]
print('possibilites = ' + poss)
question="Ou voulez-vous mettre la queue de " + elem.nom + " parmi les possibilites : " + poss + " ?"
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'
time.sleep(1)
# 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