Lab 5
Start Now

Overview

For this program you will be making a game that calculates and shows a cannon / shooting a cannonball . at a target *. You will be using built in java Math methods as well as a professor-built method to handle the drawing of the "ascii" scene.

Assignment

The scene is arranged using a Cartesian coordinate system with cannonballX on the x-axis and cannonballY on the y-axis. Both variables are integers, and cannonballX is valid from 0 to 50 and cannonballY is valid from 0 to 15. The cannon is located at cannonballY = 1 and cannonballX = 0. You will place the target randomly on the x-axis with cannonballX between 1 and 50 and cannonballY = 0. The cannonball will be placed by you according to the calculated physics (see below).

Documentation for the professor-provided method for drawing is shown below:

    /**
     * Prints an ascii scene with a cannon on the left, the cannonball, and the target.
     * @param angle An integer value representing the angle of the cannon. 0 prints _,
     *              1 prints /, and 2 prints |
     * @param cannonballX horizontal position of the cannonball
     * @param cannonballY vertical position of the cannonball
     * @param targetX horizontal position of the target
     */
    public static void printScene(int angle, int cannonballX, int cannonballY, int targetX)

You should pass a 0, 1, or 2 as the angle where 0 will print _, 1 will print /, and 2 will print |. These should correspond generally to the angle the cannon is pointed. The user should be able to select the angle of the cannon in degrees (between 1 and 89 where 1 is pointed almost completely horizontally and 89 is almost vertical). After selecting the cannon angle, you should step through the simulation of the cannonball flying through the air and landing. To step through, you should ask the user to "hit enter to continue..." to trigger the next step. The trajectory ends when the cannonball hits the ground (cannonballY = 0).

To calculate the location of the cannonball, we need to calculate the velocity of the cannonball in both the x and y directions. These velocities will depend on the cannon angle, and they can be calculated for our situation by these simplified equations (where numSteps is the number of steps the simulation has completed):

xvelocity = cos(angle) * 3

yvelocity = sin(angle) - 0.07 * numSteps

Java's Math API provides the trig functions you will need for this, but be warned that they expect radians not degrees as an input!

To calculate the updated position, you should keep in mind the equation:

newPosition = previousPosition + curentVelocity

Lastly, you should tell the user whether or not they hit the target.

The following are some example executions of an implementation of the ascii cannon.

The first view of the game:

















/                                                    
                    *                              

Please enter the angle in degrees you want to shoot (1-89): 

Mid game:
















               .                                   

/                                                  
                    *                              
Press enter to continue...

The last view of a missed shot:


















/                                  .               
                    *                              
Press enter to continue...
MISS :(
Do you want to continue? Enter 1 for yes and 2 for no: 

The last view of a hit shot:


















/                                  .               
                                   *                              
Press enter to continue...
HIT!!!!!!!
Do you want to continue? Enter 1 for yes and 2 for no: 

Requirements

  • Use the Lab5.java as a starting point for your code. You will need to change the package declaration and add your main method.
  • The program must be created in a class named Lab5 that is in a package named the same as your MSOE username.
  • Your program must not crash if the user enters valid input. The program should accept valid numbers for the angle and any integer for the play-again question. If the user enters an unexpected integer option, the program should report the error professionally and reprint the prompt to play again.

Lab Deliverables

See your professor's instructions for details on submission guidelines and due dates.
Prof. Contino's class: See Blackboard
Prof. Jones' class: See Blackboard
See Dr. Dennis for instructions
Dr. Riley's class: See Blackboard
Dr. Taylor's class: See below
See Prof. Thomas for instructions
If you have any questions, consult your instructor.

Acknowledgement

This laboratory assignment was developed by Dr. Derek Riley.

Wednesday, 28-Sep-2016 15:56:24 CDT