File: FiancoAI.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 (57 lines) | stat: -rw-r--r-- 1,590 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
//
//  FiancoAI.m
//  Gridlock
//
//  Created by Brian on Thu Mar 11 2004.
//  Copyright (c) 2004 __MyCompanyName__. All rights reserved.
//

#import "FiancoAI.h"


@implementation FiancoAI

-(int)utilityForGame:(Game *)game player:(int)pnum {
  int maxr = [game numberOfRows];
  int baserow = (pnum==1) ? 0 : maxr-1;
  int dr = (pnum==1) ? 1 : -1;
  
  int utility = 0;
  int maxUnblockedDist = 0;
  NSEnumerator *ce = [[game grid] enumeratorForPositionsWithValue:pnum];
  id pos;
  while (pos=[ce nextObject]) {
    int dist = abs([pos row]-baserow);
    utility += 100+dist*dist;
    
    if (dist>maxUnblockedDist && dist>maxr-4) {
      // check for unblocked path to end
      int oppnum = [game playerNumberMovingAfterPlayer:pnum];
      int r0 = [pos row];
      int c0 = [pos column];
      BOOL unblocked = YES;
      int d;
      for(d=1; dist+d<=maxr && unblocked; d++) {
        int r = r0+d*dr;
        int dc;
        int crange = ([game currentPlayerNumber]==pnum) ? d-1 : d;
        for(dc=-crange; dc<=crange && unblocked; dc++) {
          int c = c0+dc;
          if ([game isValidRow:r column:c] && oppnum==[game valueAtRow:r column:c]) unblocked = NO;
        }
      }
      if (unblocked) {
        maxUnblockedDist = dist;
        //NSLog(@"Found unblocked piece at %@ with distance %d", pos, dist);
      }
      
    }
  }
  return utility+10000*maxUnblockedDist;
}

-(int)relativeUtilityForGame:(Game *)game player:(int)pnum {
  return [self utilityForGame:game player:pnum] - [self utilityForGame:game player:[game playerNumberMovingAfterPlayer:pnum]];
}

@end