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.
Start file
#include <TimeTravel.h> // Includes time machine code #include <DeLorean.h> // 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)) { Vehicle.setTime (ltime, TimePresent); // Updates last time with // time as of before traveling Vehicle.setTime (ptime, TimeDestination); // Updates present time with // time as of after traveling } 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; }
End file
#include <TimeTravel.h> // Includes time machine code #include <DeLorean.h> // 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; }
View Diff
2c2 < #include <DeLorean.h> // Includes DeLorean code --- > #include <DeLorean.h> // Includes DeLorean code 12,13c12,13 < DeLorean Vehicle; // Manages the storage, display and input of time, tracks < // speed --- > DeLorean Vehicle; // Manages the storage, display and input of time, > // tracks speed 16,17c16,17 < long TimePresent = -2682276364; // Present time < long TimeLast = -2682276364; // Last departed time --- > long TimePresent = -2682276364; // Present time > long TimeLast = -2682276364; // Last departed time 19,20c19,20 < unsigned float SpeedCurr = 0.0; // Current speed, in miles per hour < unsigned float SpeedTarget = 88.0; // Target speed --- > unsigned float SpeedCurr = 0.0; // Current speed, in miles per hour > unsigned float SpeedTarget = 88.0; // Target speed 22,23c22,23 < unsigned float PowerCurr = 0.0; // Current power, in gigawatts < unsigned float PowerTarget = 1.21; // Target power --- > unsigned float PowerCurr = 0.0; // Current power, in gigawatts > unsigned float PowerTarget = 1.21; // Target power 30,31c30,31 < TimePresent = Vehicle.getTime (ptime); < TimeLast = Vehicle.getTime (ltime); --- > TimePresent = Vehicle.getTime (ptime); > TimeLast = Vehicle.getTime (ltime); 39c39 < bool VReady = Vehicle.getReady (); --- > bool VReady = Vehicle.getReady (); 45,46c45,48 < if (SpeedCurr >= SpeedTarget && PowerCurr >= PowerTarget && TMReady && VReady) { < // Prompts for a time travel of given length, checks if no error returned --- > if (SpeedCurr >= SpeedTarget && PowerCurr >= PowerTarget && > TMReady && VReady) { > // Prompts for a time travel of given length, checks if no > // error returned 48,51c50,53 < Vehicle.setTime (ltime, TimePresent); // Updates last time with < // time as of before traveling < Vehicle.setTime (ptime, TimeDestination); // Updates present time with < // time as of after traveling --- > // 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); 65c67 < char errV = Vehicle.begin (); --- > char errV = Vehicle.begin ();
Solutions by @MicaelJarniac:
Unlock 2 remaining solutions by signing in and submitting your own entry