diff --git a/Konane.py b/Konane.py index 95a9b8d..ffd9f4c 100644 --- a/Konane.py +++ b/Konane.py @@ -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 = [] @@ -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() diff --git a/__pycache__/client.cpython-36.pyc b/__pycache__/client.cpython-36.pyc index 8531e5b..dec3fe6 100644 Binary files a/__pycache__/client.cpython-36.pyc and b/__pycache__/client.cpython-36.pyc differ diff --git a/__pycache__/state.cpython-36.pyc b/__pycache__/state.cpython-36.pyc index 8e25d00..7bc1eab 100644 Binary files a/__pycache__/state.cpython-36.pyc and b/__pycache__/state.cpython-36.pyc differ diff --git a/client.py b/client.py index 7ffeec1..98fa29f 100644 --- a/client.py +++ b/client.py @@ -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 diff --git a/state.py b/state.py index 01e29ec..84ef76f 100644 --- a/state.py +++ b/state.py @@ -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: @@ -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