Skip to content

Commit

Permalink
finished client
Browse files Browse the repository at this point in the history
  • Loading branch information
john.wohl@uconn.edu committed Dec 13, 2019
1 parent 0884920 commit bbe2182
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 13 deletions.
75 changes: 71 additions & 4 deletions Konane.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import client
import sys
import state
import math

# Top level class that has the client and the state

class Konane:
def __init__(self):
self.client = client.ArtemisClient("artemis.engr.uconn.edu", 4705)
self.state = state.State()
self.playerNumber = None
self.color = None
self.player = None

def stringToCoords(self, string):
coords = []
Expand All @@ -16,15 +20,78 @@ def stringToCoords(self, string):

return coords

def coordsToString(self, coords):
return "[{0}:{1}]".format(coords[0], coords[1])

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

self.color = self.client.color
self.player = int(self.client.playerNumber)

if(self.client.color == "WHITE"):
state.playerColor = self.color = state.WHITE
else:
state.playerColor = self.color = state.BLACK

if(self.player == 1):
try:
removedCoords = self.stringToCoords(self.client.waitREMOVE())
except:
return
self.state.removePiece(removedCoords[0], removedCoords[1])
myTurn = True

else:
secondMove = self.coordsToString(self.state.second_moves_set()[0])
self.client.sendREMOVE(secondMove)
try:
removedCoords = self.stringToCoords(self.client.waitREMOVE())
except:
return

self.state.removePiece(removedCoords[0], removedCoords[1])
myTurn = False

finished = False
#print(self.state)
turns = 0
while(not finished):
if(myTurn):
moveCoords = self.state.makeMove(self.color)
if(moveCoords == 1):
finished = True
else:
startCoords = self.coordsToString(moveCoords[0])
endCoords = self.coordsToString(moveCoords[1])
self.client.sendMOVE(startCoords, endCoords)

movedString = self.client.waitMOVE(myTurn)

try:
movedStart = self.stringToCoords(movedString[0])
movedEnd = self.stringToCoords(movedString[1])
self.state.movePiece(movedStart, movedEnd)
except:
break
else:
movedString = self.client.waitMOVE(myTurn)

try:
movedStart = self.stringToCoords(movedString[0])
movedEnd = self.stringToCoords(movedString[1])
self.state.movePiece(movedStart, movedEnd)
except:
break

myTurn = not myTurn
print(self.state)

self.client.close()

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

Binary file modified __pycache__/client.cpython-36.pyc
Binary file not shown.
Binary file modified __pycache__/state.cpython-36.pyc
Binary file not shown.
59 changes: 53 additions & 6 deletions client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,75 @@ def __init__(self, server_host, server_port):
self.opponent = None
self.playerNumber = None
self.color = None
# self.username = sys.stdin.readline()
# self.password = sys.stdin.readline()
self.server_sock = None

self.gameOver = False

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

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

def extractMOVED(self, string):
#len(Move)
offset = 4

firstSegment = string[string.find("Move")+offset:string.find("]")+1]

string = string[string.find("]")+2:]
secondSegment = string[:string.find("]")+1]

return (firstSegment, secondSegment)

def waitForResponse(self):
response = self.server_sock.recv(1024).decode("utf-8")
print(response)
if(self.gameOver):
return "GAME OVER"

response = self.server_sock.recv(4096).decode("utf-8")
if(response.find("Opponent wins!") != -1):
self.gameOver = True
elif(response.find("You win!") != -1):
self.gameOver = True
elif(response.find("Error") != -1):
self.gameOver = True
print("----------------------\n" + response + "----------------------")
return response

def send(self, string):
if(self.gameOver):
return "GAME OVER"

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

def sendREMOVE(self, piece):
self.send(piece)

def sendMOVE(self, start, end):
string = "{0}:{1}".format(start, end)
#print("I AM SENDING THIS MOVE: " + string)
self.send(string)

def waitREMOVE(self):
response = self.waitForResponse()
return self.extractREMOVED(response)

def waitMOVE(self, myTurn):
response = dummyResponse = self.waitForResponse()
# wait til i receive '?Move', so I know can transmit once it's my turn
while(not myTurn and dummyResponse.find("?Move") == -1):
#print("hit")
dummyResponse = self.waitForResponse()


#print("I JUST GOT THIS MOVE:" + response)
return self.extractMOVED(response)

def initialize(self, firstMove):
# Create socket and connect to server
Expand Down
7 changes: 4 additions & 3 deletions state.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ def eval(self, board, playerColor, opponentColor):
if self.playerMode == 2:
return len(player_moves)/len(opponent_moves)
if self.playerMode == 3:
return len(player_moves)/(len(oppenent_moves)*2)
return len(player_moves)/(len(opponent_moves)*2)
if self.playerMode == 4:
return len(player_moves)/(len(opponent_moves)*3)
if self.playerMode == 5:
Expand All @@ -274,7 +274,8 @@ def eval(self, board, playerColor, opponentColor):
def makeMove(self, color):
if len(self.generate_moves(self.board, color)) > 0:
move = Minimax(self, 0, color, math.inf * -1, math.inf)
print(move)
self.movePiece(move[1][0], move[1][1])
#self.movePiece(move[1][0], move[1][1])
return move[1]
else:
self.gameOver = 1
return 1

0 comments on commit bbe2182

Please sign in to comment.