def allMoves(self, pieceMap, currentPos) -> list[BoardMove]: moves = [] for x in range(len(pieceMap)): for y in range(len(pieceMap[x])): if self.canMove(pieceMap, currentPos, BoardPosition(x, y)): triggerChange = BoardChange(currentPos, BoardPosition(x, y)) boardMove = BoardMove(triggerChange) moves.append(boardMove) return moves
def clickField(self, x, y): # Gibt den Klick an den GameManager weiter, # Je nach Spielstand, ist es ein selectPiece Klick # oder ein Move Klick if self._gameManager.isPieceSelected is False: if self._gameManager.selectPiece(BoardPosition(x, y)): self.view.update() else: if self._gameManager.moveSelectedPiece(BoardPosition(x, y)): self.view.update() else: self._gameManager.unselectPiece() self.view.update()
def canMove(self, pieceMap, currentPos, toPos): pieceAtTo = self._getPiece(pieceMap, toPos) if pieceAtTo is None: if self.moveDir == Direction.UP: x1 = toPos.X x2 = currentPos.X y1 = toPos.Y y2 = currentPos.Y else: x1 = toPos.X x2 = currentPos.X y1 = currentPos.Y y2 = toPos.Y if currentPos.Y == 1: p = self._getPiece(pieceMap, BoardPosition( currentPos.X, currentPos.Y + 1)) if p == None: moveDistance = 2 else: moveDistance = 1 elif currentPos.Y == 6: p = self._getPiece(pieceMap, BoardPosition( currentPos.X, currentPos.Y - 1)) if p == None: moveDistance = 2 else: moveDistance = 1 else: moveDistance = 1 if(x1 is x2 and (y1 - y2 >= 0 and y1 - y2 <= moveDistance)): return True elif pieceAtTo.color != self.color: if self.moveDir == Direction.UP: x1 = toPos.X x2 = currentPos.X y1 = toPos.Y y2 = currentPos.Y else: x1 = toPos.X x2 = currentPos.X y1 = currentPos.Y y2 = toPos.Y if(abs(x1-x2) == 1 and (y1 - y2 == 1)): return True return False
def _getAllPiecePosOfColor(self, color): piecePos = [] for x in range(len(self._pieceMap)): for y in range(len(self._pieceMap)): piece = self._pieceMap[x][y] if piece is not None and piece.color is color: piecePos.append(BoardPosition(x, y)) return piecePos
def checkIfSomethingBetweenVertical(self, pieceMap, pos1, pos2): somethingBetween = False if pos1.Y < pos2.Y: yStep = 1 else: yStep = -1 if pos1.X < pos2.X: xStep = 1 else: xStep = -1 pos = pos1 pos = BoardPosition(pos.X + xStep, pos.Y + yStep) while somethingBetween == False and pos != pos2: piece = self._getPiece(pieceMap, pos) if piece is not None: somethingBetween = True pos = BoardPosition(pos.X + xStep, pos.Y + yStep) return somethingBetween
def isInCheck(self, color: str) -> bool: """ It checks if any king of color is in check Args: color: Player.name of player that has to be ckec if is in check Return: If is in check or not. """ kingPosList = [] for x in range(len(self.pieceMap)): for y in range(len(self.pieceMap[x])): piece = self.pieceMap[x][y] if piece is not None and piece.isKing == True and piece.color == color: kingPosList.append(BoardPosition(x, y)) isInCheck = False for kingPos in kingPosList: if self._canBeTaken(kingPos) is True: isInCheck = True return isInCheck
def __init__(self, gameBoard: GameBoard, selectedPiecePos): self.map = [] selectedPiece = None if selectedPiecePos is not None: selectedPiece = gameBoard.getPiece(selectedPiecePos) for x in range(gameBoard.boardSize[0]): self.map.append([]) for y in range(gameBoard.boardSize[1]): pos = BoardPosition(x, y) piece = gameBoard.getPiece(pos) if piece is not None: content = piece.name color = piece.color else: content = "None" color = "None" self.map[x].append(FieldState(content, color)) if selectedPiecePos == pos: self.map[x][y].isSelected = True elif selectedPiece is not None: if (gameBoard.getValidMove(selectedPiecePos, pos) != None): self.map[x][y].isPossible = True else: self.map[x][y].isPossible = False
def _getCastlingMove(self, y, rockX) -> BoardMove: king = self.getPiece(BoardPosition(4, y)) rock = self.getPiece(BoardPosition(rockX, y)) if king != None and king.name == "king" and rock != None and rock.name == "rock": if not self.checkIfSomethingBetween(BoardPosition(4, y), BoardPosition(rockX, y), "y"): if rockX == 7: if not self._canAnyBeTaken([BoardPosition(4, y), BoardPosition(5, y), BoardPosition(6, y)], king.color): triggerChange = BoardChange( BoardPosition(4, y), BoardPosition(6, y)) otherChange = BoardChange( BoardPosition(rockX, y), BoardPosition(5, y)) boardMove = BoardMove(triggerChange, otherChange) return boardMove elif rockX == 0: if not self._canAnyBeTaken([BoardPosition(4, y), BoardPosition(3, y), BoardPosition(2, y)], king.color): triggerChange = BoardChange( BoardPosition(4, y), BoardPosition(2, y)) otherChange = BoardChange( BoardPosition(rockX, y), BoardPosition(3, y)) boardMove = BoardMove(triggerChange, otherChange) return boardMove return None
def _canBeTaken(self, pos: BoardPosition, ignoreColor=""): """ It checks if pos can be taken by any piece (or any piece but ignoreColor), if they would be able to move right now. Args: pos: Position to check if can be taken ignoreColor: Player.name of player who's pieces should be ignored Return: If can be taken or not """ isThreatenend = False for x in range(len(self.pieceMap)): for y in range(len(self.pieceMap[x])): piece = self.pieceMap[x][y] if(piece is not None): if (ignoreColor == "" or piece.color != ignoreColor) and piece.canMove(self.pieceMap, BoardPosition( x, y), pos): isThreatenend = True return isThreatenend