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)))
def retrieve_move(match): if(match.count == 0): omoves = OpeningMove.objects.filter(movecnt=1) if(omoves): idx = random.randint(0, len(omoves) - 1) x1, y1 = Match.koord_to_index(omoves[idx].src) x2, y2 = Match.koord_to_index(omoves[idx].dst) gmove = calc.GenMove(x1, y1, x2, y2, Match.PIECES['blk']) print("------------ opening move found! --------------") return gmove else: print("############ No opening move found ############") return None lastmove = Move.objects.get(match_id=match.id, count=match.count) movesrc = Match.index_to_koord(lastmove.srcx, lastmove.srcy) movedst = Match.index_to_koord(lastmove.dstx, lastmove.dsty) prev_omoves = OpeningMove.objects.filter(movecnt=match.count, src=movesrc, dst=movedst) prev_list = list(prev_omoves) for prev_omove in prev_omoves: previous = prev_omove.previous while(previous): try: move = Move.objects.get(match_id=match.id, count=previous.movecnt) except Move.DoesNotExist: print("---------None---------") return None prevsrc = Match.index_to_koord(move.srcx, move.srcy) prevdst = Match.index_to_koord(move.dstx, move.dsty) if(previous.src != prevsrc or previous.dst != prevdst): prev_list.remove(prev_omove) break previous = previous.previous if(prev_list): previous = prev_list[0] omoves = OpeningMove.objects.filter(movecnt=match.count + 1, previous_id=previous.id) if(omoves): idx = random.randint(0, len(omoves) - 1) x1, y1 = Match.koord_to_index(omoves[idx].src) x2, y2 = Match.koord_to_index(omoves[idx].dst) gmove = calc.GenMove(x1, y1, x2, y2, Match.PIECES['blk']) print("------------ opening move found! --------------") return gmove else: print("############ 1: No opening move found! ###############") return None else: print("############ 2: No opening move found! ###############") return None
def generate_move(match, cnt): if(match.next_color() == Match.COLORS['white']): move_list = WHITEMOVES else: move_list = BLACKMOVES if(cnt >= CNT): return CNT + 1, None for i in range(cnt, CNT): x1, y1 = Match.koord_to_index(move_list[i][0]) x2, y2 = Match.koord_to_index(move_list[i][1]) if(rules.is_move_valid(match, x1, y1, x2, y2, Match.PIECES['blk'])[0]): gmove = calc.GenMove(x1, y1, x2, y2, Match.PIECES['blk']) return i, gmove return CNT + 1, None