;; 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-advanced-reader.ss" "lang")((modname battleship) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #t #t none #f ()))) (require 2htdp/image) ;; A home-view is a list-of-boat ;; where the boats do not overlap ;; A boat is a list-of-peg ;; where the pegs are adjacent and in a row/column ;; A hpeg is ;; (make-hpeg posn boolean) ;; where posn is on the board, indexing from 0 ;; and the boolean is untouched (false) or hit (true) (define-struct hpeg (loc hit?)) ;; An away-view is a list-of-apeg ;; An apeg is ;; (make-apeg posn boolean) ;; where posn is on the board, indexing from 0 ;; and the boolean is miss (false) or hit (true) (define-struct apeg (loc hit?)) ;; A board is ;; (make-board away-view home-view) (define-struct board (away home)) (define COLS 10) (define ROWS 10) (define boat-2 (list (make-hpeg (make-posn 5 6) #f) (make-hpeg (make-posn 5 7) #f))) (define boat-3 (list (make-hpeg (make-posn 0 0) #f) (make-hpeg (make-posn 1 0) #f) (make-hpeg (make-posn 2 0) #f))) (define home-view-1 (list boat-2 boat-3)) (define away-view-1 (list (make-apeg (make-posn 0 0) #f) (make-apeg (make-posn 0 1) #f) (make-apeg (make-posn 0 2) #t))) ;; Todo: ;; GUI .... board -> scene ;; board generation... list-of-num -> board ;; opponent-hits? .... board -> response ;; computer player .... ;; n-copies : nat (image image -> image) image (define (n-copies n combine i) (cond [(zero? n) empty-image] [else (combine i (n-copies (sub1 n) combine i))])) (define CELL-SIZE 16) (define gray-peg (circle (/ CELL-SIZE 3) "solid" "gray")) (define plain-cell (overlay gray-peg (rectangle CELL-SIZE CELL-SIZE "solid" "blue"))) (define red-peg (circle (/ CELL-SIZE 3) "solid" "red")) (define white-peg (circle (/ CELL-SIZE 3) "solid" "white")) (define boat-cell (rectangle CELL-SIZE CELL-SIZE "solid" "gray")) (define transparent-view (rectangle (* COLS CELL-SIZE) (* ROWS CELL-SIZE) "outline" "blue")) (define plain-view (n-copies ROWS above (n-copies COLS beside plain-cell))) ;; draw-posn : posn image image -> image ;; To put the image i+ci at the right relative location ;; in the result image (define (draw-posn p i ci) (overlay/xy (overlay i ci) (- (* CELL-SIZE (posn-x p))) (- (* CELL-SIZE (posn-y p))) transparent-view)) ;; draw-apeg : apeg -> image (define (draw-apeg a) (draw-posn (apeg-loc a) (if (apeg-hit? a) red-peg white-peg) plain-cell)) ;; draw-hpeg : hpeg -> image (define (draw-hpeg a) (draw-posn (hpeg-loc a) (if (hpeg-hit? a) red-peg gray-peg) boat-cell)) ;; draw-list : list-of-X image (X -> image) -> image (define (draw-list draw-elem base-view a) (cond [(empty? a) base-view] [(cons? a) (overlay (draw-elem (first a)) (draw-list draw-elem base-view (rest a)))])) ;; draw-away : away-view -> image (define (draw-away a) (draw-list draw-apeg plain-view a)) ;; draw-boat : boat -> image (define (draw-boat b) (draw-list draw-hpeg transparent-view b)) ;; draw-home : home-view -> image (define (draw-home h) (draw-list draw-boat plain-view h)) ;; draw-board : board -> image (define (draw-board b) (above (draw-away (board-away b)) (rectangle 10 10 "solid" "white") (draw-home (board-home b)))) (draw-board (make-board away-view-1 home-view-1))