chess-board.scm 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. (add-to-load-path (dirname (current-filename)))
  2. (define-module (chess-board)
  3. #:export (chessboard-letter->column-table
  4. chessboard-number->row-table
  5. column->chessboard-letter-table
  6. chessboard-letter->column
  7. chessboard-number->row
  8. column->chessboard-letter
  9. make-bitvector-from-chessboard-coords
  10. create-initial-chess-board
  11. make-chessboard
  12. chessboard?
  13. chessboard-white-bb
  14. chessboard-black-bb
  15. chessboard-pawns-bb
  16. chessboard-knights-bb
  17. chessboard-bishops-bb
  18. chessboard-rooks-bb
  19. chessboard-queens-bb
  20. chessboard-kings-bb))
  21. ;; SRFI 9: record types
  22. (use-modules (ice-9 hash-table)
  23. (srfi srfi-9)
  24. (srfi srfi-9 gnu)
  25. (bit-vector-utils)
  26. ((bitboard) #:prefix bb:))
  27. (define-immutable-record-type <chessboard>
  28. ;; define constructor
  29. (make-chessboard white-bb
  30. black-bb
  31. pawns-bb
  32. knights-bb
  33. bishops-bb
  34. rooks-bb
  35. queens-bb
  36. kings-bb)
  37. ;; define predicate
  38. chessboard?
  39. ;; define accessors
  40. (white-bb chessboard-white-bb)
  41. (black-bb chessboard-black-bb)
  42. (pawns-bb chessboard-pawns-bb)
  43. (knights-bb chessboard-knights-bb)
  44. (bishops-bb chessboard-bishops-bb)
  45. (rooks-bb chessboard-rooks-bb)
  46. (queens-bb chessboard-queens-bb)
  47. (kings-bb chessboard-kings-bb))
  48. (set-record-type-printer!
  49. <chessboard>
  50. (λ (record port)
  51. (display (simple-format #f "\n#<<chessboard>\n") port)
  52. (display (simple-format #f " white:\n ~s\n" (chessboard-white-bb record)) port)
  53. (display (simple-format #f " black:\n ~s\n" (chessboard-black-bb record)) port)
  54. (display (simple-format #f " pawns:\n ~s\n" (chessboard-pawns-bb record)) port)
  55. (display (simple-format #f " knights:\n ~s\n" (chessboard-knights-bb record)) port)
  56. (display (simple-format #f " bishops:\n ~s\n" (chessboard-bishops-bb record)) port)
  57. (display (simple-format #f " rooks:\n ~s\n" (chessboard-rooks-bb record)) port)
  58. (display (simple-format #f " queens:\n ~s\n" (chessboard-queens-bb record)) port)
  59. (display (simple-format #f " kings:\n ~s>" (chessboard-kings-bb record)) port)))
  60. (define chessboard-letter->column-table
  61. (alist->hash-table
  62. '((a . 0) (b . 1) (c . 2) (d . 3) (e . 4) (f . 5) (g . 6) (h . 7))))
  63. (define chessboard-number->row-table
  64. (alist->hash-table
  65. '((1 . 0) (2 . 1) (3 . 2) (4 . 3) (5 . 4) (6 . 5) (7 . 6) (8 . 7))))
  66. (define column->chessboard-letter-table
  67. (alist->hash-table
  68. '((0 . a) (1 . b) (2 . c) (3 . d) (4 . e) (5 . f) (6 . g) (7 . h))))
  69. (define (chessboard-letter->column letter)
  70. (hash-ref chessboard-letter->column-table
  71. letter
  72. #f))
  73. (define (chessboard-number->row number)
  74. (hash-ref chessboard-number->row-table
  75. number
  76. #f))
  77. ;; (define (get-row-part coords)
  78. ;; (cdr coords))
  79. ;; (define (get-col-part coords)
  80. ;; (car coords))
  81. (define (column->chessboard-letter column)
  82. (hash-ref column->letter-table column #f))
  83. (define (make-bitvector-from-chessboard-coords coords row-count col-count)
  84. (define (iter bv coords)
  85. (cond
  86. [(null? coords) bv]
  87. [else
  88. (let* ([current-elem (car coords)]
  89. [idx (bb:coords->index (chessboard-number->row (get-row-part current-elem))
  90. (chessboard-letter->column (get-col-part current-elem))
  91. row-count
  92. col-count)])
  93. (display (simple-format #f "index: ~a\n" idx))
  94. (bitvector-set! bv idx #t)
  95. (iter bv (cdr coords)))]))
  96. (let ([initial-bv (make-bitvector (* row-count col-count) #f)])
  97. (iter initial-bv coords)))
  98. (define (create-initial-chess-board)
  99. (define black-bb
  100. (make-bitboard
  101. (make-bitvector-from-chessboard-coords
  102. '((a . 8) (b . 8) (c . 8) (d . 8) (e . 8) (f . 8) (g . 8) (h . 8)
  103. (a . 7) (b . 7) (c . 7) (d . 7) (e . 7) (f . 7) (g . 7) (h . 7))
  104. 8 8)
  105. 8 8 'chess))
  106. (define white-bb
  107. (make-bitboard
  108. (make-bitvector-from-chessboard-coords
  109. '((a . 1) (b . 1) (c . 1) (d . 1) (e . 1) (f . 1) (g . 1) (h . 1)
  110. (a . 2) (b . 2) (c . 2) (d . 2) (e . 2) (f . 2) (g . 2) (h . 2))
  111. 8 8)
  112. 8 8 'chess))
  113. (define pawns-bb
  114. (make-bitboard
  115. (make-bitvector-from-chessboard-coords
  116. '((a . 2) (b . 2) (c . 2) (d . 2) (e . 2) (f . 2) (g . 2) (h . 2)
  117. (a . 7) (b . 7) (c . 7) (d . 7) (e . 7) (f . 7) (g . 7) (h . 7))
  118. 8 8)
  119. 8 8 'chess))
  120. (define knights-bb
  121. (make-bitboard
  122. (make-bitvector-from-chessboard-coords
  123. '((b . 1) (g . 1) (b . 8) (g . 8))
  124. 8 8)
  125. 8 8 'chess))
  126. (define bishops-bb
  127. (make-bitboard
  128. (make-bitvector-from-chessboard-coords
  129. '((c . 1) (f . 1) (c . 8) (f . 8))
  130. 8 8)
  131. 8 8 'chess))
  132. (define rooks-bb
  133. (make-bitboard
  134. (make-bitvector-from-chessboard-coords
  135. '((a . 1) (h . 1) (a . 8) (h . 8))
  136. 8 8)
  137. 8 8 'chess))
  138. (define queens-bb
  139. (make-bitboard
  140. (make-bitvector-from-chessboard-coords
  141. '((d . 1) (d . 8))
  142. 8 8)
  143. 8 8 'chess))
  144. (define kings-bb
  145. (make-bitboard
  146. (make-bitvector-from-chessboard-coords
  147. '((e . 1) (e . 8))
  148. 8 8)
  149. 8 8 'chess))
  150. (make-chessboard white-bb
  151. black-bb
  152. pawns-bb
  153. knights-bb
  154. bishops-bb
  155. rooks-bb
  156. queens-bb
  157. kings-bb))