def allowed_move(self, pawn_type, owner, vertical_coord, horizontal_coord, player, check_king_under_attack=True): pawn = Pawn(owner, pawn_type) allowed_moves = [] if pawn.owner == "player1" and pawn.pawn_type == "pawn": allowed_moves = self._allowed_move_pawn_player1(vertical_coord, horizontal_coord, not check_king_under_attack) elif pawn.owner == "player2" and pawn.pawn_type == "pawn": allowed_moves = self._allowed_move_pawn_player2(vertical_coord, horizontal_coord, not check_king_under_attack) elif pawn.pawn_type == "king": allowed_moves = self._allowed_move_king(vertical_coord, horizontal_coord, pawn.owner) elif pawn.pawn_type == "queen": allowed_moves = self._allowed_move_queen(vertical_coord, horizontal_coord, pawn.owner) elif pawn.pawn_type == "bishop": allowed_moves = self._allowed_move_bishop(vertical_coord, horizontal_coord, pawn.owner) elif pawn.pawn_type == "rook": allowed_moves = self._allowed_move_rook(vertical_coord, horizontal_coord, pawn.owner) elif pawn.pawn_type == "knight": allowed_moves = self._allowed_move_knight(vertical_coord, horizontal_coord, pawn.owner) if check_king_under_attack: allowed_moves = self._remove_king_under_attack_moves(Coord(vertical_coord, horizontal_coord), allowed_moves, pawn, player) return allowed_moves
def play(request): if request.method == 'POST': params = json.loads(request.body.decode('utf-8')) moved_pawn = Pawn.objects.get(id=params["pawn_id"]) game = Game.objects.get(id=params["game_id"]) destination = Coord(params["vertical_coord"], params["horizontal_coord"]) move = Move(params["pawn_id"], destination) #check move allowed board = Board() board.buildBoard(game) if board.is_move_allowed(moved_pawn, destination, "player1"): game.strokes_number = game.strokes_number + 1 game.save() Pawn.move_pawn_and_kill_if_needed(move, game.strokes_number) # control check mat board.buildBoard(game) check_mate = board.is_check_mate_or_draw("player2") if check_mate == "checkmate": return HttpResponse(json.dumps("player1")) if check_mate == "draw": return HttpResponse(json.dumps("draw")) #move player 2 pawn with ia ia = IA(game, "player2") ia_move = ia.play() Pawn.move_pawn_and_kill_if_needed(ia_move, game.strokes_number) # control check mat check_mate = board.is_check_mate_or_draw("player1") if check_mate == "checkmate": return HttpResponse(json.dumps("player2")) if check_mate == "draw": return HttpResponse(json.dumps("draw")) return HttpResponse( json.dumps(board.get_pawns_and_allowed_moves(game, "player1"))) elif request.method == 'OPTIONS': return HttpResponse("ok")
def _allowed_move_pawn_player2(self, vertical_coord, horizontal_coord, check_king_under_attack=False): allowed_move = [] if vertical_coord == 0: return [] if self._is_empty(horizontal_coord, vertical_coord-1, self.board)and not check_king_under_attack: allowed_move.append(Coord(vertical_coord-1, horizontal_coord)) if (not self._is_empty(horizontal_coord - 1, vertical_coord-1, self.board) and self.board[vertical_coord-1][horizontal_coord-1].owner == "player1") \ or (check_king_under_attack and not self._is_mine(Coord(vertical_coord-1,horizontal_coord - 1), "player12", self.board)): allowed_move.append(Coord(vertical_coord - 1, horizontal_coord-1)) if (not self._is_empty(horizontal_coord + 1, vertical_coord - 1, self.board) and self.board[vertical_coord-1][horizontal_coord+1].owner == "player1") \ or (check_king_under_attack and not self._is_mine(Coord(vertical_coord - 1,horizontal_coord + 1), "player2", self.board)): allowed_move.append(Coord(vertical_coord - 1, horizontal_coord+1)) if vertical_coord == 6 and self._is_empty(horizontal_coord, vertical_coord-1, self.board) and self._is_empty(horizontal_coord, vertical_coord-2, self.board) and not check_king_under_attack: allowed_move.append(Coord(vertical_coord-2, horizontal_coord)) return allowed_move