I found Project 1 to be one of the most challenging assignments in csc104, and so I figured i’d talk a bit about it here. Going around reading a few slogs from fellow students, I was relieved to know that I was not the only one who had struggled with the first project. There were many problems while trying to solve the code, but in the end when it all worked out without error it was a learning experience!
Let me take a problem me and my group had with the ball program. The key-press function was the reason of many confused looks from my group. The first thing we did was ask ourselves what the function wants the program to do or accomplish? After understanding the problem, we devised a plan - that plan was to first take a look at the check expect for the up and down keypresses, which we had trouble with.
(check-expect (key-press (make-ball 0 0 1 1) "down") (make-ball 0 0 1 2))
(check-expect (key-press (make-ball 0 0 1 1) "up") (make-ball 0 0 1 0))
(check-expect (key-press (make-ball 0 0 1 1) "left") (make-ball 0 0 0 1))
(check-expect (key-press (make-ball 0 0 1 1) "right") (make-ball 0 0 2 1))
From the check expect, we got a little relief looking at how the program is supposed to function with an arbitrary input. From the check-expect we observed that pressing the "down" arrow key required adding 1 to the dy value of the function make-ball, and pressing "up" on the arrow key requires the decreasing of 1 in the dy value of the function.
(meaning that make-ball changed its values from 0 0 1 1 to 0 0 1 0 when the up key was pressed). Key-press "left" and "right" were already given for the project.
The plan was to use the given "left" and "right" conditions as a template, and by doing that along with the knowledge from check-expect to solve the problem. What was left now was to just carry out the plan and call it a success (or failure, in which case we would have to go backward and make/revise a new plan after seeing why the original didn’t work)
This is the code we wrote following the steps:
(define (key-press b k)
(make-ball
(ball-x b)
(ball-y b)
(cond
[(equal? k "left") (- (ball-dx b) 1)]
[(equal? k "right") (+ (ball-dx b) 1)]
[else (ball-dx b)])
(cond
[(equal? k "up") (- (ball-dy b) 1)]
[(equal? k "down") (+ (ball-dy b) 1)]
[else (ball-dy b)])))
This was the result, and the final step that was left was to look back and make sure that the function was... well, “functioning” properly (bad pun). We made sure of that by running the program many times and trying out different keypresses of the arrow keys, making sure they live up to their function/use.
We saw that the function was working properly and had solved the problem. Hooray!, and off to MarkUs the program went.