Sign in to follow this  
Ken

python Grid class in Python

4 posts in this topic

Hey everyone, it's a nice day to share something ^_^ I just wrote Grid class in python language. You can use it as fast moving into the window with this or something else.

Spoiler

"""
    This module was written by Ken for metin2dev.org (Please don't try to change it.)
    
    Note:
        Use reset function instead of deleting the object and creating it again. (That's more easy)        
"""


class Grid:
    """
        Args:
            width (int): Grid's width.
            height (int): Grid's height.

        Attributes:
            grid (list): The list will hold the position empty or not information.
            width (int): Grid's width
            height (int): Grid's height
    """

    def __init__(self, width, height):
        self.grid = [False] * (width * height)
        self.width = width
        self.height = height

    def __str__(self):
        output = "Grid {}x{} Information\n".format(self.width, self.height)
        for row in range(self.height):
            for col in range(self.width):
                output += "Status of %d: " % (row * self.width + col)
                output += "NotEmpty, " if self.grid[row *
                                                   self.width + col] else "Empty, "
            output += "\n"

        return output

    def find_blank(self, width, height):
        """
            Args:
                width (int): The item's width you can call width item.GetItemSize()[0]
                height (int): The item's height you can call width item.GetItemSize()[1]

            Returns:
                int: The return value would be an int if successful. Otherwise -1.
        """
        if width > self.width or height > self.height:
            return -1

        for row in range(self.height):
            for col in range(self.width):
                index = row * self.width + col
                if self.is_empty(index, width, height):
                    return index

        return -1

    def put(self, pos, width, height):
        """
            Args:
                pos (int): Position of the item to put.
                width (int): The item's width you can call width item.GetItemSize()[0]
                height (int): The item's height you can call width item.GetItemSize()[1]

            Returns:
                bool: The return value. True for success, False otherwise.
        """
        if not self.is_empty(pos, width, height):
            return False

        for row in range(height):
            start = pos + (row * self.width)
            self.grid[start] = True
            col = 1
            while col < width:
                self.grid[start + col] = True
                col += 1

        return True

    def clear(self, pos, width, height):
        """
            Args:
                pos (int): Position of the item to put.
                width (int): The item's width you can call width item.GetItemSize()[0]
                height (int): The item's height you can call width item.GetItemSize()[1]

            Returns:
                There is nothing to return
        """
        if pos < 0 or pos >= (self.width * self.height):
            return

        for row in range(height):
            start = pos + (row * self.width)
            self.grid[start] = True
            col = 1
            while col < width:
                self.grid[start + col] = False
                col += 1

    def is_empty(self, pos, width, height):
        """
            Args:
                pos (int): Position of the item to put.
                width (int): The item's width you can call width item.GetItemSize()[0]
                height (int): The item's height you can call width item.GetItemSize()[1]

            Returns:
                bool: The return value. True for success, False otherwise.
        """
        if pos < 0:
            return False

        row = pos // self.width
        if (row + height) > self.height:
            return False

        if (pos + width) > ((row * self.width) + self.width):
            return False

        for row in range(height):
            start = pos + (row * self.width)            
            if self.grid[start]:                
                return False

            col = 1
            while col < width:
                if self.grid[start + col]:                    
                    return False
                col += 1

        return True

    def get_size(self):
        """
            Returns:
                int: The return value will give you maximum capacity of grid. (width * height)
        """
        return self.width * self.height

    def reset(self):
        """
            With this function, you can reset instead of deleting it and create again.
        """
        self.grid = [False] * (self.width * self.height)

 

Usage:

from grid import Grid
import item
import chat

grid = Grid(width=5, height=9)

item.SelectItem(11299)
(width, height) = item.GetItemSize()

available_position = grid.find_blank(width, height)
if available_position == -1:
  chat.AppendChat(chat.CHAT_TYPE_INFO, "There is no available position.")
  return

chat.AppendChat(chat.CHAT_TYPE_INFO, "Available position is %d" % (available_position))

Best Regards

Ken

  • Like 8
  • Thanks 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.