Skip to content

Instantly share code, notes, and snippets.

@arvsrao
Last active December 17, 2023 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arvsrao/49732a5a81292f602b58d14d4b19b0d6 to your computer and use it in GitHub Desktop.
Save arvsrao/49732a5a81292f602b58d14d4b19b0d6 to your computer and use it in GitHub Desktop.
Lösung für Mathe im Advent 2023 Aufgabe #6
BretTyp = list[list[str]]
BLAU = "B"
ORANGE = "O"
LEER = "X"
anfangBret: BretTyp = [[BLAU, BLAU, ORANGE, BLAU],
[ORANGE, ORANGE, BLAU, ORANGE],
[BLAU, BLAU, ORANGE, BLAU],
[BLAU, BLAU, BLAU, BLAU]]
leeresBret: BretTyp = [[LEER, LEER, LEER, LEER],
[LEER, LEER, LEER, LEER],
[LEER, LEER, LEER, LEER],
[LEER, LEER, LEER, LEER]]
def druckSpielBret(bret: BretTyp):
for reihe in bret:
for feld in reihe:
print(feld + " ", end='')
print("")
# stehl die Koordinate der gültigen Nachbarn her
def getNachbarschaftV0(idx, idy):
nachbarn = []
idxs = list(filter(lambda x: 0 <= x < 4, range(idx - 1, idx + 2)))
idys = list(filter(lambda y: 0 <= y < 4, range(idy - 1, idy + 2)))
for ix in idxs:
for iy in idys:
if ix != idx or iy != idy:
nachbarn.append((ix, iy))
return nachbarn
# stehl die Koordinate der gültigen Nachbarn her
def getNachbarschaftV1(idx, idy):
nachbarn = [(idx - 1, idy), (idx, idy + 1), (idx, idy - 1), (idx + 1, idy)]
return list(filter(lambda x: 0 <= x[0] < 4 and 0 <= x[1] < 4, nachbarn))
def processBlau(bret: BretTyp, idx: int, idy: int):
felder = [bret[x][y] for x, y in getNachbarschaftV0(idx, idy)]
num = felder.count(ORANGE)
return ORANGE if num == 3 else BLAU
def processOrange(bret: BretTyp, idx: int, idy: int):
felder = [bret[x][y] for x, y in getNachbarschaftV0(idx, idy)]
num = felder.count(ORANGE)
return ORANGE if 2 <= num < 4 else BLAU
def spielRunde(bret: BretTyp) -> BretTyp:
neuesBret: BretTyp = [[LEER, LEER, LEER, LEER],
[LEER, LEER, LEER, LEER],
[LEER, LEER, LEER, LEER],
[LEER, LEER, LEER, LEER]]
for idx, reihe in enumerate(bret):
for idy, feld in enumerate(reihe):
neuesBret[idx][idy] = processBlau(bret, idx, idy) if feld == BLAU else processOrange(bret, idx, idy)
return neuesBret
print("---------------------: " + "Anfangszustand" + " :-------------------")
druckSpielBret(anfangBret)
temp = anfangBret
for idx in range(1,10):
runde = spielRunde(temp)
print("---------------------: " + str(idx) + ". Runde :-------------------")
druckSpielBret(runde)
if runde == temp: # end Bedingung
print("\n... Beharrungszustand !!!")
break
temp = runde
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment