Sie sind auf Seite 1von 6

Public RainDelay { RainDelay() { this.EnterRainZone(Car obj); } EnterRainZone() { obj.rainDelayStartTime = getTime(); do{ ; } while( keyPress != S ); // Until Race is again started.

obj.rainDelayEndTime = getTime(); } }

public Class Car { // A variable which is incremented whenever a new object is created. static int no_of_Car_Objects_running; Time time, x, y, intervalTime, intervalStartTime, intervalEndTime, timeLapsed; Time rainDelayStartTime, rainDelayEndTime, intervalTimeStartPointer; String name; decimal startpos, totalDistanceCovered, totalDistanceRequired; int flag, speed; Car(string name, int pos, int speed, int totalDistance) { this.time = getTime(); this.name = name; this.startpos = pos; this.x = this.time; this.speed = speed; this.y = this.time; this.intervalStartTime intervalTimeStartPointer this.totalDistanceRequired } // static objects, blocks will be loaded first. static incrementStaticValues() { no_of_Car_Objects_running ++; } // Test No Of Objects Created. DisplayEveryThing(Systen.Object Obj1, String msg) { println( msg + " : " + no_of_Car_Objects_running); = = = this.time; this.time; totalDistance;

} EnterRaceZone() { if(this.IsTotalDistanceCovered()) { this.EndGame(); } else { this.DisplayPositions(); } }

TimeLapsed() { tempTimeVar = getTime() - this.y; this.lapsed = tempTimeVar; this.SetTimeCursors(); } TotalDistanceCovered() { this.TimeLapsed(); this.totalDistanceCovered += this.speed * this.lapsedTime; } boolean IsTotalDistanceCovered() { this.TotalDistanceCovered(); if((this.totalDistanceRequired - this.totalDistanceCovered) <= 0) { return (true); } else { return (false); } } DisplayPositions() { decimal[] array_positions = new decimal[no_of_Car_Objects_running]; string[] array_names = new string[no_of_Car_Objects_running]; for(int i = 1; i <= no_of_Car_Objects_running; i++) { array_positions[i-1] = carobj + i.ToString().totalDistanceCovere d; array_names[i-1] = carobj + i.ToString().name;

} // There is function in C# which sorts array1 in Sync with array2 Array.Sort(array_positions, array_names); Println ( " Racers positions in ascending order: ") for(int i = 1; i <= no_of_Car_Objects_running; i++) { println( array_names[ array_positions[i-1] ]); //println( array_names[ i - 1 ] ); } }

SetTimeCursors() { this.y = getTime(); }

CalculateIntervalTime() { this.intervalTime = getTime() - this.intervalTimeStartPointer; this.intervalTimeStartPointer = getTime(); } CalculateIntervalDistance() { this.calculateIntervalTime(); this.intervalDistance + = this.speed * this.intervalTime; } DecreaseSpeed(int a) { // calculate interval distance covered before increasing speed. this.CalculateIntervalDistance(); if( this.totalDistanceCovered() == false ) { if((this.speed - a) <= 0 ) { this.speed = 1 } else { this.speed -= a; }

} }

IncreaseSpeed(int a) { this.CalculateIntervalDistance(); if( this.totalDistanceCovered() == false ) { this.speed += a; } else { this.EndGame(); } }

KeyPressEvent() { if( keypress == UpArrow ) // increase speed { int a = get_no_Of_Seconds_Key_Pressed(); this.IncreaseSpeed( (3^a) mod 20) ) } if( keypress == UpArrow ) // decrease speed { int a = get_no_Of_Seconds_Key_Pressed(); this.DecreaseSpeed( (3^a) mod 20) ) }

if( keypress == R ) // Indicates Rain-Delay. { RainDelay rX = new RainDelay(this); this.RestoreTimePointers(); } } RestoreTimePointers() { Time tempTimeVar = (this.rainDelayEndTime - this.rainDelayStartTime); for(int i = 1; i <= no_of_Car_Objects_running; i++) { carobj + i.ToString().y - = tempTimeVar; carobj + i.ToString().y - = tempTimeVar;

} }

} // End Car-Class.

public class Test { public static void Main() { boolean raceEnd = false; Println("Please enter number of cars participating in the race : "); Read(n); Car[] carobjs = new Car[n]; for( int i = 1; i<=n; i++) { // pass name, position speed, total_track_distance. Car carobjs[i] = new Car("ABC", 0.00, 50, 2000); } // calling a static method to check no of objects created. Car.DisplayEveryThing(Car.no_of_Car_Objects_running, "No Of Cars partici pating are "); while(!raceEnd) { for(int i = 1; i <= n; i++) { carObjs[i].EnterRaceZone(); if(carObjs[i].flag == 1) { raceEnd = true; break; }

} } } }

Das könnte Ihnen auch gefallen