def update(self, ctx): x = ctx.getServerMsg().getX() y = ctx.getServerMsg().getY() ws = ctx.getServerMsg().getWhiteScore() bs = ctx.getServerMsg().getBlackScore() sendersays = ctx.getServerMsg().getSendersays() if x == -1 and y == -1: # First move of this client or illegal move of this client if ctx.getBoard().getOldSquare() == None: # First move ctx.getBoard().getPlayer().setPieceColor("* { background-color: rgb(255,255,255) }") # white ctx.getBoard().getOpponent().setPieceColor("* { background-color: rgb(0,0,0) }") # black ctx.setState(WF_USER()) ctx.getSignalEmitter().emitStatusBarText(sendersays) else: # Illegal move ctx.setState(WF_USER()) if ctx.getBoard().getOldSquare().getPiece() != None: # If there was a piece on the square ctx.getBoard().getSquare(ctx.getBoard().getOldSquare().getX(), ctx.getBoard().getOldSquare().getY()).setPiece(ctx.getBoard().getOldSquare().getPiece()) ctx.getSignalEmitter().emitMove(ctx.getBoard().getOldSquare().getX(), ctx.getBoard().getOldSquare().getY(), ctx.getBoard().getOldSquare().getPiece().getColor()) else: ctx.getBoard().getSquare(ctx.getBoard().getOldSquare().getX(), ctx.getBoard().getOldSquare().getY()).setPiece(ctx.getBoard().getOldSquare().getPiece()) ctx.getSignalEmitter().emitMove(ctx.getBoard().getOldSquare().getX(), ctx.getBoard().getOldSquare().getY(), "* { background-color: rgb(232,192,134) }") ctx.getSignalEmitter().emitStatusBarText(sendersays) else: # Legal move of opponent if ctx.getBoard().getOldSquare() == None: # The client sets the colors of players' pieces ctx.getBoard().getPlayer().setPieceColor("* { background-color: rgb(0,0,0) }") # black ctx.getBoard().getOpponent().setPieceColor("* { background-color: rgb(255,255,255) }") # white piece = Piece(ctx.getBoard().getOpponent().getPieceColor()) ctx.getBoard().addPiece(piece, x, y) ctx.setState(WF_USER()) ctx.getSignalEmitter().emitMove(x, y, piece.getColor()) ctx.getSignalEmitter().emitStatusBarText(sendersays) ctx.getSignalEmitter().emitWhiteScore(ws) ctx.getSignalEmitter().emitBlackScore(bs) ctx.getSignalEmitter().emitLastMove("(" + str(x) + ", " + str(y) + ")")
def move(self, ctx, x, y): piece = Piece(ctx.getBoard().getPlayer().getPieceColor()) ctx.getBoard().addPiece(piece, x, y) movepdu = MovePDU() movepdu.setX(x) movepdu.setY(y) ctx.fgpclient.send(movepdu) ctx.getSignalEmitter().emitMove(x, y, piece.getColor()) ctx.getSignalEmitter().emitStatusBarText("Waiting for move confirmation.") ctx.setState(WF_MOVE_CONFIRM()) ctx.fgpclient.listen()
def box_to_board(box): board = [] red_hor = box[0] red_ver = box[1] blue_hor = box[2] blue_ver = box[3] masters = box[4] for i in range(len(box[0][0])): board.append([]) for j in range(len(box[0][0])): if red_hor[i][j] == 1: if masters[i][j] == 1: board[-1].append(Piece(0, 0, 1)) else: board[-1].append(Piece(0, 0, 0)) elif red_ver[i][j] == 1: if masters[i][j] == 1: board[-1].append(Piece(0, 1, 1)) else: board[-1].append(Piece(0, 1, 0)) elif blue_hor[i][j] == 1: if masters[i][j] == 1: board[-1].append(Piece(1, 0, 1)) else: board[-1].append(Piece(1, 0, 0)) elif blue_ver[i][j] == 1: if masters[i][j] == 1: board[-1].append(Piece(1, 1, 1)) else: board[-1].append(Piece(1, 1, 0)) else: board[-1].append(0) return board
def gameover(self, ctx): x = ctx.getServerMsg().getX() y = ctx.getServerMsg().getY() ws = ctx.getServerMsg().getWhiteScore() bs = ctx.getServerMsg().getBlackScore() sendersays = ctx.getServerMsg().getSendersays() if ctx.getBoard().isBoardFull() == False: piece = Piece(ctx.getBoard().getOpponent().getPieceColor()) ctx.getBoard().addPiece(piece, x, y) ctx.getSignalEmitter().emitMove(x, y, piece.getColor()) ctx.setState(IDLE()) ctx.getSignalEmitter().emitStatusBarText(sendersays) ctx.getSignalEmitter().emitWhiteScore(ws) ctx.getSignalEmitter().emitBlackScore(bs) ctx.getSignalEmitter().emitLastMove("(" + str(x) + ", " + str(y) + ")")
def copyGrid(self, grid): newGrid = [] for row in range(len(grid)): currentRow = [] for column in range(len(grid)): currentRow.append( Piece(row, column, grid[row][column].getToken())) newGrid.append(currentRow) return newGrid
def generateGrid(self, gridlength, compressionString): currentGrid = [] currentRow = [] for index in range(len(compressionString)): currentCharacter = compressionString[index] currentRow.append( Piece(index // gridlength, index % gridlength, currentCharacter)) if index % gridlength == gridlength - 1: currentGrid.append(currentRow) currentRow = [] return currentGrid
def getAvailableMoves(self, Piece): #TODO:Finish function totalMovelist = [] FromSpot = Piece.getPosition() if self.player_number == 1: #Checking Forward self.getMove(totalMovelist, FromSpot, 1, 0, " ") #Checking right diagonally self.getMove(totalMovelist, FromSpot, 1, 1, "Y") #Checking left diagonally self.getMove(totalMovelist, FromSpot, 1, -1, "Y") else: #Checking Forward self.getMove(totalMovelist, FromSpot, -1, 0, " ") #Checking right diagonally self.getMove(totalMovelist, FromSpot, -1, -1, "X") #Checking left diagonally self.getMove(totalMovelist, FromSpot, -1, 1, "X") return totalMovelist