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

  1. /*
  2.  * @(#)Grid.java
  3.  */
  4.  
  5. package games.Battle.shared.sys;
  6.  
  7. /**
  8.  * Grid is a container which eases access to data arranges in (r,c)
  9.  * grids. 
  10.  *
  11.  * @version 1.0 04/02/96
  12.  * @author Jay Steele
  13.  * @author Alex Nicolaou
  14.  */
  15.  
  16. public class Grid {
  17.  
  18.     /**
  19.      * a flat array of data
  20.      */
  21.     Object[] data;
  22.     /**
  23.      * the number of rows and columns
  24.      */
  25.     int rows, cols;
  26.  
  27.     /**
  28.      * Construct a grid with r rows and c columns.
  29.      * @param r the number of rows in the grid
  30.      * @param c the number of columns in the grid
  31.      */
  32.     public Grid(int r, int c) {
  33.         rows = r;
  34.         cols = c;
  35.         data = new Object[rows*cols];
  36.         for (int i=0; i<data.length; i++) {
  37.             data[i] = null;
  38.         }
  39.     }
  40.  
  41.     /**
  42.      * Return the number of rows in this grid
  43.      */
  44.     public final int getNumRows()     { return rows; }
  45.  
  46.     /**
  47.      * Return the number of colums in this grid
  48.      */
  49.     public final int getNumCols()    { return cols; }
  50.  
  51.     /**
  52.      * Return an index into a contiguous array which represents
  53.      * the given (r,c) pair.
  54.      * @param r the row
  55.      * @param r the column
  56.      */
  57.     private final int getOffset(int r, int c) {
  58.         return (r*cols+c);
  59.     }
  60.  
  61.     /**
  62.      * Set the value of the cell in the given (row, col) location
  63.      * with the given object.
  64.      * @param row the row
  65.      * @param col the column
  66.      * @param cell the data to place in that cell
  67.      */
  68.     public final void setCell(int row, int col, Object cell) {
  69.         data[getOffset(row,col)] = cell;
  70.     }
  71.  
  72.     /**
  73.      * Return the data located in the given cell location
  74.      * @param row the row
  75.      * @param col the column
  76.      */
  77.     public final Object getCell(int row, int col) {
  78.         return data[getOffset(row, col)];
  79.     }
  80.  
  81.     /**
  82.      * Return the contiguous array of data representing this
  83.      * grid. The data is arranged in row-major order: for example,
  84.      * in a 2x2 grid, the first 2 elements in the array represent the
  85.      * first row, and the second two elements represent the second
  86.      * row.
  87.      */
  88.     public final Object[] getData() { return data; }
  89.  
  90.     /**
  91.      * Set the data for this grid object. Will raise an 
  92.      * IllegalArgumentException if the data block is not the correct size.
  93.      * @param data the data block
  94.      */
  95.      public final void setData(Object[] x) {
  96.          if (x.length != rows*cols)
  97.             throw new IllegalArgumentException("data array is not the correct size");
  98.         data = x;
  99.      }
  100. }
  101.