Source/Grid.h
author snej@snej.local
Tue Mar 10 22:36:23 2009 -0700 (2009-03-10)
changeset 27 b0affce7beb1
parent 22 4cb50131788f
child 29 0b1c315ffc64
permissions -rw-r--r--
Fixed some problems reported by the CLANG static analyzer.
     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 typedef struct {
    28     unsigned col, row;
    29 } GridCoord;
    30 
    31 
    32 /** Abstract superclass of regular geometric grids of GridCells that Bits can be placed on. */
    33 @interface Grid : GGBLayer
    34 {
    35     unsigned _nRows, _nColumns;                         
    36     CGSize _spacing;                                    
    37     Class _cellClass;                                   
    38     CGColorRef _cellColor, _lineColor;                  
    39     CGImageRef _backgroundImage;
    40     BOOL _usesDiagonals, _allowsMoves, _allowsCaptures, _reversed;
    41     NSMutableArray *_cells;                             // Really a 2D array, in row-major order.
    42     CATransform3D _bitTransform;
    43 }
    44 
    45 /** Initializes a new Grid with the given dimensions and cell size, and position in superview.
    46     Note that a new Grid has no cells! Either call -addAllCells, or -addCellAtRow:column:. */
    47 - (id) initWithRows: (unsigned)nRows columns: (unsigned)nColumns
    48             spacing: (CGSize)spacing
    49            position: (CGPoint)pos;
    50 
    51 /** Initializes a new Grid with the given dimensions and frame in superview.
    52     The cell size will be computed by dividing frame size by dimensions.
    53     Note that a new Grid has no cells! Either call -addAllCells, or -addCellAtRow:column:. */
    54 - (id) initWithRows: (unsigned)nRows columns: (unsigned)nColumns
    55               frame: (CGRect)frame;
    56 
    57 @property Class cellClass;                      // What kind of GridCells to create
    58 @property (readonly) unsigned rows, columns;    // Dimensions of the grid
    59 @property (readonly) CGSize spacing;            // x,y spacing of GridCells
    60 @property CGColorRef cellColor, lineColor;      // Cell background color, line color (or nil)
    61 @property CGImageRef backgroundImage;           // Image drawn in background, behind lines and cells
    62 @property BOOL usesDiagonals;                   // Affects GridCell.neighbors, for rect grids
    63 @property BOOL allowsMoves, allowsCaptures;     // Can pieces be moved, and can they land on others?
    64 @property BOOL reversed;                        // Reverses board (rotates 180°) by exchanging cell positions
    65 
    66 @property CATransform3D bitTransform;
    67 - (void) updateCellTransform;
    68 
    69 @property (readonly) NSArray *cells;
    70 
    71 /** Returns the GridCell at the given coordinates, or nil if there is no cell there.
    72     It's OK to call this with off-the-board coordinates; it will just return nil.*/
    73 - (GridCell*) cellAtRow: (unsigned)row column: (unsigned)col;
    74 
    75 /** Adds cells at all coordinates, creating a complete grid. */
    76 - (void) addAllCells;
    77 
    78 /** Adds a GridCell at the given coordinates. */
    79 - (GridCell*) addCellAtRow: (unsigned)row column: (unsigned)col;
    80 
    81 /** Removes a particular cell, leaving a blank space. */
    82 - (void) removeCellAtRow: (unsigned)row column: (unsigned)col;
    83 
    84 - (GridCell*) cellWithName: (NSString*)identifier;
    85 
    86 /** Returns all of the Players who have any Bits on the grid, with each Player's count being the
    87     number of Bits. */
    88 - (NSCountedSet*) countPiecesByPlayer;
    89 
    90 
    91 /** Utility to get and set the entire state of the Grid. The stateString is made by concatenating
    92     the name of the Bit of every GridCell in order, with "-" for empty cells.
    93     The setter method calls the Game's optional -makePieceNamed: method to create the pieces. */
    94 @property (copy) NSString *stateString;
    95 
    96 /** Interprets the string as a series of cell names separated by "-", and tells the Game to move
    97     the piece at the first cell to each cell in succession by calling its -animateMoveFrom:to:. */
    98 - (BOOL) applyMoveString: (NSString*)move;
    99 
   100 
   101 // protected:
   102 - (GridCell*) createCellAtRow: (unsigned)row column: (unsigned)col 
   103                suggestedFrame: (CGRect)frame;
   104 
   105 @end
   106 
   107 
   108 /** Abstract superclass of a single cell in a grid. */
   109 @interface GridCell : BitHolder
   110 {
   111     Grid *_grid;
   112     unsigned _row, _column;
   113 }
   114 
   115 - (id) initWithGrid: (Grid*)grid 
   116                 row: (unsigned)row column: (unsigned)col
   117               frame: (CGRect)frame;
   118 
   119 @property (readonly) Grid* grid;
   120 @property (readonly) unsigned row, column;
   121 @property (readonly) NSArray* neighbors;        // Dependent on grid.usesDiagonals
   122 
   123 /** Returns YES if 'forward' is north (increasing row#) for the current player */
   124 @property (readonly) BOOL fwdIsN;
   125 
   126 /** Go-style group detection. Returns the set of contiguous GridCells that have pieces of the same
   127    owner as this one, and optionally a count of the number of "liberties", or adjacent empty cells. */
   128 - (NSSet*) getGroup: (int*)outLiberties;
   129 
   130 // protected:
   131 - (void) drawInParentContext: (CGContextRef)ctx fill: (BOOL)fill;
   132 @end
   133 
   134 
   135 
   136 /** A rectangular grid of squares. */
   137 @interface RectGrid : Grid
   138 {
   139     CGColorRef _altCellColor;
   140 }
   141 
   142 /** If non-nil, alternate cells will be drawn with this background color, in a checkerboard pattern.
   143     The precise rule is that cells whose row+column is odd use the altCellColor.*/
   144 @property CGColorRef altCellColor;
   145 
   146 @end
   147 
   148 
   149 
   150 /* A square in a RectGrid */
   151 @interface Square : GridCell
   152 
   153 @property (readonly) Square *nw, *n, *ne, *e, *se, *s, *sw, *w;    // Absolute directions (n = increasing row#)
   154 @property (readonly) Square *fl, *f, *fr, *r, *br, *b, *bl, *l;    // Relative to player (upside-down for player 2)
   155 
   156 /** Returns the absolute direction selector (see above) for the straight line from self to dst;
   157     or NULL if there is no straight line, or if dst==self.
   158     Diagonal lines are allowed only if the Grid's -usesDiagonals is YES. */
   159 - (SEL) directionToCell: (GridCell*)dst;
   160 
   161 /** Returns an array of all the cells in a straight line from self to dst;
   162     or NULL if there is no straight line, or if dst==self.
   163     If 'inclusive' is YES, the array will include self and dst, otherwise not.
   164     Diagonal lines are allowed only if the Grid's -usesDiagonals is YES. */
   165 - (NSArray*) lineToCell: (GridCell*)dst inclusive: (BOOL)inclusive;
   166 
   167 @end
   168 
   169 
   170 /* Substitute this for Square in a RectGrid's cellClass to draw the lines through the centers
   171    of the squares, so the pieces sit on the intersections, as in a Go board. */
   172 @interface GoSquare : Square
   173 {
   174     BOOL _dotted;
   175 }
   176 
   177 /** Set to YES to put a dot at the intersection, as in the handicap points of a Go board. */
   178 @property BOOL dotted;
   179 
   180 @end