#lang racket ; jumpsaround - jumper game ; (c) 2020 Alexander Kulbartsch (define SIZEX 6) ;6 (define SIZEY 5) ;5 (define JUMP '{{-1 -2} {1 -2} {-2 -1} {2 -1} {-2 1} {2 1} {-1 2} {1 2}} ) (define board (make-vector (* SIZEX SIZEY) 0)) (define solutions 0) (define roundtrips 0) (define (board-ref x y) (vector-ref board (+ x (* y SIZEX)))) (define (board-set! x y v) (vector-set! board (+ x (* y SIZEX)) v)) (define (printboard rt) (printf "===== Solution # ~a - Roundtripps: ~a ====== \n" solutions roundtrips) (if rt (printf " *** Roundtrip *** ") #f) (for ([i SIZEY]) (for ([j SIZEX]) (printf "~a:" (board-ref j i))) (printf "\n")) (printf "\n")) (define (jumpto x y iter) (board-set! x y iter) ;check if finished (if (>= iter (* SIZEX SIZEY)) (begin ;check for roundtrips (let ([round #f]) (map (lambda (d) (let ([nx (+ x (car d))] [ny (+ y (cadr d))]) (if (and (not round) (>= nx 0) (< nx SIZEX) (>= ny 0) (< ny SIZEY)) (if (= (board-ref nx ny) 1) (begin (set! roundtrips (+ roundtrips 1)) (set! round #t)) #f ) #f ))) JUMP) (set! solutions (+ solutions 1)) (printboard round))) ; else try next (map (lambda (d) (let ([nx (+ x (car d))] [ny (+ y (cadr d))]) (if (and (>= nx 0) (< nx SIZEX) (>= ny 0) (< ny SIZEY)) (if (= (board-ref nx ny) 0) (begin ; (display nx) (jumpto nx ny (add1 iter)) ) #f) #f))) JUMP)) (board-set! x y 0) ) (define (start) (printf "Hello Jumper!\n") ;(display board) (jumpto 0 0 1) (printf "I am done.\n")) (let ([start-sec (current-inexact-milliseconds)]) (start) (printf "Duration: ~a seconds.\n" (/ (- (current-inexact-milliseconds) start-sec) 1000))) ; EOF