File: ReactorGame.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 (71 lines) | stat: -rw-r--r-- 2,443 bytes parent folder | download | duplicates (3)
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
//
//  ReactorGame.m
//  Gridlock
//
//  Created by Brian on Sun Feb 15 2004.
//  Copyright (c) 2004 __MyCompanyName__. All rights reserved.
//

#import "ReactorGame.h"
#import "AnyDirectionUntilBlockedMoveRule.h"

@implementation ReactorGame

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

  [self fillRandomEmptyCellsWithValue:1  count:[[[self configurationInfo] objectForKey:@"p1pieces"] intValue]];
  [self fillRandomEmptyCellsWithValue:2  count:[[[self configurationInfo] objectForKey:@"p2pieces"] intValue]];
  [self fillRandomEmptyCellsWithValue:-1 count:[[[self configurationInfo] objectForKey:@"neutrons"] intValue]];
}

-(BOOL)prepareMoveSequence:(NSArray *)positions {
  id pos0 = [positions objectAtIndex:0];
  id pos1 = [positions objectAtIndex:1];
  id previousUnmoveable = [[self grid] positionWithValue:-2]; // inefficient to call this every time
  NSArray *neighbors = [[self grid] neighborsOfPosition:pos1 distance:1];
  NSEnumerator *ne = [neighbors objectEnumerator];
  id npos;
  
  [self resetFutureGrid];
  
  [[self futureGrid] setValue:0 atPosition:pos0];
  // remove all non-neutrons around destination
  while ((npos=[ne nextObject])) {
    if ([self valueAtPosition:npos]>0) {
      [[self futureGrid] setValue:0 atPosition:npos];
    }
  }
  // restore moving neutron, can't be moved on next turn
  [[self futureGrid] setValue:-2 atPosition:pos1];
  // neutron that couldn't be moved this turn can on next turn (inefficient to call -positionWithValue: every time)
  if (previousUnmoveable) [[self futureGrid] setValue:-1 atPosition:previousUnmoveable];
  return YES;
}

-(int)winningPlayer {
  int n1 = [[self grid] numberOfCellsWithValue:1];
  int n2 = [[self grid] numberOfCellsWithValue:2];
  if (n1>0 && n2==0) return 1;
  if (n2>0 && n1==0) return 2;
  // either both have no pieces, are tied at 1, or current player has no move
  return 0;
}

-(BOOL)isGameOver {
  // game over if either player has no pieces, or both have exactly 1, or current player can't move
  int n1 = [[self grid] numberOfCellsWithValue:1];
  int n2 = [[self grid] numberOfCellsWithValue:2];
  return (n1==0 || n2==0 || ![AnyDirectionUntilBlockedMoveRule gameHasAnyMoves:self fromPositionsWithValue:-1] || [self moveCount]>100);
}

-(NSArray *)allValidMoveSequences {
  return [AnyDirectionUntilBlockedMoveRule allValidMoveSequences:self fromPositionsWithValue:-1];
}

-(BOOL)showScores {
  return YES;
}

@end