Created
February 27, 2014 18:54
-
-
Save Bjacksonshorts/9256598 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LeeRacer implements Racer{ | |
private int maxSpeed; | |
private int maxHandling; | |
private String[] track; | |
private int position; | |
public LeeRacer(){ | |
maxSpeed = 50; | |
maxHandling = 50; | |
position = 0; | |
} | |
public boolean crossedFinishLine(){ | |
if(getTrackLength() == 0){ | |
return true; | |
}else{ | |
return false; | |
} | |
} | |
public void tick(){ | |
int counter = 0; | |
if(counter == 10){ | |
System.out.println("you are at the " + position + "position"); | |
}else{ | |
counter++; | |
} | |
} | |
public void setTrack(String[] t){ | |
track = t; | |
} | |
public int getTrackLength(){ | |
return track.length; | |
} | |
public int getPositionOnTrack(){ | |
return position; | |
} | |
public int getMaxSpeed(){ | |
return maxSpeed; | |
} | |
public int getMaxHandling(){ | |
return maxHandling; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Race { | |
public static void main(String[] args) { | |
Race r = new Race(); | |
try { | |
r.beginRace(); | |
} catch (InterruptedException e) { | |
System.out.println("Unable to execute race..."); | |
} | |
System.out.println(r.getWinnersList()); | |
} | |
private Racer[] racers; | |
private String[] map; | |
public Race() { | |
System.out.println("Welcome to the race! Let's get started..."); | |
map = new String[20]; | |
initTrack(); | |
racers = new Racer[9]; | |
initRacers(); | |
} | |
private void initRacers() { | |
/*racers[0] = new GianlucaRacer(); | |
racers[0].setTrack(map); | |
...etc...*/ | |
racers[0] = new LeeRacer(); | |
racers[0].setTrack(map); | |
} | |
private void initTrack() { | |
map = new String[] {"0", "0", "0", "x", "0", "s", "0", "p", "x", "x", "x", "0", "0", "p", "0", "s", "x", "x", "0", "0"}; | |
} | |
public void beginRace() throws InterruptedException { | |
while(raceIsInProgress()) { | |
for (int i = 0; i < racers.length; i++) { | |
if (!racers[i].crossedFinishLine()) { | |
racers[i].tick(); | |
} | |
} | |
Thread.sleep(100); | |
} | |
} | |
public boolean raceIsInProgress() { | |
int completedRacers = 0; | |
for (int i = 0; i < racers.length; i++) { | |
if (racers[i].crossedFinishLine()) { | |
completedRacers++; | |
} | |
} | |
return (completedRacers != racers.length); | |
} | |
/* | |
public boolean raceIsInProgress() { | |
for(int i = 0; i < racers.length; i++) { | |
if(!racers[i].crossedFinishLine()) { | |
return false; | |
} | |
} | |
return true; | |
} | |
*/ | |
public String getWinnersList() { | |
String str = ""; | |
// collect winners list | |
return str; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Racer { | |
boolean crossedFinishLine(); | |
void tick(); | |
void setTrack(String[] t); | |
int getTrackLength(); | |
int getPositionOnTrack(); | |
int getMaxSpeed(); | |
int getMaxHandling(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment