Source/Grid.h
author Jens Alfke <jens@mooseyard.com>
Thu Jul 31 13:23:44 2008 -0700 (2008-07-31)
changeset 23 efe5d4523a23
parent 15 73f8c889f053
child 24 db8640a38faf
permissions -rw-r--r--
* Fixed scaling of king pieces when the board's state is set.
* Added IBOutlets for tilting the BoardView.
     1 /*  This code is based on Apple's "GeekGameBoard" sample code, version 1.0.
     2     http://developer.apple.com/samplecode/GeekGameBoard/
     3     Copyright © 2007 Apple Inc. Copyright © 2008 Jens Alfke. All Rights Reserved.
     4 
     5     Redistribution and use in source and binary forms, with or without modification, are permitted
     6     provided that the following conditions are met:
     7 
     8     * Redistributions of source code must retain the above copyright notice, this list of conditions
     9       and the following disclaimer.
    10     * Redistributions in binary form must reproduce the above copyright notice, this list of
    11       conditions and the following disclaimer in the documentation and/or other materials provided
    12       with the distribution.
    13 
    14     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
    15     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
    16     FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
    17     BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    18     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
    19     PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
    20     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
    21     THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    22 */
    23 #import "BitHolder.h"
    24 @class GridCell;
    25 
    26 
    27 /** Abstract superclass of regular geometric grids of GridCells that Bits can be placed on. */
    28 @interface Grid : GGBLayer
    29 {
    30     unsigned _nRows, _nColumns;                         
    31     CGSize _spacing;                                    
    32     Class _cellClass;                                   
    33     CGColorRef _cellColor, _lineColor;                  
    34     CGImageRef _backgroundImage;
    35     BOOL _usesDiagonals, _allowsMoves, _allowsCaptures, _reversed;
    36     NSMutableArray *_cells;                             // Really a 2D array, in row-major order.
    37     CATransform3D _bitTransform;
    38 }
    39 
    40 /** Initializes a new Grid with the given dimensions and cell size, and position in superview.
    41     Note that a new Grid has no cells! Either call -addAllCells, or -addCellAtRow:column:. */
    42 - (id) initWithRows: (unsigned)nRows columns: (unsigned)nColumns
    43             spacing: (CGSize)spacing
    44            position: (CGPoint)pos;
    45 
    46 /** Initializes a new Grid with the given dimensions and frame in superview.
    47     The cell size will be computed by dividing frame size by dimensions.
    48     Note that a new Grid has no cells! Either call -addAllCells, or -addCellAtRow:column:. */
    49 - (id) initWithRows: (unsigned)nRows columns: (unsigned)nColumns
    50               frame: (CGRect)frame;
    51 
    52 @property Class cellClass;                      // What kind of GridCells to create
    53 @property (readonly) unsigned rows, columns;    // Dimensions of the grid
    54 @property (readonly) CGSize spacing;            // x,y spacing of GridCells
    55 @property CGColorRef cellColor, lineColor;      // Cell background color, line color (or nil)
    56 @property CGImageRef backgroundImage;           // Image drawn in background, behind lines and cells
    57 @property BOOL usesDiagonals;                   // Affects GridCell.neighbors, for rect grids
    58 @property BOOL allowsMoves, allowsCaptures;     // Can pieces be moved, and can they land on others?
    59 @property BOOL reversed;                        // Reverses board (rotates 180°) by exchanging cell positions
    60 
    61 @property CATransform3D bitTransform;
    62 - (void) updateCellTransform;
    63 
    64 @property (readonly) NSArray *cells;
    65 
    66 /** Returns the GridCell at the given coordinates, or nil if there is no cell there.
    67     It's OK to call this with off-the-board coordinates; it will just return nil.*/
    68 - (GridCell*) cellAtRow: (unsigned)row column: (unsigned)col;
    69 
    70 /** Adds cells at all coordinates, creating a complete grid. */
    71 - (void) addAllCells;
    72 
    73 /** Adds a GridCell at the given coordinates. */
    74 - (GridCell*) addCellAtRow: (unsigned)row column: (unsigned)col;
    75 
    76 /** Removes a particular cell, leaving a blank space. */
    77 - (void) removeCellAtRow: (unsigned)row column: (unsigned)col;
    78 
    79 - (GridCell*) cellWithName: (NSString*)identifier;
    80 
    81 /** Returns all of the Players who have any Bits on the grid, with each Player's count being the
    82     number of Bits. */
    83 - (NSCountedSet*) countPiecesByPlayer;
    84 
    85 
    86 /** Utility to get and set the entire state of the Grid. The stateString is made by concatenating
    87     the name of the Bit of every GridCell in order, with "-" for empty cells.
    88     The setter method calls the Game's optional -makePieceNamed: method to create the pieces. */
    89 @property (copy) NSString *stateString;
    90 
    91 /** Interprets the string as a series of cell names separated by "-", and tells the Game to move
    92     the piece at the first cell to each cell in succession by calling its -animateMoveFrom:to:. */
    93 - (BOOL) applyMoveString: (NSString*)move;
    94 
    95 
    96 // protected:
    97 - (GridCell*) createCellAtRow: (unsigned)row column: (unsigned)col 
    98                suggestedFrame: (CGRect)frame;
    99 
   100 @end
   101 
   102 
   103 /** Abstract superclass of a single cell in a grid. */
   104 @interface GridCell : BitHolder
   105 {
   106     Grid *_grid;
   107     unsigned _row, _column;
   108 }
   109 
   110 - (id) initWithGrid: (Grid*)grid 
   111                 row: (unsigned)row column: (unsigned)col
   112               frame: (CGRect)frame;
   113 
   114 @property (readonly) Grid* grid;
   115 @property (readonly) unsigned row, column;
   116 @property (readonly) NSArray* neighbors;        // Dependent on grid.usesDiagonals
   117 
   118 /** Returns YES if 'forward' is north (increasing row#) for the current player */
   119 @property (readonly) BOOL fwdIsN;
   120 
   121 /** Go-style group detection. Returns the set of contiguous GridCells that have pieces of the same
   122    owner as this one, and optionally a count of the number of "liberties", or adjacent empty cells. */
   123 - (NSSet*) getGroup: (int*)outLiberties;
   124 
   125 // protected:
   126 - (void) drawInParentContext: (CGContextRef)ctx fill: (BOOL)fill;
   127 @end
   128 
   129 
   130 
   131 /** A rectangular grid of squares. */
   132 @interface RectGrid : Grid
   133 {
   134     CGColorRef _altCellColor;
   135 }
   136 
   137 /** If non-nil, alternate cells will be drawn with this background color, in a checkerboard pattern.
   138     The precise rule is that cells whose row+column is odd use the altCellColor.*/
   139 @property CGColorRef altCellColor;
   140 
   141 @end
   142 
   143 
   144 
   145 /* A square in a RectGrid */
   146 @interface Square : GridCell
   147 
   148 @property (readonly) Square *nw, *n, *ne, *e, *se, *s, *sw, *w;    // Absolute directions (n = increasing row#)
   149 @property (readonly) Square *fl, *f, *fr, *r, *br, *b, *bl, *l;    // Relative to player (upside-down for player 2)
   150 
   151 /** Returns the absolute direction selector (see above) for the straight line from self to dst;
   152     or NULL if there is no straight line, or if dst==self.
   153     Diagonal lines are allowed only if the Grid's -usesDiagonals is YES. */
   154 - (SEL) directionToCell: (GridCell*)dst;
   155 
   156 /** Returns an array of all the cells in a straight line from self to dst;
   157     or NULL if there is no straight line, or if dst==self.
   158     If 'inclusive' is YES, the array will include self and dst, otherwise not.
   159     Diagonal lines are allowed only if the Grid's -usesDiagonals is YES. */
   160 - (NSArray*) lineToCell: (GridCell*)dst inclusive: (BOOL)inclusive;
   161 
   162 @end
   163 
   164 
   165 /* Substitute this for Square in a RectGrid's cellClass to draw the lines through the centers
   166    of the squares, so the pieces sit on the intersections, as in a Go board. */
   167 @interface GoSquare : Square
   168 {
   169     BOOL _dotted;
   170 }
   171 
   172 /** Set to YES to put a dot at the intersection, as in the handicap points of a Go board. */
   173 @property BOOL dotted;
   174 
   175 @end