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