TweetFollow Us on Twitter

Oct 98 Prog Challenge

Volume Number: 14 (1998)
Issue Number: 10
Column Tag: Programmer's Challenge

October 98 Programmer's Challenge

by Bob Boonstra, Westford, MA

Hearts

A number of past Challenges have been based on games where one player competed against another player. This month the Challenge is based on the four-handed game of Hearts. Hearts is a card game where one attempts to avoid taking tricks containing hearts or the queen of spades.

The prototype for the code you should write is:

#if defined(__cplusplus)
extern "C" {
#endif

#pragma enumsalwaysint on

typedef enum {kNoSuit=0, kSpade, kHeart, kDiamond,
                                 kClub} Suit;
typedef enum {kNoSpot=0, k2, k3, k4, k5, k6, k7, k8, k9, k10, 
            kJack, kQueen, kKing, kAce} Spot;
typedef enum {kPassLeft=0, kPassRight, kPassAcross,
                                 kNoPass} Pass;
typedef struct Card {
   Suit suit;
   Spot spot;
} Card;

pascal void InitTournament(
   const UInt16 numPlayers,
           /* number of players in the tournament, indexed by seat */
   const UInt16 gameEndingScore
           /* game is over when one player reaches this score */
);
pascal void InitGame(
   const Uint32 playerID[4],   /* Identifier for players in this hand, indexed
                                  by seat */
   const UInt16 yourSeat       /* your seat at the table, 0..3 */
);
pascal void SelectPass(
   const Card dealtHand[13],   /* cards you are dealt */
   const Pass passDirection,
      /* direction you are passing, rotates kPassLeft, kPassRight,
      kPassAcross, kNoPass */
   UInt16 passedCards[3]   /* index in dealtHand of cards you choose to pass */
);
pascal void PlayTrick(
   const UInt16 trickNumber,   /* 13 tricks per hand, 0..12 */
   const UInt16 trickLeader,   /* which player leads this trick */
   const Card yourHand[13],    /* your cards at the beginning of this trick */
   const Card cardsPlayed[4],
                     /* cards already played this trick, indexed by seat */
                     /* entries from [trickLeader] to [yourSeat-1 (mod 4)] are
                        valid */
   UInt16 *yourPlay  /* index into yourHand of the card you choose to play */
);
pascal void TrickResults(
   const Card lastTrick[4],   /* cards played on previous trick, indexed by
                                 seat */
   const UInt16 trickWinner   /* which player won this trick */
);
pascal void HandResults(
   const SInt16 pointsThisHand[4],
                        /* points earned by each player this hand, -26 .. 25 */
   const SInt32 cumPoints[4]
                        /* cumulative points earned by each player this game */
      /* both pointsThisHand and cumPoints are indexed by seat number */
      /* your points are pointsThisHand[yourSeat] */
);

#if defined(__cplusplus)
}
#endif

At the beginning of the tournament, your InitTournament routine will be called with the number of participating players (numPlayers) and the gameEndingScore. Before each game, your InitGame routine will be called to provide you with the playerID identifiers of the players involved in this game and your position at the table (yourSeat) relative to the other players. A game consists of a sequence of hands, each of which consists of 13 tricks. At the end of a hand, each player is awarded points based on the cards in the tricks he has taken, one point for each heart taken and 13 points for the queen of spades, with the objective being to take the fewest points. A player who captures all the hearts and the queen of spades, however, is said to "shoot the moon" and receives -26 points. At the end of each hand, your HandResults routine will be called to confirm the number of points received by each player (pointsThisHand) and the cumulative number of points earned by each player during this game (cumPoints).

At the beginning of a hand, each player is dealt 13 cards (dealtHand) and selects three cards to pass to another player. The passDirection rotates from passing left (kPassLeft, where player [i] passes to player [i+1], mod 4), to passing right (kPassRight, player [i] passes to player [i-1]), to passing across (kPassAcross, player [i] passes to player [i+2]), to not passing (kNoPass). Your SelectPass routine is called at the beginning of each hand with your dealtHand and the passDirection; you select the cards to be passed and return in passedCards the index in dealtHand of the cards you want to pass.

Each trick, your PlayTrick and TrickResults routines will be called. PlayTrick provides you with the seat index of the player leading this trick, the cards in your hand at the beginning of this trick, and the cards already played on this trick. On the first trick, yourHand will be the same as dealtHand, except that the cards you passed will be replaced by the cards that were passed to you. On subsequent tricks, yourHand will be unchanged except to remove the card you played on the previous trick (by changing the suit and spot fields to kNoSuit and kNoSpot, respectively). When PlayTrick is called, you select the card you wish to play and return in yourPlay its index in yourHand. After all four players have played, your TrickResults routine will be called to identify the winner of the trick (trickWinner) and all of the cards played on the lastTrick.

The two of clubs is led on the first trick of each hand - the test code will ensure that the PlayTrick routine of the player who has the two of clubs is called first on that trick. The player who won the previous trick leads the next trick. Hearts may not be led until a point card has been played on an earlier trick, unless a player has only hearts left in his hand. The queen of spades may be led at any time.

If the number of entries is reasonable, the tournament will have each player compete against every combination of other players in all possible seat arrangements. If the number of entries is large, the best four entries will be selected in some fair way, after which those entries will compete against one another in a final tournament. If fewer than four entries are submitted, I will round out the table with a simple-minded player that tries to avoid taking any points.

The winner will be the solution that wins the tournament by achieving the lowest score. The score will be the sum of the tournament points earned and a time penalty of one point per millisecond of execution time. The execution time of all of your routines (InitTournament, InitGame, SelectPass, PlayTrick, TrickResults, and HandResults) will count toward the time penalty.

This will be a native PowerPC Challenge, using the latest CodeWarrior environment. Solutions may be coded in C, C++, or Pascal. Thanks to Jim Lloyd for suggesting this Challenge.

Three Months Ago Winner

Congratulations to Tom Saxton for winning his second consecutive Challenge, this time by efficiently controlling the elevators in the Programmer's Challenge Skyscraper. Entries in the Elevator Challenge were required to deliver passengers to their destinations during a simulated workday, starting with their arrival at the lower floors at the beginning of the day, continuing with conduct of their jobs in the morning, through the lunch hour, during another period of roaming during the afternoon, and concluding with their departure at the end of the day. Tom won the Challenge by requiring passengers to spend the least amount of time in elevators waiting to be delivered to their destinations.

One of the keys to the success of Tom's solution was his management of express elevators. Express elevators in our Skyscraper are somewhat unusual, in that they can be dynamically converted between express and non-express status. Tom allocates 80% of the elevators to be of the express type, where each express elevator stops at the bottom three floors (the parking garage, the ground floor, and the cafeteria floor) and an equally divided share of the remaining floors. This efficiently delivered passengers at the beginning and end of the day, as well as during lunch. Tom converts an express elevator into a normal elevator under a number of conditions, including when the express elevator is closest to a call from a floor outside of its range, and while on the way to delivering passengers to higher or lower floors.

The second and third place solutions of JG Heithcock and Dave Nebinger did not attempt to use the express elevator option, nor did the solution of Gregory Sadetsky. Ernst Munter and Ludovic Nicolle used express elevators, but with less success than the winning solution.

Here are the statistics for all entries to the Elevator Challenge. Solutions were evaluated using a simulated skyscraper of 100 floors, with 50 elevators, and 20000 building occupants. The Points column lists the number of passenger-timesteps required to deliver passengers to their destinations. The Elevator execution time in milliseconds is also listed, although the execution time penalty turned out to be too small to affect the results. The Score column is the sum of the passenger-timestep points and the time penalty of one point per ten milliseconds. Also listed are the code and data sizes for the entries, along with the programming language used. As usual, the number in parentheses after the entrant's name is the total number of Challenge points earned in all Challenges prior to this one.

Name Points (x 10^6) Time (msecs) Score (x 10^6) Code Size Data Size Lang
Tom Saxton (39) 87.9 49228 87.9 4824 8 C
JG Heithcock (27) 173.4 52831 173.4 1496 251552 C
Dave Nebinger 221.3 89020 221.3 1920 254780 C
Ernst Munter (384) 226.1 144501 226.1 6664 269457 C++
Gregory Sadetsky 237.3 105669 237.3 2296 305684 C
Ludovic Nicolle (48) 265.7 78193 265.7 1952 258988 C

Top Contestants

Here are the Top Contestants for the Programmer's Challenge, including everyone who has accumulated 20 or more points during the past two years. The numbers below include points awarded over the 24 most recent contests, including points earned by this month's entrants.

  1. Munter, Ernst 174
  2. Boring, Randy 76
  3. Mallett, Jeff 50
  4. Saxton, Tom 49
  5. Rieken, Willeke 47
  6. Cooper, Greg 44
  7. Heithcock, JG 37
  8. Nicolle, Ludovic 34
  9. Lewis, Peter 31
  10. Maurer, Sebastian 30
  11. Murphy, ACC 24
  12. Gregg, Xan 22
  13. Hart, Alan 21
  14. Antoniewicz, Andy 20
  15. Day, Mark 20
  16. Higgins, Charles 20
  17. Hostetter, Mat 20
  18. Studer, Thomas 20

There are three ways to earn points: (1) scoring in the top 5 of any Challenge, (2) being the first person to find a bug in a published winning solution or, (3) being the first person to suggest a Challenge that I use. The points you can win are:

  • 1st place 20 points
  • 2nd place 10 points
  • 3rd place 7 points
  • 4th place 4 points
  • 5th place 2 points
  • Finding bug 2 points
  • Suggesting Challenge 2 points

Here is Tom's winning Elevator solution:

Elevator.c
Copyright © 1998 Tom Saxton

#include "Elevator.h"
#include <stdlib.h> // for malloc and free
enum
{
   fFalse = 0,
   fTrue = 1
};
#define DIM(a)      (sizeof(a)/sizeof(a[0]))

// disable debug assert code
#define Assert(f)
#define AssertSz(f,sz)

typedef struct EFLOOR EFLOOR;
struct EFLOOR
{
   int celevUp;
   int celevDown;
   int fCallUp;
   int fCallDown;
   int fUpBusy;
   int fDownBusy;
   int cuserLoadUp;
   int cuserLoadDown;
   int cuserDest;
};

typedef struct ELEV ELEV;
struct ELEV
{
   int       fExpectLoad;
   int       cuser;
   int       cuserUnload;
   int       cuserLoaded;
   int       floorNextStop;
   
   int       fWantExpress;
   int       fExpress;
   int       fNeverExpress;
   
   int       floorHigh;
   int       floorLow;
   CarAction actionPrev;
};

typedef enum
{
   dirUpward,
   dirDownward,
   dirMixed
} DIR;

static void _SetElevAction(int ielev, CarAction action,
            CarAction mp_ielev_action[], CarState mp_ielev_state[],
      int cfloor);
static void _SendElevToFloor(int ielev, int floorWant, 
            CarAction actionDft, CarAction mp_ielev_action[], 
            CarState mp_ielev_state[]);
static void _UpdateElev(int ielev, ELEV aelev[], CarAction action, 
            CarState *pstate, int cfloor, EFLOOR dnfloor[]);
static int _IelevFindNearest(int floorWant, CarAction mp_ielev_action[],
            CarState mp_ielev_state[], int celev);
static void _ClearFloorStats(int cfloor, EFLOOR dnfloor[]);
static void _ClearExpressElev(int ielev, ELEV *pelev, Boolean 
stopsAtFloor[kMaxElevators][kMaxFloors], int cfloor);
static void _SetOneExpressElev(int ielev, ELEV aelev[], Boolean
stopsAtFloor[kMaxElevators][kMaxFloors], int cfloor, int floorMin,
             int floorMac, int i, int c);
static void _SetExpressElevators(int celev, ELEV aelev[], 
             CarState mp_ielev_state[], int cfloor, Boolean
             stopsAtFloor[kMaxElevators][kMaxFloors]);

// set the action of the specified elevator to the specified value
// (the debug version did sanity checking)
#define _SetElevAction(ielev, action, mp_ielev_action, mp_ielev_state, cfloor) \
   (mp_ielev_action)[ielev] = action

// parameters to AdvanceTime
//
// static CarAction   mp_ielev_action[kMaxElevators];
//      /* direction you move each elevator */
// static CarState      mp_ielev_state[kMaxElevators];
//      /* returns new state of each elevator */
// static Boolean      stopsAtFloor[kMaxElevators][kMaxFloors];
//      /* stopsAtFloor[i]==TRUE means elevator stops at floor i */
// static Boolean      callGoingUp[kMaxFloors];
//      /* callGoingUp[i]==TRUE means a passenger on floor i wants to go up */
// static Boolean      callGoingDown[kMaxFloors];
//      /* callGoingDown[i]==TRUE means a passenger on floor i wants to go down */

Elevator
void Elevator(
  long cfloor, /* number of floors in our building, < kMaxFloors */
  long celev,  /* number of elevators in our building, < kMaxElevators */
  AdvanceTimeProc pfnAdvanceTime  /* callback to get new state */
)
{
   int         ielev;
   int         floor;
   DIR         dir;
   
   /* direction you move each elevator */
   CarAction *   mp_ielev_action = 
                 malloc(sizeof(CarAction) * kMaxElevators);
   /* returns new state of each elevator */
   CarState *   mp_ielev_state = 
                malloc(sizeof(CarState) * kMaxElevators);
   /* stopsAtFloor[i]==TRUE means elevator stops at floor i */
   Boolean    (*stopsAtFloor)[kMaxFloors] = 
   malloc(sizeof(Boolean)*kMaxElevators*kMaxFloors);   
   /* callGoingUp[i]==TRUE means a passenger on floor i wants to go up */
   Boolean *   callGoingUp = malloc(sizeof(Boolean) * kMaxFloors);
   /* callGoingDown[i]==TRUE means a passenger on floor i wants to go down */
   Boolean   *   callGoingDown = malloc(sizeof(Boolean) * kMaxFloors);
   
   EFLOOR *   dnfloor = (EFLOOR *)malloc(sizeof(EFLOOR) * cfloor);
   ELEV *     aelev = (ELEV *)malloc(sizeof(ELEV) * celev);
   
   Assert(mp_ielev_action != NULL);
   Assert(mp_ielev_state != NULL);
   Assert(stopsAtFloor != NULL);
   Assert(callGoingUp != NULL);
   Assert(callGoingDown != NULL);
   Assert(dnfloor != NULL);
   Assert(aelev != NULL);

   for (ielev = 0; ielev < celev; ++ielev)
   {
      ELEV *      pelevT = &aelev[ielev];
      CarState *   pstateT = &mp_ielev_state[ielev];
      
      mp_ielev_action[ielev] = kStoppedGoingUp;

      pstateT->atFloor = 0;
      for (floor = 0; floor < cfloor; ++floor)
      {
         pstateT->goingToFloor[floor] = 0;
         stopsAtFloor[ielev][floor] = TRUE;
      }
      pelevT->fExpress = pelevT->fNeverExpress = fFalse;
      pelevT->fWantExpress = fTrue;
      
      pelevT->fExpectLoad = fFalse;
      pelevT->cuser = pelevT->cuserUnload
                       = pelevT->cuserLoaded = 0;
   }
      
   _ClearFloorStats(cfloor, dnfloor);
   dnfloor[0].celevUp = celev;

   for(;;)
   {
      CarAction   action;
      int         floorCallLow, floorCallHigh;
      int         cCallUp, cCallDown;
      ELEV *      pelev = NULL;
      CarState *  pstate = NULL;
      int         celevIdle = 0;
      
      // set fields to indicate what we are expecting to happen
         for each elevator
      // during the upcoming time slice
      for (ielev = 0; ielev < celev; ++ielev)
      {
         action = mp_ielev_action[ielev];
         pstate = &mp_ielev_state[ielev];
         pelev = &aelev[ielev];

         switch (pelev->actionPrev = action)
         {
         case kStoppedGoingUp:
            pelev->fExpectLoad = dnfloor[pstate->atFloor].fCallUp;
            goto LStop;
            
         case kStoppedGoingDown:
            pelev->fExpectLoad = dnfloor[pstate->atFloor].fCallDown;
            goto LStop;
            
         case kStoppedIdle:
LStop:
            Assert(stopsAtFloor[ielev][pstate->atFloor]);
            pelev->cuserUnload = pstate->goingToFloor
                                    [pstate->atFloor];
            break;
            
         default:
            pelev->fExpectLoad = fFalse;
            pelev->cuserUnload = 0;
            break;
         }
      }
      
      // if appropriate, set up express elevators
      _SetExpressElevators(celev, aelev, mp_ielev_state, 
                  cfloor, stopsAtFloor);
      
      // Advance Time
      if ((*pfnAdvanceTime)(mp_ielev_action, mp_ielev_state, 
                     stopsAtFloor, callGoingUp, callGoingDown))
      {
         break;
      }
      
      // update floor stats
      _ClearFloorStats(cfloor, dnfloor);
      floorCallLow = cfloor;
      floorCallHigh = -1;
      cCallUp = cCallDown = 0;
      for (floor = 0; floor < cfloor; ++floor)
      {
         EFLOOR *   pefloor = &dnfloor[floor];
         
         if (!callGoingUp[floor] && !callGoingDown[floor])
            continue;
         if (callGoingUp[floor])
         {
            ++cCallUp;
            pefloor->fCallUp = fTrue;
            if (floor < floorCallLow)
               floorCallLow = floor;
         }
         if (callGoingDown[floor])
         {
            ++cCallDown;
            pefloor->fCallDown = fTrue;
            if (floor > floorCallHigh)
               floorCallHigh = floor;
         }
      }

      // update elevator stats
      for (ielev = 0; ielev < celev; ++ielev)
      {
         int cuserRemain;
         
         action = mp_ielev_action[ielev];
         pstate = &mp_ielev_state[ielev];
         pelev = &aelev[ielev];
         
         cuserRemain = pelev->cuser - pelev->cuserUnload;

         _UpdateElev(ielev, aelev, mp_ielev_action[ielev], 
                  pstate, cfloor, dnfloor);
         pelev->cuserLoaded = pelev->cuser - cuserRemain;
         
         Assert(0 <= pelev->cuserLoaded && 
                     pelev->cuserLoaded <= kElevatorCapacity);

         if (pelev->cuserLoaded > 0)
         {
            Assert(action == kStoppedGoingUp || 
                           action == kStoppedGoingDown);
            if (action == kStoppedGoingUp)
               dnfloor[pstate->atFloor].cuserLoadUp += 
                        pelev->cuserLoaded;
            if (action == kStoppedGoingDown)
               dnfloor[pstate->atFloor].cuserLoadDown += 
                        pelev->cuserLoaded;
         }

         if (pelev->cuser == kElevatorCapacity)
         {
            if (action != kStoppedGoingDown)
               dnfloor[pstate->atFloor].fUpBusy = fTrue;
            if (action != kStoppedGoingUp)
               dnfloor[pstate->atFloor].fDownBusy = fTrue;
         }
      }
      
      // what's the traffic pattern?
      {
         int cuserUpper, cuserGround;
         
         cuserUpper = 0;
         for (floor = 3; floor < cfloor; ++floor)
            cuserUpper += dnfloor[floor].cuserDest;
         cuserGround = dnfloor[0].cuserDest + 
                        dnfloor[1].cuserDest + dnfloor[2].cuserDest;
         
         dir = dirMixed;
         if (cuserGround + cuserUpper > 0)
         {
            if (cuserUpper > cuserGround)
            {
               int cCallUpFromGround;
               
               cCallUpFromGround = 0;
               for (floor = 0; floor <= 2; ++floor)
                  if (callGoingUp[floor])
                     ++cCallUpFromGround;
               
               if (cCallUpFromGround >= 2)
                  dir = dirUpward;
            }
            
            if (dir == dirMixed)
            {
               cuserGround *= (cfloor - 3);
               cuserUpper *= 3;
               
               if (cuserGround > 2*cuserUpper)
                  dir = dirDownward;
               else if (cuserUpper > 2*cuserGround)
                  dir = dirUpward;
            }
         }
         else if (cCallUp + cCallDown > 0)
         {
            if (cCallUp > 3*cCallDown)
               dir = dirUpward;
            else if (cCallDown > 3*cCallUp)
               dir = dirDownward;
         }
      }

      // process the elevators
      Assert(celevIdle == 0);
      for (ielev = 0; ielev < celev; ++ielev)
      {            
         action = mp_ielev_action[ielev];
         pstate = &mp_ielev_state[ielev];
         pelev = &aelev[ielev];

         pelev->fWantExpress = fTrue;
         switch(action)
         {
         case kStoppedGoingUp:
            if (pelev->fExpectLoad && pelev->cuserUnload == 0 && 
                     pelev->cuserLoaded == 0 && 
                     dnfloor[pstate->atFloor].cuserLoadUp == 0 && 
                     callGoingUp[pstate->atFloor])
            {
         _ClearExpressElev(ielev, pelev, stopsAtFloor, cfloor);
               break;
            }
            // fall through
         case kGoingUp:
            if (pelev->floorNextStop == -1)
               goto LIdle;
               
            Assert(pelev->floorNextStop >= pstate->atFloor);
            action = kGoingUp;
            if (pelev->floorNextStop == pstate->atFloor)
            {
               action = kStoppedGoingUp;
            }
            else if (stopsAtFloor[ielev][pstate->atFloor]
               && callGoingUp[pstate->atFloor]
               && mp_ielev_action[ielev] == kGoingUp
               && pelev->cuser < kElevatorCapacity)
            {
               if (dnfloor[pstate->atFloor].celevUp == 0 || 
                        dnfloor[pstate->atFloor].fUpBusy)
               {
                  action = kStoppedGoingUp;
                  if (!pelev->fExpress)
                     ++dnfloor[pstate->atFloor].celevUp;
               }
            }
            else
            {
            }
            _SetElevAction(ielev, action, mp_ielev_action, 
                     mp_ielev_state, cfloor);
            break;

         case kGoingDown:
            if (pelev->fExpectLoad && pelev->cuserUnload == 0 && 
                     pelev->cuserLoaded == 0 && 
                     dnfloor[pstate->atFloor].cuserLoadDown == 0 && 
                     callGoingDown[pstate->atFloor])
            {
         _ClearExpressElev(ielev, pelev, stopsAtFloor, cfloor);
               break;
            }
            // fall through
         case kStoppedGoingDown:
            if (pelev->floorNextStop == -1)
               goto LIdle;
               
            Assert(pelev->floorNextStop <= pstate->atFloor);
            action = kGoingDown;
            if (pelev->floorNextStop == pstate->atFloor)
            {
               action = kStoppedGoingDown;
            }
            else if (stopsAtFloor[ielev][pstate->atFloor]
               && callGoingDown[pstate->atFloor]
               && mp_ielev_action[ielev] == kGoingDown
               && pelev->cuser < kElevatorCapacity)
            {
               if (dnfloor[pstate->atFloor].celevDown == 0 || 
                        dnfloor[pstate->atFloor].fDownBusy)
               {
                  action = kStoppedGoingDown;
                  if (!pelev->fExpress)
                     ++dnfloor[pstate->atFloor].celevDown;
               }
            }
            _SetElevAction(ielev, action, mp_ielev_action, 
                  mp_ielev_state, cfloor);
            
            break;

         case kStoppedIdle:
LIdle:
            {
               int cuserUp, cuserDown;
               
               cuserUp = cuserDown = 0;
               for (floor = 0; floor < pstate->atFloor; ++floor)
                  cuserDown += pstate->goingToFloor[floor];
         for (floor = pstate->atFloor; floor < cfloor; ++floor)
                  cuserUp += pstate->goingToFloor[floor];
               
               if (cuserUp + cuserDown > 0)
               {
                  _SetElevAction(ielev, 
                     cuserUp > cuserDown ? kGoingUp : kGoingDown, 
                        mp_ielev_action, mp_ielev_state, cfloor);
               }
               else
               {
                  // mark elevator as idle
                  mp_ielev_action[ielev] = kStoppedIdle;
                  ++celevIdle;
               }
            }
            break;
         
         default:
            AssertSz(fFalse, "illegal action value");
            break;
         }   // switch action
      }      // for ielev

      // if there are idle elevators, figure out which way to send them
      if (celevIdle > 0)
      {
         int cCallSkipPerElev, cElevPerCall;
         int cCalls;
         
         cCalls = cCallUp + cCallDown;
                           
         if (celevIdle > 0 && cCalls > 0)
         {
            int ielevDown;
            
            // try to send each idle express elevator to a call in its range
            for (ielevDown = 0; ielevDown < celev; ++ielevDown)
            {
            int      floorCur = mp_ielev_state[ielevDown].atFloor;

               if (mp_ielev_action[ielevDown] != kStoppedIdle)
                  continue;
               
               if (!aelev[ielevDown].fWantExpress || 
                           aelev[ielevDown].fNeverExpress)
                  continue;
               
               if (dir == dirUpward && floorCur > 2 && 
                  aelev[ielevDown].fExpress && floorCallLow <= 2)
               {
                  // let's stay the course and head for the ground floors...
                  if (callGoingDown[floorCur] && 
                           dnfloor[floorCur].celevDown == 0 && 
               aelev[ielevDown].actionPrev != kStoppedGoingDown)
                  {                     
         // as long as we are going anyway, try for some passengers on the way down
                  _ClearExpressElev(ielevDown, &aelev[ielevDown], 
                              stopsAtFloor, cfloor);
                     _SetElevAction(ielevDown, kStoppedGoingDown, 
                        mp_ielev_action, mp_ielev_state, cfloor);
                  }
                  else
                  {
                     // just send it
                     _SendElevToFloor(ielevDown, floorCallLow, 
               kStoppedGoingUp, mp_ielev_action, mp_ielev_state);
                  }
                  
                  -celevIdle;
                  continue;
               }
                  
               if (floorCur <= 2 && callGoingUp[floorCur] && 
                        dnfloor[floorCur].celevUp == 0 && 
                        aelev[ielevDown].actionPrev == kGoingUp)
               {                  
            // as long as we are going anyway, try for some passengers on the way up
                  _SetElevAction(ielevDown, kStoppedGoingUp, 
                        mp_ielev_action, mp_ielev_state, cfloor);
                  
                  -celevIdle;
                  continue;
               }
               
               for (floor = aelev[ielevDown].floorHigh; 
                                 -floor >= aelev[ielevDown].floorLow; )
               {
                  Assert(0 <= floor && floor < cfloor);
                  Assert(stopsAtFloor[ielevDown][floor]);
                  
                  if (callGoingDown[floor])
                  {
                     -celevIdle;
                     if (aelev[ielevDown].fExpress && 
                              stopsAtFloor[ielevDown][floorCur] && 
                              callGoingUp[floorCur] && 
                              dnfloor[floorCur].celevUp == 0 && 
                  aelev[ielevDown].actionPrev != kStoppedGoingUp)
                     {
            // as long as we are going anyway, try for some passengers on the way up
                        _SetElevAction(ielevDown, kStoppedGoingUp, 
                           mp_ielev_action, mp_ielev_state, cfloor);
                     }
                     else
                     {
                        // just send it
            _SendElevToFloor(ielevDown, floor, kStoppedGoingDown, 
                                 mp_ielev_action, mp_ielev_state);
                     }
                     // clear the call we're going to handle for 
                     ( ; floor >= aelev[ielevDown].floorLow; -floor)
                     {
                        Assert(0 <= floor && floor < cfloor);
                        if (callGoingDown[floor])
                        {
                           callGoingDown[floor] = fFalse;
                           -cCalls;
                           -cCallDown;
                           break;
                        }
                     }
                     break;
                  }
               }
               
               // if the elevator can't do anything in its express zone, un-express it
               if (mp_ielev_action[ielevDown] == kStoppedIdle && 
                                    dir == dirDownward)
               {
                  _ClearExpressElev(ielevDown, &aelev[ielevDown], 
                              stopsAtFloor, cfloor);
               }
            }
            
            // recalc highest floor with a down call
   while (floorCallHigh > 0 && !callGoingDown[floorCallHigh])
               -floorCallHigh;
         }
                  
         if (cCalls > 0)
         {

            cElevPerCall = 0;
            cCallSkipPerElev = cCalls/celevIdle;
            if (cCallSkipPerElev == 0)
               cElevPerCall = celevIdle/cCalls;
         }
         
         while (cCalls > 0 && celevIdle > 0)
         {
            int ielevNearest, cSkip;

            if (cCallUp > cCallDown)
            {
               // dispatch an up elevator
               
      for (cSkip = cElevPerCall; cSkip- >= 0 && celevIdle > 0; 
                                    -celevIdle)
               {
                  // find the nearest elevator
                  ielevNearest = _IelevFindNearest(floorCallLow, 
                        mp_ielev_action, mp_ielev_state, celev);
            Assert(0 <= ielevNearest && ielevNearest < cfloor);
            
                  // if we're sending an elevator out of its home range,
                  // don't make any more elevators into express elevators.
                  if (floorCallLow > 2 && 
                  (floorCallLow < aelev[ielevNearest].floorLow || 
                     aelev[ielevNearest].floorHigh < floorCallLow))
                  {
            _ClearExpressElev(ielevNearest, &aelev[ielevNearest], 
                           stopsAtFloor, cfloor);
                  }
                  
                  // send it
                  _SendElevToFloor(ielevNearest, floorCallLow,
                  kStoppedGoingUp, mp_ielev_action, mp_ielev_state);
               }
               
               // skip some up calls
         for (cSkip = cCallSkipPerElev, floor = floorCallLow; 
              cSkip > 0 && floor < cfloor; ++floor)
               {
                  if (callGoingUp[floor])
                  {
                     callGoingUp[floor] = fFalse;
                     -cCallUp;
                     -cSkip;
                  }
               }
               
               // find new low call floor
               while (floorCallLow < cfloor && 
                                 !callGoingUp[floorCallLow])
                  ++floorCallLow;
            }
            else
            {
               // dispatch a down elevator
               
      for (cSkip = cElevPerCall; cSkip- >= 0 && celevIdle > 0; 
                                                      -celevIdle)
               {
                  // find the nearest elevator
                  ielevNearest = _IelevFindNearest(floorCallHigh, 
                           mp_ielev_action, mp_ielev_state, celev);
            Assert(0 <= ielevNearest && ielevNearest < cfloor);
            
                  // if we're sending an elevator out of its home range,
                  // don't make any more elevators into express elevators.
                  if (floorCallHigh > 2 && 
                  (floorCallHigh < aelev[ielevNearest].floorLow || 
                  aelev[ielevNearest].floorHigh < floorCallHigh))
                  {
            _ClearExpressElev(ielevNearest, &aelev[ielevNearest], 
                           stopsAtFloor, cfloor);
                  }
                  
                  // send it
                  _SendElevToFloor(ielevNearest, floorCallHigh,
                  kStoppedGoingDown, mp_ielev_action, mp_ielev_state);
               }
               
               // skip some down calls
         for (cSkip = cCallSkipPerElev, floor = floorCallHigh; 
                        cSkip > 0 && floor > 0; -floor)
               {
                  if (callGoingDown[floor])
                  {
                     callGoingDown[floor] = fFalse;
                     -cCallDown;
                     -cSkip;
                  }
               }
               
               // find new low call floor
               while (floorCallHigh > 0 && 
                                       !callGoingDown[floorCallHigh])
                  -floorCallHigh;
               
            }
            
            cCalls = cCallUp + cCallDown;
         }
         
         if (celevIdle > 0)
         {
            int ielevSend, celevSent, floorWant;
            
            // send everything else to the ground floor
         for (ielevSend = celevSent = 0; celevSent < celevIdle; 
                           ++ielevSend)
            {
               Assert(0 <= ielevSend && ielevSend < celev);
               if (mp_ielev_action[ielevSend] != kStoppedIdle)
                  continue;

               floorWant = aelev[ielevSend].floorLow;
               
               ++celevSent;
               _SendElevToFloor(ielevSend, floorWant, 
                     floorWant < cfloor/2 ? kStoppedGoingUp : 
                                                         kStoppedGoingDown, 
                     mp_ielev_action, mp_ielev_state);
            }
         }   // celevIdle
      }      // if celevIdle > 0
   }         // while !AdvanceTime
   
   free(mp_ielev_action);
   free(mp_ielev_state);
   free(stopsAtFloor);
   free(callGoingUp);
   free(callGoingDown);
   free(dnfloor);
   free(aelev);
}

SetExpressElevators
// setup express elevators
static void _SetExpressElevators(int celev, ELEV aelev[], CarState
mp_ielev_state[], int cfloor, Boolean stopsAtFloor[kMaxElevators][kMaxFloors])
{
   int         ielev;
   int         celevExpress;

   // allocate 4/5th of the elevators as express elevators.
   // note that there will be no express elevators unless there
   // are at least 3 elevators total
   celevExpress = 4*celev/5;
   if (celevExpress < 2)
      celevExpress = 0;
   
   // setup the express elevators
   for (ielev = 0; ielev < celevExpress; ++ielev)
   {
      ELEV *      pelev = &aelev[ielev];
      CarState *   pstate = &mp_ielev_state[ielev];
      
      if (!pelev->fExpress && pelev->fWantExpress && 
            (pelev->cuser == 0 || 
      (pelev->cuser == pstate->goingToFloor[pstate->atFloor] && 
                     pstate->atFloor <= 2)))
      {
         pelev->fExpress = fTrue;
         
      _SetOneExpressElev(ielev, aelev, stopsAtFloor, cfloor, 3, 
                  cfloor, ielev, celevExpress);
      }
   }
   
   // setup the non-express elevators
   Assert(ielev == celevExpress);
   for ( ; ielev < celev; ++ielev)
   {
      ELEV *      pelev = &aelev[ielev];
      
      if (pelev->fExpress)
         _ClearExpressElev(ielev, pelev, stopsAtFloor, cfloor);
      pelev->fNeverExpress = fTrue;
      pelev->floorLow = 0;
      pelev->floorHigh = cfloor - 1;
   }
}

SetOneExpressElev
// set properties for a single express elevator.
// floors between 3 and cfloor-1 are divided evenly among "c" elevators,
// of which the specifiec elevator is number "i" in the sequence
static void _SetOneExpressElev(int ielev, ELEV aelev[], Boolean
stopsAtFloor[kMaxElevators][kMaxFloors], int cfloor, int floorMin, int floorMac, int i, int c)
{
   int floorT, floorLow, floorHigh;
   ELEV *pelev = &aelev[ielev];
   
   Assert(0 <= floorMin && floorMin
                                    < floorMac && floorMac <= cfloor);
   
   i = c - i - 1;
   floorLow =  ((c - i) * (floorMin) + (i) * (floorMac))/(c);
   ++i;
   floorHigh =  ((c - i) * (floorMin) + (i) * (floorMac))/(c);
   
   Assert(floorMin <= floorLow && floorLow
                              < floorHigh && floorHigh <= floorMac);
   
   pelev->floorLow = floorLow;
   pelev->floorHigh = floorHigh;
   for (floorT = 0; floorT < cfloor; ++floorT)
      stopsAtFloor[ielev][floorT] = floorT <= 2 ||
                        (floorLow <= floorT && floorT < floorHigh);
   
   pelev->fExpress = fTrue;
}

ClearExpressElev
// clear the express status from an elevator
static void _ClearExpressElev(int ielev, ELEV *pelev, 
Boolean stopsAtFloor[kMaxElevators][kMaxFloors], int cfloor)
{
   int floorT;
   
   if (pelev->fExpress)
   {
      for (floorT = 0; floorT < cfloor; ++floorT)
         stopsAtFloor[ielev][floorT] = fTrue;
      pelev->fExpress = fFalse;
   }
   pelev->fWantExpress = fFalse;
}

ClearFloorStats
// zero out the floor stats
static void _ClearFloorStats(int cfloor, EFLOOR dnfloor[])
{
   int floor;

   for (floor = 0; floor < cfloor; ++floor)
   {
      EFLOOR *pefloor = &dnfloor[floor];
      
      pefloor->celevUp = pefloor->celevDown = 0;
      pefloor->fCallUp = pefloor->fCallDown = fFalse;
      pefloor->cuserLoadUp = pefloor->cuserLoadDown = 
          pefloor->cuserDest = 0;
      pefloor->fUpBusy = pefloor->fDownBusy = fFalse;
   }
}

SendElevToFloor
// send the specified elevator to the specified floor
// if it's already there, set its action to actionDft.
static void _SendElevToFloor(int ielev, int floorWant,
CarAction actionDft, CarAction mp_ielev_action[], 
CarState mp_ielev_state[])
{
   CarAction   action;
   int         floorCur;
   
   floorCur = mp_ielev_state[ielev].atFloor;
   if (floorCur == floorWant)
   {
      action = actionDft;
   }
   else
   {
      action = floorWant > floorCur ? kGoingUp : kGoingDown;
   }
   
   _SetElevAction(ielev, action, mp_ielev_action,
              mp_ielev_state, cfloor);
}

UpdateElev
// update the stats for the specifed elevator
static void _UpdateElev(int ielev, ELEV aelev[], CarAction action, 
CarState *pstate, int cfloor, EFLOOR dnfloor[])
{
   int      cuser;
   int      floor, floorCur, floorHigh, floorLow;
   
   ELEV *   pelev = &aelev[ielev];
   
   floorHigh = -1;
   floorLow = cfloor;
   floorCur = pstate->atFloor;
   
   cuser = 0;
   for (floor = 0; floor < cfloor; ++floor)
   {
      int cuserFloor = pstate->goingToFloor[floor];
      if (cuserFloor == 0)
         continue;
      Assert(stopsAtFloor[ielev][floor]);
      cuser += cuserFloor;
      dnfloor[floor].cuserDest += cuserFloor;
      
      if (floor >= floorCur && floorHigh == -1)
         floorHigh = floor;
      if (floor <= floorCur)
         floorLow = floor;
   }
   
   pelev->cuser = cuser;
   pelev->floorNextStop = -1;
   
   switch (action)
   {
   case kStoppedGoingUp:
   case kGoingUp:
      if (floorHigh >= pstate->atFloor)
         pelev->floorNextStop = floorHigh;
      break;
   
   case kStoppedGoingDown:
   case kGoingDown:
      if (floorLow <= pstate->atFloor)
         pelev->floorNextStop = floorLow;
      break;
   
   case kStoppedIdle:
      break;
   }
}

IelevFindNearest
// return the index to the idle elevator nearest to floorWant
static int _IelevFindNearest(int floorWant, CarAction mp_ielev_action[],
CarState mp_ielev_state[], int celev)
{
   int      dfloorBest, dfloor;
   int      ielev, ielevBest;

   ielevBest = -1;
   dfloorBest = kMaxFloors;
   for (ielev = 0; ielev < celev; ++ielev)
   {
      if (mp_ielev_action[ielev] != kStoppedIdle)
         continue;
      
      dfloor = abs(mp_ielev_state[ielev].atFloor - floorWant);
      
      if (dfloor >= dfloorBest)
         continue;
      
      ielevBest = ielev;
      if ((dfloorBest = dfloor) == 0)
         break;
   }
   
   return ielevBest;
}
 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Challenge those pesky wyverns to a dance...
After recently having you do battle against your foes by wildly flailing Hello Kitty and friends at them, GungHo Online has whipped out another surprising collaboration for Puzzle & Dragons. It is now time to beat your opponents by cha-cha... | Read more »
Pack a magnifying glass and practice you...
Somehow it has already been a year since Torchlight: Infinite launched, and XD Games is celebrating by blending in what sounds like a truly fantastic new update. Fans of Cthulhu rejoice, as Whispering Mist brings some horror elements, and tests... | Read more »
Summon your guild and prepare for war in...
Netmarble is making some pretty big moves with their latest update for Seven Knights Idle Adventure, with a bunch of interesting additions. Two new heroes enter the battle, there are events and bosses abound, and perhaps most interesting, a huge... | Read more »
Make the passage of time your plaything...
While some of us are still waiting for a chance to get our hands on Ash Prime - yes, don’t remind me I could currently buy him this month I’m barely hanging on - Digital Extremes has announced its next anticipated Prime Form for Warframe. Starting... | Read more »
If you can find it and fit through the d...
The holy trinity of amazing company names have come together, to release their equally amazing and adorable mobile game, Hamster Inn. Published by HyperBeard Games, and co-developed by Mum Not Proud and Little Sasquatch Studios, it's time to... | Read more »
Amikin Survival opens for pre-orders on...
Join me on the wonderful trip down the inspiration rabbit hole; much as Palworld seemingly “borrowed” many aspects from the hit Pokemon franchise, it is time for the heavily armed animal survival to also spawn some illegitimate children as Helio... | Read more »
PUBG Mobile teams up with global phenome...
Since launching in 2019, SpyxFamily has exploded to damn near catastrophic popularity, so it was only a matter of time before a mobile game snapped up a collaboration. Enter PUBG Mobile. Until May 12th, players will be able to collect a host of... | Read more »
Embark into the frozen tundra of certain...
Chucklefish, developers of hit action-adventure sandbox game Starbound and owner of one of the cutest logos in gaming, has released their roguelike deck-builder Wildfrost. Created alongside developers Gaziter and Deadpan Games, Wildfrost will... | Read more »
MoreFun Studios has announced Season 4,...
Tension has escalated in the ever-volatile world of Arena Breakout, as your old pal Randall Fisher and bosses Fred and Perrero continue to lob insults and explosives at each other, bringing us to a new phase of warfare. Season 4, Into The Fog of... | Read more »
Top Mobile Game Discounts
Every day, we pick out a curated list of the best mobile discounts on the App Store and post them here. This list won't be comprehensive, but it every game on it is recommended. Feel free to check out the coverage we did on them in the links below... | Read more »

Price Scanner via MacPrices.net

Free iPhone 15 plus Unlimited service for $60...
Boost Infinite, part of MVNO Boost Mobile using AT&T and T-Mobile’s networks, is offering a free 128GB iPhone 15 for $60 per month including their Unlimited service plan (30GB of premium data).... Read more
$300 off any new iPhone with service at Red P...
Red Pocket Mobile has new Apple iPhones on sale for $300 off MSRP when you switch and open up a new line of service. Red Pocket Mobile is a nationwide MVNO using all the major wireless carrier... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, available for $759 for 8-Core CPU/7-Core GPU/256GB models and $929 for 8-Core CPU/8-Core GPU/512GB models. Apple’s one-year warranty is... Read more
Updated Apple MacBook Price Trackers
Our Apple award-winning MacBook Price Trackers are continually updated with the latest information on prices, bundles, and availability for 16″ and 14″ MacBook Pros along with 13″ and 15″ MacBook... Read more
Every model of Apple’s 13-inch M3 MacBook Air...
Best Buy has Apple 13″ MacBook Airs with M3 CPUs in stock and on sale today for $100 off MSRP. Prices start at $999. Their prices are the lowest currently available for new 13″ M3 MacBook Airs among... Read more
Sunday Sale: Apple iPad Magic Keyboards for 1...
Walmart has Apple Magic Keyboards for 12.9″ iPad Pros, in Black, on sale for $150 off MSRP on their online store. Sale price for online orders only, in-store price may vary. Order online and choose... Read more
Apple Watch Ultra 2 now available at Apple fo...
Apple has, for the first time, begun offering Certified Refurbished Apple Watch Ultra 2 models in their online store for $679, or $120 off MSRP. Each Watch includes Apple’s standard one-year warranty... Read more
AT&T has the iPhone 14 on sale for only $...
AT&T has the 128GB Apple iPhone 14 available for only $5.99 per month for new and existing customers when you activate unlimited service and use AT&T’s 36 month installment plan. The fine... Read more
Amazon is offering a $100 discount on every M...
Amazon is offering a $100 instant discount on each configuration of Apple’s new 13″ M3 MacBook Air, in Midnight, this weekend. These are the lowest prices currently available for new 13″ M3 MacBook... Read more
You can save $300-$480 on a 14-inch M3 Pro/Ma...
Apple has 14″ M3 Pro and M3 Max MacBook Pros in stock today and available, Certified Refurbished, starting at $1699 and ranging up to $480 off MSRP. Each model features a new outer case, shipping is... Read more

Jobs Board

IT Systems Engineer ( *Apple* Platforms) - S...
IT Systems Engineer ( Apple Platforms) at SpaceX Hawthorne, CA SpaceX was founded under the belief that a future where humanity is out exploring the stars is Read more
*Apple* Systems Administrator - JAMF - Activ...
…**Public Trust/Other Required:** None **Job Family:** Systems Administration **Skills:** Apple Platforms,Computer Servers,Jamf Pro **Experience:** 3 + years of Read more
IT Systems Engineer ( *Apple* Platforms) - S...
IT Systems Engineer ( Apple Platforms) at SpaceX Hawthorne, CA SpaceX was founded under the belief that a future where humanity is out exploring the stars is Read more
Nurse Anesthetist - *Apple* Hill Surgery Ce...
Nurse Anesthetist - Apple Hill Surgery Center Location: WellSpan Medical Group, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply Now Read more
Housekeeper, *Apple* Valley Village - Cassi...
Apple Valley Village Health Care Center, a senior care campus, is hiring a Part-Time Housekeeper to join our team! We will train you for this position! In this role, Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.