Created
October 24, 2016 13:48
-
-
Save Telltak/c59377e65bbbe2711ffc2592234e1b01 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import random | |
import threading | |
class game: | |
def __init__(self): | |
self.number = random.randint(1, 30) | |
def guess(self, number): | |
distance = abs(self.number - number) | |
if self.number == number: | |
#print("Correct") | |
return b"Correct\r\n" | |
elif distance > 3: | |
#print('Your guess is far away.') | |
return b"Far\r\n" | |
elif distance <= 3: | |
#print('Your guess is close.') | |
return b"Close\r\n" | |
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) | |
s.bind(("127.0.0.1",4000)) | |
s.listen(5) | |
def handle_client(c,a): | |
data = c.recv(10000) | |
if data == b"Hello\r\n": | |
c.sendall (b"Greetings\r\n") | |
data = c.recv(10000) | |
if data == b"Game\r\n": | |
ngame = game() | |
c.sendall (b"Ready\r\n") | |
while True: | |
data = c.recv(10000).decode() | |
if data.startswith("My Guess is: "): | |
number = int(data.split()[-1]) | |
#print(number) | |
reply = ngame.guess(number) | |
c.sendall(reply) | |
if reply == b"Correct\r\n": | |
c.close() | |
break | |
while True: | |
(c,a) = s.accept() | |
#print ("Received connection from", a) | |
t = threading.Thread(target = handle_client, args =(c,a)) | |
t.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment