-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.py
166 lines (127 loc) · 6.44 KB
/
Client.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
##*********************************************************************
## File: Client.py
## Name: Taylor H. Paul
## Course: CS3502 Computer Networks
## Project: Game Project
## Operating Environment: Windows 10 Pro
## Compiler: PyCharm 5.0.4, Python 3.4.3
## Date: 29 Jan 16
## Description: This file contains the source code for a client that will
## run "Guess that Number!" as a networking game.
##*********************************************************************
##Imorts and Globals
import socket
import sys
#PUBLIC METHODS:
#----------------------------------------------------------------------------------
def connect2server():
if len(sys.argv) < 3:
print ("Need to provide <hostname> <port>, e.g., localhost in the command line!")
exit(1)
# Connect the socket to the port on the server given by the caller
server_name = sys.argv[1]
server_port = int(sys.argv[2])
server_address = (server_name, server_port)
print("connecting to %s port %s" % server_address)
sock.connect(server_address)
return 0
#----------------------------------------------------------------------------------
def guessSend(guess):
print("sending %s" % guess)
try:
sock.sendall(guess.encode())
amount_received = 0
amount_expected = len(guess.encode())
count = 0
while (amount_received < amount_expected) and (count <= 25):
data = sock.recv(16)
if data: ##Only print if receiving data
if data.decode() == "noFriends":
print("Waiting on a second player to join...")
else:
amount_received += len(data)
#print("received %s" % data.decode()) #UNCOMMENT FOR TESTING PURPOSES
count+=1 ##Allow 25 loops without data before exiting loop
except ConnectionResetError or ConnectionAbortedError:
print("Could not connect, the server is not available, please restart the program and try again:-(")
exit()
return data.decode()
#----------------------------------------------------------------------------------
def quitGuess():
print("Thanks for playing!!! Closing Connection!")
sock.close()
return
#----------------------------------------------------------------------------------
def getvalidinput():
"""Requests a user input for the guess, converts to an integer for checking value is between 1-100, if
not it prompts the user again, if it is in returns the valid response as a string for further use by
the program."""
notValidResponse = True
while notValidResponse == True:
try:
response = int(input("\n What number would you like to guess? (### from 1-100): "))
if (1 > response) or (response > 100):
print("\nInvalid guess, please guess an integer from 1-100 in format ###\n")
notValidResponse = True #Set variable back to true to prompt user for guess once more
else: #Guess was good so exit the loop
notValidResponse = False
except: #If a user inputs a special character or letter prompt and allow loop to restart
print("\nInvalid guess, please guess an integer from 1-100 in format ###\n")
return str(response)
#----------------------------------------------------------------------------------
def checkWinner(message, guess):
#Split the returned server string into a list at commas:
results = message.split(',')
#Check first value to see if win or lose
if results[0] == '1':
winOrLose='WIN!!!'
elif results[0] =='2':
winOrLose='LOSE:-(...'
#Store answer returned by server:
answer = results[1]
#Store num of winners:
num_win=results[2]
#Store winning answer(s) sent by server:
winAns = results[3] #First winning result
#Check if more than one winner, concatenate answers in single string:
if int(num_win)>1:
for x in range(4,len(results)):
winAns = winAns + ', ' + results[x]
preface = "\nThe results are in... we have " + num_win + " winner(s)!"
print(preface, 'YOU', winOrLose, "Here is the answer:", answer + ", and the winning guess(es):", winAns +'!\n')
return
## MAIN PROGRAM:----------------------------------------------------------------------
#WELCOME SCREEN:
print(" _ ________ _________ __ _______ ")
print("| | /| / / __/ / / ___/ __ \/ |/ / __/ ")
print("| |/ |/ / _// /__/ /__/ /_/ / /|_/ / _/ ")
print("|__/|__/___/____/\___/\____/_/ /_/___/ ")
print(" ")
print(" __________ ")
print("/_ __/ __ \ ")
print(" / / / /_/ / ")
print("/_/ \____/ ")
print(" ")
print(" _______ ___________________ _______ ")
print(" / ___/ / / / __/ __/ __/ _/ |/ / ___/ ")
print("/ (_ / /_/ / _/_\ \_\ \_/ // / (_ / ")
print("\___/\____/___/___/___/___/_/|_/\___/ ")
print(" ")
print(" ________ __ _______ ")
print(" / ___/ _ | / |/ / __/ ")
print("/ (_ / __ |/ /|_/ / _/ ")
print("\___/_/ |_/_/ /_/___/ ")
print(" ")
print(" ___ ___ ___ ___ ")
print(" |_ |/ _ \/ _ \/ _ \ ")
print(" / __// // / // / // / ")
print("/____/\___/\___/\___/ ")
#Graphic software for Atom IDE (figlet) introduced by Justin L. Downs
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)# Create a TCP/IP socket
#Actual Game:
guess=getvalidinput() #Get guess before trying to connect
connect2server() #Having Guess Connect to Server
win_mess=guessSend(guess) #Send the guess and receive response
checkWinner(win_mess, guess) #Translate message to see if win or lose
quitGuess() #Close Connection endgame
# Start Client: python client.py localhost 1000