Source/Grid.h
author Jens Alfke <jens@mooseyard.com>
Wed May 28 12:47:10 2008 -0700 (2008-05-28)
changeset 8 45c82a071aca
parent 1 3eb7be1dd7b6
child 11 436cbdf56810
permissions -rw-r--r--
* Got it working with latest iPhone SDK.
* Fixed some text alignment issues that showed up on PlayingCards.
* Working on persistence and move-tracking for Game.
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@0
    34
    BOOL _usesDiagonals, _allowsMoves, _allowsCaptures;
jens@0
    35
    NSMutableArray *_cells;                             // Really a 2D array, in row-major order.
jens@0
    36
}
jens@0
    37
jens@0
    38
/** Initializes a new Grid with the given dimensions and cell size, and position in superview.
jens@0
    39
    Note that a new Grid has no cells! Either call -addAllCells, or -addCellAtRow:column:. */
jens@0
    40
- (id) initWithRows: (unsigned)nRows columns: (unsigned)nColumns
jens@0
    41
            spacing: (CGSize)spacing
jens@0
    42
           position: (CGPoint)pos;
jens@0
    43
jens@0
    44
/** Initializes a new Grid with the given dimensions and frame in superview.
jens@0
    45
    The cell size will be computed by dividing frame size by dimensions.
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
              frame: (CGRect)frame;
jens@0
    49
jens@0
    50
@property Class cellClass;                      // What kind of GridCells to create
jens@0
    51
@property (readonly) unsigned rows, columns;    // Dimensions of the grid
jens@0
    52
@property (readonly) CGSize spacing;            // x,y spacing of GridCells
jens@0
    53
@property CGColorRef cellColor, lineColor;      // Cell background color, line color (or nil)
jens@0
    54
@property BOOL usesDiagonals;                   // Affects GridCell.neighbors, for rect grids
jens@0
    55
@property BOOL allowsMoves, allowsCaptures;     // Can pieces be moved, and can they land on others?
jens@0
    56
jens@0
    57
/** Returns the GridCell at the given coordinates, or nil if there is no cell there.
jens@0
    58
    It's OK to call this with off-the-board coordinates; it will just return nil.*/
jens@0
    59
- (GridCell*) cellAtRow: (unsigned)row column: (unsigned)col;
jens@0
    60
jens@0
    61
/** Adds cells at all coordinates, creating a complete grid. */
jens@0
    62
- (void) addAllCells;
jens@0
    63
jens@0
    64
/** Adds a GridCell at the given coordinates. */
jens@0
    65
- (GridCell*) addCellAtRow: (unsigned)row column: (unsigned)col;
jens@0
    66
jens@0
    67
/** Removes a particular cell, leaving a blank space. */
jens@0
    68
- (void) removeCellAtRow: (unsigned)row column: (unsigned)col;
jens@0
    69
jens@7
    70
- (GridCell*) cellWithName: (NSString*)identifier;
jens@0
    71
jens@0
    72
// protected:
jens@0
    73
- (GridCell*) createCellAtRow: (unsigned)row column: (unsigned)col 
jens@0
    74
               suggestedFrame: (CGRect)frame;
jens@0
    75
jens@0
    76
@end
jens@0
    77
jens@0
    78
jens@0
    79
/** Abstract superclass of a single cell in a grid. */
jens@0
    80
@interface GridCell : BitHolder
jens@0
    81
{
jens@0
    82
    Grid *_grid;
jens@0
    83
    unsigned _row, _column;
jens@0
    84
}
jens@0
    85
jens@0
    86
- (id) initWithGrid: (Grid*)grid 
jens@0
    87
                row: (unsigned)row column: (unsigned)col
jens@0
    88
              frame: (CGRect)frame;
jens@0
    89
jens@0
    90
@property (readonly) Grid* grid;
jens@0
    91
@property (readonly) unsigned row, column;
jens@0
    92
@property (readonly) NSArray* neighbors;        // Dependent on grid.usesDiagonals
jens@0
    93
jens@0
    94
/** Returns YES if 'forward' is north (increasing row#) for the current player */
jens@0
    95
@property (readonly) BOOL fwdIsN;
jens@0
    96
jens@0
    97
/* Go-style group detection. Returns the set of contiguous GridCells that have pieces of the same
jens@0
    98
   owner as this one, and optionally a count of the number of "liberties", or adjacent empty cells. */
jens@0
    99
- (NSSet*) getGroup: (int*)outLiberties;
jens@0
   100
jens@0
   101
// protected:
jens@0
   102
- (void) drawInParentContext: (CGContextRef)ctx fill: (BOOL)fill;
jens@0
   103
@end
jens@0
   104
jens@0
   105
jens@0
   106
jens@0
   107
/** A rectangular grid of squares. */
jens@0
   108
@interface RectGrid : Grid
jens@0
   109
{
jens@0
   110
    CGColorRef _altCellColor;
jens@0
   111
}
jens@0
   112
jens@0
   113
/** If non-nil, alternate cells will be drawn with this background color, in a checkerboard pattern.
jens@0
   114
    The precise rule is that cells whose row+column is odd use the altCellColor.*/
jens@0
   115
@property CGColorRef altCellColor;
jens@0
   116
jens@0
   117
@end
jens@0
   118
jens@0
   119
jens@0
   120
jens@0
   121
/* A square in a RectGrid */
jens@0
   122
@interface Square : GridCell
jens@0
   123
jens@0
   124
@property (readonly) Square *nw, *n, *ne, *e, *se, *s, *sw, *w;    // Absolute directions (n = increasing row#)
jens@0
   125
@property (readonly) Square *fl, *f, *fr, *r, *br, *b, *bl, *l;    // Relative to player (upside-down for player 2)
jens@0
   126
jens@0
   127
@end
jens@0
   128
jens@0
   129
jens@0
   130
/* Substitute this for Square in a RectGrid's cellClass to draw the lines through the centers
jens@0
   131
   of the squares, so the pieces sit on the intersections, as in a Go board. */
jens@0
   132
@interface GoSquare : Square
jens@0
   133
{
jens@0
   134
    BOOL _dotted;
jens@0
   135
}
jens@0
   136
jens@0
   137
/** Set to YES to put a dot at the intersection, as in the handicap points of a Go board. */
jens@0
   138
@property BOOL dotted;
jens@0
   139
jens@0
   140
@end