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