Question: Objective 2: Right now the race is only being run once. Adjust the code so that the race can be run 25 times to simulate
Objective 2: Right now the race is only being run once. 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.
Objective 3: Each race of the season should be a different race. Randomly create racetracks prior to the start of each race. Do this by randomly appending the four characters to a string. You can skew the random numbers to give more turns depending on your preference (so long as there is a chance of everything appearing in a race).
public class CardCar {
// instance variables
String carName;
float carHandlingS;
float carHandlingC;
float carHandlingU;
float carRaceProgress;
float carCurrentSpeed;
float carTopSpeed;
float carAcceleration;
int carWins;
char carSymbol;
public CardCar(String carName, float carHandlingS, float carHandlingC, float carHandlingU, float carRaceProgress,
float carCurrentSpeed, float carTopSpeed, float carAcceleration, int carWins, char carSymbol) {
super();
this.carName = carName;
this.carHandlingS = carHandlingS;
this.carHandlingC = carHandlingC;
this.carHandlingU = carHandlingU;
this.carRaceProgress = carRaceProgress;
this.carCurrentSpeed = carCurrentSpeed;
this.carTopSpeed = carTopSpeed;
this.carAcceleration = carAcceleration;
this.carWins = carWins;
this.carSymbol = carSymbol;
}
public String getCarName() {
return carName;
}
public float getCarHandlingS() {
return carHandlingS;
}
public float getCarHandlingC() {
return carHandlingC;
}
public float getCarHandlingU() {
return carHandlingU;
}
public float getCarRaceProgress() {
return carRaceProgress;
}
public float getCarCurrentSpeed() {
return carCurrentSpeed;
}
public float getCarTopSpeed() {
return carTopSpeed;
}
public float getCarAcceleration() {
return carAcceleration;
}
public int getCarWins() {
return carWins;
}
public char getCarSymbol() {
return carSymbol;
}
public void setCarName(String carName) {
this.carName = carName;
}
public void setCarHandlingS(float carHandlingS) {
this.carHandlingS = carHandlingS;
}
public void setCarHandlingC(float carHandlingC) {
this.carHandlingC = carHandlingC;
}
public void setCarHandlingU(float carHandlingU) {
this.carHandlingU = carHandlingU;
}
public void setCarRaceProgress(float carRaceProgress) {
this.carRaceProgress = carRaceProgress;
}
public void setCarCurrentSpeed(float carCurrentSpeed) {
this.carCurrentSpeed = carCurrentSpeed;
}
public void setCarTopSpeed(float carTopSpeed) {
this.carTopSpeed = carTopSpeed;
}
public void setCarAcceleration(float carAcceleration) {
this.carAcceleration = carAcceleration;
}
public void setCarWins(int carWins) {
this.carWins = carWins;
}
public void setCarSymbol(char carSymbol) {
this.carSymbol = carSymbol;
}
}
######################
public class RaceTrack {
public static void main(String[] args) {
int RaceNumber = 1;
CardCar car1 = new CardCar("Bob Barker Car", 0.30f, 0.65f,
0.1f, 0, 0, 0.95f,
0.11f, 0, 'B');
CardCar car2 = new CardCar("Pat Sajak Car", 0.45f, 0.85f, 0.3f,
0, 0, 0.78f, 0.08f, 0, 'P');
CardCar car3 = new CardCar("Alex Trebek Car", 0.25f, 0.55f, 0.1f, 0,
0, 0.95f, 0.19f, 0, 'A');
CardCar car4 = new CardCar("Drew Carrie Car", 0.15f, 0.25f, 0.05f, 0,
0, 0.9999f, 0.21f, 0, 'D');
// creating an array of CardCar
CardCar cardCarArr[] = {car1, car2, car3, car4};
String Racetrack = "----------U-C-S-------C---S---------C--U----";
int raceLength = Racetrack.length();
boolean runningRace = true; //once someone wins we can stop this
while (runningRace) {
System.out.println(Racetrack);
//Car 1
String Progress = "";
for (int i=0;i Progress+= " "; System.out.println(Progress+car1.getCarSymbol()); //accelerate based on where you are on the track //Racetrack[(int)car1RaceProgress)] switch (Racetrack.charAt((int)car1.getCarRaceProgress())) { case '-': //strait-away if (car1.getCarCurrentSpeed() < car1.getCarTopSpeed()) car1.setCarCurrentSpeed(car1.getCarCurrentSpeed()+car1.getCarAcceleration()); else if (car1.getCarCurrentSpeed() > car1.getCarTopSpeed()) car1.setCarCurrentSpeed(car1.getCarTopSpeed()); break; case 'S': //Chicane if (car1.getCarCurrentSpeed() < car1.getCarTopSpeed()*car1.getCarHandlingS()) car1.setCarCurrentSpeed(car1.getCarCurrentSpeed()+car1.getCarAcceleration()); else if (car1.getCarCurrentSpeed() > car1.getCarTopSpeed()*car1.getCarHandlingS()) car1.setCarCurrentSpeed(car1.getCarTopSpeed()*car1.getCarHandlingS()); break; case 'C': //curve if (car1.getCarCurrentSpeed() < car1.getCarTopSpeed()*car1.getCarHandlingC()) car1.setCarCurrentSpeed(car1.getCarCurrentSpeed()+car1.getCarAcceleration()); else if (car1.getCarCurrentSpeed() > car1.getCarTopSpeed()*car1.getCarHandlingC()) car1.setCarCurrentSpeed(car1.getCarTopSpeed()*car1.getCarHandlingC()); break; case 'U': //Hairpin if (car1.getCarCurrentSpeed() < car1.getCarTopSpeed()*car1.getCarHandlingU()) car1.setCarCurrentSpeed(car1.getCarCurrentSpeed()+car1.getCarAcceleration()); else if (car1.getCarCurrentSpeed() > car1.getCarTopSpeed()*car1.getCarHandlingU()) car1.setCarCurrentSpeed(car1.getCarTopSpeed()*car1.getCarHandlingU()); break; } //increase progress car1.setCarRaceProgress(car1.getCarRaceProgress() + car1.getCarCurrentSpeed()); if (car1.getCarRaceProgress() >= raceLength) { System.out.println(car1.getCarName()+" wins"); car1.setCarWins(car1.getCarWins() + 1); runningRace=false; } } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
