コード例 #1
0
ファイル: views.py プロジェクト: richardtraindl/immanuel
def fetch_match(request):
    context = RequestContext(request)
    matchid = request.GET['matchid']
    movecnt = request.GET['movecnt']
    switchflag = request.GET['switchflag']
    match = Match.objects.get(id=matchid)
    if(match == None):
        data = "§§"
    else:
        lastmove = Move.objects.filter(match_id=match.id).order_by("count").last()
        if(lastmove != None):
            movesrc = Match.index_to_koord(lastmove.srcx, lastmove.srcy)
            movedst = Match.index_to_koord(lastmove.dstx, lastmove.dsty)
        if(int(movecnt) == match.count):
            data = "§"
        else:
            data = html_board(match, int(switchflag), movesrc, movedst) + "§" + html_moves(match)

        thread = Match.get_active_thread(match)
        if(thread and not thread.candidate_srcx):
            data += "§<p>thread found with matchid: " + str(thread.match.id) + "</p>"
        elif(thread and thread.candidate_srcx):
            data += "§<p>move candidate: "
            data += Match.index_to_koord(thread.candidate_srcx, thread.candidate_srcy) + "-" + Match.index_to_koord(thread.candidate_dstx, thread.candidate_dsty)
            data += "</p>"
        else:
            data += "§"

    return HttpResponse(data)
コード例 #2
0
ファイル: calc.py プロジェクト: richardtraindl/immanuel
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