File: TourneyAI.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 (55 lines) | stat: -rw-r--r-- 1,586 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
//
//  TourneyAI.m
//  Gridlock
//
//  Created by Brian on Fri Jan 02 2004.
//  Copyright (c) 2004 __MyCompanyName__. All rights reserved.
//

#import "TourneyAI.h"
#import "TourneyGame.h"

@implementation TourneyAI

-(id)init {
  self = [super init];
  regWeight = 4;
  kingMinWeight = 6;
  kingMaxWeight = 10;
  return self;
}

-(int)weightForKingAtPosition:(id)pos forPlayer:(int)pnum inGame:(Game *)game {
  int rowsFromBack = (pnum==1) ? [game numberOfRows]-[pos row]-1 : [pos row];
  double ratio = 1.0*rowsFromBack/([game numberOfRows]-2);
  return kingMinWeight + ratio*(kingMaxWeight-kingMinWeight);
}

-(int)relativeUtilityForGame:(Game *)game player:(int)pnum {
  TourneyGame *cgame = game;
  int myutility  = 0;
  int opputility = 0;
  int oppnum = [cgame playerNumberMovingAfterPlayer:pnum];
  int total = 0;
  NSEnumerator *ke;
  id pos;
  // regular pieces
  myutility  = regWeight*[cgame numberOfRegularPiecesForPlayer:pnum];
  opputility = regWeight*[cgame numberOfRegularPiecesForPlayer:oppnum];
  // kings are more valuable the closer they are to the first row
  ke = [[game grid] enumeratorForPositionsWithValue:-1];
  while (pos=[ke nextObject]) {
    myutility += [self weightForKingAtPosition:pos forPlayer:1 inGame:game];
  }
  ke = [[game grid] enumeratorForPositionsWithValue:-2];
  while (pos=[ke nextObject]) {
    opputility += [self weightForKingAtPosition:pos forPlayer:2 inGame:game];
  }
  
  total = (myutility-opputility)*1000;
  
  total += (int)(1000.0*[[self class] normalizedRatioOf:myutility to:opputility maxRatio:2.0]);
  return total;
}

@end