Example #1
0
    def initialize_standard_board(self):
        self.draw_board()
        # create and place 32 pieces
        for i in range(0, 8):
            self.place_piece([i, 1], Pawn(1))
            self.place_piece([i, 6], Pawn(0))

        self.place_piece([0, 0], Rook(1))
        self.place_piece([7, 0], Rook(1))
        self.place_piece([0, 7], Rook(0))
        self.place_piece([7, 7], Rook(0))

        self.place_piece([4, 0], King(1))
        self.place_piece([4, 7], King(0))

        self.place_piece([3, 0], Queen(1))
        self.place_piece([3, 7], Queen(0))

        self.place_piece([2, 0], Bishop(1))
        self.place_piece([5, 0], Bishop(1))
        self.place_piece([2, 7], Bishop(0))
        self.place_piece([5, 7], Bishop(0))

        self.place_piece([1, 0], Knight(1))
        self.place_piece([6, 0], Knight(1))
        self.place_piece([1, 7], Knight(0))
        self.place_piece([6, 7], Knight(0))
Example #2
0
    def rook_safety(self):
        #Check to see if king is safe from all possible moves your opponent can make
        your_rook = self._position

        #MAXIMIZE only
        their_king = self._board.generic_find("k")

        if their_king is not None:
            from .King import King
            tk = King(self._board, not self._player, their_king)
            #piece = tk.king_movement()

            if tk.king_movement().count("R") == 1:
                return False

        their_knight = self._board.generic_find("n")

        if their_knight:
            from .Knight import Knight
            tn = Knight(self._board, not self._player, their_knight)
            #piece = tn.knight_movement()

            if tn.knight_movement().count("R") == 1:
                return False

        return True
Example #3
0
    def paired_combo(self, player):
        your_king = self.generic_find("K")
        your_rook = self.generic_find("R")
        
        from Pieces.King import King
        
        extended_list = [your_king - 18, your_king - 19, your_king - 20, your_king - 21, your_king - 22,
                         your_king - 8, your_king - 12, 
                         your_king - 2, your_king + 2,
                         your_king + 8, your_king + 12,
                         your_king + 18, your_king + 19, your_king + 20, your_king + 21, your_king + 22]
        
        yk = King(self, player, your_king)
        
        yk.populate_moves()
        
        king_moves = yk.ret_kings_moves()
        
        king_moves.extend(extended_list)

        for i in king_moves:
            if i == your_rook:
                return True
       
        return False
Example #4
0
    def __init__(self):
        self.currentPlayer = "White"
        self.tiles = dict()
        self.moveCounter = 1
        self.prevBoard = None
        for x in range(64):
            self.tiles[x] = (Tile(x, NullPiece()))
        for x in range(8, 16):
            pass
            self.tiles[x] = (Tile(x, Pawn("White", x)))
        for x in range(48, 56):
            self.tiles[x] = (Tile(x, Pawn("Black", x)))

        self.tiles[0] = (Tile(0, Rook("White", 0)))
        self.tiles[1] = (Tile(1, Knight("White", 1)))
        self.tiles[2] = (Tile(2, Bishop("White", 2)))
        self.tiles[3] = (Tile(3, Queen("White", 3)))
        self.tiles[4] = (Tile(4, King("White", 4)))
        self.tiles[5] = (Tile(5, Bishop("White", 5)))
        self.tiles[6] = (Tile(6, Knight("White", 6)))
        self.tiles[7] = (Tile(7, Rook("White", 7)))
        self.tiles[56] = (Tile(56, Rook("Black", 56)))
        self.tiles[57] = (Tile(57, Knight("Black", 57)))
        self.tiles[58] = (Tile(58, Bishop("Black", 58)))
        self.tiles[59] = (Tile(59, Queen("Black", 59)))
        self.tiles[60] = (Tile(60, King("Black", 60)))
        self.tiles[61] = (Tile(61, Bishop("Black", 61)))
        self.tiles[62] = (Tile(62, Knight("Black", 62)))
        self.tiles[63] = (Tile(63, Rook("Black", 63)))
Example #5
0
def heuristicY(position):
	if position.is_checkmate():
		return 900000
	
	king_index = position.generic_find("K")
	knight_index = position.generic_find("N")
	max_rook_index = position.generic_find("r")
	max_king_index = position.generic_find("k")
	max_knight_index = position.generic_find("n")
	
	if king_index:
		king = King(position,  king_index)
	else:
		return -99999
	
	if knight_index:
		knight = Knight(position, knight_index)
	else:
		return -999
		
	if max_rook_index:
		max_rook = Rook(position,  max_rook_index)
	
	if max_king_index:
		max_king = King(position,  max_king_index)
	
		
	if max_knight_index:
		max_knight = Knight(position,  max_knight_index)
	else:
		max_knight = None
		
	
	#check for king's safety, if this position is poor return a low value
	if not king.king_safety():
		return -1000
	
	if knight_index:
		if not knight.knight_safety():
			return -999
	
	#if minimize player can eliminate an opponent piece do it
	if position.check_board():
		return 99999
	
	if knight.knight_movement().count("r") == 1:
		return 157
	
	if king.king_movement().count("r") == 1:
		return 50 + (50 - abs(knight_index - max_rook_index))
	
	if king_index % 10 == 5:
		return 20 + (5 - abs(king_index // 10 - 5)) + (5- abs(knight_index - max_rook_index))
	
	return abs(knight_index % 10 - max_rook_index % 10) + abs(knight_index // 10 - max_rook_index // 10)\
	+ (5- abs(king_index // 10 - 5))
	
Example #6
0
	def king_follow(self):
		your_king = self._board.generic_find("K")
		
		from Pieces.King import King
		fake_rook_king = King(self._board, self._player, self._position)
		return fake_rook_king.increase_movement(your_king)
		
		
		
			
		class Knight:
Example #7
0
    def create_pieces_for_new_game(self):
        """
        instantiates class object for each piece type
        sets piece in corresponding start position
        white occupies rows 1 and 2
        black occupies rows 7 and 8
        """
        # maybe we don't even want to do this - what we really want to do is assign pieces as occupants of corresponding squares

        # Haven't decided if a piece needs to know about position - for now we include but I may remove in the future

        white_pieces = dict(
            white_rook_1=Rook('White'),
            white_knight_1=Knight('White'),
            white_bishop_1=Bishop('White'),
            white_queen=Queen('White'),
            white_king=King('White'),
            white_bishop_2=Bishop('White'),
            white_knight_2=Knight('White'),
            white_rook_2=Rook('White'),
            white_pawn_1=Pawn('White'),
            white_pawn_2=Pawn('White'),
            white_pawn_3=Pawn('White'),
            white_pawn_4=Pawn('White'),
            white_pawn_5=Pawn('White'),
            white_pawn_6=Pawn('White'),
            white_pawn_7=Pawn('White'),
            white_pawn_8=Pawn('White'),
        )

        black_pieces = dict(
            black_rook_1=Rook('Black'),
            black_knight_1=Knight('Black'),
            black_bishop_1=Bishop('Black'),
            black_queen=Queen('Black'),
            black_king=King('Black'),
            black_bishop_2=Bishop('Black'),
            black_knight_2=Knight('Black'),
            black_rook_2=Rook('Black'),
            black_pawn_1=Pawn('Black'),
            black_pawn_2=Pawn('Black'),
            black_pawn_3=Pawn('Black'),
            black_pawn_4=Pawn('Black'),
            black_pawn_5=Pawn('Black'),
            black_pawn_6=Pawn('Black'),
            black_pawn_7=Pawn('Black'),
            black_pawn_8=Pawn('Black'),
        )
        self.white_pieces = white_pieces
        self.black_pieces = black_pieces
Example #8
0
    def initPlayerOne(self, ):
        """This populates the playerOnePieces (black) empty set with the player one's pieces. It also adds those pieces to the board
        """
        #add pawns
        #(self,self.board,color,group,row,col)
        color = "BLACK"
        group = self.playerOnePieces
        self.playerOnePieces.update({
            Pawn(self.board, color, group, 2, 'A', self),
            Pawn(self.board, color, group, 2, 'B', self),
            Pawn(self.board, color, group, 2, 'C', self),
            Pawn(self.board, color, group, 2, 'D', self),
            Pawn(self.board, color, group, 2, 'E', self),
            Pawn(self.board, color, group, 2, 'F', self),
            Pawn(self.board, color, group, 2, 'G', self),
            Pawn(self.board, color, group, 2, 'H', self)
        })

        #Add rank 1 pieces
        self.playerOnePieces.update({
            Rook(self.board, color, group, 1, 'A', self),
            Knight(self.board, color, group, 1, 'B', self),
            Bishop(self.board, color, group, 1, 'C', self),
            Queen(self.board, color, group, 1, 'D', self),
            King(self.board, color, group, 1, 'E', self),
            Bishop(self.board, color, group, 1, 'F', self),
            Knight(self.board, color, group, 1, 'G', self),
            Rook(self.board, color, group, 1, 'H', self)
        })
def test_king_capture():
    """Tests if the king can capture
    """
    playerOnePieces = set()
    playerTwoPieces = set()
    board = [[None] * 8 for i in range(8)]
    color = "BLACK"
    myPiece = King(board, color, playerOnePieces, 2, 'A')
    playerOnePieces.update({myPiece})
    enemyPiece = Pawn(board, "WHITE", playerTwoPieces, 3, 'B')
    playerTwoPieces.update({enemyPiece})
    possibleRow = {i: i - 1 for i in range(1, 9)}
    possibleCol = {chr(i): i - ord('A') for i in range(ord('A'), ord('A') + 8)}
    moveOne = "2A"
    moveTwo = "3A"
    board[possibleRow[int(moveOne[0])]][possibleCol[moveOne[1]]].move(
        int(moveTwo[0]), moveTwo[1])
    moveOne = "3A"
    moveTwo = "3B"
    # Now I make a move
    board[possibleRow[int(moveOne[0])]][possibleCol[moveOne[1]]].move(
        int(moveTwo[0]), moveTwo[1])

    assert board[possibleRow[int(moveTwo[0])]
                 ][possibleCol[moveTwo[1]]] == myPiece
    assert len(playerTwoPieces) == 0
Example #10
0
 def initPlayerTwo(self):
     """This populates the playerTwoPieces (white) empty set with the player two's pieces. It also adds those pieces to the board
     """
     color = "WHITE"
     group = self.playerTwoPieces
     self.playerTwoPieces.update({
         Pawn(self.board, color, group, 7, 'A', self),
         Pawn(self.board, color, group, 7, 'B', self),
         Pawn(self.board, color, group, 7, 'C', self),
         Pawn(self.board, color, group, 7, 'D', self),
         Pawn(self.board, color, group, 7, 'E', self),
         Pawn(self.board, color, group, 7, 'F', self),
         Pawn(self.board, color, group, 7, 'G', self),
         Pawn(self.board, color, group, 7, 'H', self)
     })
     #Add rank 1 pieces
     self.playerTwoPieces.update({
         Rook(self.board, color, group, 8, 'A', self),
         Knight(self.board, color, group, 8, 'B', self),
         Bishop(self.board, color, group, 8, 'C', self),
         Queen(self.board, color, group, 8, 'D', self),
         King(self.board, color, group, 8, 'E', self),
         Bishop(self.board, color, group, 8, 'F', self),
         Knight(self.board, color, group, 8, 'G', self),
         Rook(self.board, color, group, 8, 'H', self)
     })
Example #11
0
def test8():
	#--Starting position for a black king at d6
	#Enemy pieces at d6, g6
	#Blocked by own piece at h3, d2, b1, e2
	k = King(Color.Black)
	P = Pawn(Color.White)
	p = Pawn(Color.Black)
	gameBoard = Board()	

	place_piece(gameBoard, "d6", k)
	place_piece(gameBoard, "c7", p)
	place_piece(gameBoard, "d7", p)
	place_piece(gameBoard, "e7", p)
	place_piece(gameBoard, "c5", P)
	place_piece(gameBoard, "e5", P)

	translate(k.legalMoves(gameBoard, False))
	display.print_board(gameBoard.board)
Example #12
0
    def initialize(self):
        team = Team(self.team)
        team.canMove = False
        self.Side.append(Rook(-1, -1, team))
        self.Side.append(Knight(-1, -1, team))
        self.Side.append(Bishop(-1, -1, team))
        self.Side.append(King(-1, -1, team))
        self.Side.append(Queen(-1, -1, team))
        self.Side.append(Bishop(-1, -1, team))
        self.Side.append(Knight(-1, -1, team))
        self.Side.append(Rook(-1, -1, team))

        for x in range(0, 8):
            self.Side.append(Pawn(-1, -1, team))
Example #13
0
    def knight_safety(self):
        max_knight = self._board.generic_find("n")
        max_king = self._board.generic_find("k")
        max_rook = self._board.generic_find("r")

        if max_knight:
            knight_piece = Knight(self._board, max_knight)
            if knight_piece.knight_movement().count("N") == 1:
                return False

        if max_king:
            from Pieces.King import King
            king_piece = King(self._board, max_king)
            if king_piece.king_movement().count("N") == 1:
                return False

        if max_rook:
            from Pieces.Rook import Rook
            rook_piece = Rook(self._board, max_rook)
            if rook_piece.rook_piece_check().count("N") == 1:
                return False

        return True
Example #14
0
	def king_safety(self):
		your_king = self._board.generic_find("K")

		#MAXIMIZE
		if your_king is not None and not self._player:
			knight = self._board.generic_find("n")
			
			king = self._board.generic_find("k")
	
			if king:
				their_king = King(self._board, not self._player, king)
				#piece = their_king.king_movement()
				#print(piece) 
				if their_king.king_movement().count("K") == 1:
					return False
		
			if knight:
				their_knight = Knight(self._board, not self._player, knight)
				if their_knight.knight_movement().count("K") == 1:
					return False
		
			return True

		#MINIMIZE
		else:
			rook = self._board.generic_find("r")
			#print("CHECKING")
			if rook:
				from .Rook import Rook
				their_rook = Rook(self._board, self._player, rook)
				#piece = their_rook.rook_piece_check()
				
				
				
				if their_rook.rook_piece_check().count("K") == 1:
					return False

			knight = self._board.generic_find("n")
			if knight:
				from .Knight import Knight
				their_knight = Knight(self._board, self._player, knight)
				#piece = their_knight.knight_movement()
				if their_knight.knight_movement().count("K") == 1:
					return False

			king = self._board.generic_find("k")
			if king:
				their_king = King(self._board, self._player, king)
				#piece = their_king.king_movement()

				if their_king.king_movement().count("K") == 1:
					return False   

		return True
Example #15
0
 def update_figures(self, board):
     for i in range(8):
         for j in range(8):
             type_of_piece = board[i][j][1]
             color = board[i][j][0]
             if type_of_piece == "q":
                 self.add_figure(Queen(j, i, color, type_of_piece))
             if type_of_piece == "r":
                 self.add_figure(Rook(j, i, color, type_of_piece))
             if type_of_piece == "b":
                 self.add_figure(Bishop(j, i, color, type_of_piece))
             if type_of_piece == "k":
                 self.add_figure(Knight(j, i, color, type_of_piece))
             if type_of_piece == "p":
                 self.add_figure(Pawn(j, i, color, type_of_piece))
             if type_of_piece == "W":
                 self.add_figure(King(j, i, color, type_of_piece))
Example #16
0
	def knight_safety(self):
		if not self._player:
			their_knight = self._board.generic_find("n")
			their_king = self._board.generic_find("k")
			if their_knight:
				knight_piece = Knight(self._board, not self._player, their_knight)

				if knight_piece.knight_movement().count("N") == 1:
					return False
			
			if their_king:
				from Pieces.King import King
				king_piece = King(self._board, not self._player, their_king)
				if king_piece.king_movement().count("N") == 1:
					return False
			
			return True
		
		else:
			max_knight = self._board.generic_find("n")
			max_king = self._board.generic_find("k")
			max_rook = self._board.generic_find("r")
			
			if max_knight:
				knight_piece = Knight(self._board, not self._player, max_knight)
				what_piece = knight_piece.knight_movement()
				if knight_piece.knight_movement().count("N") == 1:
					return False
			
			if max_king:
				from Pieces.King import King
				king_piece = King(self._board, not self._player, max_king)
				what_piece = king_piece.king_movement()
				if king_piece.king_movement().count("N") == 1:
					return False
			
			if max_rook:
				from Pieces.Rook import Rook
				rook_piece = Rook(self._board, not self._player, max_rook)
				what_piece = rook_piece.rook_piece_check()
				if rook_piece.rook_piece_check().count("N") == 1:
					return False
			
			return True
Example #17
0
def initPlayerOne(playerOnePieces,board):
    """This populates the playerOnePieces (black) empty set with the player one's pieces. It also adds those pieces to the board

    :param playerOnePieces: An empty set representing player one's pieces
    :type playerOnePieces: set
    :param board: A 2d list consisting of an 8 by 8 None objects and any prepopulated pieces
    :type board: [list]
    """    
    #add pawns
    #(self,board,color,group,row,col)
    color="BLACK"
    group=playerOnePieces
    playerOnePieces.update({Pawn(board,color,group,2,'A'),Pawn(board,color,group,2,'B'),Pawn(board,color,group,2,'C'),Pawn(board,color,group,2,'D'),Pawn(board,color,group,2,'E'),Pawn(board,color,group,2,'F'),Pawn(board,color,group,2,'G'),Pawn(board,color,group,2,'H')})

    #Add rank 1 pieces
    playerOnePieces.update({Rook(board,color,group,1,'A'),Knight(board,color,group,1,'B'),Bishop(board,color,group,1,'C'),Queen(board,color,group,1,'D'),King(board,color,group,1,'E'),Bishop(board,color,group,1,'F'),Knight(board,color,group,1,'G'),Rook(board,color,group,1,'H')})
Example #18
0
 def paired_move(self):
     yk = self._board.generic_find("K")
     from Pieces.King import King
     your_king = King(self._board, not self._player, yk)
     return your_king.increase_movement(self._position)
Example #19
0
def initPlayerTwo(playerTwoPieces,board):
    """This populates the playerTwoPieces (white) empty set with the player two's pieces. It also adds those pieces to the board

    :param playerTwoPieces: An empty set representing player two's pieces
    :type playerTwoePieces: set
    :param board: A 2d list consisting of an 8 by 8 None objects and any prepopulated pieces
    :type board: [list]
    """
    color="WHITE"
    group=playerTwoPieces
    playerTwoPieces.update({Pawn(board,color,group,7,'A'),Pawn(board,color,group,7,'B'),Pawn(board,color,group,7,'C'),Pawn(board,color,group,7,'D'),Pawn(board,color,group,7,'E'),Pawn(board,color,group,7,'F'),Pawn(board,color,group,7,'G'),Pawn(board,color,group,7,'H')})
    #Add rank 1 pieces
    playerTwoPieces.update({Rook(board,color,group,8,'A'),Knight(board,color,group,8,'B'),Bishop(board,color,group,8,'C'),Queen(board,color,group,8,'D'),King(board,color,group,8,'E'),Bishop(board,color,group,8,'F'),Knight(board,color,group,8,'G'),Rook(board,color,group,8,'H')})
Example #20
0
def heuristicX(position, player):
    if position.is_checkmate():
        return 800000

    king_index = position.generic_find("K")
    rook_index = position.generic_find("R")
    knight_index = position.generic_find("N")
    t_king_index = position.generic_find("k")
    t_knight_index = position.generic_find("n")

    if king_index:
        king = King(position, not player, king_index)
        paired_combo = position.paired_combo(player)

    if rook_index:
        rook = Rook(position, player, rook_index)
    else:
        return 0

    if knight_index:
        knight = Knight(position, player, knight_index)
        #m_knight = Knight(position, not player, knight_index)
    else:
        knight = None

    if t_king_index:
        their_king = King(position, not player, t_king_index)

    if t_knight_index:
        their_knight = Knight(position, not player, t_knight_index)

    #if an opponent piece can be eliminated do it
    if position.check_board() and (king.king_safety() or king.king_movement().count("k") == 1)\
    and (rook.rook_safety() or not rook.rook_safety() and king.king_movement().count("R") == 1):#and (king.king_movement().count("R") == 1 or king.increase_movement(rook_index)):
        return 99999

    if their_king.check_mated(king, knight, rook) and king.king_safety()\
    and (king.king_movement().count("R") == 1 or knight.knight_movement().count("R") == 1\
     or rook.rook_safety()):
        return 1000

    if not king.king_safety():
        return -1000

    if not knight.knight_safety():
        return -1

    if t_knight_index:
        if king.king_movement().count("R") == 1 and their_king.king_movement().count("R") == 1\
        and their_knight.knight_movement().count("R") == 0 and not rook.check_king_space(10):
            #print("?")
            return 200 + rook.trap_king() + \
           (10 - abs(king_index // 10 - rook_index // 10)) + \
           (10 - abs(knight_index // 10 - king_index // 10))+\
           (100 - abs(knight_index - king_index ))
        #(10 - abs(king_index // 10 - t_king_index // 10))

    if t_knight_index:
        if not rook.rook_safety() and their_knight.knight_movement().count(
                "R") == 1:
            return -5

    if not rook.rook_safety() and king.king_movement().count("R") != 1:
        return -4

    #if rook.check_king_space(5):
    #	return 750 + rook.trap_king() + (100 - abs(knight_index - t_king_index))#+\
    #(5-abs(knight_index // 10 - king_index // 10)) + (5-abs(knight_index % 10 - king_index % 10))

    #if rook.check_king_space(7):
    #	return 650 + rook.trap_king()


    if (t_king_index // 10 == 2 or t_king_index % 10 == 8\
     or t_king_index // 10 == 8 or t_king_index % 10 == 1)\
    and rook.check_king_space(10)\
    and (king.increase_movement(rook_index) or king.king_movement().count("R") == 1)\
    and (their_king.increase_movement(rook_index) or their_king.king_movement().count("R") == 1) and their_king.increase_movement(king_index):
        if knight.knight_movement().count("k") == 1:
            #print("A")
            return 500 + rook.trap_king()
        else:
            #print("B")
            return 500 + rook.trap_king() + (
                5 - abs(knight_index // 10 - t_king_index // 10)) + (
                    5 - abs(knight_index % 10 - t_king_index % 10))  #+\
            #(5-abs(rook_index // 10 - t_king_index // 10)) + (5-abs(rook_index % 10 - t_king_index % 10))


    if rook.check_king_space(7) and king.king_movement().count("R") == 1\
    and their_king.increase_movement(king_index):
        #print("!")
        if knight.knight_movement().count("k") == 1:
            #print("?")
            return 300 + rook.trap_king()
        else:
            #print("#")
            return 100 + rook.trap_king() +\
           (10 - abs(king_index // 10 - t_king_index // 10))\
           + (10 - abs(king_index % 10 - t_king_index % 10))+\
           (100 - abs(knight_index - t_king_index))

    if king.king_movement().count("R") == 1:
        if king_index // 10 < rook_index // 10 and rook_index % 10 == king_index % 10:
            pass

        elif rook_index % 10 > t_king_index % 10:
            #print("84")
            return rook.trap_king() + (
                10 - abs(king_index % 10 - t_king_index % 10))

        elif rook_index % 10 < t_king_index % 10:
            #print("85")
            return rook.trap_king() + (
                10 - abs(king_index % 10 - t_king_index % 10)) + (
                    10 - abs(king_index // 10 - t_king_index // 10))


    if king.increase_movement(rook_index) and rook_index // 10 != 9\
    and rook_index % 10 < t_king_index % 10:
        return 20


    if rook_index // 10 == 8 and king_index // 10 == 9\
    and knight_index // 10 == 9:
        return 10

    return 0
Example #21
0
def heuristicY(position):
	try:
		if position.is_checkmate():
			return 900000

		king_index = position.generic_find("K")
		knight_index = position.generic_find("N")
		max_rook_index = position.generic_find("r")
		max_king_index = position.generic_find("k")
		max_knight_index = position.generic_find("n")

		if king_index:
			king = King(position,  king_index)

		if knight_index:
			knight = Knight(position, knight_index)

		if max_rook_index:
			max_rook = Rook(position,  max_rook_index)

		if max_king_index:
			max_king = King(position,  max_king_index)


		if max_knight_index:
			max_knight = Knight(position,  max_knight_index)

		#check for king's safety, if this position is poor return a low value
		if not king.king_safety():
			return -1000

		if knight_index:
			if not knight.knight_safety():
				return -999

		#if minimize player can eliminate an opponent piece do it
		if position.check_board():
			return 99999

		if knight_index:
			if knight.knight_movement().count("r") == 1:
				return 157

		if king_index and knight_index and max_rook_index:
			if king.king_movement().count("r") == 1:
				return 50 + (50 - abs(knight_index - max_rook_index))

		if king_index // 10 == 4 or king_index // 10 == 5:
			return 30 + (50 - abs(knight_index - max_rook_index))

		if knight_index and max_rook_index and king_index:
			if king_index % 10 == 5 or king_index % 10 == 6:
				return 20 + (5 - abs(king_index // 10 - 5)) + (5 - abs(knight_index - max_rook_index))\
				+ (5 - abs(king_index % 10 - 5))

		if knight_index and max_rook_index:
			return abs(knight_index % 10 - max_rook_index % 10) + abs(knight_index // 10 - max_rook_index // 10)\
			+ (5- abs(king_index // 10 - 5))

		elif max_rook_index:
			return abs(king_index % 10 - max_rook_index % 10) + abs(king_index // 10 - max_rook_index // 10)

		else:
			return abs(king_index // 10 - 5) + abs(king_index % 10 - 5)

	except Exception as error:
		with open("error.txt", "a") as e:
			e.write(str(error) + "\n")
			
		return 1
Example #22
0
    def makeMove(self):
        specialMove = False
        boardCopy = copy.deepcopy(self.board)
        prevPosition = self.piece.position
        boardCopy.tiles[self.piece.position] = Tile(self.piece.position,
                                                    NullPiece())
        enPassantHold = self.piece.position

        #after first move, castling isn't allowed
        if self.piece.toString().lower() == "k" or self.piece.toString().lower(
        ) == "r":
            self.piece.moved = True
        # deal with a queened pawn
        if self.piece.toString().lower() == "p":
            if self.piece.color == "White" and 56 <= self.coordinate < 64:
                boardCopy.tiles[self.coordinate] = Tile(
                    self.coordinate, Queen("White", self.coordinate))
                specialMove = True
            elif self.piece.color == "Black" and 0 <= self.coordinate < 7:
                boardCopy.tiles[self.coordinate] = Tile(
                    self.coordinate, Queen("Black", self.coordinate))
                specialMove = True

        #deal with en passant
        if self.piece.toString().lower() == "p":
            if self.piece.color == "White" and 32 <= enPassantHold < 39:
                if self.coordinate - enPassantHold != 8:
                    if boardCopy.tiles[
                            self.coordinate].pieceOnTile.toString() == "-":
                        boardCopy.tiles[self.coordinate - 8] = Tile(
                            self.coordinate, NullPiece())
                        boardCopy.tiles[self.coordinate] = Tile(
                            self.coordinate, self.piece)
                        specialMove = True
            elif self.piece.color == "Black" and 24 <= enPassantHold < 31:
                if self.coordinate - enPassantHold != -8:
                    if boardCopy.tiles[
                            self.coordinate].pieceOnTile.toString() == "-":
                        boardCopy.tiles[self.coordinate + 8] = Tile(
                            self.coordinate, NullPiece())
                        boardCopy.tiles[self.coordinate] = Tile(
                            self.coordinate, self.piece)
                        specialMove = True

        #deal with castling
        if self.piece.toString() == "k":
            if prevPosition == 4 and self.coordinate == 2:
                boardCopy.tiles[self.coordinate] = Tile(
                    self.coordinate, King("White", self.coordinate))
                boardCopy.tiles[0] = Tile(self.coordinate, NullPiece())
                boardCopy.tiles[3] = Tile(self.coordinate, Rook("White", 3))
                specialMove = True
            if prevPosition == 4 and self.coordinate == 6:
                boardCopy.tiles[self.coordinate] = Tile(
                    self.coordinate, King("White", self.coordinate))
                boardCopy.tiles[7] = Tile(self.coordinate, NullPiece())
                boardCopy.tiles[5] = Tile(self.coordinate, Rook("White", 5))
                specialMove = True

        if self.piece.toString() == "K":
            if prevPosition == 60 and self.coordinate == 58:
                boardCopy.tiles[self.coordinate] = Tile(
                    self.coordinate, King("Black", self.coordinate))
                boardCopy.tiles[56] = Tile(self.coordinate, NullPiece())
                boardCopy.tiles[59] = Tile(self.coordinate, Rook("Black", 59))
                specialMove = True
            if prevPosition == 60 and self.coordinate == 62:
                boardCopy.tiles[self.coordinate] = Tile(
                    self.coordinate, King("Black", self.coordinate))
                boardCopy.tiles[63] = Tile(self.coordinate, NullPiece())
                boardCopy.tiles[61] = Tile(self.coordinate, Rook("Black", 61))
                specialMove = True

        if not specialMove:
            self.piece.position = self.coordinate
            boardCopy.tiles[self.coordinate] = Tile(self.coordinate,
                                                    self.piece)

        friendlyKing = None
        for tile in boardCopy.tiles.values():
            if (tile.pieceOnTile.toString() == "K" and boardCopy.currentPlayer
                    == "Black") or (tile.pieceOnTile.toString() == "k"
                                    and boardCopy.currentPlayer == "White"):
                friendlyKing = tile.pieceOnTile
                break
        if friendlyKing.inCheck(boardCopy):
            return False
        return boardCopy