Пример #1
0
def listToMoves (board, movstrs, type=None, testvalidate=False, ignoreErrors=False):
    # Work on a copy to ensure we don't break things
    board = board.clone()
    moves = []

    for mstr in movstrs:
        try:
            if type == None:
                move = parseAny (board, mstr)
            elif type == SAN:
                move = parseSAN (board, mstr)
            elif type == AN:
                move = parseAN (board, mstr)
            elif type == LAN:
                move = parseLAN (board, mstr)
        except ParsingError:
            if ignoreErrors:
                break
            raise
        
        if testvalidate:
            if not validateMove (board, move):
                if not ignoreErrors:
                    raise ParsingError, (mstr, 'Validation', board.asFen())
                break 
        
        moves.append(move)
        board.applyMove(move)
    
    return moves
Пример #2
0
def listToMoves (board, movstrs, type=None, testvalidate=False, ignoreErrors=False):
    # Work on a copy to ensure we don't break things
    board = board.clone()
    moves = []

    for mstr in movstrs:
        try:
            if type == None:
                move = parseAny (board, mstr)
            elif type == SAN:
                move = parseSAN (board, mstr)
            elif type == AN:
                move = parseAN (board, mstr)
            elif type == LAN:
                move = parseLAN (board, mstr)
        except ParsingError:
            if ignoreErrors:
                break
            raise
        
        if testvalidate:
            if not validateMove (board, move):
                if not ignoreErrors:
                    raise ParsingError, (mstr, 'Validation', board.asFen())
                break 
        
        moves.append(move)
        board.applyMove(move)
    
    return moves
Пример #3
0
def defends(board, fcord, tcord):
    """ Could fcord attack tcord if the piece on tcord wasn't on the team of
        fcord?
        Doesn't test check. """

    # Work on a board copy, as we are going to change some stuff
    board = board.clone()

    if board.friends[WHITE] & bitPosArray[fcord]:
        color = WHITE
    else:
        color = BLACK
    opcolor = 1 - color

    boards = board.boards[color]
    opboards = board.boards[opcolor]

    # To see if we now defend the piece, we have to "give" it to the other team
    piece = board.arBoard[tcord]

    #backup = boards[piece]
    #opbackup = opboards[piece]

    boards[piece] &= notBitPosArray[tcord]
    opboards[piece] |= bitPosArray[tcord]
    board.friends[color] &= notBitPosArray[tcord]
    board.friends[opcolor] |= bitPosArray[tcord]

    # Can we "attack" the piece now?
    backupColor = board.color
    board.setColor(color)
    from lmove import newMove
    from validator import validateMove
    islegal = validateMove(board, newMove(fcord, tcord))
    board.setColor(backupColor)

    # We don't need to set the board back, as we work on a copy
    #boards[piece] = backup
    #opboards[piece] = opbackup
    #board.friends[color] |= bitPosArray[tcord]
    #board.friends[opcolor] &= notBitPosArray[tcord]

    return islegal
Пример #4
0
def defends (board, fcord, tcord):
    """ Could fcord attack tcord if the piece on tcord wasn't on the team of
        fcord?
        Doesn't test check. """
    
    if board.friends[WHITE] & bitPosArray[fcord]:
        color = WHITE
    else: color = BLACK
    opcolor = 1-color
    
    boards = board.boards[color]
    opboards = board.boards[opcolor]
    
    board.lock.acquire()
    try:
        # To see if we now defend the piece, we have to "give" it to the other team
        piece = board.arBoard[tcord]
        
        backup = boards[piece]
        opbackup = opboards[piece]
        
        boards[piece] &= notBitPosArray[tcord]
        opboards[piece] |= bitPosArray[tcord]
        board.friends[color] &= notBitPosArray[tcord]
        board.friends[opcolor] |= bitPosArray[tcord]
        
        # Can we "attack" the piece now?
        backupColor = board.color
        board.setColor(color)
        from lmove import newMove
        from validator import validateMove
        islegal = validateMove (board, newMove(fcord, tcord))
        board.setColor(backupColor)
        
        # Set board back
        boards[piece] = backup
        opboards[piece] = opbackup
        board.friends[color] |= bitPosArray[tcord]
        board.friends[opcolor] &= notBitPosArray[tcord]
    finally:
        board.lock.release()
    
    return islegal
Пример #5
0
def defends (board, fcord, tcord):
    """ Could fcord attack tcord if the piece on tcord wasn't on the team of
        fcord?
        Doesn't test check. """
    
    # Work on a board copy, as we are going to change some stuff
    board = board.clone()
    
    if board.friends[WHITE] & bitPosArray[fcord]:
        color = WHITE
    else: color = BLACK
    opcolor = 1-color
    
    boards = board.boards[color]
    opboards = board.boards[opcolor]
    
    # To see if we now defend the piece, we have to "give" it to the other team
    piece = board.arBoard[tcord]
    
    #backup = boards[piece]
    #opbackup = opboards[piece]
    
    boards[piece] &= notBitPosArray[tcord]
    opboards[piece] |= bitPosArray[tcord]
    board.friends[color] &= notBitPosArray[tcord]
    board.friends[opcolor] |= bitPosArray[tcord]
    
    # Can we "attack" the piece now?
    backupColor = board.color
    board.setColor(color)
    from lmove import newMove
    from validator import validateMove
    islegal = validateMove (board, newMove(fcord, tcord))
    board.setColor(backupColor)
    
    # We don't need to set the board back, as we work on a copy
    #boards[piece] = backup
    #opboards[piece] = opbackup
    #board.friends[color] |= bitPosArray[tcord]
    #board.friends[opcolor] &= notBitPosArray[tcord]
    
    return islegal
Пример #6
0
def defencive_moves_tactic(model, ply, phase):

    # ------------------------------------------------------------------------ #
    # Test if we threat something, or at least put more pressure on it         #
    # ------------------------------------------------------------------------ #

    # We set bishop value down to knight value, as it is what most people expect
    bishopBackup = PIECE_VALUES[BISHOP]
    PIECE_VALUES[BISHOP] = PIECE_VALUES[KNIGHT]

    board = model.getBoardAtPly(ply).board
    oldboard = model.getBoardAtPly(ply - 1).board
    move = model.getMoveAtPly(ply - 1).move
    fcord = FCORD(move)
    tcord = TCORD(move)
    piece = board.arBoard[tcord]

    found_threatens = []
    found_increases = []

    # What do we attack now?
    board.setColor(1 - board.color)
    for ncap in genCaptures(board):

        # getCaptures also generate promotions
        if FLAG(ncap) in PROMOTIONS:
            continue

        # We are only interested in the attacks of the piece we just moved
        if FCORD(ncap) != TCORD(move):
            continue

        # We don't want to move back
        if TCORD(ncap) == FCORD(move):
            continue

        # We don't thread the king. We check him! (in another function)
        if board.arBoard[TCORD(ncap)] == KING:
            continue

        # If we also was able to attack that cord last time, we don't care
        if validateMove(oldboard, newMove(FCORD(move), TCORD(ncap))):
            continue

        # Test if we threats our enemy, at least more than before
        see0 = staticExchangeEvaluate(oldboard, TCORD(ncap),
                                      1 - oldboard.color)
        see1 = staticExchangeEvaluate(board, TCORD(ncap), 1 - oldboard.color)
        if see1 > see0:

            # If a new winning capture has been created
            if see1 > 0:
                # Find the easiest attack
                attacks = getAttacks(board, TCORD(ncap), board.color)
                v, cord = min((PIECE_VALUES[board.arBoard[fc]], fc)
                              for fc in iterBits(attacks))
                easiestAttack = newMove(cord, TCORD(ncap))
                found_threatens.append(toSAN(board, easiestAttack, True))

            # Even though we might not yet be strong enough, we might still
            # have strengthened another friendly attack
            else:
                found_increases.append(reprCord[TCORD(ncap)])

    board.setColor(1 - board.color)

    # -------------------------------------------------------------------- #
    # Test if we defend a one of our pieces                                #
    # -------------------------------------------------------------------- #

    found_defends = []

    # Test which pieces were under attack
    used = []
    for ncap in genCaptures(board):

        # getCaptures also generate promotions
        if FLAG(ncap) in PROMOTIONS:
            continue

        # We don't want to know about the same cord more than once
        if TCORD(ncap) in used:
            continue
        used.append(TCORD(ncap))

        # If the attack was poining on the piece we just moved, we ignore it
        if TCORD(ncap) == FCORD(move) or TCORD(ncap) == TCORD(move):
            continue

        # If we were already defending the piece, we don't send a new
        # message
        if defends(oldboard, FCORD(move), TCORD(ncap)):
            continue

        # If the attack was not strong, we ignore it
        see = staticExchangeEvaluate(oldboard, ncap)
        if see < 0: continue

        v = defends(board, TCORD(move), TCORD(ncap))

        # If the defend didn't help, it doesn't matter. Like defending a
        # bishop, threatened by a pawn, with a queen.
        # But on the other hand - it might still be a defend...
        # newsee = staticExchangeEvaluate(board, ncap)
        # if newsee <= see: continue

        if v:
            found_defends.append(reprCord[TCORD(ncap)])

    # ------------------------------------------------------------------------ #
    # Test if we are rescuing an otherwise exposed piece                       #
    # ------------------------------------------------------------------------ #

    # Rescuing is only an option, if our own move wasn't an attack
    if oldboard.arBoard[tcord] == EMPTY:
        see0 = staticExchangeEvaluate(oldboard, fcord, oldboard.color)
        see1 = staticExchangeEvaluate(board, tcord, oldboard.color)
        if see1 > see0 and see1 > 0:
            yield _("rescues a %s") % reprPiece[board.arBoard[tcord]].lower()

    if found_threatens:
        yield _("threatens to win material by %s") % join(found_threatens)
    if found_increases:
        yield _("increases the pressure on %s") % join(found_increases)
    if found_defends:
        yield _("defends %s") % join(found_defends)

    PIECE_VALUES[BISHOP] = bishopBackup
Пример #7
0
def attack_type (model, phase):
    
    # We set bishop value down to knight value, as it is what most people expect
    bishopBackup = PIECE_VALUES[BISHOP]
    PIECE_VALUES[BISHOP] = PIECE_VALUES[KNIGHT]
    
    board = model.boards[-1].board
    oldboard = model.boards[-2].board
    
    if len(model.moves) > 1:
        oldmove = model.moves[-2].move
        oldboard3 = model.boards[-3].board
    else: oldmove = None
    move = model.moves[-1].move
    tcord = TCORD(move)
    
    if oldboard.arBoard[tcord] != EMPTY:
        if oldmove and oldboard3.arBoard[TCORD(oldmove)] != EMPTY and \
                TCORD(oldmove) == tcord:
            yield _("takes back material")
        else:
            see = staticExchangeEvaluate(oldboard, move)
            if see == 0:
                yield _("exchanges material")
            elif see > 0:
                yield _("captures material")
            elif see < 0:
                yield _("sacrifies material")
    
    # ------------------------------------------------------------------------ #
    # Test if we threats something, or at least puts more preassure on it      #
    # ------------------------------------------------------------------------ #
    
    # What do we attack now?
    board.setColor(1-board.color)
    for ncap in genCaptures(board):
        
        # getCaptures also generate promotions
        if FLAG(ncap) in PROMOTIONS:
            continue
        
        # We are only interested in the attacks of the piece we just moved
        if FCORD(ncap) != TCORD (move):
            continue
        
        # We don't thread the king. We check him! (in another function)
        if board.arBoard[TCORD(ncap)] == KING:
            continue
        
        # If we also was able to attack that cord last time, we don't care
        if validateMove(oldboard, newMove(FCORD(move), TCORD(ncap))):
            continue
        
        # We will always attack first with the lowest valued piece.
        # Where is it?
        lowest = None
        cord = None
        attacks = getAttacks (board, TCORD(ncap), board.color)
        for fcord in iterBits(attacks):
            v = PIECE_VALUES[board.arBoard[fcord]]
            if lowest == None or v < lowest:
                lowest = v
                cord = fcord
        assert cord != None, "How can there not be any attacks, when ncap exists? %s" % toString(attacks)
        easiestAttack = newMove(cord, TCORD(ncap))
        
        # Now test if we threats our enemy, or they are too strong
        see = staticExchangeEvaluate(board, easiestAttack)
        if see > 0:
            # If a new winning capture has been created
            yield _("threatens to win material %s") % toSAN(board,easiestAttack, True)
        elif bitLength(attacks) > 1:
            # Even though we might not yet be strong enough, we might still
            # have strengthened another friendly attack
            yield _("increases the pressure on %s") % reprCord[TCORD(ncap)]
    board.setColor(1-board.color)
    
    # ------------------------------------------------------------------------ #
    # Test if we defend a one of our pieces                                    #
    # ------------------------------------------------------------------------ #
    
    # Test which pieces were under attack
    used = []
    for ncap in genCaptures(board):
        
        # getCaptures also generate promotions
        if FLAG(ncap) in PROMOTIONS:
            continue
        
        # We don't want to know about the same cord more than once
        if TCORD(ncap) in used:
            continue
        used.append(TCORD(ncap))
        
        # If the attack was poining on the piece we just moved, we ignore it
        if TCORD(ncap) == FCORD(move) or TCORD(ncap) == TCORD(move):
            continue
        
        # If we were already defending the piece, we don't send a new message
        if defends(oldboard, FCORD(move), TCORD(ncap)):
            continue
        
        # If the attack was not strong, we ignore it
        oldboard.setColor(1-oldboard.color)
        see = staticExchangeEvaluate(oldboard, ncap)
        oldboard.setColor(1-oldboard.color)
        if see < 0: continue
        
        v = defends(board, TCORD(move), TCORD(ncap))
        
        # If the defend didn't help, it doesn't matter. Like defending a bishop,
        # threatened by a pawn, with a queen.
        # But on the other hand - it might still be a defend...
        # newsee = staticExchangeEvaluate(board, ncap)
        # if newsee <= see: continue
        
        if v:
            yield _("defends %s") % reprCord[TCORD(ncap)]
            
    PIECE_VALUES[BISHOP] = bishopBackup
Пример #8
0
def defencive_moves_tactic (model, ply, phase):

    # ------------------------------------------------------------------------ #
    # Test if we threat something, or at least put more pressure on it         #
    # ------------------------------------------------------------------------ #

    # We set bishop value down to knight value, as it is what most people expect
    bishopBackup = PIECE_VALUES[BISHOP]
    PIECE_VALUES[BISHOP] = PIECE_VALUES[KNIGHT]

    board = model.getBoardAtPly(ply).board
    oldboard = model.getBoardAtPly(ply-1).board
    move = model.getMoveAtPly(ply-1).move
    fcord = FCORD(move)
    tcord = TCORD(move)
    piece = board.arBoard[tcord]

    found_threatens = []
    found_increases = []

    # What do we attack now?
    board.setColor(1-board.color)
    for ncap in genCaptures(board):

        # getCaptures also generate promotions
        if FLAG(ncap) in PROMOTIONS:
            continue

        # We are only interested in the attacks of the piece we just moved
        if FCORD(ncap) != TCORD (move):
            continue

        # We don't want to move back
        if TCORD(ncap) == FCORD(move):
            continue

        # We don't thread the king. We check him! (in another function)
        if board.arBoard[TCORD(ncap)] == KING:
            continue

        # If we also was able to attack that cord last time, we don't care
        if validateMove(oldboard, newMove(FCORD(move), TCORD(ncap))):
            continue

        # Test if we threats our enemy, at least more than before
        see0 = staticExchangeEvaluate(oldboard, TCORD(ncap), 1-oldboard.color)
        see1 = staticExchangeEvaluate(board, TCORD(ncap), 1-oldboard.color)
        if see1 > see0:

            # If a new winning capture has been created
            if see1 > 0:
                # Find the easiest attack
                attacks = getAttacks (board, TCORD(ncap), board.color)
                v, cord = min((PIECE_VALUES[board.arBoard[fc]],fc)
                              for fc in iterBits(attacks))
                easiestAttack = newMove(cord, TCORD(ncap))
                found_threatens.append(toSAN(board,easiestAttack, True))

            # Even though we might not yet be strong enough, we might still
            # have strengthened another friendly attack
            else:
                found_increases.append(reprCord[TCORD(ncap)])

    board.setColor(1-board.color)

    # -------------------------------------------------------------------- #
    # Test if we defend a one of our pieces                                #
    # -------------------------------------------------------------------- #

    found_defends = []

    # Test which pieces were under attack
    used = []
    for ncap in genCaptures(board):

        # getCaptures also generate promotions
        if FLAG(ncap) in PROMOTIONS:
            continue

        # We don't want to know about the same cord more than once
        if TCORD(ncap) in used:
            continue
        used.append(TCORD(ncap))

        # If the attack was poining on the piece we just moved, we ignore it
        if TCORD(ncap) == FCORD(move) or TCORD(ncap) == TCORD(move):
            continue

        # If we were already defending the piece, we don't send a new
        # message
        if defends(oldboard, FCORD(move), TCORD(ncap)):
            continue

        # If the attack was not strong, we ignore it
        see = staticExchangeEvaluate(oldboard, ncap)
        if see < 0: continue

        v = defends(board, TCORD(move), TCORD(ncap))

        # If the defend didn't help, it doesn't matter. Like defending a
        # bishop, threatened by a pawn, with a queen.
        # But on the other hand - it might still be a defend...
        # newsee = staticExchangeEvaluate(board, ncap)
        # if newsee <= see: continue

        if v:
            found_defends.append(reprCord[TCORD(ncap)])

    # ------------------------------------------------------------------------ #
    # Test if we are rescuing an otherwise exposed piece                       #
    # ------------------------------------------------------------------------ #

    # Rescuing is only an option, if our own move wasn't an attack
    if oldboard.arBoard[tcord] == EMPTY:
        see0 = staticExchangeEvaluate(oldboard, fcord, oldboard.color)
        see1 = staticExchangeEvaluate(board, tcord, oldboard.color)
        if see1 > see0 and see1 > 0:
            yield _("rescues a %s") % reprPiece[board.arBoard[tcord]].lower()

    if found_threatens:
        yield _("threatens to win material by %s") % join(found_threatens)
    if found_increases:
        yield _("increases the pressure on %s") % join(found_increases)
    if found_defends:
        yield _("defends %s") % join(found_defends)

    PIECE_VALUES[BISHOP] = bishopBackup