| 10 | |
| 11 | |
| 12 | class checker: |
| 13 | def __init__(self, window): |
| 14 | self._init() |
| 15 | self.window = window |
| 16 | |
| 17 | # to update the position |
| 18 | def update(self): |
| 19 | self.board.draw(self.window) |
| 20 | self.draw_moves(self.valid_moves) |
| 21 | pg.display.update() |
| 22 | |
| 23 | def _init(self): |
| 24 | self.select = None |
| 25 | self.board = checker_board() |
| 26 | self.turn = black |
| 27 | self.valid_moves = {} |
| 28 | |
| 29 | # to reset the position |
| 30 | def reset(self): |
| 31 | self._init() |
| 32 | |
| 33 | # select row and column |
| 34 | def selectrc(self, row, col): |
| 35 | if self.select: |
| 36 | result = self._move(row, col) |
| 37 | if not result: |
| 38 | self.select = None |
| 39 | |
| 40 | piece = self.board.get_piece(row, col) |
| 41 | if (piece != 0) and (piece.color == self.turn): |
| 42 | self.select = piece |
| 43 | self.valid_moves = self.board.get_valid_moves(piece) |
| 44 | return True |
| 45 | return False |
| 46 | |
| 47 | # to move the pieces |
| 48 | def _move(self, row, col): |
| 49 | piece = self.board.get_piece(row, col) |
| 50 | if (self.select) and (piece == 0) and (row, col) in self.valid_moves: |
| 51 | self.board.move(self.select, row, col) |
| 52 | skip = self.valid_moves[(row, col)] |
| 53 | if skip: |
| 54 | self.board.remove(skip) |
| 55 | self.chg_turn() |
| 56 | else: |
| 57 | return False |
| 58 | return True |
| 59 | |
| 60 | # to draw next possible move |
| 61 | def draw_moves(self, moves): |
| 62 | for move in moves: |
| 63 | row, col = move |
| 64 | pg.draw.circle( |
| 65 | self.window, |
| 66 | red, |
| 67 | (col * sq_size + sq_size // 2, row * sq_size + sq_size // 2), |
| 68 | 15, |
| 69 | ) |