Esempio n. 1
0
 def getPossibleMoves(self, grid, testing=False):
     moves = []
     friendPositions = [piece.position for piece in grid[self.color]]
     for direction in [
             Vector(1, 0),
             Vector(0, 1),
             Vector(-1, 0),
             Vector(0, -1)
     ]:
         factor = 1
         position = self.position + direction.__mul__(factor)
         noEnnemyPiece = True
         while (not position in friendPositions
                ) and noEnnemyPiece and position.isInBoard():
             for piece in grid[self.otherColor]:
                 if piece.position == position:
                     moves.append(Move(self, position, targetPiece=piece))
                     noEnnemyPiece = False
             if noEnnemyPiece:
                 move = Move(self, position)
                 if not testing:
                     if not Move.isInCheck(move, grid, self.color,
                                           self.otherColor):
                         moves.append(move)
                 else:
                     moves.append(move)
                 factor += 1
                 position = self.position + direction.__mul__(factor)
     return moves
Esempio n. 2
0
 def getPossibleMoves(self, grid, testing=False):
     moves = []
     friendPositions = [piece.position for piece in grid[self.color]]
     for direction in [
             Vector(1, 1),
             Vector(-1, 1),
             Vector(-1, -1),
             Vector(1, -1)
     ]:
         moves += self.getMovesInDirection(self, direction, friendPositions,
                                           grid, testing)
     return moves
Esempio n. 3
0
 def getPossibleMoves(self, grid, testing=False):
     moves = []
     friendPositions = [piece.position for piece in grid[self.color]]
     for x in range(-1, 2):
         for y in range(-1, 2):
             if x == y == 0:
                 continue
             offset = Vector(x, y)
             newPos = self.position + offset
             if not newPos in friendPositions:
                 taking = False
                 move = None
                 for piece in grid[self.otherColor]:
                     if piece.position == newPos:
                         taking = True
                         move = Move(self, newPos, targetPiece=piece)
                         break
                 if not taking:
                     move = Move(self, newPos)
                 if not testing:
                     if not Move.isInCheck(move, grid, self.color,
                                           self.otherColor):
                         moves.append(move)
                     # check if king in possible target pieces
                 else:
                     moves.append(move)
     return moves
Esempio n. 4
0
 def ParseMove(move, grid, color):
     # Parses a move (ex be6) into a *Move* instance
     piecesDict = {
         "p": Pawn,
         "r": Rook,
         "b": Bishop,
         "q": Queen,
         "k": King,
         "n": Knight,
         "any": Piece
     }
     if len(move) == 3:
         piecePlayed = move[0]
         if piecePlayed not in piecesDict.keys():
             print(Warning("Piece not correct..."))
             return False
         x = ord(move[1]) - 97
         y = 8 - int(move[2])
     elif len(move) == 2:
         x = ord(move[0]) - 97
         y = 8 - int(move[1])
         piecePlayed = "any"
     else:
         print(Warning("Please specify the piece you are moving (ex: pe4)"))
         return False
     adequatMoves = []
     for piece in grid[color]:
         if type(piece) == piecesDict[piecePlayed] or piecePlayed == "any":
             for possibleMove in piece.getPossibleMoves(grid):
                 if Vector(x, y) == possibleMove.target:
                     adequatMoves.append(possibleMove)
     if len(adequatMoves) == 1:
         return adequatMoves[0]
     elif len(adequatMoves) == 0:
         print(Warning("No such piece can go to {}".format(move[1:])))
         return False
     else:
         if len(move) == 2:
             print(
                 Warning(
                     "Multiple pieces can go to {}, please specify which type of piece you are moving"
                     .format(move)))
             return False
         else:
             print(
                 Warning(
                     "Multiple pieces can go to {}, taking a random one (possibility of chosing which file is not yet implemented)"
                     .format(move[1:])))
             return adequatMoves[0]
Esempio n. 5
0
 def getPossibleMoves(self, grid, testing=False):
     moves = []
     friendPositions = [piece.position for piece in grid[self.color]]
     for (x, y) in [(2, 1), (2, -1), (1, -2), (-1, -2), (-2, -1), (-2, 1),
                    (-1, 2), (1, 2)]:
         offset = Vector(x, y)
         newPos = self.position + offset
         if not newPos in friendPositions and newPos.isInBoard():
             taking = False
             move = None
             for piece in grid[self.otherColor]:
                 if piece.position == newPos:
                     taking = True
                     move = Move(self, newPos, targetPiece=piece)
                     break
             if not taking:
                 move = Move(self, newPos)
             if not testing:
                 if not Move.isInCheck(move, grid, self.color,
                                       self.otherColor):
                     moves.append(move)
             else:
                 moves.append(move)
     return moves
Esempio n. 6
0
 def getPossibleMoves(self, grid, testing=False):
     moves = []
     coefficient = 1 if self.color == "blacks" else -1
     secondCase = self.position.y == coefficient % 7
     # on teste si le pion est sur sa ligne d'origine
     frontCase = True
     for piece in grid[self.color] + grid[self.otherColor]:
         if piece.position == self.position + Vector(0, coefficient):
             frontCase = False
         if secondCase and piece.position == self.position + Vector(
                 0, 2 * coefficient):
             secondCase = False
     for piece in grid[self.otherColor]:
         for newPos in [
                 self.position + i
                 for i in (Vector(1, coefficient), Vector(-1, coefficient))
         ]:
             if piece.position == newPos:
                 move = Move(self, newPos, targetPiece=piece)
                 if not testing:
                     if not Move.isInCheck(move, grid, self.color,
                                           self.otherColor):
                         moves.append(move)
                 else:
                     moves.append(move)
     if frontCase:
         move = Move(self, self.position + Vector(0, coefficient))
         if not testing:
             if not Move.isInCheck(move, grid, self.color, self.otherColor):
                 moves.append(move)
         else:
             moves.append(move)
     if secondCase:
         move = Move(self, self.position + Vector(0, 2 * coefficient))
         if not testing:
             if not Move.isInCheck(move, grid, self.color, self.otherColor):
                 moves.append(move)
         else:
             moves.append(move)
     return moves
Esempio n. 7
0
def generateGrid():
    return {
        "whites": [Pawn(Vector(i, 6), "whites") for i in range(8)] +
        [Rook(Vector(0, 7), "whites"),
         Rook(Vector(7, 7), "whites")] +
        [Bishop(Vector(2, 7), "whites"),
         Bishop(Vector(5, 7), "whites")] + [Queen(Vector(3, 7), "whites")] +
        [Knight(Vector(1, 7), "whites"),
         Knight(Vector(6, 7), "whites")] + [King(Vector(4, 7), "whites")],
        "blacks": [Pawn(Vector(i, 1), "blacks") for i in range(8)] +
        [Rook(Vector(0, 0), "blacks"),
         Rook(Vector(7, 0), "blacks")] +
        [Bishop(Vector(2, 0), "blacks"),
         Bishop(Vector(5, 0), "blacks")] + [Queen(Vector(3, 0), "blacks")] +
        [Knight(Vector(1, 0), "blacks"),
         Knight(Vector(6, 0), "blacks")] + [King(Vector(4, 0), "blacks")]
    }