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@0
|
35 |
BOOL _usesDiagonals, _allowsMoves, _allowsCaptures;
|
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@0
|
58 |
|
jens@11
|
59 |
@property (readonly) NSArray *cells;
|
jens@11
|
60 |
|
jens@0
|
61 |
/** Returns the GridCell at the given coordinates, or nil if there is no cell there.
|
jens@0
|
62 |
It's OK to call this with off-the-board coordinates; it will just return nil.*/
|
jens@0
|
63 |
- (GridCell*) cellAtRow: (unsigned)row column: (unsigned)col;
|
jens@0
|
64 |
|
jens@0
|
65 |
/** Adds cells at all coordinates, creating a complete grid. */
|
jens@0
|
66 |
- (void) addAllCells;
|
jens@0
|
67 |
|
jens@0
|
68 |
/** Adds a GridCell at the given coordinates. */
|
jens@0
|
69 |
- (GridCell*) addCellAtRow: (unsigned)row column: (unsigned)col;
|
jens@0
|
70 |
|
jens@0
|
71 |
/** Removes a particular cell, leaving a blank space. */
|
jens@0
|
72 |
- (void) removeCellAtRow: (unsigned)row column: (unsigned)col;
|
jens@0
|
73 |
|
jens@7
|
74 |
- (GridCell*) cellWithName: (NSString*)identifier;
|
jens@0
|
75 |
|
jens@12
|
76 |
/** Returns all of the Players who have any Bits on the grid, with each Player's count being the
|
jens@12
|
77 |
number of Bits. */
|
jens@12
|
78 |
- (NSCountedSet*) countPiecesByPlayer;
|
jens@12
|
79 |
|
jens@12
|
80 |
|
jens@12
|
81 |
/** Utility to get and set the entire state of the Grid. The stateString is made by concatenating
|
jens@12
|
82 |
the name of the Bit of every GridCell in order, with "-" for empty cells.
|
jens@12
|
83 |
The setter method calls the Game's optional -makePieceNamed: method to create the pieces. */
|
jens@12
|
84 |
@property (copy) NSString *stateString;
|
jens@12
|
85 |
|
jens@12
|
86 |
/** Interprets the string as a series of cell names separated by "-", and tells the Game to move
|
jens@12
|
87 |
the piece at the first cell to each cell in succession by calling its -animateMoveFrom:to:. */
|
jens@12
|
88 |
- (BOOL) applyMoveString: (NSString*)move;
|
jens@12
|
89 |
|
jens@12
|
90 |
|
jens@0
|
91 |
// protected:
|
jens@0
|
92 |
- (GridCell*) createCellAtRow: (unsigned)row column: (unsigned)col
|
jens@0
|
93 |
suggestedFrame: (CGRect)frame;
|
jens@0
|
94 |
|
jens@0
|
95 |
@end
|
jens@0
|
96 |
|
jens@0
|
97 |
|
jens@0
|
98 |
/** Abstract superclass of a single cell in a grid. */
|
jens@0
|
99 |
@interface GridCell : BitHolder
|
jens@0
|
100 |
{
|
jens@0
|
101 |
Grid *_grid;
|
jens@0
|
102 |
unsigned _row, _column;
|
jens@0
|
103 |
}
|
jens@0
|
104 |
|
jens@0
|
105 |
- (id) initWithGrid: (Grid*)grid
|
jens@0
|
106 |
row: (unsigned)row column: (unsigned)col
|
jens@0
|
107 |
frame: (CGRect)frame;
|
jens@0
|
108 |
|
jens@0
|
109 |
@property (readonly) Grid* grid;
|
jens@0
|
110 |
@property (readonly) unsigned row, column;
|
jens@0
|
111 |
@property (readonly) NSArray* neighbors; // Dependent on grid.usesDiagonals
|
jens@0
|
112 |
|
jens@0
|
113 |
/** Returns YES if 'forward' is north (increasing row#) for the current player */
|
jens@0
|
114 |
@property (readonly) BOOL fwdIsN;
|
jens@0
|
115 |
|
jens@12
|
116 |
/** Go-style group detection. Returns the set of contiguous GridCells that have pieces of the same
|
jens@0
|
117 |
owner as this one, and optionally a count of the number of "liberties", or adjacent empty cells. */
|
jens@0
|
118 |
- (NSSet*) getGroup: (int*)outLiberties;
|
jens@0
|
119 |
|
jens@0
|
120 |
// protected:
|
jens@0
|
121 |
- (void) drawInParentContext: (CGContextRef)ctx fill: (BOOL)fill;
|
jens@0
|
122 |
@end
|
jens@0
|
123 |
|
jens@0
|
124 |
|
jens@0
|
125 |
|
jens@0
|
126 |
/** A rectangular grid of squares. */
|
jens@0
|
127 |
@interface RectGrid : Grid
|
jens@0
|
128 |
{
|
jens@0
|
129 |
CGColorRef _altCellColor;
|
jens@0
|
130 |
}
|
jens@0
|
131 |
|
jens@0
|
132 |
/** If non-nil, alternate cells will be drawn with this background color, in a checkerboard pattern.
|
jens@0
|
133 |
The precise rule is that cells whose row+column is odd use the altCellColor.*/
|
jens@0
|
134 |
@property CGColorRef altCellColor;
|
jens@0
|
135 |
|
jens@0
|
136 |
@end
|
jens@0
|
137 |
|
jens@0
|
138 |
|
jens@0
|
139 |
|
jens@0
|
140 |
/* A square in a RectGrid */
|
jens@0
|
141 |
@interface Square : GridCell
|
jens@0
|
142 |
|
jens@0
|
143 |
@property (readonly) Square *nw, *n, *ne, *e, *se, *s, *sw, *w; // Absolute directions (n = increasing row#)
|
jens@0
|
144 |
@property (readonly) Square *fl, *f, *fr, *r, *br, *b, *bl, *l; // Relative to player (upside-down for player 2)
|
jens@0
|
145 |
|
jens@12
|
146 |
/** Returns the absolute direction selector (see above) for the straight line from self to dst;
|
jens@12
|
147 |
or NULL if there is no straight line, or if dst==self.
|
jens@12
|
148 |
Diagonal lines are allowed only if the Grid's -usesDiagonals is YES. */
|
jens@12
|
149 |
- (SEL) directionToCell: (GridCell*)dst;
|
jens@12
|
150 |
|
jens@12
|
151 |
/** Returns an array of all the cells in a straight line from self to dst;
|
jens@12
|
152 |
or NULL if there is no straight line, or if dst==self.
|
jens@12
|
153 |
If 'inclusive' is YES, the array will include self and dst, otherwise not.
|
jens@12
|
154 |
Diagonal lines are allowed only if the Grid's -usesDiagonals is YES. */
|
jens@12
|
155 |
- (NSArray*) lineToCell: (GridCell*)dst inclusive: (BOOL)inclusive;
|
jens@12
|
156 |
|
jens@0
|
157 |
@end
|
jens@0
|
158 |
|
jens@0
|
159 |
|
jens@0
|
160 |
/* Substitute this for Square in a RectGrid's cellClass to draw the lines through the centers
|
jens@0
|
161 |
of the squares, so the pieces sit on the intersections, as in a Go board. */
|
jens@0
|
162 |
@interface GoSquare : Square
|
jens@0
|
163 |
{
|
jens@0
|
164 |
BOOL _dotted;
|
jens@0
|
165 |
}
|
jens@0
|
166 |
|
jens@0
|
167 |
/** Set to YES to put a dot at the intersection, as in the handicap points of a Go board. */
|
jens@0
|
168 |
@property BOOL dotted;
|
jens@0
|
169 |
|
jens@0
|
170 |
@end
|