Exemplo n.º 1
0
def put_man(topic_name, v, h, merels_storage):
    """Puts a man into the specified cell in topic_name

    :param topic_name: Topic name
    :param v: Vertical position of cell
    :param h: Horizontal position of cell
    :param merels_storage: MerelsDatabase object
    :return: A response string
    """
    merels = database.MerelsStorage(topic_name, merels_storage)
    data = game_data.GameData(merels.get_game_data(topic_name))

    # Get the grid
    grid = data.grid()

    # Check legal put
    if is_legal_put(v, h, grid, data.get_phase()):
        # Put the man
        put_man_legal(data.turn, v, h, grid)
        # Construct the board back from updated grid
        board = interface.construct_board(grid)
        # Insert/update form current board
        data.board = board
        # Update the game data
        merels.update_game(data.topic_name, data.turn, data.x_taken,
                           data.o_taken, data.board, data.hill_uid,
                           data.take_mode)
        return "Put a man to ({}, {}) for {}.".format(v, h, data.turn)
    else:
        raise BadMoveException(
            "Failed: That's not a legal put. Please try again.")
Exemplo n.º 2
0
 def make_move(self, move: str, player: int, is_computer: bool = False) -> Any:
     if not is_computer:
         if int(move.replace("move ", "")) < 9:
             return "mock board"
         else:
             raise BadMoveException("Invalid Move.")
     return "mock board"
Exemplo n.º 3
0
def move_man(topic_name, p1, p2, merels_storage):
    """Moves the current man in topic_name from p1 to p2

    :param topic_name: Topic name
    :param p1: First cell location
    :param p2: Second cell location
    :param merels_storage: Merels' storage
    :return: A response string
    """
    merels = database.MerelsStorage(topic_name, merels_storage)
    data = game_data.GameData(merels.get_game_data(topic_name))

    # Get the grid
    grid = data.grid()

    # Check legal move
    if is_legal_move(p1[0], p1[1], p2[0], p2[1], data.turn, data.get_phase(),
                     data.grid()):
        # Move the man
        move_man_legal(p1[0], p1[1], p2[0], p2[1], grid)
        # Construct the board back from updated grid
        board = interface.construct_board(grid)
        # Insert/update the current board
        data.board = board
        # Update the game data
        merels.update_game(data.topic_name, data.turn, data.x_taken,
                           data.o_taken, data.board, data.hill_uid,
                           data.take_mode)
        return "Moved a man from ({}, {}) -> ({}, {}) for {}.".format(
            p1[0], p1[1], p2[0], p2[1], data.turn)
    else:
        raise BadMoveException(
            "Failed: That's not a legal move. Please try again.")
Exemplo n.º 4
0
def unknown_command():
    """Returns an unknown command info

    :return: A string containing info about available commands
    """
    message = "Unknown command. Available commands: put (v,h), take (v,h), move (v,h) -> (v,h)"
    raise BadMoveException(message)
Exemplo n.º 5
0
 def make_move(self, move: str, player_number: int, computer_move: bool=False) -> Any:
     if computer_move:
         return self.computer_move(self.current_board, player_number + 1)
     move_coords_str = coords_from_command(move)
     if not self.is_valid_move(move_coords_str):
         raise BadMoveException('Make sure your move is from 0-9')
     board = self.current_board
     move_coords = move_coords_str.split(',')
     # Subtraction must be done to convert to the right indices,
     # since computers start numbering at 0.
     row = (int(move_coords[1])) - 1
     column = (int(move_coords[0])) - 1
     if board[row][column] != 0:
         raise BadMoveException('Make sure your space hasn\'t already been filled.')
     board[row][column] = player_number + 1
     return board
Exemplo n.º 6
0
 def make_move(self,
               move: str,
               player: int,
               is_computer: bool = False) -> Any:
     if not is_computer:
         if int(move.replace('move ', '')) < 9:
             return 'mock board'
         else:
             raise BadMoveException('Invalid Move.')
     return 'mock board'
Exemplo n.º 7
0
    def make_move(self,
                  move: str,
                  player_number: int,
                  computer_move: bool = False) -> Any:
        board = self.current_board
        move = move.strip()
        move = move.split(' ')

        if '' in move:
            raise BadMoveException('You should enter space separated digits.')
        moves = len(move)
        for m in range(1, moves):
            tile = int(move[m])
            coordinates = self.get_coordinates(board)
            if tile not in coordinates:
                raise BadMoveException(
                    'You can only move tiles which exist in the board.')
            i, j = coordinates[tile]
            if (j - 1) > -1 and board[i][j - 1] == 0:
                board[i][j - 1] = tile
                board[i][j] = 0
            elif (i - 1) > -1 and board[i - 1][j] == 0:
                board[i - 1][j] = tile
                board[i][j] = 0
            elif (j + 1) < 3 and board[i][j + 1] == 0:
                board[i][j + 1] = tile
                board[i][j] = 0
            elif (i + 1) < 3 and board[i + 1][j] == 0:
                board[i + 1][j] = tile
                board[i][j] = 0
            else:
                raise BadMoveException(
                    'You can only move tiles which are adjacent to :grey_question:.'
                )
            if m == moves - 1:
                return board
Exemplo n.º 8
0
    def make_move(self, move, player_number, is_computer=False):
        if player_number == 1:
            token_number = -1
        if player_number == 0:
            token_number = 1
        finding_move = True
        row = 5
        column = int(move.replace('move ', '')) - 1

        while finding_move:
            if row < 0:
                raise BadMoveException('Make sure your move is in a column with free space.')
            if self.current_board[row][column] == 0:
                self.current_board[row][column] = token_number
                finding_move = False

            row -= 1

        return deepcopy(self.current_board)
Exemplo n.º 9
0
def take_man(topic_name, v, h, merels_storage):
    """Takes a man from the grid

    :param topic_name: Topic name
    :param v: Vertical position of cell
    :param h: Horizontal position of cell
    :param merels_storage: Merels' storage
    :return: A response string
    """
    merels = database.MerelsStorage(topic_name, merels_storage)
    data = game_data.GameData(merels.get_game_data(topic_name))

    # Get the grid
    grid = data.grid()

    # Check legal put
    if is_legal_take(v, h, data.turn, grid, data.take_mode):
        # Take the man
        take_man_legal(v, h, grid)

        if data.turn == "X":
            data.o_taken += 1
        else:
            data.x_taken += 1

        # Construct the board back from updated grid
        board = interface.construct_board(grid)
        # Insert/update form current board
        data.board = board
        # Update the game data
        merels.update_game(
            data.topic_name,
            data.turn,
            data.x_taken,
            data.o_taken,
            data.board,
            data.hill_uid,
            data.take_mode,
        )
        return f"Taken a man from ({v}, {h}) for {data.turn}."
    else:
        raise BadMoveException("Failed: That's not a legal take. Please try again.")
Exemplo n.º 10
0
def beat(message, topic_name, merels_storage):
    """ This gets triggered every time a user send a message in any topic
    :param message: User's message
    :param topic_name: User's current topic
    :param merels_storage: Merels' storage
    :return: a tuple of response string and message, non-empty string
             we want to keep the turn of the same played,
             an empty string otherwise.
    """
    database.MerelsStorage(topic_name, merels_storage)
    match = COMMAND_PATTERN.match(message)
    same_player_move = ""  # message indicating move of the same player

    if match is None:
        return unknown_command()
    if match.group(1) is not None and match.group(
            2) is not None and match.group(3) is not None:

        responses = ""
        command = match.group(1)

        if command.lower() == "move":

            p1 = [int(x) for x in match.group(2).split(",")]
            p2 = [int(x) for x in match.group(3).split(",")]

            if mechanics.get_take_status(topic_name, merels_storage) == 1:

                raise BadMoveException("Take is required to proceed."
                                       " Please try again.\n")

            responses += mechanics.move_man(topic_name, p1, p2,
                                            merels_storage) + "\n"
            no_moves = after_event_checkup(responses, topic_name,
                                           merels_storage)

            mechanics.update_hill_uid(topic_name, merels_storage)

            responses += mechanics.display_game(topic_name,
                                                merels_storage) + "\n"

            if no_moves != "":
                same_player_move = no_moves

        else:
            return unknown_command()

        if mechanics.get_take_status(topic_name, merels_storage) == 1:
            same_player_move = "Take is required to proceed.\n"
        return responses, same_player_move

    elif match.group(4) is not None and match.group(5) is not None:
        command = match.group(4)
        p1 = [int(x) for x in match.group(5).split(",")]

        # put 1,2
        if command == "put":
            responses = ""

            if mechanics.get_take_status(topic_name, merels_storage) == 1:
                raise BadMoveException("Take is required to proceed."
                                       " Please try again.\n")
            responses += mechanics.put_man(topic_name, p1[0], p1[1],
                                           merels_storage) + "\n"
            no_moves = after_event_checkup(responses, topic_name,
                                           merels_storage)

            mechanics.update_hill_uid(topic_name, merels_storage)

            responses += mechanics.display_game(topic_name,
                                                merels_storage) + "\n"

            if no_moves != "":
                same_player_move = no_moves
            if mechanics.get_take_status(topic_name, merels_storage) == 1:
                same_player_move = "Take is required to proceed.\n"
            return responses, same_player_move
        # take 5,3
        elif command == "take":
            responses = ""
            if mechanics.get_take_status(topic_name, merels_storage) == 1:
                responses += mechanics.take_man(topic_name, p1[0], p1[1],
                                                merels_storage) + "\n"
                if "Failed" in responses:
                    raise BadMoveException(responses)
                mechanics.update_toggle_take_mode(topic_name, merels_storage)
                no_moves = after_event_checkup(responses, topic_name,
                                               merels_storage)

                mechanics.update_hill_uid(topic_name, merels_storage)

                responses += mechanics.display_game(topic_name,
                                                    merels_storage) + "\n"
                responses += check_win(topic_name, merels_storage)

                if no_moves != "":
                    same_player_move = no_moves
                return responses, same_player_move
            else:
                raise BadMoveException("Taking is not possible.")
        else:
            return unknown_command()