Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.83 KB | None | 0 0
  1.  
  2.  
  3.  
  4. import java.util.Random;
  5. import java.util.ArrayList;
  6. import java.util.Arrays;
  7.  
  8. public class day9 {
  9. public static void main(String[] args) {
  10.  
  11. // Cars
  12. Car carro1 = new Car("Bob Barker Car", 0.3f, 0.65f, 0.1f, 0.0f, 0.0f, 0.95f, 0.11f, 0, 'B');
  13. Car carro2 = new Car("Pat Sajak Car", 0.45f, 0.85f, 0.3f, 0.0f, 0.0f, 0.78f, 0.08f, 0, 'P');
  14. Car carro3 = new Car("Alex Trebek Car", 0.29f, 0.60f, 0.02f, 0.0f, 0.0f, 0.88f, 0.19f, 0, 'A');
  15. Car carro4 = new Car("Drew Carey Car", 0.15f, 0.20f, 0.03f, 0.0f, 0.0f, 0.925f, 0.21f, 0, 'D');
  16. Car carro5 = new Car("Steve Harvey Car", 0.20f, 0.22f, 0.13f, 0.01f, 0.0f, 0.9135f, 0.19f, 0, 'S');
  17.  
  18.  
  19.  
  20. //Array of cars
  21. ArrayList<Car> cars = new ArrayList<Car>();
  22. cars.add(carro1);
  23. cars.add(carro2);
  24. cars.add(carro3);
  25. cars.add(carro4);
  26. cars.add(carro5);
  27.  
  28. boolean lastlap = false; //boolean is for the winner count of wins
  29.  
  30.  
  31. //Original instruction for race laps is the following:
  32. /*String Racetrack = "----------U-C-S-------C---S---------C--U----";
  33. int raceLength = Racetrack.length();*/
  34.  
  35. //The 2nd instruction is:"Adjust the code so that the race can be run 25
  36. //times to simulate an entire season. At the end of the season the car
  37. //with the most wins should be output along with the race wins total."
  38.  
  39. //This is the part I created to try and randomize the race tracks
  40. for(int k = 0; k < 25; k++)
  41. {
  42. //System.out.println("Run " + k);
  43.  
  44. for (int i = 0;i < 25;i++) {
  45. //System.out.println("test " + i);
  46. String Racetrack = "";
  47.  
  48. for (int j = 0 ;j<41;j++) {
  49. switch((int)(Math.random()*7))
  50. {
  51. case 0:
  52. Racetrack +="-";
  53. break;
  54. case 1:
  55. Racetrack +="-";
  56. break;
  57. case 2:
  58. Racetrack +="-";
  59. break;
  60. case 3:
  61. Racetrack +="-";
  62. break;
  63. case 4:
  64. Racetrack +="C";
  65. break;
  66. case 5:
  67. Racetrack +="U";
  68. break;
  69. case 6:
  70. Racetrack +="S";
  71. break;
  72.  
  73. }
  74. }
  75.  
  76.  
  77.  
  78. int raceLength = Racetrack.length();
  79.  
  80. /*dosomething(Racetrack);*/
  81. //generate track*/
  82.  
  83. boolean runningRace = true; // once someone wins we can stop this
  84. if(i == 24)
  85. {
  86. lastlap = true;
  87. }
  88. else
  89. lastlap = false;
  90.  
  91. running(cars, Racetrack, raceLength, runningRace, lastlap);
  92. //System.out.println("test " + i);
  93. }
  94. }
  95. int winner = 0;
  96. for(int p = 0; p < cars.size(); p++)
  97. {
  98. if(cars.get(winner).wins < cars.get(p).wins)
  99. {
  100. winner = p;
  101. }
  102. }
  103.  
  104. System.out.println();
  105. System.out.println();
  106. System.out.println();
  107.  
  108. System.out.println("The Tournanment winner is: " + cars.get(winner).name);
  109. System.out.println("total wins in tournament: " + cars.get(winner).wins );
  110. }
  111.  
  112. private static void running(ArrayList<Car> cars, String Racetrack, int raceLength, boolean runningRace, boolean lastlap) {
  113. while (runningRace) {
  114. System.out.println(Racetrack);
  115. for (int i = 0; i < cars.size(); i++) {
  116. //System.out.println("testR " + i);
  117. Car carro = cars.get(i);
  118. // Car 1
  119. // display the car
  120. String Progress = "";
  121. for (int j = 0; j < carro.raceProgress; j++)
  122. Progress += " ";
  123. System.out.println(Progress + carro.carSymbol);
  124.  
  125. // accelerate based on where you are on the track
  126. // Racetrack[(int)carro1RaceProgress)]
  127. switch (Racetrack.charAt((int) carro.raceProgress)) {
  128. case '-': // strait-away
  129. if (carro.currentSpeed < carro.topSpeed)
  130. carro.currentSpeed += carro.acceleration;
  131. if (carro.currentSpeed > carro.topSpeed)
  132. carro.currentSpeed = carro.topSpeed;
  133. break;
  134. case 'S': // Chicane
  135. if (carro.currentSpeed < carro.topSpeed * carro.handlingS)
  136. carro.currentSpeed += carro.acceleration;
  137. else if (carro.currentSpeed > carro.topSpeed * carro.handlingS)
  138. carro.currentSpeed = carro.topSpeed * carro.handlingS;
  139. break;
  140. case 'C': // curve
  141. if (carro.currentSpeed < carro.topSpeed * carro.handlingC)
  142. carro.currentSpeed += carro.acceleration;
  143. else if (carro.currentSpeed > carro.topSpeed * carro.handlingC)
  144. carro.currentSpeed = carro.topSpeed * carro.handlingC;
  145. break;
  146. case 'U': // Hairpin
  147. if (carro.currentSpeed < carro.topSpeed * carro.handlingU)
  148. carro.currentSpeed += carro.acceleration;
  149. else if (carro.currentSpeed > carro.topSpeed * carro.handlingU)
  150. carro.currentSpeed = carro.topSpeed * carro.handlingU;
  151. break;
  152. default:
  153. System.out.println("default " + i);
  154. break;
  155. }
  156. // increase progress
  157. carro.raceProgress += carro.currentSpeed;
  158.  
  159. if (carro.raceProgress >= raceLength) {
  160. if(lastlap)
  161. {
  162.  
  163. //System.out.println(carro.name + " wins race");
  164. //System.out.println("total wins in tournament: " + carro.wins );
  165. carro.wins++;
  166. for (int w =0; w < cars.size(); w++) {
  167. cars.get(w).raceProgress = 0;
  168.  
  169.  
  170. }
  171. }
  172. carro.raceProgress = 0;
  173. runningRace = false;
  174. //System.out.println("Stop ");
  175. }
  176. }
  177. }
  178. }
  179.  
  180. // new class for car
  181. public static class Car {
  182. String name;
  183. float handlingS;
  184. float handlingC;
  185. float handlingU;
  186. float raceProgress;
  187. float currentSpeed;
  188. float topSpeed;
  189. float acceleration;
  190. int wins;
  191. char carSymbol;
  192.  
  193. // method for car
  194. public Car(String carName, float carHandlingS, float carHandlingC, float carHandlingU, float carRaceProgress,
  195. float carCurrentSpeed, float carTopSpeed, float carAcceleration, int carWins, char carSymbol) {
  196. name = carName;
  197. handlingS = carHandlingS;
  198. handlingC = carAcceleration;
  199. handlingU = carHandlingU;
  200. raceProgress = carRaceProgress;
  201. this.currentSpeed = carCurrentSpeed;
  202. topSpeed = carTopSpeed;
  203. acceleration = carAcceleration;
  204. wins = carWins;
  205. this.carSymbol = carSymbol;
  206.  
  207. //FileInputStream
  208. //FileOututStream
  209. //try & catch walks
  210.  
  211. }
  212.  
  213. }
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement