home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 14 / IOPROG_14.ISO / soft / sdkjava / sdkjava.exe / SDKJava.cab / Samples / AFC / Puzzle15 / Src / Pz15Data.Java < prev    next >
Encoding:
Java Source  |  1998-03-05  |  4.1 KB  |  185 lines

  1. //
  2. // (c) 1998 Microsoft Corporation.  All rights reserved.
  3. //
  4. import java.util.Stack;
  5. import java.util.Random;
  6. import java.lang.Math;
  7.  
  8. public class Pz15Data implements Pz15Consts
  9. {
  10.     public int current[] = new int[16];
  11.     public int original[] = new int[16];
  12.     public int hole;
  13.     public Stack movstk;
  14.     public int initial_cost, current_cost;
  15.     public boolean solved;
  16.  
  17.     public Pz15Data()
  18.     {
  19.         setToGoal();
  20.     }
  21.  
  22.     public void genNew(int numscramble) 
  23.     {
  24.         setToGoal();
  25.         scramble(numscramble);
  26.         init();
  27.     }
  28.  
  29.     private void setToGoal()
  30.     {
  31.         // Set Puzzle to goal
  32.         for ( int i = 0 ; i < SIZE ; i++ ) {
  33.             original[i] = (i + 1) % SIZE; // original[i] = i;
  34.             current[i] = original[i];
  35.         }
  36.         hole = 15; // hole = 0;
  37.         solved = true;
  38.     }
  39.  
  40.     public void genNew(int puzzle[])
  41.     {
  42.         for ( int i = 0; i < SIZE; i++ ) {
  43.             original[i] = puzzle[i];
  44.             if ( puzzle[i] == 0 )
  45.                 hole = i;
  46.         }
  47.         init();
  48.     }
  49.  
  50.     public void init()
  51.     {
  52.         movstk = new Stack();
  53.  
  54.         // Set current to original
  55.         resetToOrg();
  56.  
  57.         initial_cost = getCost();
  58.         current_cost = initial_cost;
  59.  
  60.         if ( initial_cost != 0 )
  61.             solved = false;
  62.     }
  63.  
  64.     public void scramble(int num)
  65.     {
  66.         // Since we cannot generate a random puzzle and guarantee or even 
  67.         // verify that it is achievable. We will take a goal puzzle and 
  68.         // scramble it by making moves.
  69.         
  70.         Random rand = new Random();
  71.         int move, lastmove = NO_MOVE, i = 0;
  72.  
  73.         while ( i < ((num <= MAX_DEPTH) ? num : MAX_DEPTH) ) {
  74.             move = Math.abs(rand.nextInt()) % 4;
  75.             move = validateMove(MOVES[move]);
  76.             if ( (move != NO_MOVE) && (move != -lastmove) ) {
  77.                 lastmove = move;
  78.                 i++;
  79.                 // Swap hole and button that corresponds to move
  80.                 original[hole] = original[hole+move];
  81.                 original[hole+move] = 0;
  82.                 // Set hole index to point to hole
  83.                 hole += move;
  84.             }
  85.         }
  86.     }
  87.  
  88.     // gets cost of original puzzle
  89.     public int getCost()
  90.     {
  91.         int cost = 0;
  92.  
  93.         for ( int i = 0; i < SIZE; i++ ) {
  94.             if ( original[i] != 0 )
  95.                 cost += Math.abs(((original[i]-1)/4) - i/4) + 
  96.                         Math.abs(((original[i]-1)%4) - i%4);
  97.                 //cost += Math.abs(((original[i])/4) - i/4) + 
  98.                 //        Math.abs(((original[i])%4) - i%4);
  99.         }
  100.         return cost;
  101.     }
  102.  
  103.     /* 
  104.     ** getCost - Get cost of making move, either increases by 1 or 
  105.     **           decreases by 1.
  106.     **
  107.     ** Determines cost by determining the row/column that the piece which
  108.     ** the hole will be swapped with belongs in, and then determining if 
  109.     ** piece will be move farther away from it's goal (+1), or closer to its
  110.     ** goal (-1).
  111.     **
  112.     */
  113.     public int getCost(int move)
  114.     {
  115.         int goal;
  116.  
  117.         switch ( move ) {
  118.         case O_UP:
  119.         case O_DOWN:
  120.             goal = (current[hole+move] - 1) / 4;
  121.             //goal = current[hole+move] / 4;
  122.             if ( Math.abs(hole/4 - goal) > Math.abs((hole+move)/4 - goal) )
  123.                 return +1;
  124.             else
  125.                 return -1;
  126.  
  127.         case O_LEFT:
  128.         case O_RIGHT:
  129.             goal = (current[hole+move] - 1) % 4;
  130.             //goal = current[hole+move] % 4;
  131.             if ( Math.abs(hole%4 - goal) > Math.abs((hole+move)%4 - goal) )
  132.                 return +1;
  133.             else
  134.                 return -1;
  135.  
  136.         default: 
  137.             break;
  138.         }
  139.         return 0;
  140.     }
  141.  
  142.     // returns offset that hole will be moving
  143.     public int validateMove(int move)
  144.     {
  145.         switch ( move ) {
  146.         case O_UP: if ( hole >= 4 ) return O_UP; break;
  147.         case O_LEFT: if ( (hole % 4) != 0 ) return O_LEFT; break;
  148.         case O_RIGHT: if ( (hole % 4) != 3 ) return O_RIGHT; break;
  149.         case O_DOWN: if ( hole < 12 ) return O_DOWN; break;
  150.         default: break;
  151.         }
  152.         return NO_MOVE;
  153.     }
  154.  
  155.     public void resetToOrg()
  156.     {
  157.         for ( int i = 0; i < SIZE; i++ ) {
  158.             current[i] = original[i];
  159.             if ( original[i] == 0 )
  160.                 hole = i;
  161.         }
  162.     }
  163.  
  164.     public void update(int move)
  165.     {
  166.         if ( !movstk.empty() ) {
  167.             Object stktop = movstk.peek();
  168.  
  169.             if ( (stktop instanceof Integer) && 
  170.                  (((Integer)stktop).intValue() == -move) )
  171.                 movstk.pop();
  172.             else
  173.                 movstk.push(new Integer(move));
  174.         }
  175.         else
  176.             movstk.push(new Integer(move));
  177.  
  178.         current_cost += getCost(move);
  179.         current[hole] = current[hole+move];
  180.         hole += move;
  181.         current[hole] = 0;
  182.         if ( current_cost == 0 )
  183.             solved = true;
  184.     }
  185. }