Is there a way I can fix the way ji get neighbors of a 2d coordinate?

"""6.009 Lab 5 -- Mines"""

def dump(game):
"""Print a human-readable representation of game.

Arguments:
   game (dict): Game state

>>> dump({'dimensions': [1, 2], 'mask': [[False, False]], 'board': [['.', 1]]})
dimensions: [1, 2]
board: ['.', 1]
mask:  [False, False]
"""
lines = ["dimensions: {}".format(game["dimensions"]),
         "board: {}".format("\n       ".join(map(str, game["board"]))),
         "mask:  {}".format("\n       ".join(map(str, game["mask"])))]
print("\n".join(lines))

def get_2d_neighbors(coord):
'''Get neighbors of a 2D coordinate

Take a 2d coordinate and return a list of all 8 possible neighbors as if the coordinate 
does not fall on any edges of the board

>>> get_2d_neighbors((2,2))
[(1,1), (1,2), (1,3), (2,1), (2,3), (3,1), (3,2), (3,3)]
'''
neighbors = [] 
x = coord[0]
y = coord[1]

neighbor_list = [(x-1,y-1) , (x-1,y) , (x-1,y+1), 
        (x,y-1), (x, y+1),
        (x+1,y-1) , (x+1,y) , (x+1,y+1)]

for neighbor in neighbor_list:
    neighbors.append(neighbor)

return neighbors

def new_game(num_rows, num_cols, bombs):
"""Start a new game.

Return a game state dictionary, with the "board" and "mask" fields
adequately initialized.

Args:
   num_rows (int): Number of rows
   num_cols (int): Number of columns
   bombs (list): List of bombs, given in (row, column) pairs

Returns:
   A game state dictionary

>>> dump(new_game(2, 4, [(0, 0), (1, 0), (1, 1)]))
dimensions: [2, 4]
board: ['.', 3, 1, 0]
       ['.', '.', 1, 0]
mask:  [False, False, False, False]
       [False, False, False, False]
"""
game = {}
game['dimensions'] = [num_rows, num_cols]
game['mask'] = [[False]*num_cols]*num_rows
#game['board'] = [[0]*num_cols]*num_rows |||||   board_dict[i,j] = game['board'][i][j]
bomb_num = len(bombs)
if bomb_num == 0:
    return game

r = range(bomb_num)

board_dict = {}

for i in range(num_rows):
    for j in range(num_cols):
        board_dict[i,j] = 0

for (x,y) in bombs: 

    board_dict[x,y] = '.'

    possible_neighbors = get_2d_neighbors((x,y))

    for neighbor in possible_neighbors:
        try:
            board_dict[neighbor] += 1
        except TypeError:
            continue

        except KeyError:
            continue

sorted_1d_board = [value for (key,value) in sorted(board_dict.items())]

board_2d_list = []
while len(sorted_1d_board)>0:
    board_2d_list.append(sorted_1d_board[:num_cols])
    del sorted_1d_board[:num_cols]

game['board'] = board_2d_list 

return game

def dig(game, row, col):
"""Recursively dig up (row, col) and neighboring squares.

Update game["mask"] to reveal (row, col); then recursively reveal (dig up)
its neighbors, as long as (row, col) does not contain and is not adjacent to
a bomb.  Return a pair: the first element indicates whether the game is over
using a string equal to "victory", "defeat", or "ongoing", and the second
one is a number indicates how many squares were revealed.

The first element is "defeat" when at least one bomb is visible on the board
after digging (i.e. game["mask"][bomb_location] == True), "victory" when all
safe squares (squares that do not contain a bomb) and no bombs are visible,
and "ongoing" otherwise.

Args:
   game (dict): Game state
   row (int): Where to start digging (row)
   col (int): Where to start digging (col)

Returns:
   Tuple[str,int]: A pair of game status and number of squares revealed

>>> game = {"dimensions": [2, 4],
...         "board": [[".", 3, 1, 0],
...                   [".", ".", 1, 0]],
...         "mask": [[False, True, False, False],
...                  [False, False, False, False]]}
>>> dig(game, 0, 3)
('victory', 4)
>>> dump(game)
dimensions: [2, 4]
board: ['.', 3, 1, 0]
       ['.', '.', 1, 0]
mask:  [False, True, True, True]
       [False, False, True, True]

>>> game = {"dimensions": [2, 4],
...         "board": [[".", 3, 1, 0],
...                   [".", ".", 1, 0]],
...         "mask": [[False, True, False, False],
...                  [False, False, False, False]]}
>>> dig(game, 0, 0)
('defeat', 1)
>>> dump(game)
dimensions: [2, 4]
board: ['.', 3, 1, 0]
       ['.', '.', 1, 0]
mask:  [True, True, False, False]
       [False, False, False, False]
"""

game['mask'][row][col] = True
square = game['board'][row][col]

if square == '.' : 
    return ('defeat', 1)

possible_neighbors = get_2d_neighbors((row,col))
row_range = range(game['dimensions'][0])
col_range = range(game['dimensions'][1])

actual_neighbors = [] 
for (r,c) in possible_neighbors:
    if r in row_range and c in col_range:
        actual_neighbors.append((r,c))

count = 1 

if square == 0:
    for (r,c) in actual_neighbors:
        game['mask'][r][c] = True
        count += 1

return (status, count)

def render(game, xray=False):
"""Prepare a game for display.

Returns a two-dimensional array (list of lists) of "_" (hidden squares), "."
(bombs), " " (empty squares), or "1", "2", etc. (squares neighboring bombs).
game["mask"] indicates which squares should be visible.  If xray is True (the
default is False), game["mask"] is ignored and all cells are shown.

Args:
   game (dict): Game state
   xray (bool): Whether to reveal all tiles or just the ones allowed by
                game["mask"]

Returns:
   A 2D array (list of lists)

>>> render({"dimensions": [2, 4],
...         "board": [[".", 3, 1, 0],
...                   [".", ".", 1, 0]],
...         "mask":  [[False, True, True, False],
...                   [False, False, True, False]]}, False)
[['_', '3', '1', '_'],
 ['_', '_', '1', '_']]

>>> render({"dimensions": [2, 4],
...         "board": [[".", 3, 1, 0],
...                   [".", ".", 1, 0]],
...         "mask":  [[False, True, False, True],
...                   [False, False, False, True]]}, True)
[['.', '3', '1', ' '],
 ['.', '.', '1', ' ']]
"""
raise NotImplementedError

def render_ascii(game, xray=False):
"""Render a game as ASCII art.

Returns a string-based representation of argument "game".  Each tile of the
game board should be rendered as in the function "render(game)".

Args:
   game (dict): Game state
   xray (bool): Whether to reveal all tiles or just the ones allowed by
                game["mask"]

Returns:
   A string-based representation of game

>>> print(render_ascii({"dimensions": [2, 4],
...                     "board": [[".", 3, 1, 0],
...                               [".", ".", 1, 0]],
...                     "mask":  [[True, True, True, False],
...                               [False, False, True, False]]}))
.31_
__1_
"""
raise NotImplementedError

def nd_new_game(dims, bombs):
"""Start a new game.

Return a game state dictionary, with the "board" and "mask" fields
adequately initialized.  This is an N-dimensional version of new_game().

Args:
   dims (list): Dimensions of the board
   bombs (list): bomb locations as a list of tuples, each an N-dimensional coordinate

Returns:
   A game state dictionary

>>> dump(nd_new_game([2, 4, 2], [(0, 0, 1), (1, 0, 0), (1, 1, 1)]))
dimensions: [2, 4, 2]
board: [[3, '.'], [3, 3], [1, 1], [0, 0]]
       [['.', 3], [3, '.'], [1, 1], [0, 0]]
mask:  [[False, False], [False, False], [False, False], [False, False]]
       [[False, False], [False, False], [False, False], [False, False]]
"""
raise NotImplementedError

def nd_dig(game, coords):
"""Recursively dig up square at coords and neighboring squares.

Update game["mask"] to reveal square at coords; then recursively reveal its
neighbors, as long as coords does not contain and is not adjacent to a
bomb.  Return a pair: the first element indicates whether the game is over
using a string equal to "victory", "defeat", or "ongoing", and the second
one is a number indicates how many squares were revealed.

The first element is "defeat" when at least one bomb is visible on the board
after digging (i.e. game["mask"][bomb_location] == True), "victory" when all
safe squares (squares that do not contain a bomb) and no bombs are visible,
and "ongoing" otherwise.

This is an N-dimensional version of dig().

Args:
   game (dict): Game state
   coords (tuple): Where to start digging

Returns:
   A pair of game status and number of squares revealed

>>> game = {"dimensions": [2, 4, 2],
...         "board": [[[3, '.'], [3, 3], [1, 1], [0, 0]],
...                   [['.', 3], [3, '.'], [1, 1], [0, 0]]],
...         "mask": [[[False, False], [False, True], [False, False], [False, False]],
...                  [[False, False], [False, False], [False, False], [False, False]]]}
>>> nd_dig(game, (0, 3, 0))
('ongoing', 8)
>>> dump(game)
dimensions: [2, 4, 2]
board: [[3, '.'], [3, 3], [1, 1], [0, 0]]
       [['.', 3], [3, '.'], [1, 1], [0, 0]]
mask:  [[False, False], [False, True], [True, True], [True, True]]
       [[False, False], [False, False], [True, True], [True, True]]

>>> game = {"dimensions": [2, 4, 2],
...         "board": [[[3, '.'], [3, 3], [1, 1], [0, 0]],
...                   [['.', 3], [3, '.'], [1, 1], [0, 0]]],
...         "mask": [[[False, False], [False, True], [False, False], [False, False]],
...                  [[False, False], [False, False], [False, False], [False, False]]]}
>>> nd_dig(game, (0, 0, 1))
('defeat', 1)
>>> dump(game)
dimensions: [2, 4, 2]
board: [[3, '.'], [3, 3], [1, 1], [0, 0]]
       [['.', 3], [3, '.'], [1, 1], [0, 0]]
mask:  [[False, True], [False, True], [False, False], [False, False]]
       [[False, False], [False, False], [False, False], [False, False]]
"""
raise NotImplementedError

def nd_render(game, xray=False):
"""Prepare a game for display.

Returns an N-dimensional array (nested lists) of "_" (hidden squares), "."
(bombs), " " (empty squares), or "1", "2", etc. (squares neighboring bombs).
game["mask"] indicates which squares should be visible.  If xray is True (the
default is False), game["mask"] is ignored and all cells are shown.

This is an N-dimensional version of render().

Args:
   game (dict): Game state
   xray (bool): Whether to reveal all tiles or just the ones allowed by
                game["mask"]

Returns:
   An n-dimensional array (nested lists)

>>> nd_render({"dimensions": [2, 4, 2],
...            "board": [[[3, '.'], [3, 3], [1, 1], [0, 0]],
...                      [['.', 3], [3, '.'], [1, 1], [0, 0]]],
...            "mask": [[[False, False], [False, True], [True, True], [True, True]],
...                     [[False, False], [False, False], [True, True], [True, True]]]},
...           False)
[[['_', '_'], ['_', '3'], ['1', '1'], [' ', ' ']],
 [['_', '_'], ['_', '_'], ['1', '1'], [' ', ' ']]]

>>> nd_render({"dimensions": [2, 4, 2],
...            "board": [[[3, '.'], [3, 3], [1, 1], [0, 0]],
...                      [['.', 3], [3, '.'], [1, 1], [0, 0]]],
...            "mask": [[[False, False], [False, True], [False, False], [False, False]],
...                     [[False, False], [False, False], [False, False], [False, False]]]},
...           True)
[[['3', '.'], ['3', '3'], ['1', '1'], [' ', ' ']],
 [['.', '3'], ['3', '.'], ['1', '1'], [' ', ' ']]]
"""
raise NotImplementedError

The actual homework goes as follow:

Introduction

International Mines tournaments have declined lately, but there is word that a new one is in the

works — rumor has it that it could be even bigger than the legendary Budapest 2005 one, in no

small part because inhabitants of planet Htrae might be attending.

What better way to get prepared for the tournament than to write your own implementation?

There's just a small twist. Planet Htrae is not very different from Earth, except that space in the

Yklim way, Htrae's star cluster, doesn't have three dimensions — at least, not always: it

fluctuates between 2 and, on the worst days, 60. In fact, it's not uncommon for an Htraean to

wake up flat, for example, and finish the day in 7 dimensions — only to find themselves living in

to three or four dimensions on the next morning. It takes a bit of time to get used to it, of course.

In any case, Htraeans are pretty particular about playingMines. Kids on Htrae always play on

regular, 2D boards, but champions like to play on higher-dimensional boards, usually on as

many dimensions as the surrounding space. Your code will have to support this, of course.

Here's the weather advisory for the week of the tournament:

...VERY DIMENSIONAL IN SOUTHWEST OHADI ON YADRIF...

.AN EXITING LOW PRESSURE SYSTEM WILL INCREASE NORTHWEST DIMENSIONAL

FLUX IN AND SOUTH OF THE EKANS RIVER BASIN ON YADRIF. ESIOB IS NOW

INCLUDED IN THE ADVISORY BUT THE STRONGEST FLUX WILL BE SOUTH AND

EAST OF MOUNTAIN EMOH TOWARD THE CIMAG VALLEY.

ZDI014>030-231330-016-

UPPER ERUSAERT VALLEY-SOUTHWEST HYPERLANDS-WESTERN CIMAG VALLEY-

1022 MP TDM UHT PES 22 6102

...DIMENSION ADVISORY REMAINS IN EFFECT FROM 10 MA TO 9 MP ON

YADIRF...

  • DIMENSIONAL FLUX...30 TO 35 DIMENSIONS WITH GUSTS TO 45.

  • IMPACTS...CROSSFLUXES WILL MAKE FOR DIFFICULT TRAVELLING

CONDITIONS ON LOW-DIMENSIONAL ROADS.

This lab trains the following skills:

Manipulating 2- and N-dimensional arrays implemented with nested lists

Recursively exploring simple graphs and nested data structures

Writing doctests and documenting functions

Since most of the difficulty of this lab lies in implementing recursive functions, please do not use

standard library modules that use recursion behind the scenes, such as itertools .

Part 2 of this lab is tricky; please plan accordingly.

okay thank you for your reply. I apologize. I dindt nkow how to post a quesiton. this is the first time.

Can you please help me with the first 40 lines please. and let me know what i am doing wrong there.

I think you need to pare this down more. I started reading at line 1 and by line 20 I finally reached where you might be asking a question.

Since you know your use of the arrays and what it means, I think you need a python debugger tool. Or need to learn how to print values so you can see what your code and variables are at some line of code.
https://www.google.com/search?q=python+debugging shows there is a debugger so if I was working this, I would be using that to check over the varialbes as it runs.

The article starter is a financial contributor. Sponsored articles offer a bounty for quality replies.