CS 010 - Introduction to Computer Science I
Assignment 7:

DUE: March 6th 7:00pm


Collaboration Policy

Collaboration is strongly ENCOURAGED. You can work in teams, but programs must represent YOUR OWN original work. Teams can work on the algorithm together and help debug/test each others code. However, copying code from ANY source (any book, current or past students, past solutions, the web, etc.) is STRICTLY FORBIDDEN. Code between teamates might be similar, but you are not allowed to just copy a teamate's solution. For example, just changing the variable names, moving functions around, and changing the spacing to make the programs look different is considered cheating, and will result in at least a zero for the assignment.


Code that is turned in, must be contained in a .cpp file named main.cpp. Files of any other format will not be graded (e.g. main.doc, main.txt, etc…).

You must turn your work in from a lab computer on campus.

Turn in online to as7 folder. If you turn your assignment in to the wrong folder, your assignment may not be graded. If it is graded, you will lose 2 pts (out of 10).

Programs that do not compile will receive a zero.

Remember to include the following header information at the top of your program:

// Course: CS 10
//
// Lecture Section: ... 001 or 002
// Lab Section: ... 021, 022, etc)
//
// Assignment #: ... assignment 2, 3, etc.
//
// Last Name: Enter your LAST (family) name here (eg, Doe)
// First Name: Enter your FIRST (given) name here (eg, John)
//
// ID Number: Enter your ID number here (eg, 860-00-0000)
// lab login id: Enter your cs10 login here (eg, jdoe)
//
// Email address: Enter your UCR email address here (eg, jdoe@cs.ucr.edu)
//
// Teammates: List the names of the teammates you worked with

// =======================================================================



Problem Definition:

    For this assignment you will impliment the final moon lander game! To do this, you will need to add code to, and modify your work from programing assignment 6. Specifically, you will add a function that will draw your ship instead of a circle, modify your set acceleration function to set both x and y acceleration, add some code to ccc_win_main, create a collision detection function, add a function that draws a simple landscape, and add a function that draws an explosion based on the position of the ship. I know, it seems like a lot, but many of the above additions are trivial (e.g. using a function to draw your ship--you already did that in assignment 5, the explosion drawing function works just like the draw ship function, etc...).
    You will have to add variables to ccc_win_main for the x position, velocity, and acceleration, and you should change the constant CURRENT_X to a variable. You will also want to delete the constant for the circle's radius.
    You will have to modify your set_acceleration function so that now it takes two reference parameters (which will be the x, and y acceleration as doubles), and returns void. Now the user will be asked to set the main, right, and left thruster. Unlike the last assignment, now the user can choose Medium in addition to Hi, Low, or None. For the y acceleration, High should be equal to 1, Medium 0.5, Low to 0, and None to -1. Setting the right thruster to High should be equivalent to an acceleration of -1, Medium -0.5, Low -0.2, and None 0. Setting the left thruster to High should be equivalent to an acceleration of +1, Medium +0.5, Low +0.2, and None 0. If a selection is made for both the right AND left thruster, the resulting thrust should just be the sum of the two selections. For example if the user selects High acceleration for both the left, and the right thruster, this would equal a net x acceleration of zero.
    Movement to the left and right will be according to the same equations used in assignment 6. You will use the same functions that you wrote for assignment 6 to set the x position and velocity. You just need to call the same functions you wrote in assignment 6, using the new variables for x_pos, x_vel, etc....
The draw landscape function is passed nothing, and returns void, the draw explosion function is passed a point that represents the bottom center of your ship, and returns void. Keep your landscape simple, have no more than THREE places to land as in the example main below! This means there should be no more than 5 lines drawn in your landscape, and they should only be horizontal, and vertical.
    For the collision detection function, you will pass it a SINGLE Point object that represents the bottom center of the ship.  The function will then check to see if that point is inside your landscape, if it is, the function will return true, otherwise it will return false. The easiest way to do this is to break up your landscape into squares (as mentioned only draw your landscape as a sequance of horizontal lines, followed by vertical lines), the area under each horizontal line would constitute a square. Then, all you need to do is check if your point lies within any of the squares. For example; if the upper left corner of the square was at point (0,3500), and the lower right point of the square was at point (2000,0), to check if the point representing your ship was inside the box you would use the statement:
    if(x >= 0 && x <= 2000 && y <= 3500)
        {
            //inside the square
        }
    else
        {
            //outside the square
        }
This would need to be done for each square, so you will need to use a switch statement or an if-else if structure. Becuase this is a relatively simple way to check for collisions, it is OK if your ship *lands* on a wall (vertical line). Also, since the point representing your ship will be in the at the ship's center, it is OK if half your ship passes through a vertical line/wall before the collision is detected.
You do not need to worry about collision with the window's edge.
    You will then need to add more code your ccc_win_main function. The program should continue until the ship either lands, or explodes. At the point in ccc_win_main where you were previously drawing the circle, you will now need to call your collision detection function to decide weither you will draw the ship, or draw an explosion. If there is no collision you can just draw the ship, otherwise you need to check the ship's current velocity. If the ship is over some acceptable negative velocity, it has landed safely, otherwise KABOOM! Using the code that was given for assignment 6, I found that any velocity greater than or equal to -17.5 was acceptable but difficult to land. For an easier game (and testing my code), a velocity greater than or equal to -35 was reasonable.
    After the ship lands safely or explodes, you will use a break statement (page 137) to exit the main animation loop, this should cause the program to end.
    Lastly, you must provide in the header area of your code, a list of commands that will cause the ship to land safely on your landscape.  Write the commands in the format below:
/*
    other header info, name, etc...
    1) Main: <value>, Right: <value>, Left: <value>
    2) Main: <value>, Right: <value>, Left: <value>
    .
    .
    .
    n) Main: <value>, Right: <value>, Left: <value>
*/
To receive full credit, the ship must be able to land with less than 15 commands.  To receive extra credit, the ship must land in greater than or equal to 5 commands, and less than or equal to 10 commands (5 <= n <= 10).  Each set of three thruster values (main, right, left) is considered one command.

Hints:
Program incrementaly!  If you try to do all the above in one pass, and then compile your program, you will have a mess on your hands!  Break the above into smaller steps, and get each smaller step to work first.  Below is what I would do...
1)  Write the draw ship function first, and get the program to move your ship up and down instead of the circle.
2)  Next modify the acceleration function, and add the x_pos, x_vel, etc... variables to ccc_win_main to make the ship move left and right.
3)  Write the collision detection function, and your draw landscape function.  Write down on paper what the landscape looks like, and the Points that will be used to draw it. You will need these same values in your collision detection function.
4)  Write the draw explosion function.
5)  Modify the code in ccc_win_main to not just draw the ship at line 9, but detect if there is a collision.  Don't worry about checking the velocity, just have the program either draw the ship (no collision), or draw the explosion (collision).
6)  Once it is correctly detecting collision, add the code to check the ships velocity.  If the velocity is OK, break out of the loop, else draw an explosion, then break out of the loop.
7)  Start this as soon as possible! I suggest starting no later than Tuesday.

Solution to as6
main.cpp

Example Program Download
main



Rubric: (10 pts total)

1 pt:  Ship lands with less than 15 commands
1.5 pt:  Collision function detects collision with landscape and returns true
0.5 pt: Collision function returns false when there is no collision
0.5 pt: Landscape has no more than three landing pads
0.5 pt: Point that represents ship is located at the bottom center of ship (don't draw this point though)

0.5 pt: Correctly breaks out of animation loop when ship lands or explodes, otherwise continues
1 pt: On landing, draws ship when velocity is greater than or equal to -17.5 or -35
1 pt: Draws explosion when ship lands too fast
1 pt: Just draws ship when no collision is detected
1.5 pt: Program uses correct functions to draw ship, landscape, and explosion

1 pts: Style
- Good variable names
- Proper indentation and spacing
- Good comments
- No line wraps
- No magic numbers

Bonus 1 pt: Ship lands in greater than or equal to 5 commands, and less than or equal to 10 commands (5 <= n <= 10)