piece.scm 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. (add-to-load-path (dirname (current-filename)))
  2. (define-module (piece)
  3. #:export ())
  4. (use-modules (srfi srfi-9)
  5. (srfi srfi-9 gnu)
  6. (math)
  7. (bit-vector-utils)
  8. (bit-utils)
  9. ((bitboard) #:prefix bb:)
  10. ((chess-board) #:prefix cb:))
  11. (define-record-type <piece>
  12. ;; define constructor
  13. (make-piece kind move-validity-pred)
  14. ;; define predicate
  15. piece?
  16. ;; define accessors
  17. (kind piece-kind)
  18. (move-validity-pred piece-move-pred))
  19. ;; ===============
  20. ;; MOVE PREDICATES
  21. ;; ===============
  22. (define* (chess-knight-move-valid? board start-pos end-pos #:key (capturing #f))
  23. (let ([vertical-delta (abs (- (car start-pos) (car end-pos)))]
  24. [horizontal-delta (abs (- (cdr start-pos) (cdr end-pos)))])
  25. (and (not (= vertical-delta 3))
  26. (not (= horizontal-delta 3))
  27. (= (+ vertical-delta horizontal-delta) 3))))
  28. (define (chess-rook-move-valid? board start-pos end-pos #:key (capturing #f))
  29. (let ([vertical-delta (abs (- (car start-pos) (car end-pos)))]
  30. [horizontal-delta (abs (- (cdr start-pos) (cdr end-pos)))])
  31. (and
  32. ;; one delta must be positive, but never two
  33. (xor (positive? vertical-delta) (positive? horizontal-delta))
  34. ;; vertical delta must be lower than the height of the board
  35. (< vertical-delta (bitboard-height board))
  36. ;; horizontal delta must be lower than the width of the board
  37. (< horizontal-delta (bitboard-width board))
  38. ;; check for obstacles
  39. (not (rank/file-obstacle-between? start-pos end-pos)))))
  40. ;; =======
  41. ;; HELPERS
  42. ;; =======
  43. (define (row-obstacle-between? board start-pos end-pos)
  44. "assume that start-pos and end-pos are on the same row; assume
  45. start-pos and end-pos use indices (0-7) rather than chessboard
  46. coordinates;"
  47. (or (+ (bb:get-row-part start-pos) 1)
  48. )))
  49. (define (col-obstacle-between? board start-pos end-pos)
  50. ())
  51. (define (row/col-obstacle-between? board start-pos end-pos)
  52. ())
  53. ;; =============
  54. ;; MOVEMENTMENTS
  55. ;; =============
  56. ;; - directions:
  57. ;; How many fields turning clockwise counting fields adjacent to the starting field?
  58. ;; How many fields to movement ahead?
  59. ;; ... (more tuples)
  60. ;;
  61. ;; - A movement can either be a capturing movement or a non-capturing movement.
  62. ;;
  63. ;; - The movement can be jumping (ignoring any obstables on its way)
  64. ;; or not jumping (being blocked by other pieces or needing blockings to work).
  65. ;;
  66. ;; - `directions` is a list of (cons "field-counted rotations" "field-counted movements ahead").
  67. ;;
  68. ;; - For example the knight in chess:
  69. ;; '(((0 . 2) (1 . 1))
  70. ;; ((0 . 2) (3 . 1))
  71. ;; ((1 . 2) (0 . 1))
  72. ;; ((1 . 2) (2 . 1))
  73. ;; ((2 . 2) (1 . 1))
  74. ;; ((2 . 2) (3 . 1))
  75. ;; ((3 . 2) (0 . 1))
  76. ;; ((3 . 2) (2 . 1)))
  77. ;;
  78. ;; - required-obstacles-present?:
  79. ;; Predicate that checks if a obstacles on a board would allow for the movement
  80. ;; The procedures takes:
  81. ;; - a list of obstacles from the starting position of the piece to the target position
  82. ;; The predicate returns #t or #f.
  83. ;; - range:
  84. ;; The range of the movementment in times of application of the movement.
  85. ;; For example a movement can be to move one step forward, but have infinite range.
  86. ;; This would be a Lance in Shogi.
  87. #;(define-record-type <movement>
  88. (make-movement directions
  89. required-obstacles-pred
  90. capturing-pred
  91. jumping-pred
  92. range)
  93. movement?
  94. (directions movement-directions)
  95. (required-obstacles-pred movement-required-obstacles-pred)
  96. (capturing movement-capturing-pred)
  97. (jumping movement-jumping-pred)
  98. (range movement-range))
  99. ;; =====================
  100. ;; Directional Movements
  101. ;; =====================
  102. ;; clockwise counting adjacent squares
  103. ;; movement ahead
  104. ;; clockwise counting adjacent
  105. ;; movement ahead
  106. ;; ...
  107. ;; Knight in Chess:
  108. ;; jumping: #t
  109. ;; range: 1
  110. ;; '(((0 2) (1 1)) ; 2 directional movements, each in a list
  111. ;; ((0 2) (3 1))
  112. ;; ((1 2) (0 1))
  113. ;; ((1 2) (2 1))
  114. ;; ((2 2) (1 1))
  115. ;; ((2 2) (3 1))
  116. ;; ((3 2) (0 1))
  117. ;; ((3 2) (2 1)))