Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. def captures(self, (py, px), piece, board, captured=[], start=None):
  2. """ Return a list of possible capture moves for given piece in a
  3. checkers game.
  4.  
  5. :param (py, px): location of piece on the board
  6. :param piece: piece type (BLACK/WHITE|MAN/KING)
  7. :param board: the 2D board matrix
  8. :param captured: list of already-captured pieces (can't jump twice)
  9. :param start: from where this capture chain started.
  10. """
  11. if start is None:
  12. start = (py, px)
  13. # Look for capture moves
  14. for dx in [-1, 1]:
  15. for dy in [-1, 1]:
  16. dist = 1
  17. while True:
  18. jx, jy = px + dist * dx, py + dist * dy # Jumped square
  19. # Check if piece at jx, jy:
  20. if not (0 <= jx < 8 and 0 <= jy < 8):
  21. break
  22. if board[jy, jx] != EMPTY:
  23. tx, ty = px + (dist + 1) * dx, py + (dist + 1) * dy # Target square
  24. # Check if it can be captured:
  25. if ((0 <= tx < 8 and 0 <= ty < 8) and
  26. ((ty, tx) == start or board[ty, tx] == EMPTY) and
  27. (jy, jx) not in captured and
  28. ((piece & WHITE) and (board[jy, jx] & BLACK) or
  29. (piece & BLACK) and (board[jy, jx] & WHITE))
  30. ):
  31. # Normal pieces cannot continue capturing after reaching last row
  32. if not piece & KING and (piece & WHITE and ty == 0 or piece & BLACK and ty == 7):
  33. yield (NUMBERING[py, px], NUMBERING[ty, tx])
  34. else:
  35. for sequence in self.captures((ty, tx), piece, board, captured + [(jy, jx)], start):
  36. yield (NUMBERING[py, px],) + sequence
  37. break
  38. else:
  39. if piece & MAN:
  40. break
  41. dist += 1
  42. yield (NUMBERING[py, px],)
  43.  
  44. dist = 1
  45. while True:
  46. jx, jy = px + dist * dx, py + dist * dy
  47. dist += 1
  48.  
  49. jx, jy = px, py
  50. while True:
  51. jx += dx
  52. jy += dy
  53.  
  54. if not (0 <= jx < 8 and 0 <= jy < 8):
  55. break
  56. if board[jy, jx] != EMPTY:
  57.  
  58. try:
  59. if board[jy, jx] != EMPTY:
  60. ...
  61. except KeyError:
  62. break
  63.  
  64. ((piece & WHITE) and (board[jy, jx] & BLACK) or
  65. (piece & BLACK) and (board[jy, jx] & WHITE))
  66.  
  67. opponent = BLACK if piece & WHITE else WHITE
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement