jens@0: /* This code is based on Apple's "GeekGameBoard" sample code, version 1.0. jens@0: http://developer.apple.com/samplecode/GeekGameBoard/ jens@0: Copyright © 2007 Apple Inc. Copyright © 2008 Jens Alfke. All Rights Reserved. jens@0: jens@0: Redistribution and use in source and binary forms, with or without modification, are permitted jens@0: provided that the following conditions are met: jens@0: jens@0: * Redistributions of source code must retain the above copyright notice, this list of conditions jens@0: and the following disclaimer. jens@0: * Redistributions in binary form must reproduce the above copyright notice, this list of jens@0: conditions and the following disclaimer in the documentation and/or other materials provided jens@0: with the distribution. jens@0: jens@0: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR jens@0: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND jens@0: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI- jens@0: BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES jens@0: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR jens@0: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN jens@0: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF jens@0: THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. jens@0: */ jens@0: #import "BitHolder.h" jens@0: @class GridCell; jens@0: jens@0: jens@0: /** Abstract superclass of regular geometric grids of GridCells that Bits can be placed on. */ jens@1: @interface Grid : GGBLayer jens@0: { jens@0: unsigned _nRows, _nColumns; jens@0: CGSize _spacing; jens@0: Class _cellClass; jens@0: CGColorRef _cellColor, _lineColor; jens@0: BOOL _usesDiagonals, _allowsMoves, _allowsCaptures; jens@0: NSMutableArray *_cells; // Really a 2D array, in row-major order. jens@0: } jens@0: jens@0: /** Initializes a new Grid with the given dimensions and cell size, and position in superview. jens@0: Note that a new Grid has no cells! Either call -addAllCells, or -addCellAtRow:column:. */ jens@0: - (id) initWithRows: (unsigned)nRows columns: (unsigned)nColumns jens@0: spacing: (CGSize)spacing jens@0: position: (CGPoint)pos; jens@0: jens@0: /** Initializes a new Grid with the given dimensions and frame in superview. jens@0: The cell size will be computed by dividing frame size by dimensions. jens@0: Note that a new Grid has no cells! Either call -addAllCells, or -addCellAtRow:column:. */ jens@0: - (id) initWithRows: (unsigned)nRows columns: (unsigned)nColumns jens@0: frame: (CGRect)frame; jens@0: jens@0: @property Class cellClass; // What kind of GridCells to create jens@0: @property (readonly) unsigned rows, columns; // Dimensions of the grid jens@0: @property (readonly) CGSize spacing; // x,y spacing of GridCells jens@0: @property CGColorRef cellColor, lineColor; // Cell background color, line color (or nil) jens@0: @property BOOL usesDiagonals; // Affects GridCell.neighbors, for rect grids jens@0: @property BOOL allowsMoves, allowsCaptures; // Can pieces be moved, and can they land on others? jens@0: jens@0: /** Returns the GridCell at the given coordinates, or nil if there is no cell there. jens@0: It's OK to call this with off-the-board coordinates; it will just return nil.*/ jens@0: - (GridCell*) cellAtRow: (unsigned)row column: (unsigned)col; jens@0: jens@0: /** Adds cells at all coordinates, creating a complete grid. */ jens@0: - (void) addAllCells; jens@0: jens@0: /** Adds a GridCell at the given coordinates. */ jens@0: - (GridCell*) addCellAtRow: (unsigned)row column: (unsigned)col; jens@0: jens@0: /** Removes a particular cell, leaving a blank space. */ jens@0: - (void) removeCellAtRow: (unsigned)row column: (unsigned)col; jens@0: jens@0: jens@0: // protected: jens@0: - (GridCell*) createCellAtRow: (unsigned)row column: (unsigned)col jens@0: suggestedFrame: (CGRect)frame; jens@0: jens@0: @end jens@0: jens@0: jens@0: /** Abstract superclass of a single cell in a grid. */ jens@0: @interface GridCell : BitHolder jens@0: { jens@0: Grid *_grid; jens@0: unsigned _row, _column; jens@0: } jens@0: jens@0: - (id) initWithGrid: (Grid*)grid jens@0: row: (unsigned)row column: (unsigned)col jens@0: frame: (CGRect)frame; jens@0: jens@0: @property (readonly) Grid* grid; jens@0: @property (readonly) unsigned row, column; jens@0: @property (readonly) NSArray* neighbors; // Dependent on grid.usesDiagonals jens@0: jens@0: /** Returns YES if 'forward' is north (increasing row#) for the current player */ jens@0: @property (readonly) BOOL fwdIsN; jens@0: jens@0: /* Go-style group detection. Returns the set of contiguous GridCells that have pieces of the same jens@0: owner as this one, and optionally a count of the number of "liberties", or adjacent empty cells. */ jens@0: - (NSSet*) getGroup: (int*)outLiberties; jens@0: jens@0: // protected: jens@0: - (void) drawInParentContext: (CGContextRef)ctx fill: (BOOL)fill; jens@0: @end jens@0: jens@0: jens@0: jens@0: /** A rectangular grid of squares. */ jens@0: @interface RectGrid : Grid jens@0: { jens@0: CGColorRef _altCellColor; jens@0: } jens@0: jens@0: /** If non-nil, alternate cells will be drawn with this background color, in a checkerboard pattern. jens@0: The precise rule is that cells whose row+column is odd use the altCellColor.*/ jens@0: @property CGColorRef altCellColor; jens@0: jens@0: @end jens@0: jens@0: jens@0: jens@0: /* A square in a RectGrid */ jens@0: @interface Square : GridCell jens@0: jens@0: @property (readonly) Square *nw, *n, *ne, *e, *se, *s, *sw, *w; // Absolute directions (n = increasing row#) jens@0: @property (readonly) Square *fl, *f, *fr, *r, *br, *b, *bl, *l; // Relative to player (upside-down for player 2) jens@0: jens@0: @end jens@0: jens@0: jens@0: /* Substitute this for Square in a RectGrid's cellClass to draw the lines through the centers jens@0: of the squares, so the pieces sit on the intersections, as in a Go board. */ jens@0: @interface GoSquare : Square jens@0: { jens@0: BOOL _dotted; jens@0: } jens@0: jens@0: /** Set to YES to put a dot at the intersection, as in the handicap points of a Go board. */ jens@0: @property BOOL dotted; jens@0: jens@0: @end