File: AnyDirectionUntilBlockedMoveRule.m

package info (click to toggle)
gridlock.app 1.10-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,556 kB
  • sloc: objc: 10,334; ansic: 669; makefile: 12
file content (65 lines) | stat: -rw-r--r-- 2,054 bytes parent folder | download | duplicates (7)
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
//
//  AnyDirectionUntilBlockedMoveRule.m
//  Gridlock
//
//  Created by Brian on Sat Nov 22 2003.
//  Copyright (c) 2003 __MyCompanyName__. All rights reserved.
//

#import "AnyDirectionUntilBlockedMoveRule.h"


@implementation AnyDirectionUntilBlockedMoveRule

+(BOOL)appendValidMovesForGame:(Game *)game fromPosition:(id)pos intoArray:(NSMutableArray *)moves {
  static int ROW_DIRECTIONS[] = {+1,+1,+1, 0, 0,-1,-1,-1};
  static int COL_DIRECTIONS[] = {+1, 0,-1,+1,-1,+1, 0,-1};
  BOOL found = NO;
  int r = [pos row];
  int c = [pos column];
  int i;
  for(i=0; i<8; i++) {
    int dr = ROW_DIRECTIONS[i];
    int dc = COL_DIRECTIONS[i];
    int dist = 1;
    while ([game isValidRow:r+dist*dr column:c+dist*dc] && [game valueAtRow:r+dist*dr column:c+dist*dc]==0) {
      dist++;
    }
    dist--; // we hit the edge or another piece, so back up 1
    if (dist>0) {
      if (!moves) return YES; // if no array, we just want to know if there's a valid move
      found = YES;
      [moves addObject:[NSArray arrayWithObjects:pos, [DCHypergridPosition positionWithRow:r+dist*dr column:c+dist*dc], nil]];
    }
  }
  return found;
}

+(NSArray *)allValidMoveSequences:(Game *)game fromPositionsWithValue:(int)sval {
  NSMutableArray *moves = [NSMutableArray array];
  NSEnumerator *pe = [[game grid] enumeratorForPositionsWithValue:sval];
  id pos;
  while (pos=[pe nextObject]) {
    [self appendValidMovesForGame:game fromPosition:pos intoArray:moves];
  }
  return moves;
}

+(NSArray *)allValidMoveSequences:(Game *)game {
  return [self allValidMoveSequences:game fromPositionsWithValue:[game currentPlayerNumber]];
}

+(BOOL)gameHasAnyMoves:(Game *)game fromPositionsWithValue:(int)sval {
  NSEnumerator *pe = [[game grid] enumeratorForPositionsWithValue:sval];
  id pos;
  while (pos=[pe nextObject]) {
    if ([self appendValidMovesForGame:game fromPosition:pos intoArray:nil]) return YES;
  }
  return NO;
}

+(BOOL)gameHasAnyMoves:(Game *)game {
  return [self gameHasAnyMoves:game fromPositionsWithValue:[game currentPlayerNumber]];
}

@end