Created
May 26, 2022 06:11
-
-
Save panki/a14ab17aa9c346a62bd9fe136e51d5c4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import product | |
from typing import List, Tuple | |
def __find_neighbours(x: int, y: int, size: int) -> List[Tuple]: | |
""" | |
Finds neighbours for a cell. Those which fall out of the field are skipped | |
:param x: current cell's X coord | |
:param y: current cell's Y coord | |
:param size: field single dimension size (because field is square) | |
:return: list of (x, y) pairs of valid neighbours | |
""" | |
return [ | |
(x + dx, y + dy) for dx, dy in product([-1,0,1], repeat=2) | |
if (dx or dy) and 0 <= x + dx < size and 0 <= y + dy < size | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment