Über Graphen loopen
Verfasst: Donnerstag 4. Dezember 2025, 21:42
Hallo zusammen,
ich würde gerne eine Funktion programmieren, die mir alle Positionen zurückgibt, welche bei einem Graphen noch nicht belegt sind. Bei dem Graphen würde es sich um ein Spielbrett handeln. Dies sind Auszüge von dem Coding:
Und wenn nun ein Stein auf ein Feld gesetzt wird:
Weiß jemand nun wie man eine Funktion programmiert, die mir alle Felder von dem Graphen(Spielbrett) zurückgibt, welche noch nicht belegt sind?
Vielen Dank im Voraus
ich würde gerne eine Funktion programmieren, die mir alle Positionen zurückgibt, welche bei einem Graphen noch nicht belegt sind. Bei dem Graphen würde es sich um ein Spielbrett handeln. Dies sind Auszüge von dem Coding:
Code: Alles auswählen
class Board:
def __init__(self, width, height, visualizer=None):
self.width = width
self.height = height
self.points = []
self.connections = []
self.fixedpoints = {
"corners": [(1, 1), (self.width, 1), (1, self.height), (self.width, self.height)],
"edgepoints_red_bottom": [(x, 1) for x in range(2, self.width)],
"edgepoints_red_top": [(x, self.height) for x in range(2, self.width)],
"edgepoints_black_left": [(1, x) for x in range(2, self.height)],
"edgepoints_black_right": [(self.width, x) for x in range(2, self.height)],
}
self.visualizer = visualizer
self.graph_red = nx.Graph()
self.graph_black = nx.Graph()
Code: Alles auswählen
array_x = []
array_y = []
for x in range(1, self.board.height+1):
for y in range(1, self.board.width+1):
if (x == 1 and y == 1) or (x == 1 and y == self.board.width) or (x == self.board.height and y == 1) or (x == self.board.height and y == self.board.width):
pass
else:
array_x.append(y)
for x in range(1, self.board.height+1):
for y in range(1, self.board.width+1):
if (x == 1 and y == 1) or (x == 1 and y == self.board.width) or (x == self.board.height and y == 1) or (x == self.board.height and y == self.board.width):
pass
else:
array_y.append(x)
x = np.array(array_x)
y = np.array(array_y)
Code: Alles auswählen
def setpoint(self, x, y):
# save the point in list
self.board.add_point(self.color, x, y)
Vielen Dank im Voraus