Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Random;
- public class ProjectilePROJECT {
- //declaring static objects scan,random and creating array1 and array2
- static Scanner scan = new Scanner(System.in);
- static Random random = new Random();
- static String[] array1 = {"You win this round !","You made it!!!",
- "You are a good thrower", "You are a good mathematician"};
- static String [] array2 = {"You suck at this game","You should try other games",
- "I see you are bad in physics"};
- /* declaring variables clearingDist,heightOfProjectile,wallHeight,speed,angle,mass,dragC
- userPoints,distFromWall,userChoice,element,element2*/
- static String userInput;
- static double clearingDist,heightOfProjectile,wallHeight,speed,angle,mass,dragC;
- static int userPoints,distFromWall,userChoice,element,element2;
- static final double g = 9.8;
- // main method for the game
- public static void main(String[] args) {
- System.out.println("*** WELCOME TO PROJECTILES GAME ***");
- System.out.println("Type 'quit' or any string to end game anytime");
- // userPoints,the initial Life of the user
- userPoints = 10;
- do {
- element = random.nextInt(array1.length);
- element2 = random.nextInt(array2.length);
- // try catch end game when user enter quit/ any string
- try {
- StartGame();
- SelectGame();
- }catch(Exception e) {
- userInput="quit";
- }
- }
- while((!userInput.equalsIgnoreCase("quit")) && (userPoints > 0));
- System.out.println("Game Ended");
- }// end main
- // method to start the game
- public static void StartGame() {
- // printing out the instructions
- System.out.println("Select :\n 1.Instructions of the game. ");
- SelectGame();
- switch(userChoice) {
- case 1 : userChoice = 1;
- System.out.println("================================================================\n");
- Loading(100);
- System.out.println(" ***INSTRUCTIONS**** ");
- Loading(200);
- System.out.println(" 1.Aim of game is to send stuntman as over the wall.");
- Loading(300);
- System.out.println(" 2.Each launch costs 1 point.");
- Loading(400);
- System.out.println(" 3.A close clear by less 3 metres gets 5 points.");
- Loading(500);
- System.out.println(" 4.A far clear by more than 3 metres gets +2 points net.");
- Loading(600);
- System.out.println(" 5.A near miss by less than 3 metres loses 1 point net.");
- Loading(700);
- System.out.println(" 6.A far miss by more than 3 metres loses 3 points net.");
- Loading(800);
- System.out.println("================================================================");
- break;
- case 2 : userChoice = 2;
- System.out.println("***PLAYING IN NO WIND MODE***");
- //getUserInput();
- for(int i =1;i <=3;i++) {
- Loading(300);
- System.out.printf("****Note: This is trial %s of your three trials.****\n",i);
- Loading(400);
- getUserVals();
- getFeedBack(calcHeightNoWindMode() , wallHeight);
- }
- System.out.println("To continue type 'enter' or 'quit' to end game!!! ");
- userInput = scan.next();
- launchLoss();
- break;
- case 3 : userChoice = 3;
- System.out.println("***PLAYING IN WIND MODE***");
- for(int i =1;i <=3;i++) {
- Loading(800);
- System.out.printf(" ***Note: This is trial %s of your three trials.****\n",i);
- getUserVals();
- System.out.println("Enter drag coefficient of medium in range (5000-10000): ");
- dragC = scan.nextDouble();
- System.out.println("Please enter mass of the projectile: ");
- mass = scan.nextDouble();
- getFeedBack(calcHeightWindMode(), wallHeight);
- System.out.println("***************************************************************");
- System.out.println("IF DRAG IS NEGLICTED,PROJECTILE HEIGHT IS: " + calcHeightNoWindMode());
- System.out.println("***************************************************************");
- }
- launchLoss();
- break;
- default :
- System.out.println("Invalid input");
- break;
- }
- }
- // getting feeding depending on values heightOfProjectile,wallHeight
- public static void getFeedBack(double heightOfProjectile, double wallHeight) {
- clearingDist = heightOfProjectile - wallHeight;
- if (heightOfProjectile > wallHeight) {
- System.out.println(array1[element]);
- if (clearingDist <= 3 ) {
- System.out.println("===================================================");
- Loading(300);
- System.out.println(array1[element]);
- Loading(400);
- System.out.printf("Over the wall by a few %s metres: ",clearingDist);
- Loading(500);
- System.out.println(array1[element]);
- System.out.println("Height of Projectile :" + heightOfProjectile);
- userPoints +=5;
- System.out.println("===================================================");
- }
- else if( clearingDist > 3){
- System.out.printf("Over the wall by %s metres.\n",clearingDist);
- System.out.println("Height of Projectile :" + heightOfProjectile);
- userPoints += 2 ;
- }
- }
- else {
- clearingDist= Math.abs(clearingDist);
- if(clearingDist <= 3) {
- System.out.println("===================================================");
- System.out.println(array2[element2]);
- System.out.println("Projectile not quite over");
- Loading(500);
- System.out.printf("Missed the wall by %s\n",clearingDist);
- System.out.println("Height of Projectile :" + heightOfProjectile);
- System.out.println(array2[element2]);
- userPoints -= 1;
- System.out.println("===================================================");
- }
- else {
- System.out.println("===================================================");
- System.out.println(array2[element2]);
- System.out.println("Projectile not even close");
- Loading(400);
- System.out.printf("Missed the wall by %s metres\n",clearingDist);
- System.out.println("Height of Projectile :" + heightOfProjectile);
- userPoints -= 3;
- System.out.println(array2[element2]);
- System.out.println("===================================================");
- }
- }
- System.out.println("Points for this Round :" + userPoints);
- Loading(800);
- }//end getFeedBack
- // method to calculate heightofProjectile in No wind Mode
- public static double calcHeightNoWindMode() {
- // calculating the height of projectile in no windMode
- double a,c;
- a = Math.tan(angle);
- c = -g /(2*Math.pow((speed*Math.cos(angle)),2));
- heightOfProjectile = distFromWall* a + Math.pow(distFromWall,2)*c;
- return heightOfProjectile;
- }
- // method to ask for the launch angle,projectile speed
- public static void getUserVals() {
- wallHeight = random.nextInt(30) + 1 ;
- distFromWall= random.nextInt(41) + 1;
- System.out.println("===================================================");
- Loading(200);
- System.out.println("Wall height : "+ wallHeight + " Distance from the wall: " + distFromWall );
- Loading(300);
- System.out.println("===================================================");
- Loading(400);
- System.out.println("Please enter speed of your projectile : ");
- speed = scan.nextDouble();
- Loading(500);
- System.out.println("Enter the angle of projectile in degrees : ");
- angle = Math.toRadians(scan.nextInt());
- }
- // method to decrease the points of user per each launch
- public static void launchLoss() {
- userPoints--;
- System.out.println("You've lost one point for your launch.");
- }
- // method to calculate terminal velocity to use in Wind Mode
- public static double terminalVelocity() {
- return ((mass*g) /dragC);
- }
- //timeWind is used in Wind Mode
- public static double timeWind() {
- return (Math.sqrt(2*dragC/g));
- }
- // calculating height of projectile in wind mode
- public static double calcHeightWindMode() { //calculates the height of the catapult in wind mode
- double x, y, z, b;
- x=(terminalVelocity()/g);
- y=(speed *Math.sin(angle))+terminalVelocity();
- z= 1-(Math.exp(-g*timeWind()/terminalVelocity()));
- b=Math.pow(terminalVelocity(), timeWind());
- heightOfProjectile = (x*y*z)-b;
- return heightOfProjectile;
- }
- // method which sleep program for number of milliseconds
- public static void Loading(int n) {
- try
- {
- Thread.sleep(n);
- }
- catch (InterruptedException e) {
- System.out.println("Exception error thrown!");
- }
- }
- // selecting game
- public static void SelectGame() {
- Loading(800);
- System.out.println(" 2.Play in No Wind Mode");
- System.out.println(" 3.Play in Wind Mode");
- userChoice = scan.nextInt();
- }
- }// end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement