home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / enxle1f6 / src / games / battle / shared / sys / common.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  3.0 KB  |  143 lines

  1. /*
  2.  * @(#)Common.java
  3.  */
  4. package games.Battle.shared.sys;
  5.  
  6. import java.util.Random;
  7. import java.lang.Math;
  8.  
  9. /**
  10.  * Common is a collection of static functions that perform a packing and
  11.  * unpacking services for both the client and server side.
  12.  *
  13.  * @version 1.00
  14.  * @author Jay Steele
  15.  * @author Alex Nicolaou
  16.  */
  17.  
  18. public class Common {
  19.  
  20.     /**
  21.      * a random number generator.
  22.      */
  23.     private static Random randGen = new Random();
  24.  
  25.     /**
  26.      * return whether the number representing a cell
  27.      * occupancy is a player (not invisible, not
  28.      * unoccupied and not unchanged)
  29.      * @param occupancy a particular cell's occupancy
  30.      */
  31.     public static final boolean isPlayer(int occupancy) {
  32.         return (occupancy >= Symbols.PLAYER0)
  33.                 && (occupancy <= Symbols.PLAYER4);
  34.     }
  35.  
  36.     /**
  37.      * extract the occupancy from a packed byte
  38.      */
  39.     public static final int extractOccupancy(byte b) { 
  40.         return (b >> 5) & 0x0007;
  41.     }
  42.     /**
  43.      * extract the terrain fom a packed byte
  44.      */
  45.     public static final int extractTerrain(byte b) { 
  46.         return (b >> 2) & 0x0007; 
  47.     }
  48.     /**
  49.      * extract the number of troops from a packed byte
  50.      */
  51.     public static final int extractTroops(byte b) { 
  52.         return b & 0x001f; 
  53.     }
  54.     /**
  55.      * extract the pipes froma packed byte.
  56.      */
  57.     public static final int extractPipes(byte b) { 
  58.         return (b >> 4) & 0x000f; 
  59.     }
  60.     /**
  61.      * extract the run length encoding count from a packed byte.
  62.      */
  63.     public static final int extractCount(byte b) { 
  64.         return b & 0x000f; 
  65.     }
  66.  
  67.     /**
  68.      * stuffs occupancy in bits 7-5, and terrain in bits 4-2
  69.      */
  70.     public static final byte stuffOccupancyAndTerrain(int o, int t) {
  71.         int a = (o << 5) & 0x00e0;
  72.         int b = (t << 2) & 0x001c;
  73.         return (byte)(a | b);
  74.     }
  75.  
  76.     /**
  77.      * stuffs occupancy and a 4-bit counter int a byte
  78.      */
  79.     public static final byte stuffOccupancyAndCount(int o, int c) {
  80.         return (byte)((o << 5) | (c & 0x000f));
  81.     }
  82.  
  83.     /**
  84.      * stuffs occupancy and troops
  85.      */
  86.     public static final byte stuffOccupancyAndTroops(int o, int t) {
  87.         return (byte)((o << 5) | (t & 0x001f));
  88.     }
  89.  
  90.     /**
  91.      * stuffs pipes into hi byte
  92.      */
  93.     public static final byte stuffPipes(int p) {
  94.         return (byte)(p << 4);
  95.     }
  96.  
  97.     /**
  98.      * converts a (row,col) tuple to a unique index in a contiguous
  99.      * array
  100.      */
  101.     public static final int RCtoIndex(int r, int c) {
  102.         return (r*Rules.cols+c);
  103.     }
  104.  
  105.     /**
  106.      * converts an index to a row
  107.      */
  108.     public static final int indexToRow(int index) {
  109.         return index / Rules.rows;
  110.     }
  111.     /**
  112.      * converts an index to a column
  113.      */
  114.     public static final int indexToCol(int index) {
  115.         return index % Rules.rows;
  116.     }
  117.  
  118.     /**
  119.      * returns a random row 
  120.      */
  121.     public static final int getRandomRow() {
  122.         return Math.abs(randGen.nextInt()) % Rules.rows;
  123.     }
  124.     /**
  125.      * returns a random column
  126.      */
  127.     public static final int getRandomCol() {
  128.         return Math.abs(randGen.nextInt()) % Rules.cols;
  129.     }
  130.  
  131.     /**
  132.      * wraps System.exit() to be friendly as a standlaone application
  133.      * and as an applet.
  134.      */
  135.      public static final void exit(int code) {
  136.         try {
  137.             System.exit(code);
  138.         } catch ( Exception e ) {
  139.             // ignore any problems
  140.         }
  141.      }
  142. }
  143.