[plt-scheme] thanks, but help again
2004-03-30 10:27:11 Connor Ferguson£º
>
>Sorry for the delay. Things have been really hectic for me the past few
>days.
>
>Ok, here are my data definitions for UFO and shot/f
>
>;; a UFO is a shape
>;; (make-ufo p l w c)
>;; where p is a posn, l is a number, w is a number
>;; and c is a symbol
>
>;; a shot is a stucture
>;;(make-shot start l w col)
>;; where start is a posn (the position of the AUP)
>;; l is a number, w is a number, and col is a symbol
>
>;; a shot/f is either
>;; a shot or;
>;; false
>
>Like Matthias said, I altered hit-shot? so that there are no numbers, only
>constants.
>
>;; hit-shot? : shot/f ufo -> boolean
>(define (hit-shot? shot/f ufo)
> (cond
> [ (boolean? shot/f) false]
> [ else
> (cond
> [ (and (>= (+ (posn-x (ufo-nw ufo)) (ufo-len ufo))
> (posn-x (shot-posn shot/f)))
> (>= (+ (posn-x (ufo-nw ufo)) (ufo-len ufo)
> (+ (posn-x (shot-posn shot/f)) (shot-l shot/f))
> (<= (posn-x (ufo-nw ufo))
> (+ (posn-x (shot-posn shot/f)) (shot-l shot/f))
> (>= (posn-y (ufo-nw ufo))
> (posn-y (shot-posn shot/f)))
> (>= (posn-y (ufo-nw ufo))
> (+ (posn-y (shot-posn shot/f)) (shot-w shot/f)))) true]
> [ else false])]))
>
>I had been testing the program only by running it in the context of the
>game, but that is not something I should not do, so I formulated these
>tests.
>
>(hit-shot? (make-shot (make-posn 10 10) 2 5 'red)
> (make-ufo (make-posn 10 10) 22 4 'green))
>= false
>
>(hit-shot? (make-shot (make-posn 30 30) 2 5 'red)
> (make-ufo (make-posn 10 10) 22 4 'green))
>= false
>
>(hit-shot? (make-shot (make-posn 100 100) 2 5 'red)
> (make-ufo (make-posn 10 10) 22 4 'green))
>= false
>
>Clearly, these examples demonstrate the problem I¹m having. Basically,
>everything I put into the program is evaluating to false.
>
>Thank you for helping and sorry for taking so long to respond.
>
>-Connor
>
I have to say that your code is rather poor. The parentheses can't
pair; the names in the structure seems different from you defination;
there is one redundant cond expression.
I think that the hole game didn't use anything beyond the Beginner
language level. So you can test it with the stepper yourself.
Here I revise and test the code, and find that the main bug is in the
forth clause of the and expression. It should be a <= rather than >= .
The following code may work.
;; hit-shot? : shot/f ufo -> boolean
(define (hit-shot? shot/f ufo)
(cond
[(boolean? shot/f) false]
[else
(and (>= (+ (posn-x (ufo-nw ufo)) (ufo-len ufo))
(posn-x (shot-posn shot/f)))
(<= (posn-x (ufo-nw ufo))
(+ (posn-x (shot-posn shot/f)) (shot-l shot/f)))
(>= (posn-y (ufo-nw ufo))
(posn-y (shot-posn shot/f)))
(<= (posn-y (ufo-nw ufo))
(+ (posn-y (shot-posn shot/f)) (shot-w shot/f))))]))
--
Zhu Chongkai