Commit 463f6d4981653965137b4c4b175434c2c39f9def

Authored by Mestari Yousra
1 parent b05120bd

code serveur

Showing 1 changed file with 77 additions and 44 deletions   Show diff stats
serveur.py
  1 +# Hello World server in Python
  2 +# Binds REP socket to tcp://*:5555
  3 +# Expects b"Hello" from client, replies with b"World"
  4 +#
  5 +
  6 +import time
1 7 import zmq
  8 +import numpy as np
  9 +import builtins
  10 +import grille_serveur.py
  11 +import frappe_serveur.py
  12 +
  13 +class Bateau:
  14 + "definition de la classe bateau"
  15 +
  16 +porte_avion=Bateau()
  17 +porte_avion.longueur=5
  18 +porte_avion.nom="Porte-avion"
  19 +
  20 +croiseur=Bateau()
  21 +croiseur.longueur=4
  22 +croiseur.nom="Croiseur"
  23 +
  24 +contre_torpilleur=Bateau()
  25 +contre_torpilleur.longueur=3
  26 +contre_torpilleur.nom="Contre-torpilleur"
  27 +
  28 +sous_marin=Bateau()
  29 +sous_marin.longueur=3
  30 +sous_marin.nom="Sous-marin"
  31 +
  32 +torpilleur=Bateau()
  33 +torpilleur.longueur=2
  34 +torpilleur.nom="Torpilleur"
  35 +
  36 +bateaux=[porte_avion, croiseur, contre_torpilleur, sous_marin, torpilleur]
  37 +
  38 +class Joueur:
  39 + "definition d'un joueur"
  40 +
  41 +J1=Joueur()
  42 +M1=np.zeros((10,10))
  43 +J1.grille=M1
  44 +J2=Joueur()
  45 +M2=np.zeros((10,10))
  46 +J2.grille=M2
2 47  
3 48 context = zmq.Context()
  49 +socket = context.socket(zmq.ROUTER)
  50 +socket.bind("tcp://*:5555")
  51 +
  52 +while True:
  53 + # Wait for next request from client
  54 + addr,empty,message=socket.recv_multipart()
  55 + print(message.decode())
  56 + if message.decode() =="PLAY":
  57 + socket.send_multipart([addr,b'',b"VS P or C?"])
  58 + J1.addr,empty,vs=socket.recv_multipart()
  59 + print(vs.decode())
  60 + [M1,position1]=grille_serveur(J1,bateaux)
  61 + if vs.decode()== "P":
  62 + J2.addr,empty,vs=socket.recv_multipart()
  63 + [M2,position2]=grille_serveur(J2,bateaux)
  64 + message="jeu en cours"
  65 + socket.send_multipart([J1.addr,b'',message.encode()])
  66 + while message!="jeu fini":
  67 + message,victoire, M2, position2=frappe_serveur(J1,M2,position2)
  68 + socket.send_multipart([J1.addr,b'',message.encode()])
  69 + if message!="jeu fini":
  70 + message,victoire, M1, position1=frappe_serveur(J2,M1,position1)
  71 + socket.send_multipart([J1.addr,b'',message.encode()])
  72 + socket.recv_multipart([J1.addr,b'',victoire.encode()])
  73 + elif vs.decode()=='C':
  74 + #appeler l'IA
  75 + else:
  76 + quit
  77 + # Do some 'work'
  78 + time.sleep(1)
4 79  
5   -# Socket to talk to server
6   -print("Connecting to hello world server…")
7   -socket = context.socket(zmq.DEALER)
8   -socket.connect("tcp://147.250.9.87:5555")
9   -
10   -# Do 10 requests, waiting each time for a response
11   -while True :
12   - jeu=input("Voulez-vous jouer ?:")
13   - if jeu=="PLAY":
14   - socket.send_multipart([b'',b"PLAY"])
15   - empty,reponse=socket.recv_multipart()
16   - print(reponse.decode())
17   - JouC=input()
18   - socket.send_multipart([b'',JouC.encode()])
19   - empty,reponse=socket.recv_multipart()
20   - print(reponse.decode())
21   - nom=input()
22   - socket.send_multipart([b'',nom.encode()])
23   - for i in range(5):
24   - empty,reponse=socket.recv_multipart()
25   - print(reponse.decode())
26   - position_avant=input()
27   - socket.send_multipart([b'',position_avant.encode()])
28   - empty,reponse=socket.recv_multipart()
29   - print(reponse.decode())
30   - position_queue=input()
31   - socket.send_multipart([b'',position_queue.encode()])
32   - empty,reponse=socket.recv_multipart()
33   - print(reponse.decode())
34   - case=input()
35   - socket.send_multipart([b'',case.encode()])
36   - empty,reponse=socket.recv_multipart()
37   - reponse=reponse.decode()
38   - print(reponse)
39   - while reponse=="Touche! Quelle case frapper?":
40   - case=input()
41   - socket.send_multipart([b'',case.encode()])
42   - empty,reponse=socket.recv_multipart()
43   - print(reponse.decode())
44   - empty,reponse=socket.recv_multipart()
45   - print(reponse.decode())
46   -
47   - # Get the reply.
48   - #message = socket.recv()
49 80 \ No newline at end of file
  81 + # Send reply back to client
  82 + socket.send(b"World")
50 83 \ No newline at end of file
... ...