0

I've been working on an implementation for a problem from HackerRank. Namely this one: https://www.hackerrank.com/challenges/battleship1p/problem

My language of choice is Python3 and it will be a good fit, I guess. Now here is a basic construct on which my plan was, to build the project upon:

class Cell():
    # - status open
    # h hit
    # m miss
    neighboring = []
    status = None
    cell_x = None
    cell_y = None

    def __init__(self, cell_x, cell_y):
        self.cell_x = cell_x
        self.cell_y = cell_y
        status = '-'

    def check_status(self):
        return self.status

    def check_ut(self):
        if (self.status == "-"):
            return True

    def check_hit(self):
        if (self.status == "h"):
            return True

    def check_miss(self):
        if (self.status == "m"):
            return True

    def add_neighboring(self, c):
        self.neighboring.append(c)

    def check_neighboring(self):
        for x in self.neighboring:
            if (x.check_ut()):
                return x

    def check_neighboring_is_hit(self):
        if self.check_hit():
            for x in self.neighboring:
                if (x.check_ut()):
                    return x


class Row():
    Cells = []
    y = None

    def __init__(self, y):
        for i in range(10):
            self.Cells.append(Cell(i, y))


class Board():
    Rows = None

    def populate_neighbors(self):
        for l in self.Rows:
            for c in l.Cells:
                if (c.cell_x > 0):
                    prev_cell = l.Cells[c.cell_x - 1]
                    prev_cell.add_neighboring(c)
                    c.add_neighboring(prev_cell)
                if (c.cell_y > 0):
                    above_cell = self.Rows[c.cell_y - 1].Cells[c.cell_x]
                    above_cell.add_neighboring(c)
                    c.add_neighboring(above_cell)

        print("test")

    def NewRow(self):
        self.Rows.append(Row(len(self.Rows)))

    def __init__(self, rows):
        self.Rows = []
        for i in range(rows):
            self.NewRow()


list_ships = [1, 1, 2, 2, 3, 4, 5]

z = Board(10)
z.populate_neighbors()

It tries to rebuild a Board of a player, which I initalise with 10 rows Board(10) and it should also create 10 of Cells per row. But because of something happening in the background it seems to create 100 fields per row, at least my debugger says that. I would appreciate if you can give me a hind where duplication or recreation or something like that happens.

In populate_neighbors, my goal was to find all adjacent cells for a particular cell, by iterating through first all rows and then cell by cell, to add to the previous the current selected and to the current selected the previous, same for the height of the gamefield. Now I hope you understood what this intentionally was about.

martineau
  • 119,623
  • 25
  • 170
  • 301
marena
  • 3
  • 2

1 Answers1

0

Well, in your code Row.Cells is a class attribute and is shared between all of the instances of Row. The loop always append to the same list that's why we have 100 cells. To fix you will need:

class Row():
    Cells = []  # this is not a field declaration like Java
    y = None

    def __init__(self, y):
        self.Cells = []  # need this
        for i in range(10):
            self.Cells.append(Cell(i, y))

I'd recommend you to re-read the basics of Python.

tungd
  • 14,467
  • 5
  • 41
  • 45