Best Python code snippet using avocado_python
mine_sweeper.py
Source:mine_sweeper.py  
...70            if event.button == 1:71                if first_click:72                    first_click = False73                    place_mines(x, y)74                uncover_cell(x, y)75            # if right click76            else:77                place_flag(x, y)'''787980def agent_move(agent_location):81    # mine sweeper agent82    # TODO allow agent to guess8384    # if adjacent mines85    if isinstance(mine_field[agent_location['y']][agent_location['x']], int):86        # if all adjacent mines have been flagged, uncover adjacent covered cells and remove adjacent mines87        if mine_field[agent_location['y']][agent_location['x']] == adjacent_flags(agent_location):88            uncover_adjacent(agent_location['x'], agent_location['y'])89        # compare number of adjacent mines to number of adjacent covered cells, if equal, flag adjacent covered cells90        if mine_field[agent_location['y']][agent_location['x']] == covered_adjacent(agent_location) + adjacent_flags(agent_location):91            flag_adjacent(agent_location)92        # if adjacent mines move along covered cells and mines right hand maze move93        if isinstance(mine_field[agent_location['y']][agent_location['x']], int):94            agent_location = follow_wall(agent_location)9596    # if no adjacent mines and adjacent covered cells uncover adjacent covered cells97    elif mine_field[agent_location['y']][agent_location['x']] == 'uncovered' and covered_adjacent(agent_location) > 0:98        uncover_adjacent(agent_location['x'], agent_location['y'])99    # if no adjacent mines and no adjacent covered cells move up100    elif mine_field[agent_location['y']][agent_location['x']] == 'uncovered' and covered_adjacent(agent_location) == 0:101        agent_location = follow_wall(agent_location)102103    return agent_location104105106def follow_wall(agent_location):107108    movement_tracker[agent_location['y']][agent_location['x']] += 1109    if movement_tracker[agent_location['y']][agent_location['x']] % 10 == 0:110        agent_location['right_hand'] = 'down'111112    if agent_location['right_hand'] == 'up':113        if agent_location['y'] == 0 or mine_field[agent_location['y'] - 1][agent_location['x']] == 'covered' or mine_field[agent_location['y'] - 1][agent_location['x']] == 'flagged':114            if agent_location['x'] != 0 and mine_field[agent_location['y']][agent_location['x'] - 1] != 'covered' and mine_field[agent_location['y']][agent_location['x'] - 1] != 'flagged':115                agent_location['x'] -= 1116            elif agent_location['y'] != grid_size - 1 and mine_field[agent_location['y'] + 1][agent_location['x']] != 'covered' and mine_field[agent_location['y'] + 1][agent_location['x']] != 'flagged':117                agent_location['y'] += 1118                agent_location['right_hand'] = 'left'119            else:120                agent_location['x'] += 1121                agent_location['right_hand'] = 'down'122        else:123            agent_location['y'] -= 1124            if agent_location['x'] != grid_size - 1 and (mine_field[agent_location['y']][agent_location['x'] + 1] == 'covered' or mine_field[agent_location['y']][agent_location['x'] + 1] == 'flagged'):125                agent_location['right_hand'] = 'right'126127    elif agent_location['right_hand'] == 'left':128        if agent_location['x'] == 0 or mine_field[agent_location['y']][agent_location['x'] - 1] == 'covered' or mine_field[agent_location['y']][agent_location['x'] - 1] == 'flagged':129            if agent_location['y'] != grid_size - 1 and mine_field[agent_location['y'] + 1][agent_location['x']] != 'covered' and mine_field[agent_location['y'] + 1][agent_location['x']] != 'flagged':130                agent_location['y'] += 1131            elif agent_location['x'] != grid_size - 1 and mine_field[agent_location['y']][agent_location['x'] + 1] != 'covered' and mine_field[agent_location['y']][agent_location['x'] + 1] != 'flagged':132                agent_location['x'] += 1133                agent_location['right_hand'] = 'down'134            else:135                agent_location['y'] -= 1136                agent_location['right_hand'] = 'right'137        else:138            agent_location['x'] -= 1139            agent_location['right_hand'] = 'up'140141    elif agent_location['right_hand'] == 'down':142        if agent_location['y'] == grid_size - 1 or mine_field[agent_location['y'] + 1][agent_location['x']] == 'covered' or mine_field[agent_location['y'] + 1][agent_location['x']] == 'flagged':143            if agent_location['x'] != grid_size - 1 and mine_field[agent_location['y']][agent_location['x'] + 1] != 'covered' and mine_field[agent_location['y']][agent_location['x'] + 1] != 'flagged':144                agent_location['x'] += 1145            elif agent_location['y'] != 0 and mine_field[agent_location['y'] - 1][agent_location['x']] != 'covered' and mine_field[agent_location['y'] - 1][agent_location['x']] != 'flagged':146                agent_location['y'] -= 1147                agent_location['right_hand'] = 'right'148            else:149                agent_location['x'] -= 1150                agent_location['right_hand'] = 'up'151        else:152            agent_location['y'] += 1153            agent_location['right_hand'] = 'left'154155    elif agent_location['right_hand'] == 'right':156        if agent_location['x'] == grid_size - 1 or mine_field[agent_location['y']][agent_location['x'] + 1] == 'covered' or mine_field[agent_location['y']][agent_location['x'] + 1] == 'flagged':157            if agent_location['y'] != 0 and mine_field[agent_location['y'] - 1][agent_location['x']] != 'covered' and mine_field[agent_location['y'] - 1][agent_location['x']] != 'flagged':158                agent_location['y'] -= 1159            elif agent_location['x'] != 0 and mine_field[agent_location['y']][agent_location['x'] - 1] != 'covered' and mine_field[agent_location['y']][agent_location['x'] - 1] != 'flagged':160                agent_location['x'] -= 1161                agent_location['right_hand'] = 'up'162            else:163                agent_location['y'] += 1164                agent_location['right_hand'] = 'left'165        else:166            agent_location['x'] += 1167            #if agent_location['y'] == 0 and (mine_field[agent_location['y'] - 1][agent_location['x']] == 'covered' or mine_field[agent_location['y'] - 1][agent_location['x']] == 'flagged'):168            agent_location['right_hand'] = 'down'169170    return agent_location171172173def adjacent_flags(agent_location):174    flags = 0175    if agent_location['y'] != 0 and mine_field[agent_location['y'] - 1][agent_location['x']] == 'flagged':176        flags += 1177    if agent_location['y'] != 0 and agent_location['x'] != grid_size - 1 and mine_field[agent_location['y'] - 1][agent_location['x'] + 1] == 'flagged':178        flags += 1179    if agent_location['x'] != grid_size - 1 and mine_field[agent_location['y']][agent_location['x'] + 1] == 'flagged':180        flags += 1181    if agent_location['y'] != grid_size - 1 and agent_location['x'] != grid_size - 1 and mine_field[agent_location['y'] + 1][agent_location['x'] + 1] == 'flagged':182        flags += 1183    if agent_location['y'] != grid_size - 1 and mine_field[agent_location['y'] + 1][agent_location['x']] == 'flagged':184        flags += 1185    if agent_location['y'] != grid_size - 1 and agent_location['x'] != 0 and mine_field[agent_location['y'] + 1][agent_location['x'] - 1] == 'flagged':186        flags += 1187    if agent_location['x'] != 0 and mine_field[agent_location['y']][agent_location['x'] - 1] == 'flagged':188        flags += 1189    if agent_location['y'] != 0 and agent_location['x'] != 0 and mine_field[agent_location['y'] - 1][agent_location['x'] - 1] == 'flagged':190        flags += 1191    return flags192193194def flag_adjacent(agent_location):195    if agent_location['y'] != 0 and mine_field[agent_location['y'] - 1][agent_location['x']] == 'covered':196        place_flag(agent_location['x'], agent_location['y'] - 1)197    if agent_location['y'] != 0 and agent_location['x'] != grid_size - 1 and mine_field[agent_location['y'] - 1][agent_location['x'] + 1] == 'covered':198        place_flag(agent_location['x'] + 1, agent_location['y'] - 1)199    if agent_location['x'] != grid_size - 1 and mine_field[agent_location['y']][agent_location['x'] + 1] == 'covered':200        place_flag(agent_location['x'] + 1, agent_location['y'])201    if agent_location['y'] != grid_size - 1 and agent_location['x'] != grid_size - 1 and mine_field[agent_location['y'] + 1][agent_location['x'] + 1] == 'covered':202        place_flag(agent_location['x'] + 1, agent_location['y'] + 1)203    if agent_location['y'] != grid_size - 1 and mine_field[agent_location['y'] + 1][agent_location['x']] == 'covered':204        place_flag(agent_location['x'], agent_location['y'] + 1)205    if agent_location['y'] != grid_size - 1 and agent_location['x'] != 0 and mine_field[agent_location['y'] + 1][agent_location['x'] - 1] == 'covered':206        place_flag(agent_location['x'] - 1, agent_location['y'] + 1)207    if agent_location['x'] != 0 and mine_field[agent_location['y']][agent_location['x'] - 1] == 'covered':208        place_flag(agent_location['x'] - 1, agent_location['y'])209    if agent_location['y'] != 0 and agent_location['x'] != 0 and mine_field[agent_location['y'] - 1][agent_location['x'] - 1] == 'covered':210        place_flag(agent_location['x'] - 1, agent_location['y'] - 1)211212213# returns number of adjacent covered cells214def covered_adjacent(agent_location):215    covered = 0216    if agent_location['y'] != 0 and mine_field[agent_location['y'] - 1][agent_location['x']] == 'covered':217        covered += 1218    if agent_location['y'] != 0 and agent_location['x'] != grid_size - 1 and mine_field[agent_location['y'] - 1][agent_location['x'] + 1] == 'covered':219        covered += 1220    if agent_location['x'] != grid_size - 1 and mine_field[agent_location['y']][agent_location['x'] + 1] == 'covered':221        covered += 1222    if agent_location['y'] != grid_size - 1 and agent_location['x'] != grid_size - 1 and mine_field[agent_location['y'] + 1][agent_location['x'] + 1] == 'covered':223        covered += 1224    if agent_location['y'] != grid_size - 1 and mine_field[agent_location['y'] + 1][agent_location['x']] == 'covered':225        covered += 1226    if agent_location['y'] != grid_size - 1 and agent_location['x'] != 0 and mine_field[agent_location['y'] + 1][agent_location['x'] - 1] == 'covered':227        covered += 1228    if agent_location['x'] != 0 and mine_field[agent_location['y']][agent_location['x'] - 1] == 'covered':229        covered += 1230    if agent_location['y'] != 0 and agent_location['x'] != 0 and mine_field[agent_location['y'] - 1][agent_location['x'] - 1] == 'covered':231        covered += 1232    return covered233234235def uncover_adjacent(x, y):236    if y != 0:237        uncover_cell(x, y-1)238    if y != 0 and x != grid_size - 1:239        uncover_cell(x+1, y-1)240    if x != grid_size - 1:241        uncover_cell(x+1, y)242    if y != grid_size - 1 and x != grid_size - 1:243        uncover_cell(x+1, y+1)244    if y != grid_size - 1:245        uncover_cell(x, y+1)246    if y != grid_size - 1 and x != 0:247        uncover_cell(x-1, y+1)248    if x != 0:249        uncover_cell(x-1, y)250    if y != 0 and x != 0:251        uncover_cell(x-1, y-1)252253254def place_mines(agent_locations):255    # randomly place mines throughout field not on cell x, y256    for y in range(len(mine_locations)):257        for x in range(len(mine_locations[y])):258259            placeable = True260            for agent_location in agent_locations:261                if (x == agent_location['x'] and y == agent_location['y']) or (262                        x == agent_location['x'] and y == agent_location['y'] - 1) or (263                        x == agent_location['x'] + 1 and y == agent_location['y'] - 1) or (264                        x == agent_location['x'] + 1 and y == agent_location['y']) or (265                        x == agent_location['x'] + 1 and y == agent_location['y'] + 1) or (266                        x == agent_location['x'] and y == agent_location['y'] + 1) or (267                        x == agent_location['x'] - 1 and y == agent_location['y'] + 1) or (268                        x == agent_location['x'] - 1 and y == agent_location['y']) or (269                        x == agent_location['x'] - 1 and y == agent_location['y'] - 1):270                    placeable = False271272            if placeable:273                if random.randint(1, 5) == 1:274                    mine_locations[y][x] = 1275276277def uncover_cell(x, y):278    if mine_field[y][x] == 'covered':279        # check for mine280        if mine_locations[y][x] == 1:281            set_game_over()282            mine_field[y][x] = 'triggered'283        else:284            # count number of adjacent mines285            mines = 0286            if y != 0 and mine_locations[y-1][x] == 1:287                mines += 1288            if y != 0 and x != grid_size - 1 and mine_locations[y-1][x+1] == 1:289                mines += 1290            if x != grid_size - 1 and mine_locations[y][x+1] == 1:291                mines += 1292            if y != grid_size - 1 and x != grid_size - 1 and mine_locations[y+1][x+1] == 1:293                mines += 1294            if y != grid_size - 1 and mine_locations[y+1][x] == 1:295                mines += 1296            if y != grid_size - 1 and x != 0 and mine_locations[y+1][x-1] == 1:297                mines += 1298            if x != 0 and mine_locations[y][x-1] == 1:299                mines += 1300            if y != 0 and x != 0 and mine_locations[y-1][x-1] == 1:301                mines += 1302303            if mines == 0:304                mine_field[y][x] = 'uncovered'305                # if cell has no adjacent mines, recursively uncover all adjacent cells306                '''if y != 0:307                    uncover_cell(x, y-1)308                if y != 0 and x != grid_size - 1:309                    uncover_cell(x+1, y-1)310                if x != grid_size - 1:311                    uncover_cell(x+1, y)312                if y != grid_size - 1 and x != grid_size - 1:313                    uncover_cell(x+1, y+1)314                if y != grid_size - 1:315                    uncover_cell(x, y+1)316                if y != grid_size - 1 and x != 0:317                    uncover_cell(x-1, y+1)318                if x != 0:319                    uncover_cell(x-1, y)320                if y != 0 and x != 0:321                    uncover_cell(x-1, y-1)'''322323            else:324                mine_field[y][x] = mines325326327def place_flag(x, y):328    if mine_field[y][x] == 'covered':329        mine_field[y][x] = 'flagged'330    elif mine_field[y][x] == 'flagged':331        mine_field[y][x] = 'covered'332333    # check to see if every mine has been flagged334    game_won = True335    for y in range(len(mine_locations)):336        for x in range(len(mine_locations[y])):337            if mine_locations[y][x] == 1 and mine_field[y][x] != 'flagged':338                game_won = False339    if game_won:340        set_game_over()341342343def set_game_over():344    global game_over345    game_over = True346    # show all missed mines, and bad flags347    for y in range(len(mine_locations)):348        for x in range(len(mine_locations[y])):349            if mine_locations[y][x] == 1 and mine_field[y][x] != 'flagged':350                mine_field[y][x] = 'mine'351352    for y in range(len(mine_field)):353        for x in range(len(mine_field[y])):354            if mine_field[y][x] == 'flagged' and mine_locations[y][x] != 1:355                mine_field[y][x] = 'incorrect'356357358def render(agent_locations):359360    for y in range(len(mine_field)):361        for x in range(len(mine_field[y])):362            if mine_field[y][x] == 'covered':363                display_surf.blit(covered_image, pygame.Rect(x * cell_size, y * cell_size, cell_size, cell_size))364            elif mine_field[y][x] == 'uncovered':365                display_surf.blit(uncovered_image, pygame.Rect(x * cell_size, y * cell_size, cell_size, cell_size))366            elif mine_field[y][x] == 'flagged':367                display_surf.blit(flagged_image, pygame.Rect(x * cell_size, y * cell_size, cell_size, cell_size))368            elif mine_field[y][x] == 1:369                display_surf.blit(one_image, pygame.Rect(x * cell_size, y * cell_size, cell_size, cell_size))370            elif mine_field[y][x] == 2:371                display_surf.blit(two_image, pygame.Rect(x * cell_size, y * cell_size, cell_size, cell_size))372            elif mine_field[y][x] == 3:373                display_surf.blit(three_image, pygame.Rect(x * cell_size, y * cell_size, cell_size, cell_size))374            elif mine_field[y][x] == 4:375                display_surf.blit(four_image, pygame.Rect(x * cell_size, y * cell_size, cell_size, cell_size))376            elif mine_field[y][x] == 5:377                display_surf.blit(five_image, pygame.Rect(x * cell_size, y * cell_size, cell_size, cell_size))378            elif mine_field[y][x] == 6:379                display_surf.blit(six_image, pygame.Rect(x * cell_size, y * cell_size, cell_size, cell_size))380            elif mine_field[y][x] == 7:381                display_surf.blit(seven_image, pygame.Rect(x * cell_size, y * cell_size, cell_size, cell_size))382            elif mine_field[y][x] == 8:383                display_surf.blit(eight_image, pygame.Rect(x * cell_size, y * cell_size, cell_size, cell_size))384            elif mine_field[y][x] == 'incorrect':385                display_surf.blit(incorrect_image, pygame.Rect(x * cell_size, y * cell_size, cell_size, cell_size))386            elif mine_field[y][x] == 'mine':387                display_surf.blit(mine_image, pygame.Rect(x * cell_size, y * cell_size, cell_size, cell_size))388            elif mine_field[y][x] == 'triggered':389                display_surf.blit(triggered_image, pygame.Rect(x * cell_size, y * cell_size, cell_size, cell_size))390391    # draw agent392    for i in range(len(agent_locations)):393        pygame.draw.rect(display_surf, (255, 0, 0), pygame.Rect(agent_locations[i]['x'] * cell_size, agent_locations[i]['y'] * cell_size, cell_size, cell_size), 2)394395    pygame.display.update()396397398def terminate():399    pygame.quit()400    sys.exit()401402403# initialize pygame404pygame.init()405fps_clock = pygame.time.Clock()406display_surf = pygame.display.set_mode((window_size, window_size))407basic_font = pygame.font.Font('freesansbold.ttf', 18)408pygame.display.set_caption('Mine Sweeper')409410# initialize agent in random cell411agents = 5412agent_locations = []413414for i in range(agents):415416    cell_taken = True417    x = random.randint(0, grid_size - 1)418    y = random.randint(0, grid_size - 1)419420    while cell_taken:421        cell_taken = False422        for agent_location in agent_locations:423            if x == agent_location['x'] and y == agent_location['y']:424                cell_taken = True425                x = random.randint(0, grid_size - 1)426                y = random.randint(0, grid_size - 1)427428        if not cell_taken:429            print(str(x) + ', ' + str(y))430            agent_locations.append({'x': x, 'y': y, 'right_hand': 'up'})431432433place_mines(agent_locations)434for agent_location in agent_locations:435    uncover_cell(agent_location['x'], agent_location['y'])436437
...test_combinationRow.py
Source:test_combinationRow.py  
...44        self.assertEqual(self.row.covered_more_than_ones, 0, "cover_cell create wrong covered_more_than_ones value")45        self.assertEqual(self.row.hash_table[(0, 0)], None, "cover_cell change disabled value")46    # Tests of uncover_cell function47    def test_uncover_cell_uncovered_value(self):48        self.assertEqual(self.row.uncover_cell((0, 0)), (0, 0), "uncover_cell return wrong values")49        self.assertEqual(self.row.uncovered, 12, "uncover_cell create wrong uncovered value")50        self.assertEqual(self.row.covered_more_than_ones, 0, "uncover_cell create wrong covered_more_than_ones value")51        self.assertEqual(self.row.hash_table[(0, 0)], 0, "uncover_cell change uncovered value")52    def test_uncover_cell_covered_value(self):53        self.row.hash_table[(0, 0)] = 154        self.row.uncovered = 1155        self.assertEqual(self.row.uncover_cell((0, 0)), (1, 0), "uncover_cell return wrong values")56        self.assertEqual(self.row.uncovered, 12, "uncover_cell create wrong uncovered value")57        self.assertEqual(self.row.covered_more_than_ones, 0, "uncover_cell create wrong covered_more_than_ones value")58        self.assertEqual(self.row.hash_table[(0, 0)], 0, "uncover_cell change uncovered value")59    def test_uncover_cell_covered_more_than_one_value(self):60        self.row.hash_table[(0, 0)] = 261        self.row.uncovered = 1162        self.row.covered_more_than_ones = 163        self.assertEqual(self.row.uncover_cell((0, 0)), (0, -1), "uncover_cell return wrong values")64        self.assertEqual(self.row.uncovered, 11, "uncover_cell create wrong uncovered value")65        self.assertEqual(self.row.covered_more_than_ones, 0, "uncover_cell create wrong covered_more_than_ones value")66        self.assertEqual(self.row.hash_table[(0, 0)], 1, "uncover_cell change uncovered value")67    def test_uncover_cell_disabled_value(self):68        self.row.hash_table[(0, 0)] = None69        self.assertEqual(self.row.uncover_cell((0, 0)), (0, 0), "uncover_cell return wrong values")70        self.assertEqual(self.row.uncovered, 12, "uncover_cell create wrong uncovered value")71        self.assertEqual(self.row.covered_more_than_ones, 0, "uncover_cell create wrong covered_more_than_ones value")72        self.assertEqual(self.row.hash_table[(0, 0)], None, "uncover_cell change disabled value")73    # Test of completely_uncover function74    def test_completely_uncover(self):75        self.row.hash_table[(0, 0)] = 176        self.row.hash_table[(0, 1)] = 277        self.row.hash_table[(0, 2)] = None78        self.row.completely_uncover()79        self.assertEqual(self.row.uncovered, 11, "completely_uncover create wrong uncovered value")80        self.assertEqual(self.row.covered_more_than_ones, 0,81                         "completely_uncover create wrong covered_more_than_ones value")82        self.assertEqual(self.row.hash_table[(0, 0)], 0, "completely_uncover don't uncover value")83        self.assertEqual(self.row.hash_table[(0, 1)], 0, "completely_uncover don't uncover value")...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!!
