game-board.scm 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. (define-module (game-board)
  2. #:export (on-same-row?
  3. on-same-col?
  4. squares-between
  5. col-squares-between
  6. get-coord
  7. get-coords-of-trues
  8. index->board-coords))
  9. (define (on-same-row? start-pos end-pos)
  10. (eq? (get-row-part start-pos)
  11. (get-row-part end-pos)))
  12. (define (on-same-col? start-pos end-pos)
  13. (eq? (get-col-part start-pos)
  14. (get-col-part end-pos)))
  15. (define (squares-between board start-pos end-pos)
  16. (define (row-squares-between board start-pos end-pos)
  17. (let ([col-idxs (number-range-exclusive (get-col-part start-pos) (get-col-part end-pos))]
  18. [row-idx (get-row-part start-pos)])
  19. (map (λ (col-idx) (make-coords row-idx col-idx))
  20. col-idxs)))
  21. (define (col-squares-between board start-pos end-pos)
  22. (let ([row-idxs (number-range-exclusive (get-row-part start-pos) (get-row-part end-pos))]
  23. [col-idx (get-col-part start-pos)])
  24. (map (λ (row-idx) (make-coords row-idx col-idx))
  25. row-idxs)))
  26. (cond [(equal? start-pos end-pos) '()]
  27. [(on-same-row? start-pos end-pos) (row-squares-between board start-pos end-pos)]
  28. [(on-same-col? start-pos end-pos) (col-squares-between board start-pos end-pos)]
  29. [else (throw 'logic-error
  30. "squares not on same row or column"
  31. board start-pos end-pos)]))