-
Notifications
You must be signed in to change notification settings - Fork 0
/
moves.rkt
73 lines (61 loc) · 2.41 KB
/
moves.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#lang racket
(require "board.rkt")
(provide (all-defined-out))
(define (on-board? c)
(cond
[(null? c) #f]
[(< (coord-file c) 1) #f]
[(< (coord-rank c) 1) #f]
[(> (coord-file c) 8) #f]
[(> (coord-rank c) 8) #f]
[else #t]))
(define (legal moves)
(filter on-board? moves))
(define (diagonal-moves c)
(let ([up-left (map list (range 1 8) (range -1 -8 -1))]
[down-left (map list (range -1 -8 -1) (range -1 -8 -1))]
[up-right (map list (range 1 8) (range 1 8))]
[down-right (map list (range -1 -8 -1) (range 1 8))]
[coord-plus-move (lambda (x) (list (+ (coord-rank c) (car x)) (+ (coord-file c) (cadr x))))])
(map coord-plus-move (append up-left down-left up-right down-right))))
(define (perpendicular-moves c)
(let ([up (map list (make-list 7 0) (range 1 8))]
[down (map list (make-list 7 0) (range -1 -8 -1))]
[left (map list (range -1 -8 -1) (make-list 7 0))]
[right (map list (range 1 8) (make-list 7 0))]
[coord-plus-move (lambda (x) (list (+ (coord-rank c) (car x)) (+ (coord-file c) (cadr x))))])
(map coord-plus-move (append up down left right))))
(define (pawn-moves c)
(legal (list
(coord (+ (coord-rank c) 1) (coord-file c) )
(cond [(eq? (coord-rank c) 2) (coord (+ (coord-rank c) 2) (coord-file c))]
[else null]))))
(define (knight-moves c)
(match c
[(coord rank file)
(legal (list
(coord (+ rank 1) (+ file 2))
(coord (+ rank 2) (+ file 1))
(coord (+ rank 2) (- file 1))
(coord (+ rank 1) (- file 2))
(coord (- rank 1) (- file 2))
(coord (- rank 2) (- file 1))
(coord (- rank 2) (+ file 1))
(coord (- rank 1) (+ file 2))))]))
(define (bishop-moves c)
(legal (map pair->coord (diagonal-moves c))))
(define (rook-moves c)
(legal (map pair->coord (perpendicular-moves c))))
(define (queen-moves c)
(legal (map pair->coord (append (diagonal-moves c) (perpendicular-moves c)))))
(define (king-moves c)
(let ([coord-plus-move (lambda (x) (list (+ (coord-rank c) (car x)) (+ (coord-file c) (cadr x))))])
(legal (map pair->coord (map coord-plus-move (list '(0 1) '(1 0) '(1 1) '(0 -1) '(-1 0) '(-1 -1) '(1 -1) '(-1 1)))))))
(define (move-map piece)
(match piece
["p" pawn-moves]
["N" knight-moves]
["B" bishop-moves]
["R" rook-moves]
["Q" queen-moves]
["K" king-moves]))