I am a beginner level programmer trying to learn Python. I could not figure out how to change the values so I can restrict the pieces of checkers from moving diagonally. I want them to be able to move only horizontally and vertically.
My code is given and I think the problem is inside the _move_left
where there are moves.update
, but I have no idea how to fix it. I am watching a video about creating it but the guy didn’t explain that well how the functions work (he is making pieces move diagonally).
def get_valid_moves(self, piece):
moves = {}
left = piece.col - 1
right = piece.col + 1
up = piece.row - 1
down = piece.row + 1
row = piece.row
col = piece.col
if piece.color == BLACK or piece.king:
moves.update(self._move_left(row, max(row - 3, 1), -1, piece.color, left))
moves.update(self._move_right(row, max(row - 3, 1), -1, piece.color, right))
moves.update(self._move_up(col, max(col - 3, 1), -1, piece.color, up))
if piece.color == BEIGE or piece.king:
moves.update(self._move_left(row, min(row + 3, ROWS), 1, piece.color, left))
moves.update(self._move_right(row, min(row + 3, ROWS), 1, piece.color, right))
moves.update(self._move_down(col, min(col + 3, COLS), 1, piece.color, down))
return moves
def _move_left(self, start, stop, step, color, left, skipped=[]):
moves = {}
last = []
for r in range(start, stop, step):
if left < 0:
break
current = self.board[r][left]
if current == 0:
if skipped and not last:
break
elif skipped:
moves[(r, left)] = last + skipped
else:
moves[(r, left)] = last
if last:
if step == -1:
row = max(r - 3, 0)
else:
row = min(r + 3, ROWS)
moves.update(self._move_left(r + step, row, step, color, left - 1, skipped=last))
moves.update(self._move_right(r + step, row, step, color, left + 1, skipped=last))
break
elif current.color == color:
break
else:
last = [current]
left -= 1
return moves