Hier sind einige relevante Teile des Codes. Wie würden Sie das Problem beheben, wenn ein Benutzer falsche Koordinaten außerhalb des Bereichs oder andere Zeichen/Symbole eingibt, damit das Programm sie erneut zur Eingabe auffordert?Verhindern Programmabsturz, wenn ungültige Koordinaten eingegeben werden
Hier sind die relevanten Teile des Programms:
Import Zufalls
def SetUpGameBoard(Board, Boardsize):
for Row in range(1, BoardSize + 1):
for Column in range(1, BoardSize + 1):
if (Row == (BoardSize + 1) // 2 and Column == (BoardSize + 1) // 2 + 1) or (Column == (BoardSize + 1) // 2 and Row == (BoardSize + 1) // 2 + 1):
Board[Row][Column] = "C"
elif (Row == (BoardSize + 1) // 2 + 1 and Column == (BoardSize + 1) // 2 + 1) or (Column == (BoardSize + 1) // 2 and Row == (BoardSize + 1) // 2):
Board[Row][Column] = "H"
else:
Board[Row][Column] = " "
def ChangeBoardSize():
BoardSize = int(input("Enter a board size (between 4 and 9): "))
while not(BoardSize >= 4 and BoardSize <= 9):
BoardSize = int(input("Enter a board size (between 4 and 9): "))
return BoardSize
def CheckIfMoveIsValid(Board, Move):
Row = Move % 10
Column = Move // 10
MoveIsValid = False
if Board[Row][Column] == " ":
MoveIsValid = True
return MoveIsValid
def MakeMove(Board, BoardSize, Move, HumanPlayersTurn):
Row = Move % 10
Column = Move // 10
if HumanPlayersTurn:
Board[Row][Column] = "H"
else:
Board[Row][Column] = "C"
FlipOpponentPiecesInOneDirection(Board, BoardSize, Row, Column, 1, 0)
FlipOpponentPiecesInOneDirection(Board, BoardSize, Row, Column, -1, 0)
FlipOpponentPiecesInOneDirection(Board, BoardSize, Row, Column, 0, 1)
FlipOpponentPiecesInOneDirection(Board, BoardSize, Row, Column, 0, -1)
Vielen Dank für all die Hilfe. Hier ist der gesamte Code, wenn Sie wollen es sehen und es laufen:
http://www.writeurl.com/text/mpt643e0ryn96l0m8zhc/tpcx0mbjc8hdqo2zizwm/ettx0pq62ufshh3y2ays
Was ist falsch an Ihrem Versuch? –
Es stürzt ab. Ich habe alles versucht und es funktioniert nicht. –
Wäre hilfreich, wenn Sie die Fehlermeldung –