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 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
def evaluate_piece_moves(match, srcx, srcy): color = match.next_color() piece = match.readfield(srcx, srcy) movecnt = 0 if(Match.color_of_piece(piece) != color): return movecnt if(piece == Match.PIECES['wQu'] or piece == Match.PIECES['bQu']): dirs = [ [0, 1], [0, -1], [1, 0], [-1, 0], [1, 1], [-1, -1], [-1, 1], [1, -1] ] dircnt = 8 stepcnt = 7 value = 2 elif(piece == Match.PIECES['wRk'] or piece == Match.PIECES['bRk']): dirs = [ [0, 1], [0, -1], [1, 0], [-1, 0] ] dircnt = 4 stepcnt = 7 value = 4 elif(piece == Match.PIECES['wBp'] or piece == Match.PIECES['bBp']): dirs = [ [1, 1], [-1, -1], [-1, 1], [1, -1] ] dircnt = 4 stepcnt = 7 value = 6 elif(piece == Match.PIECES['wKn'] or piece == Match.PIECES['bKn']): dirs = [ [1, 2], [2, 1], [2, -1], [1, -2], [-1, -2], [-2, -1], [-2, 1], [-1, 2] ] dircnt = 8 stepcnt = 1 value = 6 else: return movecnt for j in range(dircnt): stepx = dirs[j][0] stepy = dirs[j][1] dstx = srcx dsty = srcy for i in range(stepcnt): dstx += stepx dsty += stepy flag,errcode = rules.is_move_valid(match, srcx, srcy, dstx, dsty, Match.PIECES['blk']) if(flag): movecnt += value elif(errcode == rules.ERROR_CODES['out-of-bounds']): break return (movecnt // 10)
def generate_move(self): gmove = GenMove() color = self.match.next_color() while(self.active): if(self.steps == None): piece = self.match.readfield(self.board_x, self.board_y) if(piece == Match.PIECES['blk'] or color != Match.color_of_piece(piece)): if(self.rotate_field()): continue else: return False, gmove else: if(piece == Match.PIECES['wPw']): if(self.board_y < 6): self.steps = WPW_STEPS self.dir_idx = 0 self.max_dir = 4 self.step_idx = 0 self.max_step = 1 else: self.steps = WPROM_STEPS self.dir_idx = 0 self.max_dir = 3 self.step_idx = 0 self.max_step = 4 elif(piece == Match.PIECES['bPw']): if(self.board_y > 1): self.steps = BPW_STEPS self.dir_idx = 0 self.max_dir = 4 self.step_idx = 0 self.max_step = 1 else: self.steps = BPROM_STEPS self.dir_idx = 0 self.max_dir = 3 self.step_idx = 0 self.max_step = 4 elif(piece == Match.PIECES['wRk'] or piece == Match.PIECES['bRk']): self.steps = RK_STEPS self.dir_idx = 0 self.max_dir = 4 self.step_idx = 0 self.max_step = 7 elif(piece == Match.PIECES['wBp'] or piece == Match.PIECES['bBp']): self.steps = BP_STEPS self.dir_idx = 0 self.max_dir = 4 self.step_idx = 0 self.max_step = 7 elif(piece == Match.PIECES['wKn'] or piece == Match.PIECES['bKn']): self.steps = KN_STEPS self.dir_idx = 0 self.max_dir = 8 self.step_idx = 0 self.max_step = 1 elif(piece == Match.PIECES['wQu'] or piece == Match.PIECES['bQu']): self.steps = QU_STEPS self.dir_idx = 0 self.max_dir = 8 self.step_idx = 0 self.max_step = 7 else: self.steps = KG_STEPS self.dir_idx = 0 self.max_dir = 10 self.step_idx = 0 self.max_step = 1 stepx, stepy, prom_piece = self.read_steps() dstx = self.board_x + stepx dsty = self.board_y + stepy flag, errmsg = rules.is_move_valid(self.match, self.board_x, self.board_y, dstx, dsty, prom_piece) if(flag): gmove = GenMove(self.board_x, self.board_y, dstx, dsty, prom_piece) self.rotate() return True, gmove else: if(errmsg == rules.ERROR_CODES['king-error']): self.rotate() else: if(self.rotate_dir() == False): return False, gmove return False, gmove