简体   繁体   中英

Conditional doesn't behave as expected

I'm an experienced programmer but new to Python. The attached code is my attempt at setting up a game board grid. I would like to fill the top part of the grid with 0s and the bottom part with 1s. However, this code always fills the entire board with 1s. I suspect I have made a beginner's error and just don't recognize it. The following is, I believe, a minimum reproducible example.

ROWS = 56 
COLS = 79

#put the cursor at position x,y on screen
def gotoxy(x,y):
    print ("%c[%d;%df" % (0x1B, y, x), end='')

# class for objects that populate the grid
class cell:
    Xloc = 0; Yloc = 0
    contents = 0

# 2-dim array of cells - here just for convenience
emptyBoard = [[cell for i in range(ROWS)] for k in range(COLS)]

# class for the board
class board:
    # cell field: Xloc & Yloc & contents of each cell
    cells = emptyBoard  

    # function: write the board to the screen
    def dispBoard(self):
        for x in range(0,COLS):
            for y in range(0, ROWS):
                gotoxy(x,y)
                print (str(self.cells[x][y].contents)) 

    # function: fill the cells with 0s or 1s 
    def fillBoard(self):
        for x in range(0,COLS):
           for y in range(0, ROWS):
                self.cells[x][y].Xloc = x
                self.cells[x][y].Yloc = y
                if y > 20:
                    self.cells[x][y].contents = 1
                else:
                    self.cells[x][y].contents = 0  

def main():    
    bord = board()
    bord.fillBoard()
    bord.dispBoard()

if __name__ == "__main__":
    main()
 

When you're creating the emptyBoard , you're not creating a new object for each cell (see the missing parenthesis?)

emptyBoard = [[cell for i in range(ROWS)] for k in range(COLS)]

So all the cell s just refer to the base cell class, even if you change the content for one cell, it gets changed for all the cell s.

Now to fix this simply add the parenthesis and it will create a new object for each cell whose attributes will be independent from the other cell s.

emptyBoard = [[cell() for i in range(ROWS)] for k in range(COLS)]

In addition to kinshukdua's answer you are also missing the __init__ method in your classes (you need it at least for the cell class).

class cell:
    def __init__(self):
       self.Xloc = 0
       self.Yloc = 0
       self.contents = 0

Otherwise Xloc , Yloc and content are class attributes, they are shared by all the instances of the class, hence no surprise all the element of the board have the same value.

And by the way, it would be better to move emptyBoard = [[cell() for i in range(ROWS)] for k in range(COLS)] in the __init__ method of the board class if you want to have a more object oriented style. Something like this:

class board:
    def __init__(self):
        # cell field: Xloc & Yloc & contents of each cell
        sellf.cells = [[cell() for i in range(ROWS)] for k in range(COLS)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM