def runHuman(self, depth, side): #@parm side: str -- "white" or "black" if side == "white": initMove = move.Move("a2a2") self.gameState() while True: moveStr = str(input("Your Move: ")) humanMove = move.Move(moveStr) self.makeMove(humanMove) #print(nextMove.mString) #self.gameState() nextMove = self.negaMax(depth, BLACK, initMove) self.makeMove(nextMove) print(f"Jerry's move: {nextMove.mString}") self.gameState() if side == "black": initMove = move.Move("a2a2") self.gameState() while True: nextMove = self.negaMax(depth, WHITE, initMove) self.makeMove(nextMove) print(f"Jerry's move: {nextMove.mString}") self.gameState() moveStr = str(input("Your Move: ")) humanMove = move.Move(moveStr) self.makeMove(humanMove)
def getMoves(self, board, ignoreCastles=False): moves = [] ## Check for castling if not ignoreCastles: if self.noMoves == 0 and not board.checkCheck(self.colour): kingSidePieces = [board.getPiece((i, self.coords[1])) for i in range(5, 8)] queenSidePieces = [board.getPiece((i, self.coords[1])) for i in range(0, 5)] if kingSidePieces[2] != False and kingSidePieces[2].noMoves == 0 and kingSidePieces[0] == False and kingSidePieces[1] == False: possible = True for x in range(6, 8): tempKing = King(self.colour, (x, self.coords[1])) board.piecesGroup.add(tempKing) if board.checkCheck(self.colour): possible = False board.piecesGroup.remove(tempKing) if possible: moves.append(move.Move((6, self.coords[1]), specialMove="Castle", thirdParty=(kingSidePieces[2], (5, self.coords[1])))) if queenSidePieces[0] != False and queenSidePieces[0].noMoves == 0 and queenSidePieces[1] == False and queenSidePieces[2] == False and queenSidePieces[3] == False: possible = True for x in range(1, 5): tempKing = King(self.colour, (x, self.coords[1])) board.piecesGroup.add(tempKing) if board.checkCheck(self.colour): possible = False board.piecesGroup.remove(tempKing) if possible: moves.append(move.Move((2, self.coords[1]), specialMove="Castle", thirdParty=(queenSidePieces[0], (3, self.coords[1])))) return moves + self.getStraightMoves(board, limit=1) + self.getDiagonalMoves(board, limit=1)
def getMoveThatFillsMainDiagonal(self, state, aSymbol): symbolCounter = 0 emptyLine = 0 emptyColumn = 0 myMove = move.Move(0, 0) if state.emptyPosition(1, 1): emptyLine = 1 emptyColumn = 1 else: if (state.getValue(1, 1) == aSymbol): symbolCounter+=1 if state.emptyPosition(2, 2): emptyLine = 2 emptyColumn = 2 else: if (state.getValue(2, 2) == aSymbol): symbolCounter+=1 if state.emptyPosition(3, 3): emptyLine = 3 emptyColumn = 3 else: if (state.getValue(3, 3) == aSymbol): symbolCounter=symbolCounter+1 if symbolCounter==2: myMove = move.Move(emptyLine, emptyColumn) return myMove
def getDiagonalMoves(self, board, limit=False): """ Gets all possible moves in the diagonals from a given piece. Returns a list of tuples, representing the co-ords which can be moved to.""" possibleDirections = [(1,1), (-1,1), (1,-1), (-1,-1)] moves = [] if not limit: for i in range(1, min([7-self.coords[0], 7-self.coords[1]]) + 1): moves.append(move.Move((self.coords[0] + i, self.coords[1] + i))) if not board.isSquareEmpty((self.coords[0] + i, self.coords[1] + i)): break for i in range(1, min([7-self.coords[0], self.coords[1]]) + 1): moves.append(move.Move((self.coords[0] + i, self.coords[1] - i))) if not board.isSquareEmpty((self.coords[0] + i, self.coords[1] - i)): break for i in range(1, min([self.coords[0], self.coords[1]]) + 1): moves.append(move.Move((self.coords[0] - i, self.coords[1] - i))) if not board.isSquareEmpty((self.coords[0] - i, self.coords[1] - i)): break for i in range(1, min([self.coords[0], 7-self.coords[1]]) + 1): moves.append(move.Move((self.coords[0] - i, self.coords[1] + i))) if not board.isSquareEmpty((self.coords[0] - i, self.coords[1] + i)): break else: for i in range(4): for j in range(1, limit+1): possibleMove = (self.coords[0]+(possibleDirections[i][0]*j), (self.coords[1]+(possibleDirections[i][1]*j))) moves.append(move.Move(possibleMove)) if not board.isSquareEmpty((possibleMove[0], possibleMove[1])): break return moves
def getMove3Way1(self, state): self.moveOrder+=1 if state.emptyPosition(2, 1): myMove = self.getBasicMove(state) else: if ((state.emptyPosition(3, 2)) and (state.emptyPosition(3, 3))): myMove = move.Move(3, 3) else: myMove = move.Move(1, 3) return myMove
def getMove3Way2(self, state): if state.emptyPosition(1, 2): myMove = self.getBasicMove(state) else: self.moveOrder+=1 if (state.emptyPosition(2, 3)): myMove = move.Move(3, 3) else: myMove = move.Move(3, 1) return myMove
def getMove3Way3(self, state): if state.occupiedPosition(3, 1): self.moveOrder+=1 myMove = move.Move(1, 3) else: if state.occupiedPosition(1, 3): self.moveOrder+=1 myMove = move.Move(3, 1) else: myMove = self.getBasicMove(state) return myMove
def getMove2(self, state): self.moveOrder+=1 if state.occupiedPosition(2, 2): self.strategyWay = 3 myMove = move.Move(3, 3) else: if ((state.occupiedPosition(2, 1)) or (state.occupiedPosition(2, 3)) or (state.occupiedPosition(3, 1)) ): self.strategyWay = 2 myMove = move.Move(1, 3) else: self.strategyWay = 1 myMove = move.Move(3, 1) return myMove
def setup(): mover = move.Move() mover.fight() mover.recharge() net = network.Network("", 8081) recThread = threading.Thread(target=net.startListening) recThread.start() while True: time.sleep(.1) if net.incomingVoiceText is not None: word = net.incomingVoiceText.lower() if "north" in word: mover.one_forward(2) if "south" in word: mover.one_backward(2) if "west" in word: mover.turn_left(1) mover.one_forward(2.5) mover.turn_right(.9) if "east" in word: mover.turn_right(1) mover.one_forward(2.5) mover.turn_left(1.1) if "fight" in word: mover.fight() if "recharge" in word: mover.recharge() net.incomingVoiceText = None
def knightMoves(self, row, col): #check all eight possible squares moveList = [] if self.nextTurn == WHITE: sideConst = 1 else: sideConst = -1 #going clockwise starting with top right . . . rowAdd = [-2, -1, 1, 2, 2, 1, -1, -2] colAdd = [1, 2, 2, 1, -1, -2, -2, -1] for i in range(0, 8): inB = inBound(row + rowAdd[i], col + colAdd[i]) if inB: tSquare = self.board[row + rowAdd[i]][col + colAdd[i]] * sideConst if tSquare <= 0: moveList.append( move.Move([[row, col], [row + rowAdd[i], col + colAdd[i]]])) return moveList
def demo_tour(odrv0) : move = m.Move(odrv0) # Fait 3 tours de carreaux (chez Martial) for i in range(0,12): move.translation(350) time.sleep(2) move.rotation(-90) time.sleep(2)
def all_possible_moves(self): options = [] spaces = self._board.space_iterator() for space in spaces: act = move.Move(space) options.append(act) return options
def mateCheck(self, currentPlayer): if self.checkCheck(currentPlayer): for piece in self.piecesGroup.sprites(): if piece.colour == currentPlayer: pieceCoords = piece.coords[:] for possibleMove in piece.getMoves(self): nextMove = possibleMove.coords if nextMove[0] > 0 and nextMove[1] > 0: if self.isSquareEmpty(nextMove): self.movePiece(piece, move.Move(nextMove)) if not self.checkCheck(currentPlayer): print("No Take", piece.piece, piece.colour, nextMove) self.movePiece(piece, move.Move(pieceCoords)) return False self.movePiece(piece, move.Move(pieceCoords)) else: targetPiece = self.getPiece(nextMove) if targetPiece.colour != piece.colour: temp = targetPiece self.takePiece(piece, targetPiece, move.Move(nextMove)) if not self.checkCheck(currentPlayer): print("Take", piece.piece, piece.colour, nextMove) self.piecesGroup.add(temp) self.movePiece(piece, move.Move(pieceCoords)) return False self.piecesGroup.add(temp) self.movePiece(piece, move.Move(pieceCoords)) self.movePiece(piece, move.Move(pieceCoords)) return True return False
def getMoves(self, board): possibleDirections = [(2, 1), (2, -1), (-2, 1), (-2, -1), (1, 2), (1, -2), (-1, -2), (-1, 2)] moves = [] for d in possibleDirections: possible = (self.coords[0] + d[0], self.coords[1] + d[1]) moves.append(move.Move(possible)) return moves
def getMove2(self, state): if (state.emptyPosition(3, 3)): self.strategyWay = 1 myMove = self.getBasicMove(state) else: self.strategyWay = 2 self.moveOrder+=1 myMove = move.Move(3, 1) return myMove
def add(self, selection1): selection = m.Move(selection1.locat) x = selection.corner[0] y = selection.corner[1] #print("corner =" , selection.corner) for r in range(len(selection.layout)): for c in range(len(selection.layout[r])): self.board[r + y][c + x] = (self.board[r + y][c + x] + selection.layout[r][c]) % 2
def createMoveJabAPart2(self): f = [ self.frameData(14, 6), self.frameData(15, 30)] t = [ ['releaseA', move.Transition(-1, None, 1, 1, 'throw2')], ['exitFrame', move.Transition(-1, None, None, None, 'throw2')]] self.moves['jabA2'] = move.Move(f, t) self.moves['jabA2'].canDI = False
def demo_simple(odrv0) : move = m.Move(odrv0) time.sleep(1) move.translation(1500) time.sleep(2) move.rotation(180) time.sleep(2) move.translation(1000)
def createMoveThrow1(self): f = [ self.frameData(11, 3), self.frameData(12, 1), self.frameData(13, 9) ] self.moves['throw1'] = move.Move(f, []) self.moves['throw1'].canDI = False self.moves['throw1'].shoot.append( (1, 0, (35, -46)) ) self.moves['throw1'].shoot.append( (1, 1, (35, -46)) ) self.moves['throw1'].shoot.append( (1, 2, (35, -46)) )
def click(self, line, column): if (self.getStatus() == 1): self.startMatch() else: if (self.getStatus() == 2 or self.getStatus() == 3): aMove = move.Move(line, column) self.proceedMove(aMove) else: if (self.getStatus() == 4 or self.getStatus() == 5): self.reset()
def allmoves(self, color, pos=None): l = [] if not pos: for row in self.squares: for s in row: if s.p.color == color: for li in self.squares: for e in li: m = move.Move(self, s.coord, e.coord) if m.is_legal(): l.append(m) else: s = self.squares[pos[0]][pos[1]] for li in self.squares: for e in li: m = move.Move(self, s.coord, e.coord) if m.is_legal(): l.append(m) return l
def getAnyPossibleMove(self, state): myMove = None for x in range(3): aLine=x+1 for y in range(3): aColumn=y+1 if state.emptyPosition(aLine, aColumn): myMove = move.Move(aLine, aColumn) return myMove return myMove
def takeback(): if b.last_move != None: if len(b.last_move) == 2: laststart, lastfinish = b.last_move[0], b.last_move[1] backmove = move.Move(b, lastfinish.coord, laststart.coord) b.update(backmove) b.append_piece(lastfinish.p, *lastfinish.coord) if len(b.last_move) == 4: laststart1, lastfinish1 = b.last_move[0], b.last_move[1] laststart2, lastfinish2 = b.last_move[2], b.last_move[3] backmove2 = move.Move(b, lastfinish2.coord, laststart2.coord) backmove1 = move.Move(b, lastfinish1.coord, laststart1.coord) b.update(backmove2) b.update(backmove1) if len(b.last_move) == 3: laststart, lastfinish = b.last_move[0], b.last_move[1] b.append_piece(piece.Pawn(b.last_move[2]), *lastfinish.coord) backmove = move.Move(b, lastfinish.coord, laststart.coord) b.update(backmove) b.append_piece(lastfinish.p, *lastfinish.coord)
def preStitch(self): ANGLE = 360 / constants.STITCH_COUNT m = move.Move() for i in range(constants.STITCH_COUNT): m.stop() rospy.sleep(0.5) cv2.imwrite( constants.STITCH_IMAGE_COMMON_PATH_PREFIX + str(i) + ".png", self.image) m.rotate("right", ANGLE) m.stop()
def getStraightMoves(self, board, limit=8): """ Gets all possible moves in the horizontal and vertical directions from a given piece. Returns a list of tuples, representing the co-ords which can be moved to.""" directions = [(1, 0), (-1, 0), (0, 1), (0, -1)] moves = [] for d in directions: distance = 1 nextMove = (self.coords[0] + distance*d[0], self.coords[1] + distance*d[1]) while distance <= limit and board.isSquareEmpty(nextMove) and nextMove[0] in range(0, 8) and nextMove[1] in range(0, 8): moves.append(move.Move(nextMove)) distance += 1 nextMove = (self.coords[0] + distance*d[0], self.coords[1] + distance*d[1]) if not board.isSquareEmpty(nextMove): moves.append(move.Move(nextMove)) for m in moves: if m.coords[0] < 0 or m.coords[1] < 0: moves.remove(m) return moves
def range(self, color): l = set() for row in self.squares: for s in row: if s.p.color == color: for li in self.squares: for e in li: m = move.Move(self, s.coord, e.coord) m.is_possible() if m.taking: l.add(m.finish) return l
def movePiece(self, piece, moveObj): piece.coords[0] = moveObj.coords[0] piece.coords[1] = moveObj.coords[1] piece.rect.x = piece.coords[0] * SQUAREWIDTH piece.rect.y = piece.coords[1] * SQUAREWIDTH if moveObj.specialMove == "Castle": self.movePiece(moveObj.thirdParty[0], move.Move(moveObj.thirdParty[1])) elif moveObj.specialMove == "EP": self.takePiece(piece, moveObj.thirdParty, moveObj, EP=True) piece.noMoves += 1
def getMoveThatFillsColumn(self, state, aSymbol): symbolCounter = 0 emptyLine = 0 emptyColumn = 0 myMove = move.Move(0, 0) for y in range(3): aColumn=y+1 if (state.occupiedPositionsOnTheColumn(aColumn) == 2): symbolCounter = 0 emptyLine = 0 emptyColumn = 0 for x in range(3): aLine = x+1 if state.emptyPosition(aLine, aColumn): emptyLine = aLine emptyColumn = aColumn else: if (state.getValue(aLine, aColumn) == aSymbol): symbolCounter=symbolCounter+1 if symbolCounter==2: myMove = move.Move(emptyLine, emptyColumn) return myMove
def pawnMoves(self, row, col): #TODO Promotions #1. check if pawn is in starting positon based on what side its on #2. if so, give ability to move up two squares moveList = [] if self.nextTurn == WHITE: # check if this is a promotion move if row == 1: promotion = True else: promotion = False if self.board[row - 1][col] == 0: moveList.append( move.Move([[row, col], [row - 1, col]], promotion)) if row == 6: # handle checking for double jumping for pawn--WHITE if self.board[row - 2][col] == 0: moveList.append(move.Move([[row, col], [row - 2, col]])) # checking if black pieces are in immediate diag if col > 0: if self.board[row - 1][col - 1] < 0: moveList.append( move.Move([[row, col], [row - 1, col - 1]], promotion)) if col < 7: if self.board[row - 1][col + 1] < 0: moveList.append( move.Move([[row, col], [row - 1, col + 1]], promotion)) else: #BLACK TODO add sideConst method to this # check if this is a promotion move if row == 6: promotion = True else: promotion = False if self.board[row + 1][col] == 0: moveList.append(move.Move([[row, col], [row + 1, col]])) if row == 1: if self.board[row + 2][col] == 0: moveList.append(move.Move([[row, col], [row + 2, col]])) # checking if white pieces are in imediate diag if col > 0: if self.board[row + 1][col - 1] > 0: moveList.append( move.Move([[row, col], [row + 1, col - 1]], promotion)) if col < 7: if self.board[row + 1][col + 1] > 0: moveList.append( move.Move([[row, col], [row + 1, col + 1]], promotion)) return moveList
def run_test(odrv0) : # Strategie proposé de parcour com = Communication('/dev/ttyACM0') move = m.Move(odrv0) print("Initialisation des Actionneurs...") com.waitEndMove(Communication.MSG["Initialisation"], True) print("Initilisation DONE") time.sleep(1) print("Palet_Floor_In...") com.send(Communication.MSG["Palet_Floor_In"]) print("Waiting moving forward...") while not com.Avancer: com.read(True) time.sleep(.1) #ICI ON AVANCE move.translation(500) print("Moving forward DONE") com.send(Communication.MSG["Action_Finished"]) print("Waiting moving backward...") while not com.Reculer: com.read(True) time.sleep(.1) #ICI ON TOURNE move.rotation(-90) print("Rotation DONE") com.send(Communication.MSG["Action_Finished"]) print("Waiting end of movement.") while not com.readyNext: com.read(True) #ICI ON RECULE move.translation(-500) print("Moving backward DONE") com.send(Communication.MSG["Action_Finished"]) print("Waiting end of movement.") while not com.readyNext: com.read(True) print("Palet_Floor_In DONE.") time.sleep(1) print("Transport...") com.waitEndMove(Communication.MSG["Transport"], True) print("Transport DONE") time.sleep(1) print("Turn off robot...") com.send(Communication.MSG["Arret"]) print("Robot is turned off!")