-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.rkt
50 lines (41 loc) · 1.42 KB
/
game.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
#lang racket
(require "board.rkt")
(require "moves.rkt")
(define (is-square? sq algn)
(equal? (algn->coord algn) (square-coord sq)))
(define (contains-piece? sq piece)
(eq? (square-piece sq) piece))
(define (square-on-board algn board)
(findf (lambda (x) (is-square? x algn)) board))
(define (remove-piece piece sq)
(square (square-coord sq) null))
(define (add-piece piece sq)
(square (square-coord sq) piece))
(define (move-piece piece from to board)
(map
(lambda (sq)
(cond
[(is-square? sq from) (remove-piece piece sq)]
[(is-square? sq to) (add-piece piece sq)]
[else sq])) board))
(define (valid-move? move board)
(define piece (first move))
(define from (second move))
(define to (third move))
(define moves (apply (move-map piece) (list (algn->coord from))))
(displayln moves)
(cond
[(not (eq? 3 (length move))) #f]
[(not (contains-piece? (square-on-board from board) piece)) #f]
[(not (member (algn->coord to) moves)) #f]
[else #t]))
(define (game-loop board)
(print-board board)
(displayln "Enter move (eg. p b2 b4)")
(define command (read-line))
(define move (string-split command))
(cond
[(string=? command "exit") (displayln "goodbye!")]
[(valid-move? move board) (game-loop (move-piece (first move) (second move) (third move) board))]
[else (displayln "not a valid move") (game-loop board)]))
;(define start (game-loop (make-board)))