Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.38 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Random;
  3. public class ProjectilePROJECT {
  4.  
  5. //declaring static objects scan,random and creating array1 and array2
  6. static Scanner scan = new Scanner(System.in);
  7. static Random random = new Random();
  8. static String[] array1 = {"You win this round !","You made it!!!",
  9. "You are a good thrower", "You are a good mathematician"};
  10. static String [] array2 = {"You suck at this game","You should try other games",
  11. "I see you are bad in physics"};
  12.  
  13. /* declaring variables clearingDist,heightOfProjectile,wallHeight,speed,angle,mass,dragC
  14. userPoints,distFromWall,userChoice,element,element2*/
  15. static String userInput;
  16. static double clearingDist,heightOfProjectile,wallHeight,speed,angle,mass,dragC;
  17. static int userPoints,distFromWall,userChoice,element,element2;
  18. static final double g = 9.8;
  19.  
  20. // main method for the game
  21. public static void main(String[] args) {
  22.  
  23. System.out.println("*** WELCOME TO PROJECTILES GAME ***");
  24. System.out.println("Type 'quit' or any string to end game anytime");
  25.  
  26. // userPoints,the initial Life of the user
  27. userPoints = 10;
  28. do {
  29. element = random.nextInt(array1.length);
  30. element2 = random.nextInt(array2.length);
  31. // try catch end game when user enter quit/ any string
  32. try {
  33. StartGame();
  34. SelectGame();
  35. }catch(Exception e) {
  36. userInput="quit";
  37. }
  38.  
  39. }
  40. while((!userInput.equalsIgnoreCase("quit")) && (userPoints > 0));
  41. System.out.println("Game Ended");
  42.  
  43.  
  44. }// end main
  45. // method to start the game
  46. public static void StartGame() {
  47. // printing out the instructions
  48. System.out.println("Select :\n 1.Instructions of the game. ");
  49. SelectGame();
  50. switch(userChoice) {
  51. case 1 : userChoice = 1;
  52. System.out.println("================================================================\n");
  53. Loading(100);
  54. System.out.println(" ***INSTRUCTIONS**** ");
  55. Loading(200);
  56. System.out.println(" 1.Aim of game is to send stuntman as over the wall.");
  57. Loading(300);
  58. System.out.println(" 2.Each launch costs 1 point.");
  59. Loading(400);
  60. System.out.println(" 3.A close clear by less 3 metres gets 5 points.");
  61. Loading(500);
  62. System.out.println(" 4.A far clear by more than 3 metres gets +2 points net.");
  63. Loading(600);
  64. System.out.println(" 5.A near miss by less than 3 metres loses 1 point net.");
  65. Loading(700);
  66. System.out.println(" 6.A far miss by more than 3 metres loses 3 points net.");
  67. Loading(800);
  68. System.out.println("================================================================");
  69. break;
  70.  
  71. case 2 : userChoice = 2;
  72. System.out.println("***PLAYING IN NO WIND MODE***");
  73. //getUserInput();
  74. for(int i =1;i <=3;i++) {
  75. Loading(300);
  76. System.out.printf("****Note: This is trial %s of your three trials.****\n",i);
  77. Loading(400);
  78. getUserVals();
  79.  
  80. getFeedBack(calcHeightNoWindMode() , wallHeight);
  81. }
  82. System.out.println("To continue type 'enter' or 'quit' to end game!!! ");
  83. userInput = scan.next();
  84. launchLoss();
  85. break;
  86.  
  87. case 3 : userChoice = 3;
  88. System.out.println("***PLAYING IN WIND MODE***");
  89. for(int i =1;i <=3;i++) {
  90. Loading(800);
  91. System.out.printf(" ***Note: This is trial %s of your three trials.****\n",i);
  92. getUserVals();
  93. System.out.println("Enter drag coefficient of medium in range (5000-10000): ");
  94. dragC = scan.nextDouble();
  95. System.out.println("Please enter mass of the projectile: ");
  96. mass = scan.nextDouble();
  97. getFeedBack(calcHeightWindMode(), wallHeight);
  98. System.out.println("***************************************************************");
  99. System.out.println("IF DRAG IS NEGLICTED,PROJECTILE HEIGHT IS: " + calcHeightNoWindMode());
  100. System.out.println("***************************************************************");
  101. }
  102. launchLoss();
  103. break;
  104. default :
  105. System.out.println("Invalid input");
  106. break;
  107.  
  108. }
  109. }
  110. // getting feeding depending on values heightOfProjectile,wallHeight
  111. public static void getFeedBack(double heightOfProjectile, double wallHeight) {
  112. clearingDist = heightOfProjectile - wallHeight;
  113. if (heightOfProjectile > wallHeight) {
  114. System.out.println(array1[element]);
  115. if (clearingDist <= 3 ) {
  116. System.out.println("===================================================");
  117. Loading(300);
  118. System.out.println(array1[element]);
  119. Loading(400);
  120. System.out.printf("Over the wall by a few %s metres: ",clearingDist);
  121. Loading(500);
  122. System.out.println(array1[element]);
  123. System.out.println("Height of Projectile :" + heightOfProjectile);
  124. userPoints +=5;
  125. System.out.println("===================================================");
  126. }
  127.  
  128. else if( clearingDist > 3){
  129.  
  130. System.out.printf("Over the wall by %s metres.\n",clearingDist);
  131. System.out.println("Height of Projectile :" + heightOfProjectile);
  132. userPoints += 2 ;
  133. }
  134.  
  135. }
  136. else {
  137. clearingDist= Math.abs(clearingDist);
  138. if(clearingDist <= 3) {
  139. System.out.println("===================================================");
  140. System.out.println(array2[element2]);
  141. System.out.println("Projectile not quite over");
  142. Loading(500);
  143. System.out.printf("Missed the wall by %s\n",clearingDist);
  144. System.out.println("Height of Projectile :" + heightOfProjectile);
  145. System.out.println(array2[element2]);
  146. userPoints -= 1;
  147. System.out.println("===================================================");
  148.  
  149. }
  150. else {
  151. System.out.println("===================================================");
  152. System.out.println(array2[element2]);
  153. System.out.println("Projectile not even close");
  154. Loading(400);
  155. System.out.printf("Missed the wall by %s metres\n",clearingDist);
  156. System.out.println("Height of Projectile :" + heightOfProjectile);
  157. userPoints -= 3;
  158. System.out.println(array2[element2]);
  159. System.out.println("===================================================");
  160. }
  161. }
  162.  
  163. System.out.println("Points for this Round :" + userPoints);
  164. Loading(800);
  165.  
  166. }//end getFeedBack
  167. // method to calculate heightofProjectile in No wind Mode
  168. public static double calcHeightNoWindMode() {
  169. // calculating the height of projectile in no windMode
  170. double a,c;
  171. a = Math.tan(angle);
  172. c = -g /(2*Math.pow((speed*Math.cos(angle)),2));
  173. heightOfProjectile = distFromWall* a + Math.pow(distFromWall,2)*c;
  174.  
  175. return heightOfProjectile;
  176. }
  177.  
  178. // method to ask for the launch angle,projectile speed
  179. public static void getUserVals() {
  180. wallHeight = random.nextInt(30) + 1 ;
  181. distFromWall= random.nextInt(41) + 1;
  182. System.out.println("===================================================");
  183. Loading(200);
  184. System.out.println("Wall height : "+ wallHeight + " Distance from the wall: " + distFromWall );
  185. Loading(300);
  186. System.out.println("===================================================");
  187. Loading(400);
  188. System.out.println("Please enter speed of your projectile : ");
  189. speed = scan.nextDouble();
  190. Loading(500);
  191. System.out.println("Enter the angle of projectile in degrees : ");
  192. angle = Math.toRadians(scan.nextInt());
  193.  
  194.  
  195. }
  196. // method to decrease the points of user per each launch
  197. public static void launchLoss() {
  198. userPoints--;
  199. System.out.println("You've lost one point for your launch.");
  200. }
  201. // method to calculate terminal velocity to use in Wind Mode
  202. public static double terminalVelocity() {
  203. return ((mass*g) /dragC);
  204. }
  205. //timeWind is used in Wind Mode
  206. public static double timeWind() {
  207. return (Math.sqrt(2*dragC/g));
  208. }
  209. // calculating height of projectile in wind mode
  210. public static double calcHeightWindMode() { //calculates the height of the catapult in wind mode
  211. double x, y, z, b;
  212. x=(terminalVelocity()/g);
  213. y=(speed *Math.sin(angle))+terminalVelocity();
  214. z= 1-(Math.exp(-g*timeWind()/terminalVelocity()));
  215. b=Math.pow(terminalVelocity(), timeWind());
  216. heightOfProjectile = (x*y*z)-b;
  217. return heightOfProjectile;
  218. }
  219. // method which sleep program for number of milliseconds
  220. public static void Loading(int n) {
  221. try
  222. {
  223. Thread.sleep(n);
  224. }
  225. catch (InterruptedException e) {
  226. System.out.println("Exception error thrown!");
  227. }
  228.  
  229. }
  230. // selecting game
  231. public static void SelectGame() {
  232. Loading(800);
  233. System.out.println(" 2.Play in No Wind Mode");
  234. System.out.println(" 3.Play in Wind Mode");
  235. userChoice = scan.nextInt();
  236.  
  237. }
  238.  
  239.  
  240.  
  241.  
  242. }// end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement