Ich habe Verbindung 4, und ich versuche, einen Algorithmus zu erarbeiten, um den Gewinner zu bestimmen. Der folgende bestimmt den horizontalen Gewinner, obwohl aus irgendeinem Grund ein Fehler auftritt, wenn die Zähler vertikal wie folgt positioniert sind. Was verursacht diesen Fehler und wie behebe ich ihn?Indexfehler in Python aus unbekannten Grund
board [5] [5] == 1 (rot-Chip)
board [4] [5] == 1 (rot-Chip)
board [3] [5] == 1 (rot-Chip), wenn dieser Fehler verursacht wird pLATZIERT
#Check for horizontal win
for y in range(6):
for x in range(7 - 3):
if board[x][y] == 1 and board[x+1][y] == 1 and board[x+2][y] == 1 and board[x+3][y] == 1:
return True
if board[x][y] == 1 and board[x+1][y] == 1 and board[x+2][y] == 1 and board[x+3][y] == 1:
IndexError: list index out of range
UPDATE: Das worke d aber jetzt funktioniert der vertikale Test nicht, was habe ich getan?
# check vertical spaces
for x in range(6):
for y in range(7 - 3):
if board[x][y] == 1 and board[x][y+1] == 1 and board[x][y+2] == 1 and board[x][y+3] == 1:
return True
UPDATE 2: Index Fehler, wenn ich vertikale arranaged, tritt auf, wenn Chips in derselben Position wie unten
Prüfung vertikale Räume
for x in range(6):
for y in range(7 - 3):
if board[y][x] == 1 and board[y+1][x] == 1 and board[y+2][x] == 1 and board[y+3][x] == 1:
return True
Also, wie löse ich das? –
, aber dann nach horizontalen Gewinnen zu suchen funktioniert nicht? –
@Rawing Ich habe nicht das gesamte Board gesehen, du hast Recht – Li357