1 19 20 package org.polepos.framework; 21 22 import java.lang.reflect.*; 23 import java.util.*; 24 25 28 public abstract class Circuit{ 29 30 private final List<Lap> mLaps; 31 32 private final TurnSetup[] mLapSetups; 33 34 private final StopWatch watch; 35 36 protected Circuit(){ 37 watch = new StopWatch(); 38 mLaps = new ArrayList<Lap>(); 39 mLapSetups = TurnSetup.read(this); 40 addLaps(); 41 } 42 43 46 public final String name(){ 47 String name = internalName(); 48 return name.substring(0,1).toUpperCase() + name.substring(1); 49 } 50 51 54 public final String internalName(){ 55 String name = this.getClass().getName(); 56 int pos = name.lastIndexOf("."); 57 return name.substring(pos + 1).toLowerCase(); 58 } 59 60 63 public abstract String description(); 64 65 68 public abstract Class requiredDriver(); 69 70 73 protected abstract void addLaps(); 74 75 public void add(Lap lap){ 76 mLaps.add(lap); 77 } 78 79 82 public TurnSetup[] lapSetups(){ 83 return mLapSetups; 84 } 85 86 public List<Lap> laps() { 87 return Collections.unmodifiableList(mLaps); 88 } 89 90 93 public TurnResult[] race( Team team, Car car, Driver driver){ 94 95 TurnResult[] results = new TurnResult[ mLapSetups.length ]; 96 97 int index = 0; 98 99 for(TurnSetup setup : mLapSetups) { 100 101 TurnResult result = new TurnResult(); 102 results[index++] = result; 103 104 try { 105 driver.takeSeatIn(car, setup); 106 } catch (CarMotorFailureException e1) { 107 e1.printStackTrace(); 108 break; 109 } 110 111 112 boolean first = true; 113 114 for(Lap lap : mLaps) { 115 116 117 Method method = null; 118 119 try { 120 method = driver.getClass().getDeclaredMethod(lap.name(), (Class [])null); 121 } catch (SecurityException e) { 122 e.printStackTrace(); 123 } catch (NoSuchMethodException e) { 124 e.printStackTrace(); 125 } 126 127 128 if( ! lap.hot() ){ 129 if(first){ 130 first = false; 131 }else{ 132 driver.backToPit(); 133 } 134 135 try { 136 driver.prepare(); 137 } catch (CarMotorFailureException e) { 138 e.printStackTrace(); 139 } 140 } 141 142 143 watch.start(); 144 145 try { 146 method.invoke(driver, (Object [])null); 147 } catch (Exception e) { 148 System.err.println("Exception on calling method " + method); 149 e.printStackTrace(); 150 } 151 152 watch.stop(); 153 154 if(lap.reportResult()){ 155 result.report(new Result(this, team, lap, setup, index, watch.millisEllapsed(), driver.checkSum())); 156 } 157 } 158 159 driver.backToPit(); 160 } 161 return results; 162 } 163 164 }
| Popular Tags
|