File: FiancoGame.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 (99 lines) | stat: -rw-r--r-- 3,074 bytes parent folder | download | duplicates (6)
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
//
//  FiancoGame.m
//  Gridlock
//
//  Created by Brian on Sun Mar 07 2004.
//  Copyright (c) 2004 __MyCompanyName__. All rights reserved.
//

#import "FiancoGame.h"


@implementation FiancoGame

-(void)reset {
  [super reset];
  [self createGridFromConfiguration];
}  

-(BOOL)prepareMoveSequence:(NSArray *)positions {
  id pos0 = [positions objectAtIndex:0];
  id pos1 = [positions objectAtIndex:1];
  
  [self resetFutureGrid];

  [[self futureGrid] setValue:0 atPosition:pos0];
  [[self futureGrid] setValue:[self valueAtPosition:pos0] atPosition:pos1];
  // remove jumped piece if applicable
  if (abs([pos0 row]-[pos1 row])>1) {
    [[self futureGrid] setValue:0 atRow:([pos0 row]+[pos1 row])/2 column:([pos0 column]+[pos1 column])/2];
  }
  return YES;
}

-(void)appendJumpsFromPosition:(DCHypergridPosition *)pos forPlayer:(int)pnum intoArray:(NSMutableArray *)moves {
  int r = [pos row];
  int c = [pos column];
  int dr = (pnum==1) ? 1 : -1;
  int dc;
  for(dc=-1; dc<=+1; dc+=2) {
    if ([self isValidRow:r+2*dr column:c+2*dc]) {
      // must jump over enemy and into empty square
      int jval = [self valueAtRow:r+dr column:c+dc];
      if (jval>0 && jval!=pnum && [self valueAtRow:r+2*dr column:c+2*dc]==0) {
        [moves addObject:[NSArray arrayWithObjects:pos, [DCHypergridPosition positionWithRow:r+2*dr column:c+2*dc], nil]];
      }
    }
  }
}

-(void)appendNonjumpsFromPosition:(DCHypergridPosition *)pos forPlayer:(int)pnum intoArray:(NSMutableArray *)moves {
  int r = [pos row];
  int c = [pos column];
  int dr = (pnum==1) ? 1 : -1;
  // check left, right, forward
  if ([self isValidRow:r column:c-1] && [self valueAtRow:r column:c-1]==0) {
    [moves addObject:[NSArray arrayWithObjects:pos, [DCHypergridPosition positionWithRow:r column:c-1], nil]];
  }
  if ([self isValidRow:r column:c+1] && [self valueAtRow:r column:c+1]==0) {
    [moves addObject:[NSArray arrayWithObjects:pos, [DCHypergridPosition positionWithRow:r column:c+1], nil]];
  }
  if ([self isValidRow:r+dr column:c] && [self valueAtRow:r+dr column:c]==0) {
    [moves addObject:[NSArray arrayWithObjects:pos, [DCHypergridPosition positionWithRow:r+dr column:c], nil]];
  }
}

-(NSArray *)allValidMoveSequences {
  NSMutableArray *jumps = [NSMutableArray array];
  NSMutableArray *nonjumps = [NSMutableArray array];
  int pnum = [self currentPlayerNumber];
  NSEnumerator *pe = [[self grid] enumeratorForPositionsWithValue:pnum];
  id pos;
  while (pos=[pe nextObject]) {
    [self appendJumpsFromPosition:pos forPlayer:pnum intoArray:jumps];
    if ([jumps count]==0) {
      [self appendNonjumpsFromPosition:pos forPlayer:pnum intoArray:nonjumps];
    }
  }
  return ([jumps count]>0) ? jumps : nonjumps;
}

-(int)playerAtLastRow {
  int c;
  int maxr = [self numberOfRows]-1;
  for(c=[self numberOfColumns]-1; c>=0; c--) {
    if ([self valueAtRow:0 column:c]==2) return 2;
    if ([self valueAtRow:maxr column:c]==1) return 1;
  }
  return 0;
}

-(BOOL)isGameOver {
  return [self playerAtLastRow]>0;
}

-(int)winningPlayer {
  return [self playerAtLastRow];
}

@end