Best Python code snippet using keyboard
grid.py
Source:grid.py
...89 return self.grid[x][y]90 91 #returns a cell neighbour in given direction92 def get_cell_neighbour(self, cell,direction):93 x,y = cell.get_position() 94 if direction == "up":95 #check if cell is in bounds96 if y-1 >= 0: 97 return self.grid[x][y-1]98 else:99 return Cell(Types.wall,x,y-1)100 elif direction == "down":101 if y+1 < self.height:102 return self.grid[x][y+1]103 else:104 return Cell(Types.wall,x,y+1)105 elif direction == "left":106 if x-1 >= 0:107 return self.grid[x-1][y]108 else:109 return Cell(Types.wall,x-1,y)110 elif direction == "right":111 if x+1 < self.width:112 return self.grid[x+1][y]113 else:114 return Cell(Types.wall,x+1,y)115 else:116 return None117 def get_all_neighbours(self, cell):118 directions = ["up", "down", "left", "right"]119 x,y = cell.get_position()120 neighbours = []121 for i in range(4):122 if self.get_cell_neighbour(cell, directions[i]).get_type() != Types.wall and self.get_cell_neighbour(cell, directions[i]).get_type() != Types.empty:123 neighbours.append(self.get_cell_neighbour(cell, directions[i]))124 log(f"found neighbour at {self.get_cell_neighbour(cell, directions[i]).get_position()}({self.get_cell_neighbour(cell, directions[i]).get_type()})",weight=70)125 if len(neighbours) == 0:126 log("no neighbours found",weight=70)127 return None128 return neighbours129 #moves cells in given direction 130 def move_cell(self, x, y, direction):131 log(f"moving cell at {(x,y)} in direction {direction} ({self.grid[x][y].get_position()})",weight=8)132 if x >= self.width or x < 0 or y >= self.height or y < 0:133 log(f"cell at {(x,y)} is out of bounds, ignoring.")134 return135 if direction == "up":136 self.grid[x][y].set_position(x, y-1)137 elif direction == "down":138 self.grid[x][y].set_position(x, y + 1)139 elif direction == "left":140 self.grid[x][y].set_position(x - 1, y)141 elif direction == "right":142 self.grid[x][y].set_position(x + 1, y)143 log(f"new position: {self.grid[x][y].get_position()}",weight=8)144 self.update_positions()145 def swap_cells(self, cell1, cell2):146 log(f"swapping cells {cell1.get_position()} and {cell2.get_position()}",weight=60)147 cell1_type = cell1.get_type()148 cell2_type = cell2.get_type()149 cell1.set_type(cell2_type)150 cell2.set_type(cell1_type)151 cell1.set_status(True)152 cell2.set_status(True)153 self.update_positions() 154 #Update cell position when it is moved155 def update_positions(self):156 for x in range(self.width):157 for y in range(self.height):158 if not self.grid[x][y].get_position() == (x,y):159 log(f"updating position of cell on {(x,y)} with position {self.grid[x][y].get_position()}",weight=10)160 #If cells position is not the same as the current cell in loop, set in array to new position161 cell = self.grid[x][y]162 self.grid[cell.get_position()[0]][cell.get_position()[1]] = cell 163 self.grid[cell.get_position()[0]][cell.get_position()[1]].set_status(False)164 self.grid[x][y] = Cell(Types.empty, x, y)165 #Update cell position based on its type166 #Main function for updating cell positions based on their type167 def update_physics(self):168 for x in range(self.width):169 for y in range(self.height):170 self.grid[x][y].set_status(True)171 for x in range(self.width):172 for y in range(self.height):173 cell = self.grid[x][y]174 def move_sides():175 rnd1=random.randint(0,3)176 if rnd1 == 0:177 if self.get_cell_neighbour(cell,"left").get_type() == Types.empty:178 self.move_cell(cell.get_position()[0], cell.get_position()[1], "left")179 elif rnd1 == 1:180 if self.get_cell_neighbour(cell,"right").get_type() == Types.empty:181 self.move_cell(cell.get_position()[0], cell.get_position()[1], "right")182 183 #-------------CLONE-------------184 if cell.get_type() == Types.clone:185 if "cloning" in cell.get_data():186 if self.get_cell_neighbour(cell,"down").get_type() == Types.empty:187 self.set_cell_type(cell.get_position()[0], cell.get_position()[1]+1, cell.get_data()["cloning"])188 else:189 190 neighbours = self.get_all_neighbours(cell)191 if neighbours != None:192 selected = neighbours[random.randint(0,len(neighbours)-1)]193 log(f"cell at {cell.get_position()} cloning {selected.get_type()}",weight=70)194 195 cell.set_data({"cloning":selected.get_type()})196 if self.get_cell_neighbour(cell,"down").get_type() == Types.empty:197 self.set_cell_type(cell.get_position()[0], cell.get_position()[1]+1, cell.get_data()["cloning"])198 199 #-------------SAND-------------200 elif cell.get_type() == Types.sand:201 if cell.get_status() == False: #check if cell was updated in previous loop202 #log(f"status blocked {cell.get_position()}")203 continue204 #Check if cell is on edge of grid205 if cell.get_position()[1] >= self.height - 1:206 continue207 #Check if cell under current cell NOT empty208 if not self.get_cell_neighbour(cell,"down").get_type() == Types.empty:209 if self.get_cell_neighbour(cell,"down").get_type() == Types.smoke or self.get_cell_neighbour(cell,"down").get_type() == Types.water:210 self.swap_cells(cell,self.get_cell_neighbour(cell,"down"))211 continue212 rnd1=random.randint(0,6)213 #move cell randomly214 if rnd1 == 0:215 if self.get_cell_neighbour(cell,"left").get_type() == Types.empty:216 if self.get_cell(cell.get_position()[0]-1,cell.get_position()[1]+1).get_type() == Types.empty:217 self.move_cell(cell.get_position()[0], cell.get_position()[1], "left")218 else:219 if self.get_cell_neighbour(cell,"left").get_type() == Types.smoke or self.get_cell_neighbour(cell,"left").get_type() == Types.water:220 self.swap_cells(cell,self.get_cell_neighbour(cell,"left"))221 elif self.get_cell_neighbour(cell,"right").get_type() == Types.empty:222 if self.get_cell(cell.get_position()[0]+1,cell.get_position()[1]+1).get_type() == Types.empty:223 self.move_cell(cell.get_position()[0], cell.get_position()[1], "right")224 else:225 if self.get_cell_neighbour(cell,"right").get_type() == Types.smoke or self.get_cell_neighbour(cell,"right").get_type() == Types.water:226 self.swap_cells(cell,self.get_cell_neighbour(cell,"right"))227 elif rnd1 == 1:228 if self.get_cell_neighbour(cell,"right").get_type() == Types.empty:229 if self.get_cell(cell.get_position()[0]+1,cell.get_position()[1]+1).get_type() == Types.empty:230 self.move_cell(cell.get_position()[0], cell.get_position()[1], "right")231 else:232 if self.get_cell_neighbour(cell,"right").get_type() == Types.smoke or self.get_cell_neighbour(cell,"right").get_type() == Types.water:233 self.swap_cells(cell,self.get_cell_neighbour(cell,"right"))234 elif self.get_cell_neighbour(cell,"left").get_type() == Types.empty:235 if self.get_cell(cell.get_position()[0]-1,cell.get_position()[1]+1).get_type() == Types.empty:236 self.move_cell(cell.get_position()[0], cell.get_position()[1], "left")237 else:238 if self.get_cell_neighbour(cell,"left").get_type() == Types.smoke or self.get_cell_neighbour(cell,"left").get_type() == Types.water:239 self.swap_cells(cell,self.get_cell_neighbour(cell,"left"))240 241 continue242 243 #move cell down244 self.move_cell(cell.get_position()[0], cell.get_position()[1], "down")245 #-------------WATER-------------246 elif cell.get_type()==Types.water:247 if cell.get_status() == False:#check if cell was updated in previous loop248 #log(f"status blocked {cell.get_position()}")249 continue250 rnd0 = random.randint(0,3)251 rnd1 = random.randint(0,2)252 253 #if it's on the floor, move it left or right from time to time254 if cell.get_position()[1] >= self.height - 1:255 move_sides()256 continue257 #Check if cell under current cell NOT empty258 if not self.get_cell_neighbour(cell,"down").get_type() == Types.empty:259 #if cell under current cell is smoke, swap cells260 if self.get_cell_neighbour(cell,"down").get_type() == Types.smoke:261 self.swap_cells(cell,self.get_cell_neighbour(cell,"down"))262 continue263 rnd1=random.randint(0,3)264 #move cell randomly265 if rnd1 == 0:266 if self.get_cell_neighbour(cell,"left").get_type() == Types.empty:267 self.move_cell(cell.get_position()[0], cell.get_position()[1], "left")268 elif self.get_cell_neighbour(cell,"right").get_type() == Types.empty:269 self.move_cell(cell.get_position()[0], cell.get_position()[1], "right")270 elif rnd1 == 1:271 if self.get_cell_neighbour(cell,"right").get_type() == Types.empty:272 self.move_cell(cell.get_position()[0], cell.get_position()[1], "right")273 elif self.get_cell_neighbour(cell,"left").get_type() == Types.empty:274 self.move_cell(cell.get_position()[0], cell.get_position()[1], "left")275 continue276 277 278 #move cell down279 self.move_cell(cell.get_position()[0], cell.get_position()[1], "down")280 #-------------SMOKE-------------281 elif cell.get_type()==Types.smoke:282 if cell.get_status() == False:#check if cell was updated in previous loop283 #log(f"status blocked {cell.get_position()}")284 continue285 #if it's on the floor, move it left or right from time to time286 if cell.get_position()[1] <=0:287 move_sides()288 continue289 #Check if cell under current cell NOT empty290 if not self.get_cell_neighbour(cell,"up").get_type() == Types.empty:291 #move cell randomly left or right, the same as on floor292 move_sides()293 continue294 295 #move cell down...
Game_Controller.py
Source:Game_Controller.py
...21 if (cell.get_y() >= self.game_board.get_board_dimenstion()[1]) or (cell.get_y() < 0):22 return False23 if self.game_board.get_board()[cell.get_x()][cell.get_y()] != '-':24 return False25 return ((abs(cell.get_x() - blob.get_position().get_x()) == 1) and (26 abs(cell.get_y() - blob.get_position().get_y()) <= 1)) or (27 (abs(cell.get_x() - blob.get_position().get_x()) <= 1) and (28 abs(cell.get_y() - blob.get_position().get_y()) == 1))29 # blob can move to 2 cell away30 def can_move(self, blob, cell):31 if (cell.get_x() >= self.game_board.get_board_dimenstion()[0]) or (cell.get_x() < 0):32 return False33 if (cell.get_y() >= self.game_board.get_board_dimenstion()[1]) or (cell.get_y() < 0):34 return False35 if self.game_board.get_board()[cell.get_x()][cell.get_y()] != '-':36 return False37 return ((abs(cell.get_x() - blob.get_position().get_x()) == 2) and (38 abs(cell.get_y() - blob.get_position().get_y()) <= 2)) or (39 (abs(cell.get_x() - blob.get_position().get_x()) <= 2) and (40 abs(cell.get_y() - blob.get_position().get_y()) == 2))41 # copy a blob to a specific cell42 def copy_blob_to(self, blob, cell_to_copy):43 new_blob = Blob(Position(cell_to_copy.get_x(), cell_to_copy.get_y()), blob.get_player())44 self.game_board.get_blobs_list().append(new_blob) # add the new blob45 self.attack_adjacent_blobs(new_blob)46 # move a blob to a specific cell47 def move_blob_to(self, blob, cell_to_move):48 blob.set_position(Position(cell_to_move.get_x(), cell_to_move.get_y())) # set the new position to blob49 self.attack_adjacent_blobs(blob)50 # apply blob movement51 def make_move_to_blob(self, blob_cell, cell_to_move, player):52 blob = None53 for iter_blob in self.game_board.get_blobs_list():54 if iter_blob.get_position().get_x() == blob_cell.get_x() and iter_blob.get_position().get_y() == blob_cell.get_y():55 blob = iter_blob56 break57 # if no blob has found58 if blob is None or blob.get_player().get_type() != player.get_type():59 return False60 if self.can_copy(blob, cell_to_move):61 self.copy_blob_to(blob, cell_to_move)62 elif self.can_move(blob, cell_to_move):63 self.move_blob_to(blob, cell_to_move)64 else:65 return False66 return True67 # turn the adjacent opponents into blob's team68 def attack_adjacent_blobs(self, blob):69 # check the upper cell70 if ((blob.get_position().get_x() - 1 >= 0) and (71 self.game_board.get_board()[blob.get_position().get_x() - 1][blob.get_position().get_y()] != '-')):72 temp_position = Position(blob.get_position().get_x() - 1, blob.get_position().get_y())73 for iter_blob in self.game_board.get_blobs_list():74 if iter_blob.get_position().get_x() == temp_position.get_x() and iter_blob.get_position().get_y() == temp_position.get_y():75 iter_blob.set_player(blob.get_player())76 break77 # check the upper right cell78 if ((blob.get_position().get_x() - 1 >= 0) and (79 blob.get_position().get_y() + 1 < self.game_board.get_board_dimenstion()[1]) and (80 self.game_board.get_board()[blob.get_position().get_x() - 1][blob.get_position().get_y() + 1] != '-')):81 temp_position = Position(blob.get_position().get_x() - 1, blob.get_position().get_y() + 1)82 for iter_blob in self.game_board.get_blobs_list():83 if iter_blob.get_position().get_x() == temp_position.get_x() and iter_blob.get_position().get_y() == temp_position.get_y():84 iter_blob.set_player(blob.get_player())85 break86 # check the right cell87 if ((blob.get_position().get_y() + 1 < self.game_board.get_board_dimenstion()[1]) and (88 self.game_board.get_board()[blob.get_position().get_x()][blob.get_position().get_y() + 1] != '-')):89 temp_position = Position(blob.get_position().get_x(), blob.get_position().get_y() + 1)90 for iter_blob in self.game_board.get_blobs_list():91 if iter_blob.get_position().get_x() == temp_position.get_x() and iter_blob.get_position().get_y() == temp_position.get_y():92 iter_blob.set_player(blob.get_player())93 break94 # check the lower right cell95 if ((blob.get_position().get_x() + 1 < self.game_board.get_board_dimenstion()[0]) and (96 blob.get_position().get_y() + 1 < self.game_board.get_board_dimenstion()[1]) and (97 self.game_board.get_board()[blob.get_position().get_x() + 1][blob.get_position().get_y() + 1] != '-')):98 temp_position = Position(blob.get_position().get_x() + 1, blob.get_position().get_y() + 1)99 for iter_blob in self.game_board.get_blobs_list():100 if iter_blob.get_position().get_x() == temp_position.get_x() and iter_blob.get_position().get_y() == temp_position.get_y():101 iter_blob.set_player(blob.get_player())102 break103 # check the lower cell104 if ((blob.get_position().get_x() + 1 < self.game_board.get_board_dimenstion()[0]) and (105 self.game_board.get_board()[blob.get_position().get_x() + 1][blob.get_position().get_y()] != '-')):106 temp_position = Position(blob.get_position().get_x() + 1, blob.get_position().get_y())107 for iter_blob in self.game_board.get_blobs_list():108 if iter_blob.get_position().get_x() == temp_position.get_x() and iter_blob.get_position().get_y() == temp_position.get_y():109 iter_blob.set_player(blob.get_player())110 break111 # check the lower left cell112 if ((blob.get_position().get_x() + 1 < self.game_board.get_board_dimenstion()[0]) and (113 blob.get_position().get_y() - 1 >= 0) and (114 self.game_board.get_board()[blob.get_position().get_x() + 1][blob.get_position().get_y() - 1] != '-')):115 temp_position = Position(blob.get_position().get_x() + 1, blob.get_position().get_y() - 1)116 for iter_blob in self.game_board.get_blobs_list():117 if iter_blob.get_position().get_x() == temp_position.get_x() and iter_blob.get_position().get_y() == temp_position.get_y():118 iter_blob.set_player(blob.get_player())119 break120 # check the left cell121 if ((blob.get_position().get_y() - 1 >= 0) and (122 self.game_board.get_board()[blob.get_position().get_x()][blob.get_position().get_y() - 1] != '-')):123 temp_position = Position(blob.get_position().get_x(), blob.get_position().get_y() - 1)124 for iter_blob in self.game_board.get_blobs_list():125 if iter_blob.get_position().get_x() == temp_position.get_x() and iter_blob.get_position().get_y() == temp_position.get_y():126 iter_blob.set_player(blob.get_player())127 break128 # check the lower left cell129 if ((blob.get_position().get_x() - 1 >= 0) and (blob.get_position().get_y() - 1 >= 0) and (130 self.game_board.get_board()[blob.get_position().get_x() - 1][blob.get_position().get_y() - 1] != '-')):131 temp_position = Position(blob.get_position().get_x() - 1, blob.get_position().get_y() - 1)132 for iter_blob in self.game_board.get_blobs_list():133 if iter_blob.get_position().get_x() == temp_position.get_x() and iter_blob.get_position().get_y() == temp_position.get_y():134 iter_blob.set_player(blob.get_player())135 break136 # state space137 def next_states(self, player):138 states_list = [] # to store all the possible moves139 blobs_moved = [] # to store blobs moved140 cells_to_move_to = [] # to store the cells that the blobs moved to in every state141 # iterate over blobs in the board142 for blob in self.game_board.get_blobs_list():143 if blob.get_player().get_type() == player.get_type(): # to ensure that player's blobs only will move144 for i in range(self.game_board.get_board_dimenstion()[0] * self.game_board.get_board_dimenstion()[145 1]): # iterate over board cells146 cell = self.mapping(i + 1) # get cell as [x, y]147 pos = Position(cell[0], cell[1])148 if (blob.get_position().get_x() != cell[0]) or (149 blob.get_position().get_y() != cell[1]): # to ensure not to take blob's cell150 # if blob can copy itself to the adjacent cell151 if self.can_copy(blob, pos):152 temp_state = copy.deepcopy(self)153 if temp_state.make_move_to_blob(blob.get_position(), pos, blob.get_player()):154 states_list.append(temp_state)155 blobs_moved.append(blob)156 cells_to_move_to.append(pos)157 # if blob can move to 2 cells away158 if self.can_move(blob, pos):159 temp_state = copy.deepcopy(self)160 if temp_state.make_move_to_blob(blob.get_position(), pos, blob.get_player()):161 states_list.append(temp_state)162 blobs_moved.append(blob)163 cells_to_move_to.append(pos)164 return states_list, blobs_moved, cells_to_move_to165 # Playing with AI166 def AI_plays(self, game_logic, depth):167 next_move = game_logic.Mini_Max_alpha_beta(self, depth, AI=True)...
dfs.py
Source:dfs.py
...34 return None35 else:36 new_position[1] += 137 return tuple(new_position)38 def get_position(self):39 return self.coordinate40all_points = []41for i in range(m):42 row = []43 for j in range(n):44 row.append(Point((i, j), 0))45 all_points.append(row)46all_points[0][0].possible_move = [0, 1, 0, 1]47all_points[m - 1][n - 1].possible_move = [0, 0, 0, 0]48stack_path = [[all_points[0][0]]]49stack_cost = [0]50min_cost = 999999951def Solution(current_path):52 global min_cost, all_points53 cost = 054 for i in range(1, len(current_path)):55 if current_path[i].get_position()[0] == current_path[i - 1].get_position()[0]:56 if current_path[i].get_position()[1] - current_path[i - 1].get_position()[1] == 1:57 cost += 258 else:59 cost -= 260 else:61 if current_path[i].get_position()[0] - current_path[i - 1].get_position()[0] == 1:62 cost *= 263 else:64 cost /= 265 if cost < min_cost:66 min_cost = cost67 print('Current minimum cost:', cost)68 print('Current optimal path:')69 print([x.get_position() for x in current_path])70 print('\n\n')71def DFS():72 global stack_path, min_cost, move_cost, all_points, stack_cost, count73 if len(stack_path) == 0:74 return75 current_path = stack_path.pop()76 current_node = current_path[-1]77 for v in range(4):78 if current_node.possible_move[v] == 1:79 if v == 0:80 if current_node.go_up() is not None:81 new_coordinate = current_node.go_up()82 current_path.append(all_points[new_coordinate[0]][new_coordinate[1]])83 stack_cost[-1] /= move_cost[0]84 all_points[new_coordinate[0]][new_coordinate[1]].possible_move[1] = 085 all_points[current_node.get_position()[0]][current_node.get_position()[1]].possible_move[0] = 086 if current_path[-1].get_position() == (m - 1, n - 1):87 Solution(current_path)88 elif current_path[-1].possible_move != [0, 0, 0, 0]:89 stack_path.append(current_path)90 DFS()91 current_path.pop()92 all_points[new_coordinate[0]][new_coordinate[1]].possible_move[1] = 193 all_points[current_node.get_position()[0]][current_node.get_position()[1]].possible_move[0] = 194 elif v == 1:95 if current_node.go_down() is not None:96 new_coordinate = current_node.go_down()97 current_path.append(all_points[new_coordinate[0]][new_coordinate[1]])98 stack_cost[-1] *= move_cost[1]99 all_points[new_coordinate[0]][new_coordinate[1]].possible_move[0] = 0100 all_points[current_node.get_position()[0]][current_node.get_position()[1]].possible_move[1] = 0101 if current_path[-1].get_position() == (m - 1, n - 1):102 Solution(current_path)103 elif current_path[-1].possible_move != [0, 0, 0, 0]:104 stack_path.append(current_path)105 DFS()106 current_path.pop()107 all_points[new_coordinate[0]][new_coordinate[1]].possible_move[0] = 1108 all_points[current_node.get_position()[0]][current_node.get_position()[1]].possible_move[1] = 1109 elif v == 2:110 if current_node.go_left() is not None:111 new_coordinate = current_node.go_left()112 current_path.append(all_points[new_coordinate[0]][new_coordinate[1]])113 stack_cost[-1] -= move_cost[2]114 all_points[new_coordinate[0]][new_coordinate[1]].possible_move[3] = 0115 all_points[current_node.get_position()[0]][current_node.get_position()[1]].possible_move[2] = 0116 if current_path[-1].get_position() == (m - 1, n - 1):117 Solution(current_path)118 elif current_path[-1].possible_move != [0, 0, 0, 0]:119 stack_path.append(current_path)120 DFS()121 current_path.pop()122 all_points[new_coordinate[0]][new_coordinate[1]].possible_move[3] = 1123 all_points[current_node.get_position()[0]][current_node.get_position()[1]].possible_move[2] = 1124 elif v == 3:125 if current_node.go_right() is not None:126 new_coordinate = current_node.go_right()127 current_path.append(all_points[new_coordinate[0]][new_coordinate[1]])128 stack_cost[-1] += move_cost[3]129 all_points[new_coordinate[0]][new_coordinate[1]].possible_move[2] = 0130 all_points[current_node.get_position()[0]][current_node.get_position()[1]].possible_move[3] = 0131 if current_path[-1].get_position() == (m - 1, n - 1):132 Solution(current_path)133 elif current_path[-1].possible_move != [0, 0, 0, 0]:134 stack_path.append(current_path)135 DFS()136 current_path.pop()137 all_points[new_coordinate[0]][new_coordinate[1]].possible_move[2] = 1138 all_points[current_node.get_position()[0]][current_node.get_position()[1]].possible_move[3] = 1...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!