/* 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 "CheckersGame.h" static NSArray* _averagesOfPositionsWithPosition(NSArray *positions, DCHypergridPosition *basepos) { if ([positions count]==0) return nil; else { int r = [basepos row]; int c = [basepos column]; NSMutableArray *array = [NSMutableArray array]; NSEnumerator *pe = [positions objectEnumerator]; id pos; while (pos=[pe nextObject]) { [array addObject:[DCHypergridPosition positionWithRow:(r+[pos row])/2 column:(c+[pos column])/2]]; } return array; } } @implementation CheckersGame -(void)reset { [super reset]; [self setGrid:[DCHypergrid gridWithRows:[[[self configurationInfo] objectForKey:@"rows"] intValue] columns:[[[self configurationInfo] objectForKey:@"cols"] intValue]]]; { int rows = [self numberOfRows]; int cols = [self numberOfColumns]; int r,c; // set alternate cells, leaving the two middle rows empty for(r=0; r0) return NO; if ([[self validJumpsForPlayer:pnum fromPosition:pos isKing:king startPosition:pos] count]>0) return NO; } } return YES; } -(int)winningPlayer { return [self nextPlayerNumber]; } -(void)_insertValidJumpsForPlayer:(int)pnum fromPosition:(DCHypergridPosition *)pos isKing:(BOOL)king ignoringJumpedPositions:(NSArray *)previousJumpedPositions startingPositions:(NSArray *)partialMove intoMoveArray:(NSMutableArray *)moves { // get available jumps NSArray *jumps = [self validJumpsForPlayer:pnum fromPosition:pos isKing:king startPosition:[partialMove objectAtIndex:0]]; NSArray *jumpedPositions = _averagesOfPositionsWithPosition(jumps, pos); int i; BOOL foundJump = NO; for(i=0; i<[jumps count]; i++) { if (![previousJumpedPositions containsObject:[jumpedPositions objectAtIndex:i]]) { foundJump = YES; // keep going... [self _insertValidJumpsForPlayer:pnum fromPosition:[jumps objectAtIndex:i] isKing:king ignoringJumpedPositions:[previousJumpedPositions arrayByAddingObject:[jumpedPositions objectAtIndex:i]] startingPositions:[partialMove arrayByAddingObject:[jumps objectAtIndex:i]] intoMoveArray:moves]; } } if (!foundJump) { if ([partialMove count]>=2) { [moves addObject:partialMove]; } } } -(NSArray *)allValidMoveSequences { NSMutableArray *jumps = [NSMutableArray array]; NSMutableArray *nonjumps = [NSMutableArray array]; int pnum = [self currentPlayerNumber]; NSEnumerator *pe = [[self grid] positionEnumerator]; id pos; while (pos=[pe nextObject]) { if (pnum==[self ownerOfPosition:pos]) { BOOL king = [self isKingAtPosition:pos]; // process jumps [self _insertValidJumpsForPlayer:pnum fromPosition:pos isKing:king ignoringJumpedPositions:[NSArray array] startingPositions:[pos arrayWithSelf_] intoMoveArray:jumps]; // process nonjumps if no jumps found if ([jumps count]==0) { NSArray *nonJumpPositions = [self validSingleMovesForPlayer:pnum fromPosition:pos isKing:king]; NSEnumerator *nje = [nonJumpPositions objectEnumerator]; id pos2; while (pos2=[nje nextObject]) { [nonjumps addObject:[NSArray arrayWithObjects:pos, pos2, nil]]; } } } } return ([jumps count]>0) ? jumps : nonjumps; } @end