Example #1
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 #2
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
Example #3
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
Example #4
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 #5
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