metin2dev python grid position

Solutions on MaxInterview for metin2dev python grid position by the best coders in the world

showing results for - "metin2dev python grid position"
Carl
03 Jul 2019
1"""
2    This module was written by Ken for metin2dev.org (Please don't try to change it.)
3    
4    Note:
5        Use reset function instead of deleting the object and creating it again. (That's more easy)        
6"""
7
8
9class Grid:
10    """
11        Args:
12            width (int): Grid's width.
13            height (int): Grid's height.
14
15        Attributes:
16            grid (list): The list will hold the position empty or not information.
17            width (int): Grid's width
18            height (int): Grid's height
19    """
20
21    def __init__(self, width, height):
22        self.grid = [False] * (width * height)
23        self.width = width
24        self.height = height
25
26    def __str__(self):
27        output = "Grid {}x{} Information\n".format(self.width, self.height)
28        for row in range(self.height):
29            for col in range(self.width):
30                output += "Status of %d: " % (row * self.width + col)
31                output += "NotEmpty, " if self.grid[row *
32                                                   self.width + col] else "Empty, "
33            output += "\n"
34
35        return output
36
37    def find_blank(self, width, height):
38        """
39            Args:
40                width (int): The item's width you can call width item.GetItemSize()[0]
41                height (int): The item's height you can call width item.GetItemSize()[1]
42
43            Returns:
44                int: The return value would be an int if successful. Otherwise -1.
45        """
46        if width > self.width or height > self.height:
47            return -1
48
49        for row in range(self.height):
50            for col in range(self.width):
51                index = row * self.width + col
52                if self.is_empty(index, width, height):
53                    return index
54
55        return -1
56
57    def put(self, pos, width, height):
58        """
59            Args:
60                pos (int): Position of the item to put.
61                width (int): The item's width you can call width item.GetItemSize()[0]
62                height (int): The item's height you can call width item.GetItemSize()[1]
63
64            Returns:
65                bool: The return value. True for success, False otherwise.
66        """
67        if not self.is_empty(pos, width, height):
68            return False
69
70        for row in range(height):
71            start = pos + (row * self.width)
72            self.grid[start] = True
73            col = 1
74            while col < width:
75                self.grid[start + col] = True
76                col += 1
77
78        return True
79
80    def clear(self, pos, width, height):
81        """
82            Args:
83                pos (int): Position of the item to put.
84                width (int): The item's width you can call width item.GetItemSize()[0]
85                height (int): The item's height you can call width item.GetItemSize()[1]
86
87            Returns:
88                There is nothing to return
89        """
90        if pos < 0 or pos >= (self.width * self.height):
91            return
92
93        for row in range(height):
94            start = pos + (row * self.width)
95            self.grid[start] = True
96            col = 1
97            while col < width:
98                self.grid[start + col] = False
99                col += 1
100
101    def is_empty(self, pos, width, height):
102        """
103            Args:
104                pos (int): Position of the item to put.
105                width (int): The item's width you can call width item.GetItemSize()[0]
106                height (int): The item's height you can call width item.GetItemSize()[1]
107
108            Returns:
109                bool: The return value. True for success, False otherwise.
110        """
111        if pos < 0:
112            return False
113
114        row = pos // self.width
115        if (row + height) > self.height:
116            return False
117
118        if (pos + width) > ((row * self.width) + self.width):
119            return False
120
121        for row in range(height):
122            start = pos + (row * self.width)            
123            if self.grid[start]:                
124                return False
125
126            col = 1
127            while col < width:
128                if self.grid[start + col]:                    
129                    return False
130                col += 1
131
132        return True
133
134    def get_size(self):
135        """
136            Returns:
137                int: The return value will give you maximum capacity of grid. (width * height)
138        """
139        return self.width * self.height
140
141    def reset(self):
142        """
143            With this function, you can reset instead of deleting it and create again.
144        """
145        self.grid = [False] * (self.width * self.height)
similar questions
queries leading to this page
metin2dev python grid position