class ChessBoardTest(tkinter.Frame): def __init__(self, parent, squareWidth=64, squareHeight=64): self.player = 'w' self.moveNum = 1 tkinter.Frame.__init__(self, parent) self.parent = parent self.chessState = ChessState(Common.initChessFEN) self.cb = ChessBoard(self) self.cb.setState(self.chessState) self.cb.refreshCanvasFromState() self.cb.pack() self.b = tkinter.Button(self, text="flip", command=self.flipIt) self.b.pack() self.b2 = tkinter.Button(self, text="clear", command=self.clearIt) self.b2.pack() #self.b = tkinter.Button(self, text="html", command=self.html) # self.b.pack() self.moveEntry = tkinter.Entry(self) self.moveEntry.pack() self.execMove = tkinter.Button(self, text="execute move", command=self.executeMove) self.execMove.pack() def clearIt(self): self.cb.clear() def flipIt(self): self.cb.flip() def executeMove(self): whatMove = self.moveEntry.get() print("executing: " + whatMove) move = ChessMove(self.player, self.moveNum, whatMove) self.chessState = self.chessState.transition(move) self.cb.setState(self.chessState) print("new state: " + str(self.chessState)) self.player = {'w': 'b', 'b': 'w'}[self.player] self.moveNum += 1
class ChessBoardTest(Tkinter.Frame): def __init__(self, parent, squareWidth=64, squareHeight=64): self.player = "w" self.moveNum = 1 Tkinter.Frame.__init__(self, parent) self.parent = parent self.chessState = ChessState(Common.initChessFEN) self.cb = ChessBoard(self) self.cb.setState(self.chessState) self.cb.refreshCanvasFromState() self.cb.pack() self.b = Tkinter.Button(self, text="flip", command=self.flipIt) self.b.pack() self.b2 = Tkinter.Button(self, text="clear", command=self.clearIt) self.b2.pack() # self.b = Tkinter.Button(self, text="html", command=self.html) # self.b.pack() self.moveEntry = Tkinter.Entry(self) self.moveEntry.pack() self.execMove = Tkinter.Button(self, text="execute move", command=self.executeMove) self.execMove.pack() def clearIt(self): self.cb.clear() def flipIt(self): self.cb.flip() def executeMove(self): whatMove = self.moveEntry.get() print "executing: " + whatMove move = ChessMove(self.player, self.moveNum, whatMove) self.chessState = self.chessState.transition(move) self.cb.setState(self.chessState) print "new state: " + str(self.chessState) self.player = {"w": "b", "b": "w"}[self.player] self.moveNum += 1
class ChessBoardTest(Tkinter.Frame): def __init__(self, parent, pieceWidth=48, pieceHeight=48): Tkinter.Frame.__init__(self, parent) self.parent = parent self.chessState = ChessState(Common.initChessFEN) self.cb = ChessBoard(self) self.cb.setState(self.chessState) self.cb.draw() self.cb.pack() self.b = Tkinter.Button(self, text="flip", command=self.flipIt) self.b.pack() self.b = Tkinter.Button(self, text="html", command=self.html) self.b.pack() self.moveEntry = Tkinter.Entry(self) self.moveEntry.pack() self.execMove = Tkinter.Button(self, text="execute move", command=self.executeMove) self.execMove.pack() def flipIt(self): self.cb.flip() self.cb.draw() def html(self): print self.cb.draw_html() def executeMove(self): whatMove = self.moveEntry.get() print "Executing: " + whatMove self.chessState = self.chessState.transition(whatMove) self.cb.setState(self.chessState) self.cb.draw()
class OccupancyProcessor: def __init__(self): self.stateSave = ChessState(Common.initChessFEN) self.stateCurr = ChessState(Common.initChessFEN) self.moves = [] self.lastOcc = [ \ 1, 1, 1, 1, 1, 1, 1, 1, \ 1, 1, 1, 1, 1, 1, 1, 1, \ 0, 0, 0, 0, 0, 0, 0, 0, \ 0, 0, 0, 0, 0, 0, 0, 0, \ 0, 0, 0, 0, 0, 0, 0, 0, \ 0, 0, 0, 0, 0, 0, 0, 0, \ 1, 1, 1, 1, 1, 1, 1, 1, \ 1, 1, 1, 1, 1, 1, 1, 1] def update(self, occ): print "INFO: awaiting move by player: %s" % self.stateCurr.activePlayer # discard duplicates if occ == self.lastOcc: print "INFO: repeat state received, ignoring..." return # determine what squares were involved in the move srcSqrIdx = None dstSqrIdx = None for i in range(64): if self.lastOcc[i] == 1 and occ[i] == 0: if srcSqrIdx != None: raise Exception('multiple pieces moved! %s and %s are potential source squares!' % \ (Common.idxToSquare(srcSqrIdx), Common.idxToSquare(i))) srcSqrIdx = i if self.lastOcc[i] == 0 and occ[i] == 1: if dstSqrIdx != None: raise Exception('multiple pieces moved! %s and %s are potential destination squares!' % \ (Common.idxToSquare[srcSqrIdx], Common.idxToSquare[i])) dstSqrIdx = i if srcSqrIdx and not dstSqrIdx: raise Exception('a piece left %s but never appeared anywhere!' % Common.idxToSquare[srcSqrIdx]) if dstSqrIdx and not srcSqrIdx: raise Exception('a piece appeared at %s but never left anywhere!' % Common.idxToSquare[dstSqrIdx]) srcSqr = Common.idxToSquare[srcSqrIdx] dstSqr = Common.idxToSquare[dstSqrIdx] moveText = '%s%s' % (srcSqr, dstSqr) # discard multiple moves by the same player (favor the last move) # whom = Common.coloredPieceToPlayer(self.stateCurr.squares[srcSqr]) if whom != self.stateCurr.activePlayer: print "INFO: another move by previous player, recalculating..." if len(self.moves): m = ChessMove(self.stateCurr.activePlayer, self.stateCurr.fullMoveNum, moveText) self.stateCurr = self.stateSave.transition(m) self.moves[-1] = '%s%s' % (Common.idxToSquare[srcSqrIdx], Common.idxToSquare[dstSqrIdx]) self.lastOcc = occ return # otherwise it's a normal move # m = ChessMove(self.stateCurr.activePlayer, self.stateCurr.fullMoveNum, moveText) print "INFO: transitioning on move %s" % str(m) self.stateSave = self.stateCurr self.stateCurr = self.stateCurr.transition(m) self.moves.append(moveText) self.lastOcc = occ # finished def done(self): # TODO: construct PGN text pass
class OccupancyProcessor: def __init__(self): self.stateSave = ChessState(Common.initChessFEN) self.stateCurr = ChessState(Common.initChessFEN) self.moves = [] self.lastOcc = [ \ 1, 1, 1, 1, 1, 1, 1, 1, \ 1, 1, 1, 1, 1, 1, 1, 1, \ 0, 0, 0, 0, 0, 0, 0, 0, \ 0, 0, 0, 0, 0, 0, 0, 0, \ 0, 0, 0, 0, 0, 0, 0, 0, \ 0, 0, 0, 0, 0, 0, 0, 0, \ 1, 1, 1, 1, 1, 1, 1, 1, \ 1, 1, 1, 1, 1, 1, 1, 1] def update(self, occ): print "INFO: awaiting move by player: %s" % self.stateCurr.activePlayer # discard duplicates if occ == self.lastOcc: print "INFO: repeat state received, ignoring..." return # determine what squares were involved in the move srcSqrIdx = None dstSqrIdx = None for i in range(64): if self.lastOcc[i] == 1 and occ[i] == 0: if srcSqrIdx != None: raise Exception('multiple pieces moved! %s and %s are potential source squares!' % \ (Common.idxToSquare(srcSqrIdx), Common.idxToSquare(i))) srcSqrIdx = i if self.lastOcc[i] == 0 and occ[i] == 1: if dstSqrIdx != None: raise Exception('multiple pieces moved! %s and %s are potential destination squares!' % \ (Common.idxToSquare[srcSqrIdx], Common.idxToSquare[i])) dstSqrIdx = i if srcSqrIdx and not dstSqrIdx: raise Exception('a piece left %s but never appeared anywhere!' % Common.idxToSquare[srcSqrIdx]) if dstSqrIdx and not srcSqrIdx: raise Exception('a piece appeared at %s but never left anywhere!' % Common.idxToSquare[dstSqrIdx]) srcSqr = Common.idxToSquare[srcSqrIdx] dstSqr = Common.idxToSquare[dstSqrIdx] moveText = '%s%s' % (srcSqr, dstSqr) # discard multiple moves by the same player (favor the last move) # whom = Common.coloredPieceToPlayer(self.stateCurr.squares[srcSqr]) if whom != self.stateCurr.activePlayer: print "INFO: another move by previous player, recalculating..." if len(self.moves): self.stateCurr = self.stateSave.transition(moveText) self.moves[-1] = '%s%s' % (Common.idxToSquare[srcSqrIdx], Common.idxToSquare[dstSqrIdx]) self.lastOcc = occ return # otherwise it's a normal move # print "INFO: transitioning on move..." self.stateSave = self.stateCurr self.stateCurr = self.stateCurr.transition(moveText) self.moves.append(moveText) self.lastOcc = occ # finished def done(self): # TODO: construct PGN text pass