Last active
December 3, 2015 03:01
-
-
Save jeremiahmarks/47d5328df1cc388d1433 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
playing = True | |
def recieveMessage(): | |
print "hi!" | |
data = s.recv(512).decode() | |
if data == "Greetings\n": | |
print("Welcome to the number guessing game.") | |
elif data == "Far\n": | |
print("Your guess was far from the correct number.") | |
elif data == "Close\n": | |
print("Your guess was close to the correct number.") | |
elif data == "Correct\n": | |
print("Your guess was correct. Thankyou for playing.") | |
global playing | |
playing = False | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.connect(("127.0.0.1", 4000)) | |
s.send(("Hello\n").encode()) | |
recieveMessage() | |
while playing: | |
guess = input("What is your guess? ") | |
totalGuess = ("Guess: " + guess) | |
s.send(totalGuess.encode()) | |
recieveMessage() | |
s.close() |
This file contains hidden or 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 select | |
import socket | |
import random | |
import ssl | |
sockList = [] | |
playing = False | |
guesses = 0 | |
guess = 0 | |
def adminCommand(): | |
for s in sockList: | |
c.send((s).encode('utf-8')) | |
def within(value, goal, n): | |
difference = abs(value - goal) | |
if difference <= n: | |
return True | |
else: | |
return False | |
def recieveGuess(guess): | |
guess = cl.recv(4096).decode() | |
guessList = guess.split(" ") | |
if guessList[0] == "Who": | |
adminCommand() | |
return int(guessList[1]) | |
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
admin = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
client.bind(("127.0.0.1", 4000)) | |
admin.bind(("127.0.0.1", 4001)) | |
client.listen(5) | |
admin.listen(1) | |
sockList.append(client) | |
sockList.append(admin) | |
while True: | |
print "svrCheckingin" | |
r, w, err = select.select(sockList, [], []) | |
for sock in r: | |
if sock == client: | |
print 'sockclient' | |
cl,a = client.accept() | |
sockList.append(client) | |
else: | |
print 'about to try' | |
try: | |
if cl.recv(4096).decode() == "Hello\n": | |
playing = True | |
cl.send(("Greetings\n").encode('utf-8')) | |
numberToGuess = random.randint(1,20) | |
print(numberToGuess) | |
while playing: | |
if recieveGuess(guess) == numberToGuess: | |
cl.send(("Correct\n").encode('utf-8')) | |
playing = False | |
cl.close() | |
elif within(numberToGuess, guess, 2): | |
cl.send(("Close\n").encode('utf-8')) | |
else: | |
cl.send(("Far\n").encode('utf-8')) | |
except: | |
sock.close() | |
sockList.remove(sock) | |
continue | |
client.close() | |
admin.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment