-- clock.ala (0.3) -- demonstrates: -- - updating a clock using a recurring event to repeat -- an action each turn -- - using some of arithmetic functions of Alan, Note that all -- numbers in Alan are integers. So the result of a division calculation -- is always an integer, a 'whole number'. If there's any decimal part -- or remainder it is disgarded. EG: 11 / 4 = 2 (Not 2.75) -- (This fact is used to advantage in this example program when -- calculating the hour of the day given the number of minutes -- since midnight.) ---------------------------------- LOCATION platform DESCRIPTION "You are standing on a subway platform, clutching a timetable, hoping your train will arrive soon." EXIT north, south, east, west, 'exit', out TO platform CHECK "Don't go away, you might miss your train. (Use quit to finish running this program.)" END Exit. END LOCATION. ---------------------------------- OBJECT clock at platform HAS Hour 4. -- set these three attributes HAS Minute 50. -- to specify the time of day HAS AmPm "pm". -- you want when the game starts HAS TimeInMinutes 0. -- (the time expressed as minutes past midnight. This is -- calculated by the game - in the START section - and -- updated each turn by the TickTock event so left -- at the default initial value here) VERB Read DOES ONLY "" -- don't need to read clock coz the time END VERB. -- displayed by the TickTock event anyway END OBJECT clock. ---------------------------------- OBJECT Timetable at platform HAS Hour 5. -- set these three attributes HAS Minute 03. -- to specify the arrival time HAS AmPm "am". -- you want for the next train HAS TimeInMinutes 0. -- calculated by the game in the START section HAS WaitMinutes 0. -- the time to wait until the next train (calculated -- in the Read verb) -- Blank description because timetable mentioned in location description DESCRIPTION VERB Read DOES ONLY "The next train is at" SAY Hour of Timetable. "$$:$$" -- ($$ means don't put a space here when formatting this text on screen) IF Minute of Timetable > 9 THEN SAY Minute of Timetable. ELSE "0$$" SAY Minute of Timetable. END IF. SAY AmPm OF Timetable. IF TimeInMinutes OF Timetable >= TimeInMinutes OF Clock THEN SET WaitMinutes OF Timetable TO TimeInMinutes OF Timetable - TimeInMinutes OF Clock. ELSE --ie:train's arrival is other side of midnight (1440 minutes) SET WaitMinutes OF Timetable TO 1440 - (TimeInMinutes OF Clock - TimeInMinutes OF Timetable). END IF. "$$. You check the clock." IF WaitMinutes OF Timetable > 20 THEN "Wow," IF WaitMinutes OF Timetable > 61 THEN SAY WaitMinutes of Timetable / 60 . "hours and" SAY WaitMinutes of Timetable - ((WaitMinutes of Timetable / 60) * 60) . "minutes to wait!" ELSE SAY WaitMinutes of Timetable. "minutes to go!" END IF. "A bit boring to wait" SAY WaitMinutes OF Timetable. "turns as there's nothing to do in this game but wait or read the timetable. Hey, maybe you could make the train arrive faster if you just quit and changed the source code!" "You can experiment with the clock and timetable time-settings to see how the two interact." ELSIF WaitMinutes OF Timetable = 0 THEN "Hmm, the train should be here by now. $pAhh!" ELSE "Only" SAY WaitMinutes of Timetable. "minutes to go." END IF. END VERB. END OBJECT Timetable. --------------------------------- EVENT Arrival IF TimeInMinutes of Timetable = TimeInMinutes of Clock THEN "The train arrives. $pThe doors open. You step aboard and take a seat. As the train moves off again you breathe a sigh of relief, happy to be on your way.$p$p$p" QUIT. ELSE SCHEDULE Arrival AFTER 1. END IF. END EVENT. --------------------------------- EVENT TickTock -- Displays The Current Time And Then Increments The Time By Another Minute -- Convert Time From "Minutes Past Midnight" To Hh:Mm Am/Pm Format SET Hour OF Clock TO TimeInMinutes of Clock / 60. IF Hour OF Clock >= 12 THEN SET AmPm OF Clock TO "pm" . IF Hour OF Clock > 12 THEN SET Hour OF Clock TO Hour of Clock - 12 . END IF. ELSE SET AmPm OF Clock TO "am" . IF Hour OF Clock = 0 THEN SET Hour OF Clock TO 12 . END IF. END IF. SET Minute of Clock TO TimeInMinutes of Clock - ((TimeInMinutes of Clock / 60) * 60). -- Display the time on screen "$pThe clock reads" SAY Hour of Clock. "$$:$$" IF Minute of Clock > 9 THEN SAY Minute of Clock. ELSE "0$$" SAY Minute of Clock. END IF. SAY AmPm OF Clock. -- Increment time by one minute -- (If its 11:59pm set clock to zero ie:midnight instead) IF TimeInMinutes OF Clock < 1439 THEN INCREASE TimeInMinutes OF Clock. ELSE SET TimeInMinutes OF Clock TO 0 . END IF. -- schedule this event to happen again next turn SCHEDULE TickTock AFTER 1. END EVENT TickTock. ---------------------------------- SYNONYMS examine,x,'look',l,'check'=read. z=wait. q=quit. ---------------------------------- SYNTAX quit=quit. VERB quit DOES QUIT. END VERB. SYNTAX wait=wait. VERB wait DOES "Another minute passes as you impatiently watch the clock." END VERB. ---------------------------------- START AT platform. "$pWaiting for a train" -- Convert the train's arrival time and clock's initial time -- from the hh:mm am/pm format -- used in source-code to "minutes past midnight" format to make -- calculations easier SET TimeInMinutes OF Clock TO (Hour OF Clock * 60) + Minute OF Clock. IF AmPm OF Clock = "pm" THEN IF Hour OF Clock < 12 THEN SET TimeInMinutes OF Clock TO (TimeInMinutes OF Clock + 720). END IF. END IF. SET TimeInMinutes OF Timetable TO (Hour OF Timetable * 60) + Minute OF Timetable. IF AmPm OF Timetable = "pm" THEN IF Hour OF Timetable < 12 THEN SET TimeInMinutes OF Timetable TO (TimeInMinutes OF Timetable + 720). END IF. END IF. SCHEDULE Arrival AFTER 0. -- checks if its time for the train to arrive SCHEDULE TickTock AFTER 0. -- start the clock "ticking"