-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
44 lines (36 loc) · 1.53 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
import socket
class BullsAndCowsClient:
def __init__(self):
self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def connect(self, server_ip, server_port):
self.client_socket.connect((server_ip, server_port))
print("Connected to the server.")
def play(self):
player_name = input("Enter your name: ")
self.client_socket.send(player_name.encode())
print("Enter 'QUIT' to exit the game.")
while True:
guess = input("Enter your guess: ")
if self.has_duplicates(guess):
print("Warning: Duplicate digits are not allowed in the guess number.")
continue
self.client_socket.send(guess.encode())
result = self.client_socket.recv(1024).decode().strip()
print(result)
if result == "Congratulations! You guessed the number.":
play_again = input("Do you want to play again? (yes/no): ")
self.client_socket.send(play_again.encode())
if play_again.lower() == "yes":
restart_message = self.client_socket.recv(1024).decode().strip()
print(restart_message)
else:
break
elif result == "QUIT":
break
self.client_socket.close()
def has_duplicates(self, number):
return len(set(number)) != len(number)
if __name__ == "__main__":
client = BullsAndCowsClient()
client.connect("localhost", 5000)
client.play()