Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class RacingGame {
- public static void main(String[] args) {
- Car[] cars = new Car[10];
- cars[0] = new Car("Richard Petty", 30);
- cars[1] = new Car("Lightning McQueen", 30);
- cars[2] = new Car("Chick Hicks", 30);
- cars[3] = new Car("Dale Jr", 30);
- cars[4] = new Car("Mark Martin", 30);
- cars[5] = new Car("Kyle Busch", 30);
- cars[6] = new Car("Scooby Doo", 30);
- cars[7] = new Car("Sydney Crosby", 30);
- cars[8] = new Car("T. J. Oshie", 30);
- cars[9] = new Car("Lillian Brown", 30);
- Announcer announce = new Announcer();
- // System.out.println(cars[6]);
- System.out
- .println("Green flag comes out, and they are off! Boogity-boogity-boogity!");
- // cars[9].setLocation(500);
- // cars[0].setLocation(400);
- while (!checkForWinner(cars)) {
- // System.out.println(cars[9]);
- // System.out.println(cars[0]);
- // cars[9].setLocation(cars[9].getLocation() + 100);
- // cars[0].setLocation(cars[0].getLocation() + 150);
- // System.out.println("The Leader is: " + cars[findLeader(cars)]);
- setRandomSpeedChange(cars);
- setNewLocation(cars);
- if (!checkForWinner(cars)) {
- Announcer.generateRandomAnnounce();
- System.out.println(announce.announceLeader(findLeader(cars),
- cars));
- }
- }
- System.out.println(announce.announceWinner(findLeader(cars), cars));
- }
- public static boolean checkForWinner(Car[] location) {
- boolean result = false;
- for (int i = 0; i < location.length; i++) {
- if (location[i].getLocation() >= 1000) {
- result = true;
- }
- }
- return result;
- }
- public static int findLeader(Car[] location) {
- int result = 0;
- for (int i = 0; i < location.length; i++) {
- if (location[i].getLocation() > location[result].getLocation()) {
- result = i;
- }
- }
- return result;
- }
- public static void setRandomSpeedChange(Car[] speed) {
- for (int i = 0; i < speed.length; i++) {
- speed[i].setSpeed(speed[i].getSpeed()
- + speed[i].randomSpeedChange());
- }
- }
- public static void setNewLocation(Car[] location) {
- for (int i = 0; i < location.length; i++) {
- location[i].setLocation(location[i].getLocation()
- + location[i].getSpeed());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement