Is there any smart way to compress the following code:
def __total(self, x, y):
count = 0
for x_offset in range(-1, 2):
x_index = x + x_offset
for y_offset in range(-1, 2):
y_index = y + y_offset
if 0 <= x_index < self.Size and 0 <= y_index < self.__Size:
count += self.__buttons[y_index][x_index].mine
if not count:
for x_offset in range(-1, 2):
x_index = x + x_offset
for y_offset in range(-1, 2):
y_index = y + y_offset
if 0 <= x_index < self.__Size and 0 <= y_index < self.__Size:
self.__push(x_index, y_index)
return count
I have been trying forever with an extreme "coder-blackout" not being able to succeed....
self.Size
orself.__Size
? Okay. I will assume for now, that you meant__Size
. def __total(self, x, y, count, old): if (not count and old) or not old: for x_index in range(-1+x, 2+x): for y_index in range(-1+y, 2+y): if 0 <= x_index < self.Size and 0 <= y_index < self.__Size: if old: self.__push(x_index, y_index) else: count += self.__bu \$\endgroup\$count
starts out at0
, so it must have been added to at some point when it was0
. Yours never will. On a side note, you should useelse:
, notelif:
becauseelif
, likeif
, expects a condition to follow. It's likeelse if ...
\$\endgroup\$count += ...
in your first block anddef __total...
in your second block are indented by four extra spaces. My first comment about not working still applies, though. \$\endgroup\$