Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.util.ArrayList;
- import java.util.Arrays;
- public class day9 {
- public static void main(String[] args) {
- // Cars
- Car carro1 = new Car("Bob Barker Car", 0.3f, 0.65f, 0.1f, 0.0f, 0.0f, 0.95f, 0.11f, 0, 'B');
- Car carro2 = new Car("Pat Sajak Car", 0.45f, 0.85f, 0.3f, 0.0f, 0.0f, 0.78f, 0.08f, 0, 'P');
- Car carro3 = new Car("Alex Trebek Car", 0.29f, 0.60f, 0.02f, 0.0f, 0.0f, 0.88f, 0.19f, 0, 'A');
- Car carro4 = new Car("Drew Carey Car", 0.15f, 0.20f, 0.03f, 0.0f, 0.0f, 0.925f, 0.21f, 0, 'D');
- Car carro5 = new Car("Steve Harvey Car", 0.20f, 0.22f, 0.13f, 0.01f, 0.0f, 0.9135f, 0.19f, 0, 'S');
- //Array of cars
- ArrayList<Car> cars = new ArrayList<Car>();
- cars.add(carro1);
- cars.add(carro2);
- cars.add(carro3);
- cars.add(carro4);
- cars.add(carro5);
- boolean lastlap = false; //boolean is for the winner count of wins
- //Original instruction for race laps is the following:
- /*String Racetrack = "----------U-C-S-------C---S---------C--U----";
- int raceLength = Racetrack.length();*/
- //The 2nd instruction is:"Adjust the code so that the race can be run 25
- //times to simulate an entire season. At the end of the season the car
- //with the most wins should be output along with the race wins total."
- //This is the part I created to try and randomize the race tracks
- for(int k = 0; k < 25; k++)
- {
- //System.out.println("Run " + k);
- for (int i = 0;i < 25;i++) {
- //System.out.println("test " + i);
- String Racetrack = "";
- for (int j = 0 ;j<41;j++) {
- switch((int)(Math.random()*7))
- {
- case 0:
- Racetrack +="-";
- break;
- case 1:
- Racetrack +="-";
- break;
- case 2:
- Racetrack +="-";
- break;
- case 3:
- Racetrack +="-";
- break;
- case 4:
- Racetrack +="C";
- break;
- case 5:
- Racetrack +="U";
- break;
- case 6:
- Racetrack +="S";
- break;
- }
- }
- int raceLength = Racetrack.length();
- /*dosomething(Racetrack);*/
- //generate track*/
- boolean runningRace = true; // once someone wins we can stop this
- if(i == 24)
- {
- lastlap = true;
- }
- else
- lastlap = false;
- running(cars, Racetrack, raceLength, runningRace, lastlap);
- //System.out.println("test " + i);
- }
- }
- int winner = 0;
- for(int p = 0; p < cars.size(); p++)
- {
- if(cars.get(winner).wins < cars.get(p).wins)
- {
- winner = p;
- }
- }
- System.out.println();
- System.out.println();
- System.out.println();
- System.out.println("The Tournanment winner is: " + cars.get(winner).name);
- System.out.println("total wins in tournament: " + cars.get(winner).wins );
- }
- private static void running(ArrayList<Car> cars, String Racetrack, int raceLength, boolean runningRace, boolean lastlap) {
- while (runningRace) {
- System.out.println(Racetrack);
- for (int i = 0; i < cars.size(); i++) {
- //System.out.println("testR " + i);
- Car carro = cars.get(i);
- // Car 1
- // display the car
- String Progress = "";
- for (int j = 0; j < carro.raceProgress; j++)
- Progress += " ";
- System.out.println(Progress + carro.carSymbol);
- // accelerate based on where you are on the track
- // Racetrack[(int)carro1RaceProgress)]
- switch (Racetrack.charAt((int) carro.raceProgress)) {
- case '-': // strait-away
- if (carro.currentSpeed < carro.topSpeed)
- carro.currentSpeed += carro.acceleration;
- if (carro.currentSpeed > carro.topSpeed)
- carro.currentSpeed = carro.topSpeed;
- break;
- case 'S': // Chicane
- if (carro.currentSpeed < carro.topSpeed * carro.handlingS)
- carro.currentSpeed += carro.acceleration;
- else if (carro.currentSpeed > carro.topSpeed * carro.handlingS)
- carro.currentSpeed = carro.topSpeed * carro.handlingS;
- break;
- case 'C': // curve
- if (carro.currentSpeed < carro.topSpeed * carro.handlingC)
- carro.currentSpeed += carro.acceleration;
- else if (carro.currentSpeed > carro.topSpeed * carro.handlingC)
- carro.currentSpeed = carro.topSpeed * carro.handlingC;
- break;
- case 'U': // Hairpin
- if (carro.currentSpeed < carro.topSpeed * carro.handlingU)
- carro.currentSpeed += carro.acceleration;
- else if (carro.currentSpeed > carro.topSpeed * carro.handlingU)
- carro.currentSpeed = carro.topSpeed * carro.handlingU;
- break;
- default:
- System.out.println("default " + i);
- break;
- }
- // increase progress
- carro.raceProgress += carro.currentSpeed;
- if (carro.raceProgress >= raceLength) {
- if(lastlap)
- {
- //System.out.println(carro.name + " wins race");
- //System.out.println("total wins in tournament: " + carro.wins );
- carro.wins++;
- for (int w =0; w < cars.size(); w++) {
- cars.get(w).raceProgress = 0;
- }
- }
- carro.raceProgress = 0;
- runningRace = false;
- //System.out.println("Stop ");
- }
- }
- }
- }
- // new class for car
- public static class Car {
- String name;
- float handlingS;
- float handlingC;
- float handlingU;
- float raceProgress;
- float currentSpeed;
- float topSpeed;
- float acceleration;
- int wins;
- char carSymbol;
- // method for car
- public Car(String carName, float carHandlingS, float carHandlingC, float carHandlingU, float carRaceProgress,
- float carCurrentSpeed, float carTopSpeed, float carAcceleration, int carWins, char carSymbol) {
- name = carName;
- handlingS = carHandlingS;
- handlingC = carAcceleration;
- handlingU = carHandlingU;
- raceProgress = carRaceProgress;
- this.currentSpeed = carCurrentSpeed;
- topSpeed = carTopSpeed;
- acceleration = carAcceleration;
- wins = carWins;
- this.carSymbol = carSymbol;
- //FileInputStream
- //FileOututStream
- //try & catch walks
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement