-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
39 lines (36 loc) · 1.52 KB
/
server.py
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
# Python Game Server Side 2021 - by A.G.
# Python Exercise in "Socket", GUI with "Tkinter";
# Game to Guess the random number generated by server.
import socket # Modules used
import random
tcpSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Socket creation
tcpSocket.bind(("", 8000)) # Any interface, port - 8000
while True:
tcpSocket.listen(1) # Accepts one connection
(client, (ip, port)) = tcpSocket.accept()
print("Connected")
num = random.randint(1, 20) # Random number
print("Random Number is ", num)
turn = 0
while turn <= 5: # Game logic
try:
turn += 1
data = int(client.recv(2048).decode())
if int(data) == num:
client.send("Win".encode()+str(turn).encode())
break
if int(data) > num:
if turn == 5:
client.send("Lost".encode() + str(turn).encode())
break
else:
client.send("High".encode()+str(turn).encode())
if int(data) < num:
if turn == 5:
client.send("Lost".encode() + str(turn).encode())
break
else:
client.send("Low".encode() + str(turn).encode())
except ValueError:
break
print("Turns taken ", turn)