-3

I am working on a game project and want to create a board of game by using the NumPy array. So I defined a function board() for that. But when I try to change an element of the board, it doesn't change.

CODE

import numpy as np

def board():
    game = np.zeros((6, 6))
    return game

board()[1, 1] = 6  

print(board()[1, 1])  # Expected Output = 6, Instead it gives 0

I guess there is some problem with local and global variables. I searched for it on this site but didn't find an expected solution. As I know how to change a global variable called inside a function but I don't know how to change a local variable. Any help will be appreciated.

Rishabh Semwal
  • 328
  • 1
  • 3
  • 12
  • 1
    ``board`` creates a *new* board every time it is called: ``board()[1, 1] = 6 `` creates a new board, changes position ``1, 1``, *then throws it away*;``print(board()[1, 1])`` creates a new board and looks at its ``1, 1`` position. The two do *not* share a board. – MisterMiyagi Jun 02 '21 at 07:27
  • Can you clarify how you expect this to work? The purpose of having a ``board()`` function is to *create a new* board. The purpose of modifying a board is to *have a single* board. Which of the two do you want? – MisterMiyagi Jun 02 '21 at 07:29
  • In some stages of the game, I want to change the values of the board by a number. And these value change will be permanent – Rishabh Semwal Jun 02 '21 at 07:31
  • You can create a global variable like in Corralien's answer or you add arguments to your `board()` function (e.g. coordinates : tuple, value : int) and change the `game` variable with these arguments. – nick Jun 02 '21 at 07:31
  • Hello @nick I am interested in your suggestion. Can you please post it as an answer with code – Rishabh Semwal Jun 02 '21 at 07:34
  • Please must state the reason before disliking the post so that I can improve my future post. Its matter really for me – Rishabh Semwal Jun 02 '21 at 07:40
  • I posted, the answer, you can call the function without parameters, then the board will be empty. If you give parameters it will modify the board. – nick Jun 02 '21 at 07:43

2 Answers2

1

See first comment of @MisterMiyagi above for explanation.

import numpy as np

def board():
    game = np.zeros((6, 6))
    return game

b = board()
b[1, 1] = 6  
>>> b[1, 1]
6.0
Corralien
  • 109,409
  • 8
  • 28
  • 52
1
import numpy as np

def board(coordinates: tuple = None, value: int = None):
    game = np.zeros((6, 6))
    if coordinates and value:
        game[coordinates] = value
    return game

print(board((1, 1), 6)) 
nick
  • 99
  • 7
  • Please can you elaborate on this so that I can understand what is happening. I will mark it as an answer if you explain it – Rishabh Semwal Jun 02 '21 at 07:43
  • How can I use a particular element of the board elsewhere in the code. It is printing board everytime – Rishabh Semwal Jun 02 '21 at 07:46
  • I added arguments to the function (see https://www.w3schools.com/python/python_functions.asp) These arguments modify the `game` variable inside the function. – nick Jun 02 '21 at 07:46
  • If you want a particular element of the board you need to index it. `board((1, 1), 6)[1,1]` but this is really ugly. You should store your board in a variable then. – nick Jun 02 '21 at 07:48
  • Yeah, this became too complicated when I change many elements of the board. Well thanks man for the efforts you put – Rishabh Semwal Jun 02 '21 at 07:51
  • No problem, if you study function parameters and global and local variables you are on your way. – nick Jun 02 '21 at 07:52