Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Konane/Konane.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
104 lines (82 sloc)
3.34 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import client | |
import sys | |
import state | |
import math | |
# Top level class that has the client and the state | |
class Konane: | |
def __init__(self, pretty=False): | |
self.client = client.ArtemisClient("artemis.engr.uconn.edu", 4705, pretty) | |
self.state = state.State() | |
self.playerNumber = None | |
self.color = None | |
self.player = None | |
self.pretty = pretty | |
def stringToCoords(self, string): | |
coords = [] | |
coords.append(int(string[string.find("[")+1:string.find(":")])) | |
coords.append(int(string[string.find(":")+1:string.find("]")])) | |
# convert from BOTTOM-lowest to TOP-lowest | |
newCoords = [17-coords[0], coords[1]] | |
return newCoords | |
def coordsToString(self, coords): | |
#convert from TOP-lowest to BOTTOM-lowest | |
newCoords = [17-coords[0], coords[1]] | |
return "[{0}:{1}]".format(newCoords[0], newCoords[1]) | |
def start(self): | |
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 | |
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 | |
if(self.pretty): | |
print(self.state) | |
self.client.close() | |
k = Konane() | |
k.start() |