;; The first three lines of this file were inserted by DrRacket. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname week10_f_full) (read-case-sensitive #t) (teachpacks ((lib "image.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "image.ss" "teachpack" "htdp"))))) (require "snake-lib.rkt") (require "graphs.rkt") (define AUTO true) ;; advance-game : game -> game (define (advance-game g) (cond [AUTO (advance-game-auto g)] [else (advance-game-manual g)])) (define (advance-game-auto g) (local [(define bp (best-path g) )] (cond [(false? bp) (advance-game-manual g)] [else (second bp)]))) (define (best-path-try g) (find-shortest-path-to-cond (make-graph empty autosnake-neighbor (lambda (x y) false)) g (lambda (g2) (> (game-score g2) (game-score g))))) ; best-path game -> list-of-game ; returns the "best" path through games from the passed-in game (define (best-path g) (find-shortest-path-to-cond (make-graph empty autosnake-neighbor (lambda (x y) false)) g (lambda (g2) (or (> (game-score g2) (game-score g)) (and (not (game-over? g2)) (< (food-dist g2) (- (food-dist g) 2))) (> (game-ticks g2) (+ (game-ticks g) 3)))))) ; autosnake-neighbor : game -> list-of-game ; returns all games reachable from this game with a change of direction (define (autosnake-neighbor g) (cond [(game-over? g) empty] [else (map (lambda (x) (advance-game-manual (change-direction g x))) (list 'up 'down 'left 'right))])) ; dist : posn posn -> number ; returns distance between points (define (dist a b) (sqrt (+ (sqr (- (posn-x a) (posn-x b))) (sqr (- (posn-y a) (posn-y b)))))) ; food-dist : game -> number ; returns the distance from the snake to food (define (food-dist g) (cond[(empty? (game-food g)) 0] [else (argmin abs (map (lambda (x) (dist (first (snake-segments (game-snake g))) x)) (game-food g)))]))