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