Source/GoGame.m
changeset 10 6c78cc6bd7a6
parent 7 428a194e3e59
child 16 28392c9a969f
     1.1 --- a/Source/GoGame.m	Sun Mar 16 15:06:47 2008 -0700
     1.2 +++ b/Source/GoGame.m	Thu Jul 03 17:44:30 2008 -0700
     1.3 @@ -32,76 +32,93 @@
     1.4  @implementation GoGame
     1.5  
     1.6  
     1.7 -- (id) initWithBoard: (GGBLayer*)board
     1.8 ++ (int) dimensions {return 19;}
     1.9 +
    1.10 +- (id) init
    1.11  {
    1.12 -    self = [super initWithBoard: board];
    1.13 +    self = [super init];
    1.14      if (self != nil) {
    1.15          [self setNumberOfPlayers: 2];
    1.16          [(Player*)[_players objectAtIndex: 0] setName: @"Red"];
    1.17          [(Player*)[_players objectAtIndex: 1] setName: @"White"];
    1.18 -        
    1.19 -        CGSize size = board.bounds.size;
    1.20 -        CGFloat boardSide = MIN(size.width,size.height);
    1.21 -        RectGrid *grid = [[RectGrid alloc] initWithRows: 9 columns: 9 
    1.22 -                                                  frame: CGRectMake(floor((size.width-boardSide)/2),
    1.23 -                                                                    floor((size.height-boardSide)/2),
    1.24 -                                                                    boardSide,boardSide)];
    1.25 -        _grid = grid;
    1.26 -        grid.backgroundColor = GetCGPatternNamed(@"Wood.jpg");
    1.27 -        grid.borderColor = kTranslucentLightGrayColor;
    1.28 -        grid.borderWidth = 2;
    1.29 -        grid.lineColor = kTranslucentGrayColor;
    1.30 -        grid.cellClass = [GoSquare class];
    1.31 -        [grid addAllCells];
    1.32 -        ((GoSquare*)[grid cellAtRow: 2 column: 2]).dotted = YES;
    1.33 -        ((GoSquare*)[grid cellAtRow: 6 column: 6]).dotted = YES;
    1.34 -        ((GoSquare*)[grid cellAtRow: 2 column: 6]).dotted = YES;
    1.35 -        ((GoSquare*)[grid cellAtRow: 6 column: 2]).dotted = YES;
    1.36 -        grid.usesDiagonals = grid.allowsMoves = grid.allowsCaptures = NO;
    1.37 -        [board addSublayer: grid];
    1.38 -        [grid release];
    1.39 -        
    1.40 -        CGRect gridFrame = grid.frame;
    1.41 -        CGFloat pieceSize = (int)grid.spacing.width & ~1;  // make sure it's even
    1.42 -        CGFloat captureHeight = gridFrame.size.height-4*pieceSize;
    1.43 -        _captured[0] = [[Stack alloc] initWithStartPos: CGPointMake(2*pieceSize,0)
    1.44 -                                               spacing: CGSizeMake(0,pieceSize)
    1.45 -                                          wrapInterval: floor(captureHeight/pieceSize)
    1.46 -                                           wrapSpacing: CGSizeMake(-pieceSize,0)];
    1.47 -        _captured[0].frame = CGRectMake(CGRectGetMinX(gridFrame)-3*pieceSize, 
    1.48 -                                          CGRectGetMinY(gridFrame)+3*pieceSize,
    1.49 -                                          2*pieceSize, captureHeight);
    1.50 -        _captured[0].zPosition = kPieceZ+1;
    1.51 -        [board addSublayer: _captured[0]];
    1.52 -        [_captured[0] release];
    1.53 -        
    1.54 -        _captured[1] = [[Stack alloc] initWithStartPos: CGPointMake(0,captureHeight)
    1.55 -                                               spacing: CGSizeMake(0,-pieceSize)
    1.56 -                                          wrapInterval: floor(captureHeight/pieceSize)
    1.57 -                                           wrapSpacing: CGSizeMake(pieceSize,0)];
    1.58 -        _captured[1].frame = CGRectMake(CGRectGetMaxX(gridFrame)+pieceSize, 
    1.59 -                                          CGRectGetMinY(gridFrame)+pieceSize,
    1.60 -                                          2*pieceSize, captureHeight);
    1.61 -        _captured[1].zPosition = kPieceZ+1;
    1.62 -        [board addSublayer: _captured[1]];
    1.63 -        [_captured[1] release];
    1.64 -
    1.65 -        [self nextPlayer];
    1.66 -        PreloadSound(@"Pop");
    1.67 -}
    1.68 +    }
    1.69      return self;
    1.70  }
    1.71 +        
    1.72 +- (void) setUpBoard
    1.73 +{
    1.74 +    int dimensions = [[self class] dimensions];
    1.75 +    CGSize size = _board.bounds.size;
    1.76 +    CGFloat boardSide = MIN(size.width,size.height);
    1.77 +    RectGrid *grid = [[RectGrid alloc] initWithRows: dimensions columns: dimensions 
    1.78 +                                              frame: CGRectMake(floor((size.width-boardSide)/2),
    1.79 +                                                                floor((size.height-boardSide)/2),
    1.80 +                                                                boardSide,boardSide)];
    1.81 +    _grid = grid;
    1.82 +    /*
    1.83 +    grid.backgroundColor = GetCGPatternNamed(@"Wood.jpg");
    1.84 +    grid.borderColor = kTranslucentLightGrayColor;
    1.85 +    grid.borderWidth = 2;
    1.86 +    */
    1.87 +    grid.lineColor = kTranslucentGrayColor;
    1.88 +    grid.cellClass = [GoSquare class];
    1.89 +    [grid addAllCells];
    1.90 +    ((GoSquare*)[grid cellAtRow: 2 column: 2]).dotted = YES;
    1.91 +    ((GoSquare*)[grid cellAtRow: 6 column: 6]).dotted = YES;
    1.92 +    ((GoSquare*)[grid cellAtRow: 2 column: 6]).dotted = YES;
    1.93 +    ((GoSquare*)[grid cellAtRow: 6 column: 2]).dotted = YES;
    1.94 +    grid.usesDiagonals = grid.allowsMoves = grid.allowsCaptures = NO;
    1.95 +    [_board addSublayer: grid];
    1.96 +    [grid release];
    1.97 +    
    1.98 +    CGRect gridFrame = grid.frame;
    1.99 +    CGFloat pieceSize = (int)grid.spacing.width & ~1;  // make sure it's even
   1.100 +    CGFloat captureHeight = gridFrame.size.height-4*pieceSize;
   1.101 +    _captured[0] = [[Stack alloc] initWithStartPos: CGPointMake(2*pieceSize,0)
   1.102 +                                           spacing: CGSizeMake(0,pieceSize)
   1.103 +                                      wrapInterval: floor(captureHeight/pieceSize)
   1.104 +                                       wrapSpacing: CGSizeMake(-pieceSize,0)];
   1.105 +    _captured[0].frame = CGRectMake(CGRectGetMinX(gridFrame)-3*pieceSize, 
   1.106 +                                      CGRectGetMinY(gridFrame)+3*pieceSize,
   1.107 +                                      2*pieceSize, captureHeight);
   1.108 +    _captured[0].zPosition = kPieceZ+1;
   1.109 +    [_board addSublayer: _captured[0]];
   1.110 +    [_captured[0] release];
   1.111 +    
   1.112 +    _captured[1] = [[Stack alloc] initWithStartPos: CGPointMake(0,captureHeight)
   1.113 +                                           spacing: CGSizeMake(0,-pieceSize)
   1.114 +                                      wrapInterval: floor(captureHeight/pieceSize)
   1.115 +                                       wrapSpacing: CGSizeMake(pieceSize,0)];
   1.116 +    _captured[1].frame = CGRectMake(CGRectGetMaxX(gridFrame)+pieceSize, 
   1.117 +                                      CGRectGetMinY(gridFrame)+pieceSize,
   1.118 +                                      2*pieceSize, captureHeight);
   1.119 +    _captured[1].zPosition = kPieceZ+1;
   1.120 +    [_board addSublayer: _captured[1]];
   1.121 +    [_captured[1] release];
   1.122  
   1.123 +    PreloadSound(@"Pop");
   1.124 +}
   1.125 +
   1.126 +- (CGImageRef) iconForPlayer: (int)playerNum
   1.127 +{
   1.128 +    return GetCGImageNamed( playerNum ?@"bot086.png" :@"bot089.png" );
   1.129 +}
   1.130 +
   1.131 +- (Piece*) pieceForPlayer: (int)index
   1.132 +{
   1.133 +    NSString *imageName = index ?@"bot086.png" :@"bot089.png";
   1.134 +    CGFloat pieceSize = (int)(_grid.spacing.width * 0.9) & ~1;  // make sure it's even
   1.135 +    Piece *stone = [[Piece alloc] initWithImageNamed: imageName scale: pieceSize];
   1.136 +    stone.owner = [self.players objectAtIndex: index];
   1.137 +    return [stone autorelease];
   1.138 +}
   1.139  
   1.140  - (Bit*) bitToPlaceInHolder: (id<BitHolder>)holder
   1.141  {
   1.142      if( holder.bit != nil || ! [holder isKindOfClass: [GoSquare class]] )
   1.143          return nil;
   1.144 -    NSString *imageName = self.currentPlayer.index ?@"White Ball.png" :@"Red Ball.png";
   1.145 -    CGFloat pieceSize = (int)(_grid.spacing.width * 0.9) & ~1;  // make sure it's even
   1.146 -    Piece *stone = [[Piece alloc] initWithImageNamed: imageName scale: pieceSize];
   1.147 -    stone.owner = self.currentPlayer;
   1.148 -    return [stone autorelease];
   1.149 +    else
   1.150 +        return [self pieceForPlayer: self.currentPlayer.index];
   1.151  }
   1.152  
   1.153  
   1.154 @@ -143,7 +160,7 @@
   1.155  - (void) bit: (Bit*)bit movedFrom: (id<BitHolder>)srcHolder to: (id<BitHolder>)dstHolder
   1.156  {
   1.157      Square *dst=(Square*)dstHolder;
   1.158 -    int curIndex = _currentPlayer.index;
   1.159 +    int curIndex = self.currentPlayer.index;
   1.160      // Check for captured enemy groups:
   1.161      BOOL captured = NO;
   1.162      for( GridCell *c in dst.neighbors )
   1.163 @@ -158,8 +175,9 @@
   1.164          }
   1.165      if( captured )
   1.166          PlaySound(@"Pop");
   1.167 -        
   1.168 -    [self nextPlayer];
   1.169 +    
   1.170 +    [self.currentTurn addToMove: dst.name];
   1.171 +    [self endTurn];
   1.172  }
   1.173  
   1.174  
   1.175 @@ -167,4 +185,62 @@
   1.176  // both of which are rather complex to decide in Go.
   1.177  
   1.178  
   1.179 +#pragma mark -
   1.180 +#pragma mark STATE:
   1.181 +
   1.182 +
   1.183 +- (NSString*) stateString
   1.184 +{
   1.185 +    int n = _grid.rows;
   1.186 +    unichar state[n*n];
   1.187 +    for( int y=0; y<n; y++ )
   1.188 +        for( int x=0; x<n; x++ ) {
   1.189 +            Bit *bit = [_grid cellAtRow: y column: x].bit;
   1.190 +            unichar ch;
   1.191 +            if( bit==nil )
   1.192 +                ch = '-';
   1.193 +            else
   1.194 +                ch = '1' + bit.owner.index;
   1.195 +            state[y*n+x] = ch;
   1.196 +        }
   1.197 +    return [NSString stringWithCharacters: state length: n*n];
   1.198 +}
   1.199 +
   1.200 +- (void) setStateString: (NSString*)state
   1.201 +{
   1.202 +    NSLog(@"Go: setStateString: '%@'",state);
   1.203 +    int n = _grid.rows;
   1.204 +    for( int y=0; y<n; y++ )
   1.205 +        for( int x=0; x<n; x++ ) {
   1.206 +            int i = y*n+x;
   1.207 +            Piece *piece = nil;
   1.208 +            if( i < state.length ) {
   1.209 +                int index = [state characterAtIndex: i] - '1';
   1.210 +                if( index==0 || index==1 )
   1.211 +                    piece = [self pieceForPlayer: index];
   1.212 +            }
   1.213 +            [_grid cellAtRow: y column: x].bit = piece;
   1.214 +        }
   1.215 +}
   1.216 +
   1.217 +
   1.218 +- (BOOL) applyMoveString: (NSString*)move
   1.219 +{
   1.220 +    NSLog(@"Go: applyMoveString: '%@'",move);
   1.221 +    return [self animatePlacementIn: [_grid cellWithName: move]];
   1.222 +}
   1.223 +
   1.224 +
   1.225  @end
   1.226 +
   1.227 +
   1.228 +@implementation Go9Game
   1.229 ++ (NSString*) displayName   {return @"Go (9x9)";}
   1.230 ++ (int) dimensions          {return 9;}
   1.231 +@end
   1.232 +
   1.233 +
   1.234 +@implementation Go13Game
   1.235 ++ (NSString*) displayName   {return @"Go (13x13)";}
   1.236 ++ (int) dimensions          {return 13;}
   1.237 +@end