Back to the Future
We can't just time-travel around without first making sure our code is pretty and columns are aligned.
And keeping lines with at most 80 characters should help if we ever need to review the code while we're stuck in the past.
This is my first challenge. I tried being creative and even did a bit of research, so I hope you enjoy it.
Back to the Future
We can't just time-travel around without first making sure our code is pretty and columns are aligned.
And keeping lines with at most 80 characters should help if we ever need to review the code while we're stuck in the past.
This is my first challenge. I tried being creative and even did a bit of research, so I hope you enjoy it.
Output
#include // Includes time machine code
#include // Includes DeLorean code
// Defines bool
typedef enum {
false,
true
} bool;
// Creates objects
TimeTravel TimeMachine; // Tracks power input, allows jumping in time
DeLorean Vehicle; // Manages the storage, display and input of time,
// tracks speed
long TimeDestination = -2682276364; // Destination time, POSIX
long TimePresent = -2682276364; // Present time
long TimeLast = -2682276364; // Last departed time
unsigned float SpeedCurr = 0.0; // Current speed, in miles per hour
unsigned float SpeedTarget = 88.0; // Target speed
unsigned float PowerCurr = 0.0; // Current power, in gigawatts
unsigned float PowerTarget = 1.21; // Target power
char
loop ()
{
// Updates local time variables
TimeDestination = Vehicle.getTime (dtime);
TimePresent = Vehicle.getTime (ptime);
TimeLast = Vehicle.getTime (ltime);
// Updates local speed and power variables
SpeedCurr = Vehicle.getSpeed ();
PowerCurr = TimeMachine.getPower ();
// Get ready statuses
bool TMReady = TimeMachine.getReady ();
bool VReady = Vehicle.getReady ();
// Calculates travel time, in seconds
long TimeOffset = TimeDestination - TimePresent;
// Checks speed, power and ready statuses
if (SpeedCurr >= SpeedTarget && PowerCurr >= PowerTarget &&
TMReady && VReady) {
// Prompts for a time travel of given length, checks if no
// error returned
if (!TimeMachine.travel (TimeOffset)) {
// Updates last time with time as of before traveling
Vehicle.setTime (ltime, TimePresent);
// Updates present time with time as of after traveling
Vehicle.setTime (ptime, TimeDestination);
} else {
return 1;
}
}
return 0;
}
char
setup ()
{
// Begin objects, store errors
char errTM = TimeMachine.begin ();
char errV = Vehicle.begin ();
if (!errTM && !errV)
return 0;
return 1;
}
char
main ()
{
// Runs setup, store error
char err = setup ();
// Loops as long as no error
while (!err) {
// Run loop, update error
err = loop ();
}
return 1;
}