Esempio n. 1
0
def is_lg_castling_ok(match, srcx, srcy, dstx, dsty, piece):
    color = Match.color_of_piece(piece)

    opp_color = Match.REVERSED_COLORS[color]

    for i in range(1, 3, 1):
        fieldx = srcx - i
        field = match.readfield(fieldx, srcy)
        if(field != Match.PIECES['blk']):
            return False

    if(color == Match.COLORS['white']):
        if(match.wKg_first_movecnt != 0 or match.wRk_a1_first_movecnt != 0):
            return False
    else:
        if(match.bKg_first_movecnt != 0 or match.bRk_a8_first_movecnt != 0):
            return False

    king = match.readfield(srcx, srcy)
    match.writefield(srcx, srcy, Match.PIECES['blk'])
    for i in range(0, -3, -1):
        castlingx = srcx + i
        attacked = rules.attacked(match, castlingx, srcy, opp_color)
        if(attacked == True):
            match.writefield(srcx, srcy, king)
            return False

    match.writefield(srcx, srcy, king)
    return True
Esempio n. 2
0
def is_move_ok(match, srcx, srcy, dstx, dsty, piece):
    DIRS = rules.DIRS

    color = Match.color_of_piece(piece)

    opp_color = Match.REVERSED_COLORS[color]

    direction = kg_dir(srcx, srcy, dstx, dsty)
    if(direction == DIRS['sh-castling']):
        return is_sh_castling_ok(match, srcx, srcy, dstx, dsty, piece)
    if(direction == DIRS['lg-castling']):
        return is_lg_castling_ok(match, srcx, srcy, dstx, dsty, piece)
    if(direction == DIRS['undefined']):
        return False

    king = match.readfield(srcx, srcy)
    captured = match.readfield(dstx, dsty)
    match.writefield(srcx, srcy, Match.PIECES['blk'])
    match.writefield(dstx, dsty, king)
    attacked = rules.attacked(match, dstx, dsty, opp_color)
    match.writefield(srcx, srcy, king)
    match.writefield(dstx, dsty, captured)
    if(attacked == True):
        return False

    field = match.readfield(dstx, dsty)
    if(match.color_of_piece(field) == color):
        return False

    return True
Esempio n. 3
0
def calc_min(match, maxdepth, depth, alpha, beta):
    generator = Generator()
    generator.match = match
    gmove = None
    color = match.next_color()
    newscore = None
    minscore = 200000
    oldscore = 0
    count = 0

    while(generator.active):
        flag, newgmove = generator.generate_move()

        if(flag):
            count += 1
            oldscore = match.score
            move = match.do_move(newgmove.srcx, newgmove.srcy, newgmove.dstx, newgmove.dsty, newgmove.prom_piece)
            match.move_list.append(move)
            if(depth == 1):
                msg = "\nmatch.id:" + str(match.id) + " calculate "
                prnt_move(msg, newgmove)
                if(gmove):
                    prnt_move(" CANDIDATE ", gmove)
                    print(" score: " + str(newscore))
                    thread = Match.get_active_thread(match)
                    if(thread and newscore):
                        thread.populate_candiate(gmove)

            if(depth <= maxdepth):
                newscore = calc_max(match, maxdepth, depth + 1, alpha, minscore)[0]
            elif(depth <= maxdepth + 2):
                if(match.next_color() == Match.COLORS['white']):
                    wkg_attacked = rules.attacked(match, match.wKg_x, match.wKg_y, Match.COLORS['black'])
                    white_promotion = match.readfield(newgmove.dstx, newgmove.dsty) == Match.PIECES['wPw'] and newgmove.dsty >= 6
                    if(oldscore != match.score or wkg_attacked or white_promotion ):
                        newscore = calc_max(match, maxdepth, depth + 1, alpha, minscore)[0]
                    else:
                        newscore = match.score + calc_helper.evaluate_position(match)
                else:
                    bkg_attacked = rules.attacked(match, match.bKg_x, match.bKg_y, Match.COLORS['white'])
                    black_promotion = match.readfield(newgmove.dstx, newgmove.dsty) == Match.PIECES['bPw'] and newgmove.dsty <= 1
                    if(oldscore != match.score or bkg_attacked or black_promotion):
                        newscore = calc_max(match, maxdepth, depth + 1, alpha, minscore)[0]
                    else:
                        newscore = match.score + calc_helper.evaluate_position(match)
            elif(depth <= maxdepth + 4 and oldscore != match.score):
                newscore = calc_max(match, maxdepth, depth + 1, alpha, minscore)[0]
            else:
                newscore = match.score + calc_helper.evaluate_position(match)

            newscore, gmove = rate(color, gmove, newgmove, minscore, newscore)
            match.undo_move(True)
            if(newscore < minscore):
                minscore = newscore
                if(minscore <= alpha):
                    break
        else:
            if(count == 0):
                status = rules.game_status(match)
                if(status == Match.STATUS['winner_black']):
                    newscore = Match.SCORES[Match.PIECES['wKg']]
                elif(status == Match.STATUS['winner_white']):
                    newscore = Match.SCORES[Match.PIECES['bKg']]
                elif(status == Match.STATUS['draw']):
                    newscore = Match.SCORES[Match.PIECES['blk']]
                else:
                    newscore = match.score

                if(depth == 1):
                    msg = "\nmatch.id:" + str(match.id) + " CANDIDATE "
                    prnt_move(msg, gmove)
                    print(" score: " + str(newscore))
                    thread = Match.get_active_thread(match)
                    if(thread):
                        thread.populate_candiate(gmove)
                return newscore, gmove

    return minscore, gmove