Advertisement
Guest User

Untitled

a guest
Apr 18th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.89 KB | None | 0 0
  1. (ns chess.state)
  2.  
  3. (defn make-board
  4. "Creates a chess board in the initial configuration."
  5. []
  6. '({:type :rook :color :black :rank 8 :file 1 }
  7. {:type :knight :color :black :rank 8 :file 2 }
  8. {:type :bishop :color :black :rank 8 :file 3 }
  9. {:type :queen :color :black :rank 8 :file 4 }
  10. {:type :king :color :black :rank 8 :file 5 }
  11. {:type :bishop :color :black :rank 8 :file 6 }
  12. {:type :knight :color :black :rank 8 :file 7 }
  13. {:type :rook :color :black :rank 8 :file 8 }
  14.  
  15. {:type :pawn :color :black :rank 7 :file 1 }
  16. {:type :pawn :color :black :rank 7 :file 2 }
  17. {:type :pawn :color :black :rank 7 :file 3 }
  18. {:type :pawn :color :black :rank 7 :file 4 }
  19. {:type :pawn :color :black :rank 7 :file 5 }
  20. {:type :pawn :color :black :rank 7 :file 6 }
  21. {:type :pawn :color :black :rank 7 :file 7 }
  22. {:type :pawn :color :black :rank 7 :file 8 }
  23.  
  24. {:type :pawn :color :white :rank 2 :file 1 }
  25. {:type :pawn :color :white :rank 2 :file 2 }
  26. {:type :pawn :color :white :rank 2 :file 3 }
  27. {:type :pawn :color :white :rank 2 :file 4 }
  28. {:type :pawn :color :white :rank 2 :file 5 }
  29. {:type :pawn :color :white :rank 2 :file 6 }
  30. {:type :pawn :color :white :rank 2 :file 7 }
  31. {:type :pawn :color :white :rank 2 :file 8 }
  32.  
  33. {:type :rook :color :white :rank 1 :file 1 }
  34. {:type :knight :color :white :rank 1 :file 2 }
  35. {:type :bishop :color :white :rank 1 :file 3 }
  36. {:type :queen :color :white :rank 1 :file 4 }
  37. {:type :king :color :white :rank 1 :file 5 }
  38. {:type :bishop :color :white :rank 1 :file 6 }
  39. {:type :knight :color :white :rank 1 :file 7 }
  40. {:type :rook :color :white :rank 1 :file 8 }))
  41.  
  42. (ns chess.movement
  43. (:require [chess.state :refer :all]
  44. [clojure.math.numeric-tower :as math]))
  45.  
  46. (defn on-board?
  47. "Determines if a position is on the board."
  48. [[rank file :as position]]
  49. (and (>= rank 1)
  50. (<= rank 8)
  51. (>= file 1)
  52. (<= file 8)))
  53.  
  54. (defn same-rank?
  55. "Determines if two positions are in the same rank."
  56. [[start-rank start-file :as position] [dest-rank dest-file :as destination]]
  57. (and (= dest-rank start-rank) (not= dest-file start-file)))
  58.  
  59. (defn same-file?
  60. "Determines if two positions are in the same file."
  61. [[start-rank start-file :as position] [dest-rank dest-file :as destination]]
  62. (and (= dest-file start-file) (not= dest-rank start-rank)))
  63.  
  64. (defn diagonal?
  65. "Determines if two positions are diagonal from each other. We need this for things like determining whether a pawn movement is a capture, etc."
  66. [[start-rank start-file :as position] [dest-rank dest-file :as destination]]
  67. (= (math/abs (- start-rank dest-rank))
  68. (math/abs (- start-file dest-file))))
  69.  
  70. (defn occupied?
  71. "Determines if a location on the board is occupied by a piece. If the color actual
  72. parameter is provided then the piece at that location must be that color. We need to know this
  73. to help determine the ability to move, capture, etc."
  74. ([board [rank file :as position]]
  75. (some #(and (= rank (% :rank)) (= file (% :file))) board))
  76. ([board [rank file :as position] color]
  77. (some #(and (= rank (% :rank)) (= file (% :file)) (= color (% :color))) board)))
  78.  
  79. (defn opponent-color
  80. "Returns the opponent's color."
  81. [color]
  82. (cond
  83. (= color :black) :white
  84. (= color :white) :black))
  85.  
  86. (defn remove-pieces
  87. "Removes pieces from the board at the specified locations."
  88. [board position & remaining-positions]
  89. (let [positions (into #{position} remaining-positions)
  90. matching-position? #(positions [(% :rank) (% :file)])]
  91. (remove matching-position? board)))
  92.  
  93. (defn positions-between
  94. "Generates a list of the positions between start and end. If the
  95. start and end positions aren't in the same rank, file, or diagonal
  96. from each other then returns an empty sequence."
  97. [[s-rank s-file :as start] [e-rank e-file :as end]]
  98. (let [min-rank (min s-rank e-rank)
  99. max-rank (max s-rank e-rank)
  100. min-file (min s-file e-file)
  101. max-file (max s-file e-file)]
  102. (cond
  103. (= min-rank max-rank)
  104. (into '() (map (fn [file] [min-rank file]) (range (inc min-file) max-file)))
  105. (= min-file max-file)
  106. (into '() (map (fn [rank] [rank min-file]) (range (inc min-rank) max-rank)))
  107. (diagonal? start end)
  108. (into '() (map (fn [rank file] [rank file]) (range (inc min-rank) max-rank) (range (inc min-file) max-file)))
  109. :else '())))
  110.  
  111. (defn movement-blocked?
  112. "Move is blocked if the destination is occupied by the player's own piece, or
  113. if any position between the start and destination contains a piece. Note that
  114. this function does not work for Pawns (yet) because a pawn is blocked when
  115. moving forward if the destination is occupied by any piece."
  116. [board [rank file :as position] [dest-rank dest-file :as destination] color]
  117. (or (occupied? board destination color)
  118. (some (into #{} (positions-between position destination))
  119. (map (fn [p] [(p :rank) (p :file)]) board))))
  120.  
  121. (ns chess.rook-movement
  122. (:require [chess.movement :refer :all]
  123. [chess.state :refer :all]))
  124.  
  125. (defn valid-rook-move?
  126. "Determines if a rook can make a move from one position to another on a board."
  127. [board [start-rank start-file :as position] [dest-rank dest-file :as destination] color]
  128. (and (on-board? destination)
  129. (or (same-rank? position destination)
  130. (same-file? position destination))
  131. (not (movement-blocked? board position destination color))))
  132.  
  133. (defn rook-destinations
  134. "Returns a lazy sequence of positions (tuples containing rank and file) that a rook can move to."
  135. [board [rank file :as position] color]
  136. (let [possible-dests (concat (map (fn [f] [rank f]) (range 1 9))
  137. (map (fn [r] [r file]) (range 1 9)))]
  138. (filter #(valid-rook-move? board position % color) possible-dests)))
  139. (same-file? position destination))
  140. (not (movement-blocked? board position destination color))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement