1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
/* Gridlock
Copyright (c) 2002-2003 by Brian Nenninger. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#import "AbstractLineGame.h"
@implementation AbstractLineGame
-(int)winningLength {
return -1;
}
-(id)init {
if ((self = [super init])) {
_cachedThreatDicts = [[NSMutableDictionary alloc] init];
}
return self;
}
idAccessor(preparedMove, setPreparedMove)
-(void)dealloc {
[_cachedThreatDicts release];
[self setPreparedMove:nil];
[super dealloc];
}
-(void)reset {
[super reset];
[self setGrid:[DCHypergrid gridWithRows:[[[self configurationInfo] objectForKey:@"rows"] intValue]
columns:[[[self configurationInfo] objectForKey:@"cols"] intValue]]];
[_cachedThreatDicts removeAllObjects];
}
-(BOOL)prepareMoveSequence:(NSArray *)positions {
[self setPreparedMove:[positions lastObject]];
return YES;
}
-(NSArray *)positionsOfAllChangingCells {
return [[self preparedMove] arrayWithSelf_];
}
-(int)futureValueAtPosition:(DCHypergridPosition *)pos {
if ([pos isEqual:[self preparedMove]]) return [self currentPlayerNumber];
else return [self valueAtPosition:pos];
}
-(void)didMakeMove:(NSArray *)positions {
[super didMakeMove:positions];
[self setPreparedMove:nil];
[_cachedThreatDicts removeAllObjects];
}
-(BOOL)isGameOver {
if ([self hasPlayerWon:1] || [self hasPlayerWon:2]) return YES;
else {
// game is over if all cells are filled
return ![[self grid] hasCellWithValue:0];
}
}
-(BOOL)hasPlayerWon:(int)pnum {
NSArray *threats = [self threatsForPlayer:pnum];
NSEnumerator *te = [threats objectEnumerator];
NSDictionary *threat;
while ((threat=[te nextObject])) {
int length = [[threat objectForKey:@"length"] intValue];
if (length>=[self winningLength]) return YES;
}
return NO;
}
-(int)winningPlayer {
int pcount = [self numberOfPlayers];
int i;
for(i=1; i<=pcount; i++) {
if ([self hasPlayerWon:i]) return i;
}
return 0;
}
-(NSArray *)positionsOfWinningPieces {
int wpnum = [self winningPlayer];
if (wpnum>=1 && wpnum<=[self numberOfPlayers]) {
NSArray *threats = [self threatsForPlayer:wpnum];
NSEnumerator *te = [threats objectEnumerator];
NSDictionary *threat;
while ((threat=[te nextObject])) {
int length = [[threat objectForKey:@"length"] intValue];
if (length>=[self winningLength]) {
NSMutableArray *positions = [NSMutableArray array];
int i=0;
DCHypergridPosition *pos = [threat objectForKey:@"position"];
DCHypergridPosition *dir = [threat objectForKey:@"direction"];
while (i<[self winningLength]) {
[positions addObject:pos];
pos = [pos positionByAddingPosition:dir];
i++;
}
return positions;
}
}
}
return nil;
}
/** Returns an array of NSDictionary objects, each of which describes a threat line. The keys and values
of each dictionary are as follows:
"position" - DCHypergridPosition, starting position of the threat line
"direction" - DCHypergridPosition, direction of the threat line relative to position
"length" - number of cells in the threat line
"openBefore" - number of empty cells "behind" the threat line (relative to direction)
"openAfter" - number of empty cells in front of the threat line
*/
-(NSArray *)threatsForPlayer:(int)pnum {
NSNumber *num = [NSNumber numberWithInt:pnum];
NSArray *result = [_cachedThreatDicts objectForKey:num];
if (!result) {
[_cachedThreatDicts setObject:(result=[self _threatsForPlayer:pnum]) forKey:num];
}
return result;
}
-(NSArray *)_threatsForPlayer:(int)pnum {
NSMutableArray *threats = [NSMutableArray array];
NSEnumerator *ge = [grid positionEnumerator];
DCHypergridPosition *pos;
while ((pos=[ge nextObject])) {
// does the player own this cell?
if ([self valueAtPosition:pos]==pnum) {
// only look in the "forward" directions
int deltas[8] = {0,1, 1,-1, 1,0, 1,1};
int i;
for(i=0; i<4; i++) {
int dr = deltas[2*i];
int dc = deltas[2*i+1];
if ([pos row]+dr<[self numberOfRows] &&
[pos column]+dc>=0 && [pos column]+dc<[self numberOfColumns]) {
id diff = [DCHypergridPosition positionWithRow:dr column:dc];
// make sure the player does not own the cell going backwards
id temppos = [pos positionBySubtractingPosition:diff];
int backval = -1;
if (![grid isValidPosition:temppos] || (backval=[self valueAtPosition:temppos])!=pnum) {
int backopen = 0;
int fwdopen = 0;
int length = 1;
BOOL done = NO;
// walk forward, incrementing length while we keep hitting cells we own, then fwdopen
BOOL threatFinished = NO;
temppos=[pos positionByAddingPosition:diff];
while (!done) {
if ([grid isValidPosition:temppos]) {
int value = [grid valueAtPosition:temppos];
if (!threatFinished) {
if (value==pnum) ++length;
else threatFinished = YES;
}
if (threatFinished) {
if (value==pnum || value==0) ++fwdopen;
else done = YES;
}
}
else done = YES;
if (!done) temppos = [temppos positionByAddingPosition:diff];
}
// walk backward, incrementing backopen
done = 0;
temppos = [pos positionBySubtractingPosition:diff];
while (!done) {
if ([grid isValidPosition:temppos]) {
int value = [grid valueAtPosition:temppos];
if (value==pnum || value==0) ++backopen;
else done = YES;
}
else done = YES;
if (!done) temppos = [temppos positionBySubtractingPosition:diff];
}
// create threat dictionary
{
int totallen = length+backopen+fwdopen;
// can't be a winning threat unless totallen>=winningLength
if (length>1 && totallen>=[self winningLength]) {
NSMutableDictionary *threatDict = [NSMutableDictionary dictionary];
[threatDict setObject:pos forKey:@"position"];
[threatDict setObject:diff forKey:@"direction"];
[threatDict setObject:[NSNumber numberWithInt:length] forKey:@"length"];
[threatDict setObject:[NSNumber numberWithInt:fwdopen] forKey:@"openAfter"];
[threatDict setObject:[NSNumber numberWithInt:backopen] forKey:@"openBefore"];
[threats addObject:threatDict];
}
}
}
}
}
}
}
return threats;
}
@end
|