Beispiel #1
0
    def promotion_message(self, color):
        if color == self.BLACK:
            color = 0
        else:
            color = 1

        knight = Knight(color)
        bishop = Bishop(color)
        rook = Rook(color)
        queen = Queen(color)

        tenth = self.size / 10
        pygame.draw.rect(
            self.screen, self.WHITE,
            [tenth, tenth, 8 * tenth, 2 * tenth])  # draw outer background
        pygame.draw.rect(self.screen, self.BLACK,
                         [tenth, tenth, 8 * tenth, 2 * tenth],
                         5)  # draw outer background

        knight.draw(self.screen, 1.5 * tenth, 1.5 * tenth, tenth)
        pygame.draw.rect(self.screen, self.BLACK,
                         [1.5 * tenth, 1.5 * tenth, tenth, tenth], 3)

        bishop.draw(self.screen, 3.5 * tenth, 1.5 * tenth, tenth)
        pygame.draw.rect(self.screen, self.BLACK,
                         [3.5 * tenth, 1.5 * tenth, tenth, tenth], 3)

        rook.draw(self.screen, 5.5 * tenth, 1.5 * tenth, tenth)
        pygame.draw.rect(self.screen, self.BLACK,
                         [5.5 * tenth, 1.5 * tenth, tenth, tenth], 3)

        queen.draw(self.screen, 7.5 * tenth, 1.5 * tenth, tenth)
        pygame.draw.rect(self.screen, self.BLACK,
                         [7.5 * tenth, 1.5 * tenth, tenth, tenth], 3)
Beispiel #2
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))
Beispiel #3
0
    def reset(self):
        row = lambda colour, row: [
            Rook(self.screen, colour, (0, row), self._board),
            Horse(self.screen, colour, (1, row), self._board),
            Bishop(self.screen, colour, (2, row), self._board),
            King(self.screen, colour, (3, row), self._board),
            Queen(self.screen, colour, (4, row), self._board),
            Bishop(self.screen, colour, (5, row), self._board),
            Horse(self.screen, colour, (6, row), self._board),
            Rook(self.screen, colour, (7, row), self._board)
        ]

        self._board[0] = row(Colour.white, 0)

        self._board[1] = [
            Pawn(self.screen, Colour.white, (i, 1), self._board)
            for i in range(self.size)
        ]

        self._board[len(self._board) - 2] = [
            Pawn(self.screen, Colour.black, (i, len(self._board) - 2),
                 self._board) for i in range(self.size)
        ]

        self._board[len(self._board) - 1] = row(Colour.black,
                                                len(self._board) - 1)
Beispiel #4
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)
     })
Beispiel #5
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)
        })
Beispiel #6
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)))
Beispiel #7
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
 def load_board_from_file_path(self, path):
     """
     Load a chess board from .txt file on the local computer.
     :param path: The path to the .txt file.
     :return: None
     """
     count = 0
     with open(path) as board_loaded:
         for line in board_loaded:
             for char in line:
                 if char != '|' and char != '\n':
                     if char == '-':
                         self.game_tiles[count] = NoPiece(count)
                         count += 1
                     if char == 'R':
                         self.game_tiles[count] = Rook("Black", count)
                         count += 1
                     if char == 'N':
                         self.game_tiles[count] = Knight("Black", count)
                         count += 1
                     if char == 'B':
                         self.game_tiles[count] = Bishop("Black", count)
                         count += 1
                     if char == 'Q':
                         self.game_tiles[count] = Queen("Black", count)
                         count += 1
                     if char == 'K':
                         self.game_tiles[count] = King("Black", count)
                         count += 1
                     if char == 'P':
                         self.game_tiles[count] = Pawn("Black", count)
                         count += 1
                     if char == 'r':
                         self.game_tiles[count] = Rook("White", count)
                         count += 1
                     if char == 'n':
                         self.game_tiles[count] = Knight("White", count)
                         count += 1
                     if char == 'b':
                         self.game_tiles[count] = Bishop("White", count)
                         count += 1
                     if char == 'q':
                         self.game_tiles[count] = Queen("White", count)
                         count += 1
                     if char == 'k':
                         self.game_tiles[count] = King("White", count)
                         count += 1
                     if char == 'p':
                         self.game_tiles[count] = Pawn("White", count)
                         count += 1
     self.original_game_tiles = self.game_tiles.copy()
     print("Done copying from file")
Beispiel #9
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))
Beispiel #10
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
Beispiel #11
0
def test3():
	#--Starting position for one rook in c3--
	#Enemy pieces at b3, e3
	#Blocked by own piece at c2


	R = Rook(Color.White)
	P = Pawn(Color.White)
	p = Pawn(Color.Black)
	gameBoard = Board()
	place_piece(gameBoard, "c3", R)
	place_piece(gameBoard, "c2", P)
	place_piece(gameBoard, "b3", p)
	place_piece(gameBoard, "e3", p)
	
	translate(R.legalMoves(gameBoard, False))
	display.print_board(gameBoard.board)
Beispiel #12
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))
	
Beispiel #13
0
def test4():
	#--Starting position for one rook in d4 --
	#Enemy pieces at d2, b4
	#Blocked by own piece at d6, g4

	r = Rook(Color.Black)
	P = Pawn(Color.White)
	p = Pawn(Color.Black)

	gameBoard = Board()
	place_piece(gameBoard, "d4", r)
	place_piece(gameBoard, "d2", p)
	place_piece(gameBoard, "b4", p)
	place_piece(gameBoard, "d6", P)
	place_piece(gameBoard, "g4", P)

	translate(r.legalMoves(gameBoard, False))
	display.print_board(gameBoard.board)
Beispiel #14
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
Beispiel #15
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')})
Beispiel #16
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
 def __init__(self):
     self.game_tiles.insert(0, Rook("Black", 0))
     self.game_tiles.insert(1, Knight("Black", 1))
     self.game_tiles.insert(2, Bishop("Black", 2))
     self.game_tiles.insert(3, Queen("Black", 3))
     self.game_tiles.insert(4, King("Black", 4))
     self.game_tiles.insert(5, Bishop("Black", 5))
     self.game_tiles.insert(6, Knight("Black", 6))
     self.game_tiles.insert(7, Rook("Black", 7))
     for i in range(8, 16):
         self.game_tiles.insert(i, Pawn("Black", i))
     for i in range(16, 48):
         self.game_tiles.insert(i, NoPiece(i))
     for i in range(48, 56):
         self.game_tiles.insert(i, Pawn("White", i))
     self.game_tiles.insert(56, Rook("White", 56))
     self.game_tiles.insert(57, Knight("White", 57))
     self.game_tiles.insert(58, Bishop("White", 58))
     self.game_tiles.insert(59, Queen("White", 59))
     self.game_tiles.insert(60, King("White", 60))
     self.game_tiles.insert(61, Bishop("White", 61))
     self.game_tiles.insert(62, Knight("White", 62))
     self.game_tiles.insert(63, Rook("White", 63))
     self.original_game_tiles = self.game_tiles.copy()
Beispiel #18
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')})
def test_rook_move():
    """Tests if rook can move
    """
    playerOnePieces = set()
    board = [[None] * 8 for i in range(8)]
    color = "BLACK"
    myPiece = Rook(board, color, playerOnePieces, 2, 'A')
    playerOnePieces.update({myPiece})
    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 = "8A"
    # 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
Beispiel #20
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))
def test_rook_capture():
    """Tests if rook can capture
    """
    playerOnePieces = set()
    playerTwoPieces = set()
    board = [[None] * 8 for i in range(8)]
    color = "BLACK"
    myPiece = Rook(board, color, playerOnePieces, 2, 'A')
    playerOnePieces.update({myPiece})
    enemyPiece = Pawn(board, "WHITE", playerTwoPieces, 8, 'A')
    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 = "8A"
    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
Beispiel #22
0
 def promote(self, newpiece):
     x = self.x
     y = self.y
     if "Queen" is newpiece.name:
         self.x = -1
         Queen(self.board, y, x, self.color)
         return True
     if "Rook" is newpiece.name:
         self.x = -1
         Rook(self.board, y, x, self.color)
         return True
     if "Bishop" is newpiece.name:
         self.x = -1
         Bishop(self.board, y, x, self.color)
         return True
     if "Knight" is newpiece.name:
         self.x = -1
         Knight(self.board, y, x, self.color)
         return True
     return False
Beispiel #23
0
def heuristicX(position, player):
    if position.is_checkmate():
        return 800000

    val1 = 0
    val2 = 0
    #create a dictionary of values and weights for player X
    piece_values = {}
    piece_values["K"] = 7
    piece_values["N"] = 2
    piece_values["R"] = 11

    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")
    #print_pos(position)

    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)
        #what_piece = rook.rook_piece_check()

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

    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

    #print(rook.check_king_space(7) ,knight.knight_movement().count("k") == 1)
    #if rook.check_king_space(7) and knight.knight_movement().count("k") == 1:
    #	return 500

    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 not rook.rook_safety() and king.king_movement().count("R") == 1: and position.pair_two_piece(knight_index, t_king_index):
    #	return 1000 + rook.trap_king()+ (50 - abs(knight_index - t_king_index))

    #if not rook.rook_safety():
    #	return -3

    #print(king.increase_movement(rook_index), king.king_movement().count("R") == 1, their_king.increase_movement(rook_index), their_king.king_movement().count("r") == 1)
    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:
            return 600 + rook.trap_king()
        else:
            return 500 + rook.trap_king() + (100 -
                                             abs(knight_index - t_king_index))



    if rook.check_king_space(7) and king.king_movement().count("R") == 1\
    and their_king.increase_movement(king_index):
        #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 rook.check_king_space(10) and king.increase_movement(t_king_index):
        #print("?")
        return 100 + rook.trap_king()

    #if their_king.increase_movement(rook_index) and king.increase_movement(t_king_index):
    #	return 111

    if king.increase_movement(t_king_index) and (t_king_index // 10 == 2 or t_king_index % 10 == 8\
                or t_king_index // 10 == 8 or t_king_index % 10 == 1):
        return 40 + rook.trap_king()

    if king.king_movement().count("R") == 1:
        #return rook.trap_king() + (9 - abs((rook_index // 10 - t_king_index// 10) + abs(9 - (king_index - t_king_index) ) + abs(king_index - rook_index) ))
        #return rook.trap_king() + (50 - abs(rook_index - king_index)) + (50 - abs(king_index - t_king_index))
        if king_index // 10 < rook_index // 10 and rook_index % 10 == king_index % 10:
            #return rook.trap_king() #+ (100 - abs(knight_index - t_knight_index))
            #return 200 + (5 - abs(rook_index % 10 - t_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
Beispiel #24
0
    def game(self):
        done = True
        promotion = False
        new_game_screen = False
        selection = []
        while done is True:
            pygame.display.flip()
            for event in pygame.event.get():  # User did something
                if event.type == pygame.QUIT:  # If user clicked close
                    done = False  # exit while loop

                if event.type == pygame.MOUSEBUTTONDOWN and new_game_screen:
                    if pygame.mouse.get_pressed(
                    )[0]:  # left mouse button pressed
                        pos = pygame.mouse.get_pos()
                        x = self.tenth

                        # yes - new game box
                        if 3 * x <= pos[
                                0] <= 4.5 * x:  #and 7 * x <= pos[1] <= 8 * x:
                            self.board.clear_board()
                            self.board.initialize_standard_board()
                            pygame.display.flip()
                            self.board.white_turn = True  # if white won, it should be their turn again
                            self.game()  # make a new game

                        # no - new game box
                        if 5.5 * x <= pos[0] <= 7 * x and 7 * x <= pos[
                                1] <= 8 * x:
                            done = False  # exit while loop

                if event.type == pygame.MOUSEBUTTONDOWN and promotion:  # promotion dialogue
                    if pygame.mouse.get_pressed(
                    )[0]:  # left mouse button pressed
                        pos = pygame.mouse.get_pos()

                        # Each box for each piece selection, if they don't press on the box, try again
                        x = self.tenth
                        if 1.5 * x <= pos[0] < 2.5 * x and 1.5 * x <= pos[
                                1] <= 2.5 * x:
                            knight = Knight(color)
                            self.board.place_piece(square, knight)
                            self.board.deselect_board()
                            pygame.display.flip()
                            promotion = False
                        elif 3.5 * x <= pos[0] < 4.5 * x and 1.5 * x <= pos[
                                1] <= 2.5 * x:
                            bishop = Bishop(color)
                            self.board.place_piece(square, bishop)
                            self.board.deselect_board()
                            pygame.display.flip()
                            promotion = False
                        elif 5.5 * x <= pos[0] < 6.5 * x and 1.5 * x <= pos[
                                1] <= 2.5 * x:
                            rook = Rook(color)
                            self.board.place_piece(square, rook)
                            self.board.deselect_board()
                            pygame.display.flip()
                            promotion = False
                        elif 7.5 * x <= pos[0] < 8.5 * x and 1.5 * x <= pos[
                                1] <= 2.5 * x:
                            queen = Queen(color)
                            self.board.place_piece(square, queen)
                            self.board.deselect_board()
                            pygame.display.flip()
                            promotion = False

                if event.type == pygame.MOUSEBUTTONDOWN:  # normal move
                    if pygame.mouse.get_pressed(
                    )[0]:  # left mouse button pressed
                        pos = pygame.mouse.get_pos()
                        square = self.board.get_square(pos)
                        selection.append(square)
                        self.board.select_piece(square)
                        pygame.display.flip()

            if len(selection) == 2:
                output = self.board.player_move(selection[0], selection[1])
                selection = []
                if output[0] == "Promotion":
                    color = output[1]
                    square = output[2]
                    pygame.display.flip()
                    promotion = True
                if output == "New_Game":
                    self.board.new_game_message()
                    pygame.display.flip()
                    new_game_screen = True

        self.board.print_move_list()
Beispiel #25
0
def heuristicX(position):
    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, king_index)

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

    if knight_index:
        knight = Knight(position, knight_index)

    else:
        knight = None

    if t_king_index:
        their_king = King(position, t_king_index)

    if t_knight_index:
        their_knight = Knight(position, t_knight_index)

    if king.king_movement().count("k") == 1 and king.king_safety():
        return 99999

    if rook.rook_piece_check().count(
            "k") == 1 and rook.rook_safety() and not king.king_safety():
        return 999

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

    #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):
        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() and king.king_movement().count("N") == 0\
    and rook.rook_piece_check().count("N") == 0:
        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(t_king_index,10):
            return 200 + rook.trap_king(t_king_index) + \
           (10 - abs(king_index // 10 - rook_index // 10)) + \
           (10 - abs(knight_index // 10 - king_index // 10))+\
           (100 - abs(knight_index - king_index ))

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

    if not rook.rook_safety():
        return -4

    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(t_king_index, 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:
            return 500 + rook.trap_king(t_king_index)
        else:
            return 500 + rook.trap_king(t_king_index) + (5 - abs(knight_index//10 - t_king_index//10))\
            + (5 - abs(knight_index % 10 - t_king_index % 10))

    if rook.check_king_space(t_king_index, 7) and king.king_movement().count("R") == 1\
    and their_king.increase_movement(king_index):
        if knight.knight_movement().count("k") == 1:
            return 300 + rook.trap_king(t_king_index)
        else:
            return 100 + rook.trap_king(t_king_index) +\
           (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:
            return rook.trap_king(t_king_index) + (
                10 - abs(king_index % 10 - t_king_index % 10))

        elif rook_index % 10 < t_king_index % 10:
            return rook.trap_king(t_king_index)+ (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
Beispiel #26
0
 def kings_space(self, player):
     rook = self.generic_find("R")
     
     from Pieces.Rook import Rook
     your_rook = Rook(self, player, rook) 
     return your_rook.check_king_space(20)
Beispiel #27
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
Beispiel #28
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
Beispiel #29
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