Пример #1
0
def do_move(request, matchid):
    context = RequestContext(request)
    if request.method == 'POST':
        match = get_object_or_404(Match, pk=matchid)
        movesrc = request.POST['move_src']
        movedst = request.POST['move_dst']
        prompiece = request.POST['prom_piece']
        switch = request.POST['switch']
        if(match.next_color_human()):
            if(len(movesrc) > 0 and len(movedst) > 0 and len(prompiece) > 0):
                srcx,srcy = Match.koord_to_index(movesrc)
                dstx,dsty = Match.koord_to_index(movedst)
                prom_piece = match.PIECES[prompiece]
                flag, msg = rules.is_move_valid(match, srcx, srcy, dstx, dsty, prom_piece)
                status = rules.game_status(match)
                if(flag == True and status == Match.STATUS['open']):
                    move = match.do_move(srcx, srcy, dstx, dsty, prom_piece)
                    move.save()
                    match.save()
                    calc_move_for_immanuel(match)
                    fmtmsg = "<p class='ok'>" + rules.ERROR_MSGS[msg] + "</p>"
                else:
                    fmtmsg = "<p class='error'>" + rules.ERROR_MSGS[msg] + "</p>"
            else:
                fmtmsg = "<p class='error'>Zug-Format ist ungültig.</p>"
        else:
            fmtmsg = "<p class='error'>Farbe ist nicht am Zug.</p>"

        fmtboard = fill_fmtboard(match, int(switch))

        moves = []
        currmove = Move.objects.filter(match_id=match.id).order_by("count").last()
        if(currmove != None):
            if(currmove.count % 2 == 0):
                limit = 22
            else:
                limit = 21
            qmoves = Move.objects.filter(match_id=match.id).order_by("-count")[:limit]
            for qmove in reversed(qmoves):
                moves.append(qmove)

        comments = Comment.objects.filter(match_id=match.id).order_by("created_at").reverse()[:3]

        status = rules.game_status(match)
        if(status != Match.STATUS['open']):
            fmtmsg = "<p class='error'>" + helper.reverse_lookup(Match.STATUS, status) + "</p>"

        if(int(switch) == 0):
            rangeobj = range(8)
        else:
            rangeobj = range(7, -1, -1)

        candidate = ""

        return render(request, 'kate/match.html', { 'match': match, 'board': fmtboard, 'switch': switch, 'movesrc': movesrc, 'movedst': movedst, 'moves': moves, 'comments': comments, 'msg': fmtmsg, 'range': rangeobj, 'candidate': candidate } )
    else:
        return HttpResponseRedirect(reverse('kate:match', args=(matchid, switch)))
Пример #2
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
Пример #3
0
def calc_move_for_immanuel(match):
    if(rules.game_status(match) == Match.STATUS['open'] and match.next_color_human() == False):
        calc.thread_do_move(match)