現在、Othelloというゲームのバージョンをプログラミングしています。ここでは、ボードの大きさを選択できます(4x4から10x10に変更する)。ボード領域の外にある座標を入力するときにエラーメッセージを挿入しようとしているとき、それは動作しません。現時点では、タイルを非凡例に置いたときの入力は、1つの座標または複数の座標を入力した場合と同様に機能します(x座標とy座標の2つにする必要があります)IndexError:マトリックスの内容をチェックするときにインデックスが範囲外になる
私は、私は非常によく似た質問を掲載している知っているが、私は、この問題を解決して、私は私の仕事を続けたときに、問題が
def isOnBoard(self, x, y):
return x >= 0 and x <= self.size-1 and y >= 0 and y <= self.size-1
def legalMove(self, tile, startX, startY):
if not self.isOnBoard(startX, startY) == False\
or self.board[startX][startY] != ' '\
or not self.isOnBoard(startX, startY):
return False
#(lots more down here that checks if the placed move is legal but nonrelevant to the question)
def playerMove(self,tile):
while True:
move = input().lower()
if self.legalMove(tile, x, y) == False:
print('Wrong input, try again') #<--- checks that the input coordinate is legal
else:
break
else:
print('this was wrong try again!.') #<-- checks that input coordinate just consists of two characters
return [x, y]
if self.board[startX][startY] != ' ' or not self.isOnBoard(startX, startY): IndexError: list index out of range
私はあなたの恋人の編集でやりたかったことをしたとは思わない。 'self.isOnBoard(startX、startY)== False'は' if self.isOnBoard(startX、startY)== True'と同じです。 –