Advertisement
Dakpluto

RacingGame.java

Oct 12th, 2014
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.17 KB | None | 0 0
  1. public class RacingGame {
  2.     public static void main(String[] args) {
  3.         Car[] cars = new Car[10];
  4.         cars[0] = new Car("Richard Petty", 30);
  5.         cars[1] = new Car("Lightning McQueen", 30);
  6.         cars[2] = new Car("Chick Hicks", 30);
  7.         cars[3] = new Car("Dale Jr", 30);
  8.         cars[4] = new Car("Mark Martin", 30);
  9.         cars[5] = new Car("Kyle Busch", 30);
  10.         cars[6] = new Car("Scooby Doo", 30);
  11.         cars[7] = new Car("Sydney Crosby", 30);
  12.         cars[8] = new Car("T. J. Oshie", 30);
  13.         cars[9] = new Car("Lillian Brown", 30);
  14.         Announcer announce = new Announcer();
  15.         // System.out.println(cars[6]);
  16.  
  17.         System.out
  18.                 .println("Green flag comes out, and they are off!  Boogity-boogity-boogity!");
  19.  
  20.         // cars[9].setLocation(500);
  21.         // cars[0].setLocation(400);
  22.         while (!checkForWinner(cars)) {
  23.             // System.out.println(cars[9]);
  24.             // System.out.println(cars[0]);
  25.             // cars[9].setLocation(cars[9].getLocation() + 100);
  26.             // cars[0].setLocation(cars[0].getLocation() + 150);
  27.             // System.out.println("The Leader is: " + cars[findLeader(cars)]);
  28.             setRandomSpeedChange(cars);
  29.             setNewLocation(cars);
  30.             if (!checkForWinner(cars)) {
  31.                 Announcer.generateRandomAnnounce();
  32.                 System.out.println(announce.announceLeader(findLeader(cars),
  33.                         cars));
  34.             }
  35.  
  36.         }
  37.  
  38.         System.out.println(announce.announceWinner(findLeader(cars), cars));
  39.     }
  40.  
  41.     public static boolean checkForWinner(Car[] location) {
  42.         boolean result = false;
  43.  
  44.         for (int i = 0; i < location.length; i++) {
  45.             if (location[i].getLocation() >= 1000) {
  46.                 result = true;
  47.             }
  48.         }
  49.         return result;
  50.     }
  51.  
  52.     public static int findLeader(Car[] location) {
  53.         int result = 0;
  54.  
  55.         for (int i = 0; i < location.length; i++) {
  56.             if (location[i].getLocation() > location[result].getLocation()) {
  57.                 result = i;
  58.             }
  59.         }
  60.  
  61.         return result;
  62.     }
  63.  
  64.     public static void setRandomSpeedChange(Car[] speed) {
  65.         for (int i = 0; i < speed.length; i++) {
  66.             speed[i].setSpeed(speed[i].getSpeed()
  67.                     + speed[i].randomSpeedChange());
  68.         }
  69.  
  70.     }
  71.  
  72.     public static void setNewLocation(Car[] location) {
  73.         for (int i = 0; i < location.length; i++) {
  74.             location[i].setLocation(location[i].getLocation()
  75.                     + location[i].getSpeed());
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement