Skip to content

Commit

Permalink
client first interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
john.wohl@uconn.edu committed Dec 13, 2019
1 parent 798fdb8 commit 0884920
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 11 deletions.
20 changes: 19 additions & 1 deletion Konane.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import client
import sys
import state

# Top level class that has the client and the state
Expand All @@ -7,6 +8,23 @@ def __init__(self):
self.client = client.ArtemisClient("artemis.engr.uconn.edu", 4705)
self.state = state.State()
self.playerNumber = None

def stringToCoords(self, string):
coords = []
coords.append(int(string[string.find("[")+1:string.find(":")]))
coords.append(int(string[string.find(":")+1:string.find("]")]))

return coords

def start(self):
self.playerNumber = self.client.initialize('TESTOPPONENT')
firstMove = self.state.first_moves_set()[0]
firstMoveCommand = "[{0}:{1}]".format(firstMove[0], firstMove[1])
firstRemoved = self.client.initialize(firstMoveCommand)
removedCoords = self.stringToCoords(firstRemoved)
self.state.removePiece(removedCoords[0], removedCoords[1])



k = Konane()
print(k.stringToCoords("[0:14]"))

Binary file added __pycache__/client.cpython-36.pyc
Binary file not shown.
Binary file added __pycache__/state.cpython-36.pyc
Binary file not shown.
50 changes: 40 additions & 10 deletions client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,35 @@ class ArtemisClient:
def __init__(self, server_host, server_port):
self.server_host = server_host
self.server_port = server_port
self.username = "MHJZ"
self.password = "4705"
self.username = None
self.password = None
self.opponent = None
self.playerNumber = None
self.color = None
# self.username = sys.stdin.readline()
# self.password = sys.stdin.readline()
self.server_sock = None

def extractREMOVED(self, string):
# len("Removed:")
offset = 8

string = string .rstrip()
segment = string[string.find("Removed:"):string.find("]")+1]
return segment

def waitForResponse(self):
response = self.server_sock.recv(1024).decode('UTF-8').rstrip()
print(response, end="")
response = self.server_sock.recv(1024).decode("utf-8")
print(response)
return response

def send(self, string):
formatted_string = string + "\r\n"
print(formatted_string, end="")
formatted_string = string.rstrip() + "\r\n"
#formatted_string = string
#print(formatted_string, end="")
self.server_sock.send(formatted_string.encode())

def initialize(self, opponent):
def initialize(self, firstMove):
# Create socket and connect to server
try:
self.server_sock = socket.socket(
Expand All @@ -34,17 +48,33 @@ def initialize(self, opponent):
#self.server_sock.sendall("!".encode("utf-8"))
self.waitForResponse()
self.waitForResponse()
self.username = input("")
self.send(self.username)
self.waitForResponse()
self.password = input("")
self.send(self.password)
self.waitForResponse()
opponent = input("")
self.send(opponent)
self.waitForResponse()
response = self.waitForResponse()
playerNumber = int(response[7])

# First player
if(response[7] == "1"):
self.playerNumber = 1
self.send(firstMove)
response = self.waitForResponse()
self.color = response[6:11]
# Second player
else:
self.playerNumber = 2
self.color = response[6:11]

# Return player's turn position
return playerNumber
response = self.waitForResponse()
# EX: [0,0]
removedPiece = self.extractREMOVED(response)

return removedPiece

def close(self):
self.server_sock.close()
Expand Down

0 comments on commit 0884920

Please sign in to comment.