Source/Grid.h
changeset 0 e9f7ba4718e1
child 1 3eb7be1dd7b6
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Source/Grid.h	Fri Mar 07 11:43:02 2008 -0800
     1.3 @@ -0,0 +1,139 @@
     1.4 +/*  This code is based on Apple's "GeekGameBoard" sample code, version 1.0.
     1.5 +    http://developer.apple.com/samplecode/GeekGameBoard/
     1.6 +    Copyright © 2007 Apple Inc. Copyright © 2008 Jens Alfke. All Rights Reserved.
     1.7 +
     1.8 +    Redistribution and use in source and binary forms, with or without modification, are permitted
     1.9 +    provided that the following conditions are met:
    1.10 +
    1.11 +    * Redistributions of source code must retain the above copyright notice, this list of conditions
    1.12 +      and the following disclaimer.
    1.13 +    * Redistributions in binary form must reproduce the above copyright notice, this list of
    1.14 +      conditions and the following disclaimer in the documentation and/or other materials provided
    1.15 +      with the distribution.
    1.16 +
    1.17 +    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
    1.18 +    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
    1.19 +    FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
    1.20 +    BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    1.21 +    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
    1.22 +    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
    1.23 +    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
    1.24 +    THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.25 +*/
    1.26 +#import "BitHolder.h"
    1.27 +@class GridCell;
    1.28 +
    1.29 +
    1.30 +/** Abstract superclass of regular geometric grids of GridCells that Bits can be placed on. */
    1.31 +@interface Grid : CALayer
    1.32 +{
    1.33 +    unsigned _nRows, _nColumns;                         
    1.34 +    CGSize _spacing;                                    
    1.35 +    Class _cellClass;                                   
    1.36 +    CGColorRef _cellColor, _lineColor;                  
    1.37 +    BOOL _usesDiagonals, _allowsMoves, _allowsCaptures;
    1.38 +    NSMutableArray *_cells;                             // Really a 2D array, in row-major order.
    1.39 +}
    1.40 +
    1.41 +/** Initializes a new Grid with the given dimensions and cell size, and position in superview.
    1.42 +    Note that a new Grid has no cells! Either call -addAllCells, or -addCellAtRow:column:. */
    1.43 +- (id) initWithRows: (unsigned)nRows columns: (unsigned)nColumns
    1.44 +            spacing: (CGSize)spacing
    1.45 +           position: (CGPoint)pos;
    1.46 +
    1.47 +/** Initializes a new Grid with the given dimensions and frame in superview.
    1.48 +    The cell size will be computed by dividing frame size by dimensions.
    1.49 +    Note that a new Grid has no cells! Either call -addAllCells, or -addCellAtRow:column:. */
    1.50 +- (id) initWithRows: (unsigned)nRows columns: (unsigned)nColumns
    1.51 +              frame: (CGRect)frame;
    1.52 +
    1.53 +@property Class cellClass;                      // What kind of GridCells to create
    1.54 +@property (readonly) unsigned rows, columns;    // Dimensions of the grid
    1.55 +@property (readonly) CGSize spacing;            // x,y spacing of GridCells
    1.56 +@property CGColorRef cellColor, lineColor;      // Cell background color, line color (or nil)
    1.57 +@property BOOL usesDiagonals;                   // Affects GridCell.neighbors, for rect grids
    1.58 +@property BOOL allowsMoves, allowsCaptures;     // Can pieces be moved, and can they land on others?
    1.59 +
    1.60 +/** Returns the GridCell at the given coordinates, or nil if there is no cell there.
    1.61 +    It's OK to call this with off-the-board coordinates; it will just return nil.*/
    1.62 +- (GridCell*) cellAtRow: (unsigned)row column: (unsigned)col;
    1.63 +
    1.64 +/** Adds cells at all coordinates, creating a complete grid. */
    1.65 +- (void) addAllCells;
    1.66 +
    1.67 +/** Adds a GridCell at the given coordinates. */
    1.68 +- (GridCell*) addCellAtRow: (unsigned)row column: (unsigned)col;
    1.69 +
    1.70 +/** Removes a particular cell, leaving a blank space. */
    1.71 +- (void) removeCellAtRow: (unsigned)row column: (unsigned)col;
    1.72 +
    1.73 +
    1.74 +// protected:
    1.75 +- (GridCell*) createCellAtRow: (unsigned)row column: (unsigned)col 
    1.76 +               suggestedFrame: (CGRect)frame;
    1.77 +
    1.78 +@end
    1.79 +
    1.80 +
    1.81 +/** Abstract superclass of a single cell in a grid. */
    1.82 +@interface GridCell : BitHolder
    1.83 +{
    1.84 +    Grid *_grid;
    1.85 +    unsigned _row, _column;
    1.86 +}
    1.87 +
    1.88 +- (id) initWithGrid: (Grid*)grid 
    1.89 +                row: (unsigned)row column: (unsigned)col
    1.90 +              frame: (CGRect)frame;
    1.91 +
    1.92 +@property (readonly) Grid* grid;
    1.93 +@property (readonly) unsigned row, column;
    1.94 +@property (readonly) NSArray* neighbors;        // Dependent on grid.usesDiagonals
    1.95 +
    1.96 +/** Returns YES if 'forward' is north (increasing row#) for the current player */
    1.97 +@property (readonly) BOOL fwdIsN;
    1.98 +
    1.99 +/* Go-style group detection. Returns the set of contiguous GridCells that have pieces of the same
   1.100 +   owner as this one, and optionally a count of the number of "liberties", or adjacent empty cells. */
   1.101 +- (NSSet*) getGroup: (int*)outLiberties;
   1.102 +
   1.103 +// protected:
   1.104 +- (void) drawInParentContext: (CGContextRef)ctx fill: (BOOL)fill;
   1.105 +@end
   1.106 +
   1.107 +
   1.108 +
   1.109 +/** A rectangular grid of squares. */
   1.110 +@interface RectGrid : Grid
   1.111 +{
   1.112 +    CGColorRef _altCellColor;
   1.113 +}
   1.114 +
   1.115 +/** If non-nil, alternate cells will be drawn with this background color, in a checkerboard pattern.
   1.116 +    The precise rule is that cells whose row+column is odd use the altCellColor.*/
   1.117 +@property CGColorRef altCellColor;
   1.118 +
   1.119 +@end
   1.120 +
   1.121 +
   1.122 +
   1.123 +/* A square in a RectGrid */
   1.124 +@interface Square : GridCell
   1.125 +
   1.126 +@property (readonly) Square *nw, *n, *ne, *e, *se, *s, *sw, *w;    // Absolute directions (n = increasing row#)
   1.127 +@property (readonly) Square *fl, *f, *fr, *r, *br, *b, *bl, *l;    // Relative to player (upside-down for player 2)
   1.128 +
   1.129 +@end
   1.130 +
   1.131 +
   1.132 +/* Substitute this for Square in a RectGrid's cellClass to draw the lines through the centers
   1.133 +   of the squares, so the pieces sit on the intersections, as in a Go board. */
   1.134 +@interface GoSquare : Square
   1.135 +{
   1.136 +    BOOL _dotted;
   1.137 +}
   1.138 +
   1.139 +/** Set to YES to put a dot at the intersection, as in the handicap points of a Go board. */
   1.140 +@property BOOL dotted;
   1.141 +
   1.142 +@end