TweetFollow Us on Twitter

Mar 96 Challenge
Volume Number:12
Issue Number:3
Column Tag:Programmer’s Challenge

Programmer’s Challenge

By Bob Boonstra, Westford, Massachusetts

Note: Source code files accompanying article are located on MacTech CD-ROM or source code disks.

Words The Reverse

Text input, of Block given a in words of order the place (in reverse) will that routine a write to is challenge the month this. Oops, what I meant to say was: This month, the Challenge is to write a routine that will reverse (in place) the order of words in a given block of input text. The prototype for the code you should write is:

pascal void ReverseTheWords(
 const char *text, /* the words you should reverse */
 const long numCharsIn    /* length of inputText in chars */
);

Specifically, ReverseTheWords should exchange the first word in the input text with the last word, the second word with the next-to-last word, etc. For the purpose of this Challenge, a word is defined as a continuous sequence of alphanumeric characters [a..zA..Z0..9]. Any nonalphanumeric characters should remain in their original positions and in their original order with respect to the new words; that is, punctuation, white space, and other characters between the first and second input words should, on output, be located between the new first and second words. As an example, ReverseTheWords would convert:

 This, however, <-is-> a (difficult) test.

into

 Test, difficult, <-a-> is (however) this.

As you can see from the example, there is one additional requirement. Your code needs to adjust the capitalization of words so that the n-th word is capitalized on output only if the n-th word was capitalized in the input text.

There are no specific restrictions on the amount of auxiliary memory you may use (within reason), so you may allocate a few buffers of size numCharsIn should you need them. Remember, however, to deallocate any memory you allocate before returning, as I will be calling your code many times.

Note that the prototype specifies the use of Pascal calling conventions. That is because this month we are conducting

A Language Experiment

Over the past months, in response to suggestions from readers, we have made a number of changes to the Challenge, including migrating to PowerPC native code and expanding to other C compilers. Now we are experimenting with some additional changes. This month, for the first time, your solution to the Challenge can be coded in C, C++, or Pascal, using your choice among the MPW, Metrowerks, or Symantec compilers for these languages. Although either 68K or PowerPC code is permitted, I will be running your code on a PowerMac 8500, so native code is obviously recommended. The environment you choose must support linking your solution with test code written in C. Along with your solution, you should specify the intended environment, compiler and compiler options, or (better yet) provide a project file or make file that will generate a stand-alone application that calls your solution from C test code.

Two Months Ago Winner

Congratulations to Jorg “jbx” Brown and Eric Gundrum for submitting the fastest entry to the Sliding Tiles Challenge. Contestants were asked to unscramble an N¥M grid of interlocking tiles, where a tile could be moved horizontally or vertically into the empty tile location. Fourteen of the 16 entries submitted worked correctly. The table below presents selected results from the timing tests, which used puzzles ranging in size from 8¥8 to 100¥300. (Two of the entries worked too slowly to complete the entire test suite.)

The most common solution technique involved solving the puzzle row-by-row from the bottom, being careful not to disturb tiles that had already been moved into their correct positions. The top two rows were solved together, starting from the right column and working left. Several solutions, including the winners’ and the even more move-efficient second-place entry by Peter Lewis, refined this by using a “greedy” algorithm that solves the bottom row and right column first, then moves to the next row and column. Since the problem involved use of a given callback routine for each move, it was important to reduce the number of tile moves. Some solutions moved tiles primarily in horizontal and vertical directions; others (including the winners’) devised operators that moved a tile diagonally in fewer moves.

Thanks to Greg Linden for sending me a reference to an article on this subject in Information Processing Letters (Oct. 1995). In that article, Ian Parberry (University of North Texas) establishes some bounds on algorithms for the n2-1 puzzle (the square version of this Challenge). The article proves that the “greedy” algorithm requires at most 5n3 moves in the worst case, and that any algorithm must make at least n3 moves (ignoring lower-order terms for both bounds). While a better algorithm may exist, Ian speculates that the lower bound could be made tighter. Interested readers can contact the author at ian@cs.unt.edu, or browse his web page at http://hercule.csci.unt.edu/ian.

Here are the times for each of the correct entries. Numbers in parentheses after a person’s name indicate that person’s cumulative point total for all previous Challenges, not including this one.

Time Time Total # of Moves

Name 20x20 100x300 Time (Millions)

Jorg Brown (10)

& Eric Gundrum 183 175730 261694 249

Peter Lewis 195 197905 294931 228

Christopher Phillips 211 200368 302774 269

Ludovic Nicolle 252 220952 337386 300

Ernst Munter (110) 159 258165 365894 295

Randy Boring 386 309630 481609 307

Cathy Saxton 264 338127 496610 299

Rishi Khan (age 16) 284 350763 506952 320

Xan Gregg (88) 296 360735 531493 299

Tom Saxton (10) 333 404155 592967 339

Miguel Cruz Picão(7) 245 652588 912267 339

John Sweeney (4) 300 875362 1223956 321

A. K. * * * *

G. L. * * * *

Top Contestants Of All Time

Here are the Top Contestants for the Programmer’s Challenges to date, including everyone who has accumulated more than 20 points. The numbers below include points awarded for this month’s entrants.

Rank Name Points Rank Name Points

1. [Name deleted] 176 11. Mallett, Jeff 44

2. Munter, Ernst 112 12. Kasparian, Raffi 42

3. Gregg, Xan 88 13. Vineyard, Jeremy 42

4. Larsson, Gustav 87 14. Lengyel, Eric 40

5. Karsh, Bill 80 15. Darrah, Dave 31

6. Stenger, Allen 65 16. Brown, Jorg 30

7. Riha, Stepan 51 17. Landry, Larry 29

8. Cutts, Kevin 50 18. Elwertowski, Tom 24

9. Goebel, James 49 19. Lee, Johnny 22

10. Nepsund, Ronald 47 20. Noll, Robert 22

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 5th place 2 points

2nd place 10 points finding bug 2 points

3rd place 7 points suggesting Challenge 2 points

4th place 4 points

Here the winning solution by Jorg and Eric:

SlidingTiles.c

Written by Jorg Brown & Eric Gundrum

Thanks also to Brad Kollmyer

typedef Boolean (*MoveProc)(
  long tileToMoveRow, long tileToMoveCol);

static MoveProc gMakeMove;
static long *gTiles, gNumCols, *gPieceRow, *gPieceCol;
static long *gBlankSquare; // initialized by SolveTiles, updated by BlankXXX()

#define gBlankRow pieceRow[0]
#define gBlankCol pieceCol[0]

BlankUp
static void BlankUp() { // move the BLANK SQUARE up
   long   tile, *oldBlankSquare, *newBlankSquare;
   long   *pieceRow = gPieceRow;
   long   *pieceCol = gPieceCol;

   oldBlankSquare = gBlankSquare;
   gBlankSquare = newBlankSquare = oldBlankSquare - gNumCols;
   tile = *newBlankSquare;
   pieceRow[tile] = gBlankRow--;
   gMakeMove(gBlankRow, gBlankCol);
   *oldBlankSquare = tile;
   *newBlankSquare = 0;
}

BlankDown
static void BlankDown() { // move the BLANK SQUARE down
   long   tile, *oldBlankSquare, *newBlankSquare;
   long   *pieceRow = gPieceRow;
   long   *pieceCol = gPieceCol;

   oldBlankSquare = gBlankSquare;
   gBlankSquare = newBlankSquare = oldBlankSquare + gNumCols;
   tile = *newBlankSquare;
   pieceRow[tile] = gBlankRow++;
   gMakeMove(gBlankRow, gBlankCol);
   *oldBlankSquare = tile;
   *newBlankSquare = 0;
}

MoveBlankToRow
static void MoveBlankToRow(long destRow) {
   long   tile, *oldBlankSquare, *newBlankSquare;
   long   *pieceRow = gPieceRow;
   long   *pieceCol = gPieceCol;
   long   blankRow = gBlankRow;
   long   rowInc, BlankSquareInc;

   if (blankRow < destRow) { // move DOWN
      rowInc = 1;
      BlankSquareInc = gNumCols;
   } else if (blankRow > destRow) { // move UP
      rowInc = -1;
      BlankSquareInc = -gNumCols;
   } else return;

   oldBlankSquare = gBlankSquare;
   do {
      newBlankSquare = oldBlankSquare + BlankSquareInc;
      tile = *newBlankSquare;
      pieceRow[tile] = blankRow;
      blankRow += rowInc;
      gMakeMove(blankRow, gBlankCol);
      *oldBlankSquare = tile;
      *newBlankSquare = 0;
      oldBlankSquare = newBlankSquare;
   } while (blankRow != destRow);
   gBlankSquare = newBlankSquare;
   gBlankRow = blankRow;
}

BlankLeft
static void BlankLeft() { // move the BLANK SQUARE left
   long   tile, *oldBlankSquare, *newBlankSquare;
   long   *pieceRow = gPieceRow;
   long   *pieceCol = gPieceCol;

   oldBlankSquare = gBlankSquare;
   gBlankSquare = newBlankSquare = oldBlankSquare - 1;
   tile = *newBlankSquare;
   pieceCol[tile] = gBlankCol--;
   gMakeMove(gBlankRow, gBlankCol);
   *oldBlankSquare = tile;
   *newBlankSquare = 0;
}


BlankRight
static void BlankRight() { // move the BLANK SQUARE right
   long   tile, *oldBlankSquare, *newBlankSquare;
   long   *pieceRow = gPieceRow;
   long   *pieceCol = gPieceCol;

   oldBlankSquare = gBlankSquare;
   gBlankSquare = newBlankSquare = oldBlankSquare + 1;
   tile = *newBlankSquare;
   pieceCol[tile] = gBlankCol++;
   gMakeMove(gBlankRow, gBlankCol);
   *oldBlankSquare = tile;
   *newBlankSquare = 0;
}

MoveBlankToCol
static void MoveBlankToCol(long destCol) {
   long   tile, *oldBlankSquare, *newBlankSquare;
   long   *pieceRow = gPieceRow;
   long   *pieceCol = gPieceCol;
   long   blankCol = gBlankCol;
   long   inc;

   if (blankCol < destCol) { // move RIGHT
      inc = 1;
   } else if (blankCol > destCol) { // move LEFT
      inc = -1;
   } else return;

   oldBlankSquare = gBlankSquare;
   do {
      newBlankSquare = oldBlankSquare + inc;
      tile = *newBlankSquare;
      pieceCol[tile] = blankCol;
      blankCol += inc;
      gMakeMove(gBlankRow, blankCol);
      *oldBlankSquare = tile;
      *newBlankSquare = 0;
      oldBlankSquare = newBlankSquare;
   } while (blankCol != destCol);
   gBlankSquare = newBlankSquare;
   gBlankCol = blankCol;
}

MoveAPiece

static void MoveAPiece(long piece, long destRow, long destCol, 
 long nextPiece) {
   long   sourceRow, sourceCol;
   long   *pieceRow = gPieceRow;
   long   *pieceCol = gPieceCol;
   long   nextRow = gNumCols;

   sourceRow = pieceRow[piece];
   sourceCol = pieceCol[piece];

   if (sourceRow >= destRow) { 
      // in this case, we have to move the tile up (or directly right) to get to its destination
      if (sourceRow == destRow) {
         if (sourceCol == destCol) return;

         // simplify: move the blank so that it is not to the right of the target
         if (gBlankCol > destCol) {
            MoveBlankToCol(destCol);
         }

         // move the blank to the left of the source, possibly moving the source
         // to the right at the same time.
         if (gBlankRow != destRow) {
            if (sourceCol == destCol-1 && gBlankRow > destRow) {
               MoveBlankToCol(sourceCol - 1);
               MoveBlankToRow(sourceRow - 1);
               BlankRight(); BlankRight();
               BlankDown();  BlankLeft();
               return; // all done
            } else {
               MoveBlankToCol(sourceCol + 1);
               MoveBlankToRow(sourceRow);
               BlankLeft();
               sourceCol++;
            }
         } else {
            if (gBlankCol < sourceCol) {
               MoveBlankToCol(sourceCol - 1);
            } else {
               MoveBlankToCol(sourceCol);
               sourceCol++;
            }
         }
         
WereOnTheSameRowNow:
         // at this point, the blank is to the left of the source,
         // and the puzzle might very well be done already.
         while (sourceCol != destCol) {
            if (nextPiece == piece - 1) { // into a row?
               if (gBlankCol != 0 && 
                     pieceCol[nextPiece]-pieceRow[nextPiece] <= 
                     gBlankCol-gBlankRow) {
                  MoveAPiece(nextPiece, gBlankRow, gBlankCol, 
                               nextPiece - 1);
               }
               if (gBlankRow == destRow) 
                 while ((gBlankSquare[-1] == gBlankSquare[1] - 1) 
                        && gBlankCol != 0) {
                  BlankLeft();
               }
            }
            if (gBlankRow == destRow) BlankUp();
            do {
               BlankRight();
            } while (gBlankCol <= sourceCol);
            BlankDown();  BlankLeft();  sourceCol++;
         }
         return;
      }
      // simplify: move the blank so that it is to the left of the target
      if (gBlankCol >= destCol) {
         MoveBlankToCol(destCol - 1);
      }
      // simplify: move the blank so that it is not above the target.
      if (gBlankRow < destRow) MoveBlankToRow(destRow);

again1:   // simplify: if the blank is below the source, move it so it’s not.
      sourceRow = pieceRow[piece];
      sourceCol = pieceCol[piece];
      if (gBlankRow >= sourceRow) {
         // if the blank is in the same column, move it away first.
         if (gBlankCol == sourceCol) {
            if (gBlankCol == destCol - 1) {
               BlankLeft();
            } else {
               BlankRight();
            }
         }
         // now that they're in different columns, move the blank so it’s not below
         MoveBlankToRow(sourceRow);
      }
      // simplify: if the blank is on the same row, and to the left, move up.
      if (gBlankRow == sourceRow && gBlankCol < sourceCol) {
         BlankUp();
      }
      // simplify: if the blank is to the left, move it to the same column.
      if (gBlankCol < sourceCol) {
         MoveBlankToCol(sourceCol);
      }
      // simplify: if the blank is to the upper right, move it to the left and down.
      if (gBlankRow < sourceRow) {
         if (gBlankCol > sourceCol) {
            MoveBlankToCol(sourceCol);
         }
         if (gBlankRow < sourceRow - 1) {
            MoveBlankToRow(sourceRow - 1);
         }
      }
      // if the blank is off to the right, move it next to the source.
      while (gBlankCol > sourceCol + 1) {
         MoveBlankToCol(sourceCol + 1);
      }
      // at this point, the blank should be either just above or just right of the piece.
      if (gBlankCol == sourceCol) {
         if (gBlankRow != sourceRow - 1) Debugger();
      } else {
         if (gBlankRow != sourceRow) Debugger();
         if (gBlankCol != sourceCol + 1) Debugger();
         BlankLeft();  BlankUp();    BlankRight();
      }
      BlankDown();
      sourceRow = pieceRow[piece];
      sourceCol = pieceCol[piece];
      if (sourceRow != destRow) goto again1;
      if (gBlankCol != destCol - 1) {
         BlankRight(); BlankUp();    BlankLeft();
         while (pieceCol[piece] != destCol) {
            BlankUp();    BlankRight(); BlankRight();
            BlankDown();  BlankLeft();
         }
         return; // DONE!!!!
      }
      BlankLeft();  BlankUp();    BlankUp();    BlankRight(); 
      BlankRight(); BlankDown();  BlankLeft();
      return; // DONE!!!!
   }

   // at this point, we know that source is above our destination.
   if (sourceCol >= destCol) { 
      // in this case, we have to move the tile left (or directly down) to get to its destination
      if (sourceCol == destCol) {

         // simplify: move the blank so that it is not below the target
         if (gBlankRow > destRow) {
            MoveBlankToRow(destRow);
         }

         // move the blank above the source, possibly moving the source
         // down at the same time.
         if (gBlankCol != destCol) {
            if (sourceRow == destRow-1 && gBlankCol > destCol) {
               MoveBlankToRow(sourceRow - 1);
               MoveBlankToCol(sourceCol - 1);
               BlankDown();  BlankDown();
               BlankRight(); BlankUp();
               return; // all done
            } else {
               MoveBlankToRow(sourceRow + 1);
               MoveBlankToCol(sourceCol);
               BlankUp();
               sourceRow++;
            }
         } else {
            if (gBlankRow < sourceRow) {
               MoveBlankToRow(sourceRow - 1);
            } else {
               MoveBlankToRow(sourceRow);
               sourceRow++;
            }
         }
         
WereInTheSameColumnNow:
         // at this point, the blank is on top of the source,
         // and the puzzle might very well be done already.
         while (sourceRow != destRow) {
            if (nextPiece == piece - nextRow) { // into a column?
               if (gBlankRow != 0 && 
                   pieceCol[nextPiece]-pieceRow[nextPiece] >= 
                     gBlankCol-gBlankRow) {
                  MoveAPiece(nextPiece, gBlankRow, gBlankCol, 
                               nextPiece - nextRow);
               }
               if (gBlankCol == destCol) 
                 while ((gBlankSquare[-nextRow] == 
                         gBlankSquare[nextRow] - 1) && 
                        gBlankRow != 0) {
                  BlankUp();
               }
            }
            if (gBlankCol == destCol) BlankLeft();
            do {
               BlankDown();
            } while (gBlankRow <= sourceRow);
            BlankRight(); BlankUp();    sourceRow++;
         }
         return;
      }
      // simplify: move the blank so that it is to the up of the target
      if (gBlankRow >= destRow) {
         MoveBlankToRow(destRow - 1);
      }
      // simplify: move the blank so that it is not to the left of the target.
      if (gBlankCol < destCol) MoveBlankToCol(destCol);

again2:   // simplify: if the blank is to the right of the source, move it so it’s not.
      sourceRow = pieceRow[piece];
      sourceCol = pieceCol[piece];
      if (gBlankCol >= sourceCol) {
         // if the blank is in the same row, move it away first.
         if (gBlankRow == sourceRow) {
            if (gBlankRow == destRow - 1) {
               BlankUp();
            } else {
               BlankDown();
            }
         }
         // now that they’re in different rows, move the blank so it’s not to the right of
         MoveBlankToCol(sourceCol);
      }
      // simplify: if the blank is on the same column, and to the up, move left.
      if (gBlankCol == sourceCol && gBlankRow < sourceRow) {
         BlankLeft();
      }
      // simplify: if the blank is to the up, move it to the same row.
      if (gBlankRow < sourceRow) {
         MoveBlankToRow(sourceRow);
      }
      // simplify: if the blank is to the lower left, move it to the right and up.
      if (gBlankCol < sourceCol) {
         if (gBlankRow > sourceRow) {
            MoveBlankToRow(sourceRow);
         }
         if (gBlankCol < sourceCol - 1) {
            MoveBlankToCol(sourceCol - 1);
         }
      }
      // if the blank is off to the down, move it next to the source.
      while (gBlankRow > sourceRow + 1) {
         MoveBlankToRow(sourceRow + 1);
      }
      // at this point, the blank should be either just below or just to the left of the piece.
      if (gBlankRow == sourceRow) {
         if (gBlankCol != sourceCol - 1) Debugger();
      } else {
         if (gBlankCol != sourceCol) Debugger();
         if (gBlankRow != sourceRow + 1) Debugger();
         BlankUp();    BlankLeft();  BlankDown();
      }
      BlankRight();
      sourceRow = pieceRow[piece];
      sourceCol = pieceCol[piece];
      if (sourceCol != destCol) goto again2;
      if (gBlankRow != destRow - 1) {
         BlankDown();  BlankLeft();  BlankUp();
         while (pieceRow[piece] != destRow) {
            BlankLeft();  BlankDown();
            BlankDown();  BlankRight(); BlankUp();
         }
         return; // DONE!!!!
      }
      BlankUp();    BlankLeft();  BlankLeft();  BlankDown();
      BlankDown();  BlankRight(); BlankUp();
      return; // DONE!!!!
   }

   // at this point, we know that we are above and to the left of our destination.

   if (destCol - sourceCol == destRow - sourceRow) {
      // we’re on the diagonal.
      if (gBlankCol >= sourceCol) {
         if (gBlankRow <= sourceRow) goto MoveSrcRightFirst;
      }
      if (gBlankRow >= sourceRow) {
         if (gBlankCol <= sourceCol) goto MoveSrcDownFirst;
      }
      if (gPieceRow[nextPiece] - gPieceCol[nextPiece] > 
          destRow - destCol) {
         // relative to the destination, the next piece is to the lower left.
         // this means we want the blank to end up to the left of the target,
         // rather than above it.
         goto MoveSrcDownFirst;
      }
      goto MoveSrcRightFirst;
   }
   if (destCol - sourceCol < destRow - sourceRow) {
MoveSrcDownFirst:
      // the source is within the 90š-135š octant.
      // we want to move the blank square just below the source.
      // we will end up just to the left of the target.
      if (gBlankCol == sourceCol && gBlankRow < sourceRow) {
         // the blank is on top of the source.  moving it
         // down would move our square in the wrong direction.
         BlankRight();
      }
      // in case the source is just to the upper left of the target,
      // we have to make sure we don’t accidentally munge the protected area.
      if (gBlankCol > destCol) MoveBlankToCol(destCol);
      MoveBlankToRow(sourceRow + 1);
      MoveBlankToCol(sourceCol);
      BlankUp();    sourceRow++;
      BlankRight(); BlankDown();
      // the blank is now to the right of the source.
   } else {
MoveSrcRightFirst:
      // the source is within the 135š-180š octant.
      // we want to move the blank square just to the right of the source.
      // we will end up just above the target.
      if (gBlankRow == sourceRow && gBlankCol < sourceCol) {
         // the blank is to the left of the source.  moving it
         // to the right would move our square in the wrong direction.
         BlankDown();
      }
      // in case the source is just to the upper left of the target,
      // we have to make sure we don't accidentally munge the protected area.
      if (gBlankRow > destRow) MoveBlankToRow(destRow);
      MoveBlankToCol(sourceCol + 1);
      MoveBlankToRow(sourceRow);
      // the blank is now to the right of the source.
   }
   BlankLeft();
   sourceCol++;
   // the blank is now to the left of the source.
   // are we done yet?
   for (;;) {
      if (sourceCol == destCol) {
         if (sourceRow == destRow) return;
         // the blank is still to the left of the source.
         BlankDown();  BlankRight(); BlankUp();   sourceRow++;
         goto WereInTheSameColumnNow;
      }
      if (sourceRow == destRow) {
         goto WereOnTheSameRowNow;
      }
      BlankDown();  BlankRight(); BlankUp();    sourceRow++;
      BlankRight(); BlankDown();  BlankLeft();  sourceCol++;
   }
}

QuickBlock
static void *QuickBlock(long size) {
   Handle   h = NewHandle(size);
   if (h == 0) return 0;
   HLock(h);
   return *h;
}

DisposeBlock
static void DisposeBlock(void *block) {
   Handle   h = RecoverHandle(block);
   DisposeHandle(h);
}


SolveTiles
void SolveTiles(
  long *tiles,      /* pointer to array of tiles where */
  long numRows,     /*   tile (row,col) is at */
  long numCols,     /*   *(tiles + row*numCols + col) */
  MoveProc MakeMove /* Callback procedure to move a tile */
) {
   long   col, row, target, tile, correctTile;
   long   colsToGo, rowsToGo;
   long   *tileRover, *pieceRow, *pieceCol;

   pieceRow = QuickBlock(numRows * numCols * sizeof(long));
   if (pieceRow == 0) return;
   pieceCol = QuickBlock(numRows * numCols * sizeof(long));
   if (pieceCol == 0) {
      DisposeBlock(pieceRow);
      return;
   }
   gPieceRow = pieceRow;
   gPieceCol = pieceCol;
   gMakeMove = MakeMove;
   gTiles = tiles;
   gNumCols = numCols;

   tileRover = tiles;
   correctTile = 0;
   for (row = 0; row < numRows; row++) {
      for (col = 0; col < numCols; col++) {
         tile = *tileRover++;
         pieceRow[tile] = row;
         pieceCol[tile] = col;
         correctTile++;
      }
   }

   gBlankSquare = &tiles[gBlankRow * numCols + gBlankCol];

   rowsToGo = numRows;
   colsToGo = numCols;

   for (;;) {
      if (rowsToGo >= colsToGo) {
         if (rowsToGo <= 2) break;
         row = rowsToGo - 1;
         for (col = colsToGo - 1; col > 1; col--) {
            tile = row * numCols + col;
            MoveAPiece(tile, row, col, tile - 1);
         }
         tile = row * numCols;
         if (pieceRow[tile]     != row || 
             pieceCol[tile    ] != 0   ||
             pieceRow[tile + 1] != row || 
             pieceCol[tile + 1] != 1) {
            MoveAPiece(tile, row, 1, tile + 1);
            if (gBlankRow == row) {
               if (tiles[(row - 1) * numCols] == tile + 1) {
                  // problem scenario 1
rowScenario1:
                  BlankRight(); BlankUp();    BlankLeft();
                  BlankUp();    BlankRight(); BlankDown();
                  BlankDown();  BlankLeft();  BlankUp();
                  BlankRight(); BlankUp();    BlankLeft();
                  BlankDown();  BlankDown();  BlankRight();
                  BlankUp();
                  goto nextRow;
               }
            } else if (tiles[row * numCols] == tile + 1) {
               // problem scenario 2
               MoveBlankToCol(0);
               MoveBlankToRow(row);
               goto rowScenario1;
            }
            MoveAPiece(tile + 1, row, 1, 0);
         }
nextRow:   if (gBlankRow == row) BlankUp();
         if (pieceRow[tile]     != row || 
             pieceCol[tile    ] != 0) {
            // the “12” isn’t in place...
            Debugger();
         }
      rowsToGo--;
      } else {
         if (colsToGo <= 2) break;
         col = colsToGo - 1;
         for (row = rowsToGo - 1; row > 1; row--) {
            tile = row * numCols + col;
            MoveAPiece(tile, row, col, tile - numCols);
         }
         if (pieceRow[          col] != 0 || 
             pieceCol[          col] != col ||
             pieceRow[numCols + col] != 1 || 
             pieceCol[numCols + col] != col) {
            MoveAPiece(col, 1, col, col + numCols);
            if (gBlankCol == col) {
               if (tiles[col - 1] == numCols + col) {
                  // problem scenario 1
colScenario1:
                  BlankDown();  BlankLeft();  BlankUp();
                  BlankLeft();  BlankDown();  BlankRight();
                  BlankRight(); BlankUp();    BlankLeft();
                  BlankDown();  BlankLeft();  BlankUp();
                  BlankRight(); BlankRight(); BlankDown();
                  BlankLeft();
                  goto nextCol;
               }
            } else if (tiles[col] == numCols + col) {
               // problem scenario 2
               MoveBlankToRow(0);
               MoveBlankToCol(col);
               goto colScenario1;
            }
            MoveAPiece(numCols + col, 1, col, 0);
         }
nextCol:   if (gBlankCol == col) BlankLeft();
         if (pieceRow[          col] != 0 || 
             pieceCol[          col] != col) {
            // the “3” isn’t in place...
            Debugger();
         }
         colsToGo--;
      }
   }

   if (gBlankRow == 0) {
      if (gBlankCol == 0) {
         if (pieceRow[1] == 0) goto pos0123;
         if (pieceCol[1] == 0) goto pos0312;
                               goto pos0231;
      } else {
         if (pieceRow[1] == 0) goto pos1023;
         if (pieceCol[1] == 0) goto pos3012;
                               goto pos2031;
      }
   } else {
      if (gBlankCol == 0) {
         if (pieceRow[1] != 0) goto pos3201;
         if (pieceCol[1] == 0) goto pos1302;
                               goto pos2103;
      } else {
         if (pieceRow[1] != 0) goto pos3210;
         if (pieceCol[1] == 0) goto pos1320;
                               goto pos2130;
      }
   }

pos3012: BlankLeft();
pos0312: BlankDown();
pos1302: BlankRight();
pos1320: BlankUp();
pos1023: BlankLeft();
         goto all_done;

pos3210: BlankLeft();
pos3201: BlankUp();
pos0231: BlankRight();
pos2031: BlankDown();
pos2130: BlankLeft();
pos2103: BlankUp();
pos0123: goto all_done;

all_done:
   DisposeBlock(pieceRow); gPieceRow = 0;
   DisposeBlock(pieceCol); gPieceCol = 0;
}

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Go from lowly lizard to wicked Wyvern in...
Do you like questing, and do you like dragons? If not then boy is this not the announcement for you, as Loongcheer Game has unveiled Quest Dragon: Idle Mobile Game. Yes, it is amazing Square Enix hasn’t sued them for copyright infringement, but... | Read more »
Aether Gazer unveils Chapter 16 of its m...
After a bit of maintenance, Aether Gazer has released Chapter 16 of its main storyline, titled Night Parade of the Beasts. This big update brings a new character, a special outfit, some special limited-time events, and, of course, an engaging... | Read more »
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 »

Price Scanner via MacPrices.net

13-inch M2 MacBook Airs in stock today at App...
Apple has 13″ M2 MacBook Airs available for only $849 today in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty is included,... Read more
New today at Apple: Series 9 Watches availabl...
Apple is now offering Certified Refurbished Apple Watch Series 9 models on their online store for up to $80 off MSRP, starting at $339. Each Watch includes Apple’s standard one-year warranty, a new... Read more
The latest Apple iPhone deals from wireless c...
We’ve updated our iPhone Price Tracker with the latest carrier deals on Apple’s iPhone 15 family of smartphones as well as previous models including the iPhone 14, 13, 12, 11, and SE. Use our price... Read more
Boost Mobile will sell you an iPhone 11 for $...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering an iPhone 11 for $149.99 when purchased with their $40 Unlimited service plan (12GB of premium data). No trade-in is required... Read more
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

Jobs Board

Solutions Engineer - *Apple* - SHI (United...
**Job Summary** An Apple Solution Engineer's primary role is tosupport SHI customers in their efforts to select, deploy, and manage Apple operating systems and Read more
DMR Technician - *Apple* /iOS Systems - Haml...
…relevant point-of-need technology self-help aids are available as appropriate. ** Apple Systems Administration** **:** Develops solutions for supporting, deploying, Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.