1 ; jumpsaround - jumper game
    2 ; (c) 2020 Alexander Kulbartsch
    3 
    4 (define SIZEX 6)
    5 (define SIZEY 5)
    6 
    7 (define JUMP '((-1 -2) (1 -2) (-2 -1) (2 -1) (-2 1) (2 1) (-1 2) (1 2)))
    8 
    9 (define board (make-vector (* SIZEX SIZEY) 0))
   10 ;(define board (make-vector SIZEX (make-vector SIZEY 0)))
   11 ;(define iters (make-vector (* SIZEX SIZEY) 0))
   12 (define solutions 0)
   13 (define roundtrips 0) 
   14   
   15 (define (board-ref x y)
   16   (vector-ref board (+ x (* y SIZEX))))
   17 
   18 (define (board-set! x y v)
   19   (vector-set! board (+ x (* y SIZEX)) v))
   20 
   21 (define (printboard rt)
   22   (format #t "===== Solution # ~a - Roundtripps: ~a ====== \n" solutions roundtrips) simple-format
   23   (if rt  (display " *** Roundtrip *** \n")  #f)
   24   (do ((i 0 (1+ i))) ((>= i SIZEY))
   25     (do ((j 0 (1+ j))) ((>= j SIZEX))
   26       (format #t "~2d:" (board-ref j i)))
   27     (newline))
   28   (newline))
   29 
   30 
   31 (define (jumpto x y iter) 
   32   (board-set! x y iter)
   33   ;check if finished
   34   (if (>= iter (* SIZEX SIZEY))
   35       (begin ;check for roundtrips
   36           (let ([round #f])
   37             (map (lambda (d)
   38               (let ([nx (+ x (car  d))]
   39                     [ny (+ y (cadr d))])
   40                 (if (and (not round)
   41                          (>= nx 0) (< nx SIZEX)
   42                          (>= ny 0) (< ny SIZEY))
   43                    (if (= (board-ref nx ny) 1)
   44                        (begin
   45                          (set! roundtrips (+ roundtrips 1))
   46                          (set! round #t)
   47                          (printboard round))
   48                        #f )
   49                    #f )))
   50            JUMP)
   51           (set! solutions (+ solutions 1))
   52           ;(printboard round)
   53           ))
   54       ; else try next
   55       (map (lambda (d)
   56              (let ([nx (+ x (car  d))]
   57                    [ny (+ y (cadr d))])
   58                (if (and (>= nx 0) (< nx SIZEX)
   59                         (>= ny 0) (< ny SIZEY))
   60                    (if (= (board-ref nx ny) 0)
   61                        (begin
   62                          ; (display nx) 
   63                          (jumpto nx ny (+ 1 iter)) )
   64                        #f)
   65                    #f)))
   66            JUMP))
   67   
   68   (board-set! x y 0)
   69 )
   70 
   71 (define (start) 
   72    (format #t "Hello Jumper!\n")
   73    ;(display board)
   74    (jumpto 0 0 1)
   75    (format #t "I am done.\n"))
   76 
   77 (let ([start-sec (current-time)])
   78   (start)
   79   (format #t "Duration: ~a seconds.\n" (- (current-time) start-sec)))
   80 
   81 ; EOF