I am currently programming a version of a game called Othello, where you can choose how big the board should be (alternating from 4x4 to 10x10). Now when I am trying to insert a faulty message as when you input a coordinate that is outside the board area it does not work. At the moment the input as when you place the tile in a nonlegal move works as well as if you input just 1 coordinate or multiple coordinates (it should only be two as one for the x and y-coordinate)
(Sorry I know I have posted a very similar question but as I then solved this problem, when I continued with my work, the problem have popped up again)
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
I'd propose you change your legalMove
function's definition. You don't need to pass isOnBoard
as a parameter (and you don't, so why keep it?):
def legalMove(self, tile, startX, startY):
isOnBoard
is defined, and checks whether indices are out of bounds or not. I would recommend making the call to isOnBoard
inside legalMove
. This way you complete the function, functionally. Here's the proposed changes:
def legalMove(self, tile, startX, startY):
if not self.isOnBoard(startX, startY)\
or self.board[startX][startY] != ' '\
or not self.isOnBoard(startX, startY):
return False