Best Python code snippet using avocado_python
board.py
Source:board.py  
...42    """43    def play(self, row, column, action):44        cell = self.board[row][column]45        if action == "U":46            cell.uncover()47            if cell.getCover():48                return True49            elif cell.getMine():50                for i in range(len(self.minesLoc)):51                    pos = self.minesLoc[i].split(',')52                    rowAux = int(pos[0])53                    colAux = int(pos[1])54                    self.board[rowAux][colAux].uncover()55                return False 56            else:57                self.uncovered += 158                self.uncoverCell(row, column)59                return True60        elif action == "M":61            if cell.getCover():62                cell.mark()63                self.marks.append(str(row) + ',' + str(column))64            else:65                print("You cannot mark an uncovered cell.")    66            return True67    '''68    Method use to uncover the appropiate cells after each play.69    return nothing70    '''71    def uncoverCell(self, row, column):72        # cellU stands for cell Up73        # cellUR stands for cell Upper Right74        # cellR stands for cell Right75        # cellLR stands for cell Lower Right76        # cellD stands for cell Down77        # cellLL stands for cell Lower Left78        # cellL stands for cell Left79        # cellUL stands for cell Upper Left80        if self.board[row][column].getNext() == 0:81            if row == 0:82                cellD = self.board[row+1][column]83                if column == 0:84                    cellR = self.board[row][column+1]85                    cellLR = self.board[row+1][column+1]86                    if cellR.checkEmpty():87                        cellR.uncover()88                        self.uncovered += 189                        self.uncoverCell(row, column+1)90                    elif cellR.hasNeighbours():91                        cellR.uncover()92                        self.uncovered += 193                    if cellLR.checkEmpty():94                        cellLR.uncover()95                        self.uncovered += 196                        self.uncoverCell(row+1, column+1)97                    elif cellLR.hasNeighbours():98                        cellLR.uncover()99                        self.uncovered += 1100                    if cellD.checkEmpty():101                        cellD.uncover()102                        self.uncovered += 1103                        self.uncoverCell(row+1, column)104                    elif cellD.hasNeighbours():105                        cellD.uncover()106                        self.uncovered += 1107                elif column == self.width-1:108                    cellL = self.board[row][column-1]109                    cellLL = self.board[row+1][column-1]110                    if cellL.checkEmpty():111                        cellL.uncover()112                        self.uncovered += 1113                        self.uncoverCell(row, column-1)114                    elif cellL.hasNeighbours():115                        cellL.uncover()116                        self.uncovered += 1117                    if cellLL.checkEmpty():118                        cellLL.uncover()119                        self.uncovered += 1120                        self.uncoverCell(row+1, column-1)121                    elif cellLL.hasNeighbours():122                        cellLL.uncover()123                        self.uncovered += 1124                    if cellD.checkEmpty():125                        cellD.uncover()126                        self.uncovered += 1127                        self.uncoverCell(row+1, column)128                    elif cellD.hasNeighbours():129                        cellD.uncover()130                        self.uncovered += 1131                else:132                    cellR = self.board[row][column+1]133                    cellLR = self.board[row+1][column+1]134                    cellL = self.board[row][column-1]135                    cellLL = self.board[row+1][column-1]136                    if cellL.checkEmpty():137                        cellL.uncover()138                        self.uncovered += 1139                        self.uncoverCell(row, column-1)140                    elif cellL.hasNeighbours():141                        cellL.uncover()142                        self.uncovered += 1143                    if cellLL.checkEmpty():144                        cellLL.uncover()145                        self.uncovered += 1146                        self.uncoverCell(row+1, column-1)147                    elif cellLL.hasNeighbours():148                        cellLL.uncover()149                        self.uncovered += 1150                    if cellD.checkEmpty():151                        cellD.uncover()152                        self.uncovered += 1153                        self.uncoverCell(row+1, column)154                    elif cellD.hasNeighbours():155                        cellD.uncover()156                        self.uncovered += 1157                    if cellLR.checkEmpty():158                        cellLR.uncover()159                        self.uncovered += 1160                        self.uncoverCell(row+1, column+1)161                    elif cellLR.hasNeighbours():162                        cellLR.uncover()163                        self.uncovered += 1164                    if cellR.checkEmpty():165                        cellR.uncover()166                        self.uncovered += 1167                        self.uncoverCell(row, column+1)168                    if cellR.hasNeighbours():169                        cellR.uncover()170                        self.uncovered += 1171            elif row == self.height-1:172                cellU = self.board[row-1][column]173                if column == 0:174                    cellR = self.board[row][column+1]175                    cellUR = self.board[row-1][column+1]176                    if cellR.checkEmpty():177                        cellR.uncover()178                        self.uncovered += 1179                        self.uncoverCell(row, column+1)180                    elif cellR.hasNeighbours():181                        cellR.uncover()182                        self.uncovered += 1183                    if cellUR.checkEmpty():184                        cellUR.uncover()185                        self.uncovered += 1186                        self.uncoverCell(row-1, column+1)187                    elif cellUR.hasNeighbours():188                        cellUR.uncover()189                        self.uncovered += 1190                    if cellU.checkEmpty():191                        cellU.uncover()192                        self.uncovered += 1193                        self.uncoverCell(row-1, column)194                    elif cellU.hasNeighbours():195                        cellU.uncover()196                        self.uncovered += 1197                elif column == self.width-1:198                    cellL = self.board[row][column-1]199                    cellUL = self.board[row-1][column-1]200                    if cellL.checkEmpty():201                        cellL.uncover()202                        self.uncovered += 1203                        self.uncoverCell(row, column-1)204                    elif cellL.hasNeighbours():205                        cellL.uncover()206                        self.uncovered += 1207                    if cellUL.checkEmpty():208                        cellUL.uncover()209                        self.uncovered += 1210                        self.uncoverCell(row-1, column-1)211                    elif cellUL.hasNeighbours():212                        cellUL.uncover()213                        self.uncovered += 1214                    if cellU.checkEmpty():215                        cellU.uncover()216                        self.uncovered += 1217                        self.uncoverCell(row+1, column)218                    elif cellU.hasNeighbours():219                        cellU.uncover()220                        self.uncovered += 1221                else:222                    cellR = self.board[row][column+1]223                    cellUR = self.board[row-1][column+1]224                    cellL = self.board[row][column-1]225                    cellUL = self.board[row-1][column-1]226                    if cellL.checkEmpty():227                        cellL.uncover()228                        self.uncovered += 1229                        self.uncoverCell(row, column-1)230                    elif cellL.hasNeighbours():231                        cellL.uncover()232                        self.uncovered += 1233                    if cellUL.checkEmpty():234                        cellUL.uncover()235                        self.uncovered += 1236                        self.uncoverCell(row-1, column-1)237                    elif cellUL.hasNeighbours():238                        cellUL.uncover()239                        self.uncovered += 1240                    if cellU.checkEmpty():241                        cellU.uncover()242                        self.uncovered += 1243                        self.uncoverCell(row-1, column)244                    elif cellU.hasNeighbours():245                        cellU.uncover()246                        self.uncovered += 1247                    if cellUR.checkEmpty():248                        cellUR.uncover()249                        self.uncovered += 1250                        self.uncoverCell(row-1, column+1)251                    elif cellUR.hasNeighbours():252                        cellUR.uncover()253                        self.uncovered += 1254                    if cellR.checkEmpty():255                        cellR.uncover()256                        self.uncovered += 1257                        self.uncoverCell(row, column+1)258                    if cellR.hasNeighbours():259                        cellR.uncover()260                        self.uncovered += 1261            else:262                cellU = self.board[row-1][column]263                cellD = self.board[row+1][column]264                if column == 0:265                    cellR = self.board[row][column+1]266                    cellUR = self.board[row-1][column+1]267                    cellLR = self.board[row+1][column+1]268                    if cellR.checkEmpty():269                        cellR.uncover()270                        self.uncovered += 1271                        self.uncoverCell(row, column+1)272                    elif cellR.hasNeighbours():273                        cellR.uncover()274                        self.uncovered += 1275                    if cellUR.checkEmpty():276                        cellUR.uncover()277                        self.uncovered += 1278                        self.uncoverCell(row-1, column+1)279                    elif cellUR.hasNeighbours():280                        cellUR.uncover()281                        self.uncovered += 1282                    if cellU.checkEmpty():283                        cellU.uncover()284                        self.uncovered += 1285                        self.uncoverCell(row+1, column)286                    elif cellU.hasNeighbours():287                        cellU.uncover()288                        self.uncovered += 1289                    if cellLR.checkEmpty():290                        cellLR.uncover()291                        self.uncovered += 1292                        self.uncoverCell(row+1, column+1)293                    elif cellLR.hasNeighbours():294                        cellLR.uncover()295                        self.uncovered += 1296                    if cellD.checkEmpty():297                        cellD.uncover()298                        self.uncovered += 1299                        self.uncoverCell(row+1, column)300                    elif cellD.hasNeighbours():301                        cellD.uncover()302                        self.uncovered += 1303                elif column == self.width-1:304                    cellL = self.board[row][column-1]305                    cellUL = self.board[row-1][column-1]306                    cellLL = self.board[row+1][column-1]307                    if cellL.checkEmpty():308                        cellL.uncover()309                        self.uncovered += 1310                        self.uncoverCell(row, column-1)311                    elif cellL.hasNeighbours():312                        cellL.uncover()313                        self.uncovered += 1314                    if cellUL.checkEmpty():315                        cellUL.uncover()316                        self.uncovered += 1317                        self.uncoverCell(row+1, column-1)318                    elif cellUL.hasNeighbours():319                        cellUL.uncover()320                        self.uncovered += 1321                    if cellU.checkEmpty():322                        cellU.uncover()323                        self.uncovered += 1324                        self.uncoverCell(row+1, column)325                    elif cellU.hasNeighbours():326                        cellU.uncover()327                        self.uncovered += 1328                    if cellD.checkEmpty():329                        cellD.uncover()330                        self.uncovered += 1331                        self.uncoverCell(row+1, column)332                    elif cellD.hasNeighbours():333                        cellD.uncover()334                        self.uncovered += 1335                    if cellLL.checkEmpty():336                        cellLL.uncover()337                        self.uncovered += 1338                        self.uncoverCell(row+1, column-1)339                    elif cellLL.hasNeighbours():340                        cellLL.uncover()341                        self.uncovered += 1342                else:343                    cellR = self.board[row][column+1]344                    cellUR = self.board[row-1][column+1]345                    cellL = self.board[row][column-1]346                    cellUL = self.board[row-1][column-1]347                    cellLR = self.board[row+1][column+1]348                    cellLL = self.board[row+1][column-1]349                    if cellU.checkEmpty():350                        cellU.uncover()351                        self.uncovered += 1352                        self.uncoverCell(row-1, column)353                    elif cellU.hasNeighbours():354                        cellU.uncover()355                        self.uncovered += 1356                    if cellUR.checkEmpty():357                        cellUR.uncover()358                        self.uncovered += 1359                        self.uncoverCell(row-1, column+1)360                    elif cellUR.hasNeighbours():361                        cellUR.uncover()362                        self.uncovered += 1363                    if cellR.checkEmpty():364                        cellR.uncover()365                        self.uncovered += 1366                        self.uncoverCell(row, column+1)367                    elif cellR.hasNeighbours():368                        cellR.uncover()369                        self.uncovered += 1370                    if cellLR.checkEmpty():371                        cellLR.uncover()372                        self.uncovered += 1373                        self.uncoverCell(row+1, column+1)374                    elif cellLR.hasNeighbours():375                        cellLR.uncover()376                        self.uncovered += 1377                    if cellD.checkEmpty():378                        cellD.uncover()379                        self.uncovered += 1380                        self.uncoverCell(row+1, column)381                    elif cellD.hasNeighbours():382                        cellD.uncover()383                        self.uncovered += 1384                    if cellLL.checkEmpty():385                        cellLL.uncover()386                        self.uncovered += 1387                        self.uncoverCell(row+1, column-1)388                    elif cellLL.hasNeighbours():389                        cellLL.uncover()390                        self.uncovered += 1391                    if cellL.checkEmpty():392                        cellL.uncover()393                        self.uncovered += 1394                        self.uncoverCell(row, column-1)395                    elif cellL.hasNeighbours():396                        cellL.uncover()397                        self.uncovered += 1398                    if cellUL.checkEmpty():399                        cellUL.uncover()400                        self.uncovered += 1401                        self.uncoverCell(row-1, column-1)402                    elif cellUL.hasNeighbours():403                        cellUL.uncover()404                        self.uncovered += 1405    406    '''407    Method used to assing number of mines next to a cell408    return nothing409    '''410    def seekMines(self):411        height = self.height412        width = self.width413        for r in range(height):414            for c in range(width):415                if not self.board[r][c].getMine():416                    if r == 0:417                        if c == 0:...main.py
Source:main.py  
1from skeleton import size2from mines import mine_area, area, mines3for mine_pos in mine_area:4    try:5        if mine_pos % size != 0 and (mine_pos + 1) % size != 0: 6            if type(area[mine_pos - size - 1]) == int and mine_pos - size - 1 >= 0:7                area[mine_pos - size - 1] += 18            if type(area[mine_pos - size]) == int and mine_pos - size >= 0:9                area[mine_pos - size] += 110            if type(area[mine_pos - size + 1]) == int and mine_pos - size + 1 >= 0:11                area[mine_pos - size + 1] += 112            if type(area[mine_pos - 1]) == int:13                area[mine_pos - 1] += 114            if type(area[mine_pos + 1]) == int:15                area[mine_pos + 1] += 116            if type(area[mine_pos + size - 1]) == int:17                area[mine_pos + size - 1] += 118            if type(area[mine_pos + size]) == int:19                area[mine_pos + size] += 120            if type(area[mine_pos + size + 1]) == int:21                area[mine_pos + size + 1] += 122        if mine_pos % size == 0:23            if type(area[mine_pos - size]) == int and mine_pos - size >= 0:24                area[mine_pos - size] += 125            if type(area[mine_pos - size + 1]) == int and mine_pos - size + 1 >= 0:26                area[mine_pos - size + 1] += 127            if type(area[mine_pos + 1]) == int:28                area[mine_pos + 1] += 129            30            if type(area[mine_pos + size + 1]) == int:31                area[mine_pos + size + 1] += 132            if type(area[mine_pos + size]) == int:33                area[mine_pos + size] += 134        if (mine_pos + 1) % size == 0:35            if type(area[mine_pos - size - 1]) == int and mine_pos - size - 1 > 0:36                area[mine_pos - size - 1] += 137            if type(area[mine_pos - size]) == int and mine_pos - size > 0:38                area[mine_pos - size] += 139            if type(area[mine_pos - 1]) == int:40                area[mine_pos - 1] += 141            if type(area[mine_pos + size - 1]) == int:42                area[mine_pos + size - 1] += 143            44            if type(area[mine_pos + size]) == int:45                area[mine_pos + size] += 146    except:47        pass48board, uncovered_board = [], []49for i in range(size):50    board.append(area[(i*size):(i*size + size)])51    uncovered_board.append(['*' for x in range(size)])52remaining_cells = -mines53for j in range(size):54    for k in range(size):55        print(uncovered_board[j][k], end = '    ')56        if uncovered_board[j][k] == '*':57            remaining_cells += 158    print('\n')59print(board)60print('Remaining cells to uncover:', remaining_cells, '\n')61uncovered_cell = None62while uncovered_cell != '#' or remaining_cells == 0:63    action = input('Select action (m - mark/unmark mine, u - uncover cell): ')64    if action == 'u':65        uncover_x = input(f'Select column to uncover (1-{size}): ')66        uncover_y = input(f'Select row to uncover (1-{size}): ')67        try:68            uncover_x = int(uncover_x) - 169            uncover_y = int(uncover_y) - 170            if uncover_x >= 0 and uncover_x < size and uncover_y >= 0 and uncover_y < size:71                uncovered_board[uncover_y][uncover_x] = board[uncover_y][uncover_x]72                73                uncovered_cell = board[uncover_y][uncover_x]74                75                if uncovered_cell == '#':76                    print('\n' + 'Game over' + '\n')77                    for j in range(size):78                        for k in range(size):79                            print(board[j][k], end = '    ')80                        print('\n')81                    82                    break83                else:84                    if board[uncover_y][uncover_x] == 0:85                        if uncover_x == 0 and uncover_y == 0:86                            uncovered_board[uncover_y][uncover_x + 1] = board[uncover_y][uncover_x + 1]87                            uncovered_board[uncover_y + 1][uncover_x + 1] = board[uncover_y + 1][uncover_x + 1]88                            uncovered_board[uncover_y + 1][uncover_x] = board[uncover_y + 1][uncover_x]89                        90                        elif uncover_x == (size - 1) and uncover_y == 0:91                            uncovered_board[uncover_y + 1][uncover_x] = board[uncover_y + 1][uncover_x]92                            uncovered_board[uncover_y + 1][uncover_x - 1] = board[uncover_y + 1][uncover_x - 1]93                            uncovered_board[uncover_y][uncover_x - 1] = board[uncover_y][uncover_x - 1]94                        elif uncover_x == 0 and uncover_y == (size - 1):95                            uncovered_board[uncover_y - 1][uncover_x] = board[uncover_y - 1][uncover_x]96                            uncovered_board[uncover_y - 1][uncover_x + 1] = board[uncover_y - 1][uncover_x + 1]97                            uncovered_board[uncover_y][uncover_x + 1] = board[uncover_y][uncover_x + 1]98                        elif uncover_x == (size - 1) and uncover_y == (size - 1):99                            uncovered_board[uncover_y][uncover_x - 1] = board[uncover_y][uncover_x - 1]100                            uncovered_board[uncover_y - 1][uncover_x - 1] = board[uncover_y - 1][uncover_x - 1]101                            uncovered_board[uncover_y - 1][uncover_x] = board[uncover_y - 1][uncover_x]102                    103                        elif uncover_x == 0:104                            uncovered_board[uncover_y - 1][uncover_x] = board[uncover_y - 1][uncover_x]105                            uncovered_board[uncover_y - 1][uncover_x + 1] = board[uncover_y - 1][uncover_x + 1]106                            uncovered_board[uncover_y][uncover_x + 1] = board[uncover_y][uncover_x + 1]107                            uncovered_board[uncover_y + 1][uncover_x + 1] = board[uncover_y + 1][uncover_x + 1]108                            uncovered_board[uncover_y + 1][uncover_x] = board[uncover_y + 1][uncover_x]109                        110                        111                        elif uncover_x == (size - 1):112                            uncovered_board[uncover_y + 1][uncover_x] = board[uncover_y + 1][uncover_x]113                            uncovered_board[uncover_y + 1][uncover_x - 1] = board[uncover_y + 1][uncover_x - 1]114                            uncovered_board[uncover_y][uncover_x - 1] = board[uncover_y][uncover_x - 1]115                            uncovered_board[uncover_y - 1][uncover_x - 1] = board[uncover_y - 1][uncover_x - 1]116                            uncovered_board[uncover_y - 1][uncover_x] = board[uncover_y - 1][uncover_x]117                        elif uncover_y == 0:118                            uncovered_board[uncover_y][uncover_x + 1] = board[uncover_y][uncover_x + 1]119                            uncovered_board[uncover_y + 1][uncover_x + 1] = board[uncover_y + 1][uncover_x + 1]120                            uncovered_board[uncover_y + 1][uncover_x] = board[uncover_y + 1][uncover_x]121                            uncovered_board[uncover_y + 1][uncover_x - 1] = board[uncover_y + 1][uncover_x - 1]122                            uncovered_board[uncover_y][uncover_x - 1] = board[uncover_y][uncover_x - 1]123                        elif uncover_y == (size - 1):124                            uncovered_board[uncover_y - 1][uncover_x] = board[uncover_y - 1][uncover_x]125                            uncovered_board[uncover_y - 1][uncover_x + 1] = board[uncover_y - 1][uncover_x + 1]126                            uncovered_board[uncover_y][uncover_x + 1] = board[uncover_y][uncover_x + 1]127                            uncovered_board[uncover_y][uncover_x - 1] = board[uncover_y][uncover_x - 1]128                            uncovered_board[uncover_y - 1][uncover_x - 1] = board[uncover_y - 1][uncover_x - 1]129                        130                        else:131                            uncovered_board[uncover_y - 1][uncover_x] = board[uncover_y - 1][uncover_x]132                            uncovered_board[uncover_y - 1][uncover_x + 1] = board[uncover_y - 1][uncover_x + 1]133                            uncovered_board[uncover_y][uncover_x + 1] = board[uncover_y][uncover_x + 1]134                            uncovered_board[uncover_y + 1][uncover_x + 1] = board[uncover_y + 1][uncover_x + 1]135                            uncovered_board[uncover_y + 1][uncover_x] = board[uncover_y + 1][uncover_x]136                            uncovered_board[uncover_y + 1][uncover_x - 1] = board[uncover_y + 1][uncover_x - 1]137                            uncovered_board[uncover_y][uncover_x - 1] = board[uncover_y][uncover_x - 1]138                            uncovered_board[uncover_y - 1][uncover_x - 1] = board[uncover_y - 1][uncover_x - 1]139                    remaining_cells = 0140                    141                    for j in range(size):142                        for k in range(size):143                            print(uncovered_board[j][k], end = '    ')144                            if uncovered_board[j][k] == '*' or uncovered_board[j][k] == 'M':145                                remaining_cells += 1146                        print('\n')147                    remaining_cells -= mines148                    if remaining_cells ==0:149                        break150                    else:151                        print('Remaining cells to uncover:', remaining_cells, '\n')152            153                action = None154            else:155                print('Invalid placement')156        except:157            print('Invalid input')158        159    if action == 'm':160        mark_x = input(f'Select column to mark/unmark (1-{size}): ')161        mark_y = input(f'Select row to mark/unmark (1-{size}): ')162        try:163            mark_x = int(mark_x) - 1164            mark_y = int(mark_y) - 1165            if mark_x >= 0 and mark_x < size and mark_y >= 0 and mark_y < size:166                if uncovered_board[mark_y][mark_x] != 'M':167                    uncovered_board[mark_y][mark_x] = 'M'168                else:169                    uncovered_board[mark_y][mark_x] = '*'170            for j in range(size):171                    for k in range(size):172                        print(uncovered_board[j][k], end = '    ')173                    print('\n')174            action = None            175        except:176            print('Invalid input')177if remaining_cells == 0:...internal.py
Source:internal.py  
...67	global flagged_table68	assert not done69	assert can_flag(x, y)70	flagged_table ^= 1 << ((y+1)*(grid_width+1)+x+1)71def can_uncover(x,y):72	return (73		not done and74		(0 <= x < grid_width) and (0 <= y < grid_height) and75		(covered_table >> ((y+1)*(grid_width+1)+x+1) & 1) and 76		not (flagged_table >> ((y+1)*(grid_width+1)+x+1) & 1)77	)78def uncover(x, y):79	global covered_table,flagged_table, done, won80	assert not done81	assert can_uncover(x,y)82	#check if mine83	if ismine_table >> ((y+1)*(grid_width+1)+x+1) & 1:84		covered_table ^= ismine_table85		flagged_table &= ~ismine_table86		won = False87		done = True88		return False89		90	covered_table ^= 1 << ((y+1)*(grid_width+1)+x+1)91	#check if won92	if covered_table == ismine_table or flagged_table == ismine_table:93		won = True94		done = True95		return True96	#uncover safe fields algorithm97	if (neighbors_map >> ((y*grid_width+x)*4) & 15) == 0:98		if can_uncover(x, y-1): uncover(x,y-1)		# top99		if can_uncover(x, y+1): uncover(x,y+1)		# bottom100		if can_uncover(x-1, y): uncover(x-1,y)		# right101		if can_uncover(x+1, y): uncover(x+1,y)		# left102		if can_uncover(x-1, y-1): uncover(x-1,y-1)	# top right103		if can_uncover(x+1, y-1): uncover(x+1,y-1)	# top left104		if can_uncover(x-1, y+1): uncover(x-1,y+1)	# bottom right...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!!
