Source/GoGame.m
author snej@snej.local
Sun Jan 11 00:02:27 2009 -0800 (2009-01-11)
changeset 26 e7a464fb6d39
parent 22 4cb50131788f
child 27 b0affce7beb1
permissions -rw-r--r--
Added ball graphics to Xcode project, which fixes the Go game. Fixed Tic-Tac-Toe. Fixed an exception in the demo app when changing games.
jens@0
     1
/*  This code is based on Apple's "GeekGameBoard" sample code, version 1.0.
jens@0
     2
    http://developer.apple.com/samplecode/GeekGameBoard/
jens@0
     3
    Copyright © 2007 Apple Inc. Copyright © 2008 Jens Alfke. All Rights Reserved.
jens@0
     4
jens@0
     5
    Redistribution and use in source and binary forms, with or without modification, are permitted
jens@0
     6
    provided that the following conditions are met:
jens@0
     7
jens@0
     8
    * Redistributions of source code must retain the above copyright notice, this list of conditions
jens@0
     9
      and the following disclaimer.
jens@0
    10
    * Redistributions in binary form must reproduce the above copyright notice, this list of
jens@0
    11
      conditions and the following disclaimer in the documentation and/or other materials provided
jens@0
    12
      with the distribution.
jens@0
    13
jens@0
    14
    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
jens@0
    15
    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
jens@0
    16
    FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
jens@0
    17
    BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
jens@0
    18
    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
jens@0
    19
    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
jens@0
    20
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
jens@0
    21
    THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
jens@0
    22
*/
jens@0
    23
#import "GoGame.h"
jens@0
    24
#import "Grid.h"
jens@0
    25
#import "Piece.h"
jens@0
    26
#import "Dispenser.h"
jens@0
    27
#import "Stack.h"
jens@0
    28
#import "QuartzUtils.h"
jens@1
    29
#import "GGBUtils.h"
jens@0
    30
jens@0
    31
jens@0
    32
@implementation GoGame
jens@0
    33
jens@0
    34
jens@10
    35
+ (int) dimensions {return 19;}
jens@10
    36
jens@24
    37
+ (const GridCoord*) spotCoords
jens@24
    38
{
jens@24
    39
    static GridCoord const sSpots[10]={ { 3,3}, { 3,9}, { 3,15},
jens@24
    40
                                        { 9,3}, { 9,9}, { 9,15},
jens@24
    41
                                        {15,3}, {15,9}, {15,15}, {NSNotFound,NSNotFound} };
jens@24
    42
    return sSpots;
jens@24
    43
}
jens@24
    44
jens@10
    45
- (id) init
jens@0
    46
{
jens@10
    47
    self = [super init];
jens@0
    48
    if (self != nil) {
jens@0
    49
        [self setNumberOfPlayers: 2];
jens@21
    50
        [(Player*)[_players objectAtIndex: 0] setName: @"Black"];
jens@3
    51
        [(Player*)[_players objectAtIndex: 1] setName: @"White"];
jens@10
    52
    }
jens@0
    53
    return self;
jens@0
    54
}
jens@10
    55
        
jens@10
    56
- (void) setUpBoard
jens@10
    57
{
jens@10
    58
    int dimensions = [[self class] dimensions];
jens@21
    59
    CGRect tableBounds = _table.bounds;
jens@21
    60
    CGSize size = tableBounds.size;
jens@21
    61
    CGFloat boardSide = MIN(size.width* dimensions/(CGFloat)(dimensions+2),size.height);
jens@16
    62
    RectGrid *board = [[RectGrid alloc] initWithRows: dimensions columns: dimensions 
jens@10
    63
                                              frame: CGRectMake(floor((size.width-boardSide)/2),
jens@10
    64
                                                                floor((size.height-boardSide)/2),
jens@10
    65
                                                                boardSide,boardSide)];
jens@16
    66
    _board = board;
jens@10
    67
    /*
jens@10
    68
    grid.backgroundColor = GetCGPatternNamed(@"Wood.jpg");
jens@10
    69
    grid.borderColor = kTranslucentLightGrayColor;
jens@10
    70
    grid.borderWidth = 2;
jens@10
    71
    */
jens@16
    72
    board.lineColor = kTranslucentGrayColor;
jens@16
    73
    board.cellClass = [GoSquare class];
jens@24
    74
    board.usesDiagonals = board.allowsMoves = board.allowsCaptures = NO;
jens@16
    75
    [board addAllCells];
jens@24
    76
    const GridCoord *spots = [[self class] spotCoords];
jens@24
    77
    for( int i=0; spots[i].row!=NSNotFound; i++ )
jens@24
    78
        ((GoSquare*)[board cellAtRow: spots[i].row column: spots[i].col]).dotted = YES;
jens@16
    79
    [_table addSublayer: board];
jens@16
    80
    [board release];
jens@10
    81
    
jens@16
    82
    CGRect gridFrame = board.frame;
jens@16
    83
    CGFloat pieceSize = (int)board.spacing.width & ~1;  // make sure it's even
jens@21
    84
    CGFloat captureMinY = CGRectGetMinY(tableBounds) + pieceSize/2,
jens@21
    85
            captureHeight = size.height - pieceSize;
jens@21
    86
    _captured[0] = [[Stack alloc] initWithStartPos: CGPointMake(pieceSize/2,0)
jens@10
    87
                                           spacing: CGSizeMake(0,pieceSize)
jens@10
    88
                                      wrapInterval: floor(captureHeight/pieceSize)
jens@21
    89
                                       wrapSpacing: CGSizeMake(pieceSize,0)];
jens@21
    90
    _captured[0].frame = CGRectMake(CGRectGetMinX(tableBounds), 
jens@21
    91
                                    captureMinY,
jens@21
    92
                                    CGRectGetMinX(gridFrame)-CGRectGetMinX(tableBounds),
jens@21
    93
                                    captureHeight);
jens@10
    94
    _captured[0].zPosition = kPieceZ+1;
jens@16
    95
    [_table addSublayer: _captured[0]];
jens@10
    96
    [_captured[0] release];
jens@10
    97
    
jens@21
    98
    _captured[1] = [[Stack alloc] initWithStartPos: CGPointMake(pieceSize/2,captureHeight)
jens@10
    99
                                           spacing: CGSizeMake(0,-pieceSize)
jens@10
   100
                                      wrapInterval: floor(captureHeight/pieceSize)
jens@21
   101
                                       wrapSpacing: CGSizeMake(-pieceSize,0)];
jens@21
   102
    _captured[1].frame = CGRectMake(CGRectGetMaxX(gridFrame), 
jens@21
   103
                                    captureMinY,
jens@21
   104
                                    CGRectGetMaxX(tableBounds)-CGRectGetMaxX(gridFrame),
jens@21
   105
                                    captureHeight);
jens@21
   106
    _captured[1].startPos = CGPointMake(CGRectGetMaxX(_captured[1].bounds)-pieceSize/2, captureHeight);
jens@10
   107
    _captured[1].zPosition = kPieceZ+1;
jens@16
   108
    [_table addSublayer: _captured[1]];
jens@10
   109
    [_captured[1] release];
jens@0
   110
jens@10
   111
    PreloadSound(@"Pop");
jens@10
   112
}
jens@10
   113
jens@10
   114
- (CGImageRef) iconForPlayer: (int)playerNum
jens@10
   115
{
jens@22
   116
    return GetCGImageNamed( playerNum ?@"ball-white.png" :@"ball-black.png" );
jens@10
   117
}
jens@10
   118
jens@10
   119
- (Piece*) pieceForPlayer: (int)index
jens@10
   120
{
jens@22
   121
    NSString *imageName = index ?@"ball-white.png" :@"ball-black.png";
jens@22
   122
    CGFloat pieceSize = (int)(_board.spacing.width * 1.0) & ~1;  // make sure it's even
jens@10
   123
    Piece *stone = [[Piece alloc] initWithImageNamed: imageName scale: pieceSize];
jens@10
   124
    stone.owner = [self.players objectAtIndex: index];
jens@10
   125
    return [stone autorelease];
jens@10
   126
}
jens@0
   127
jens@3
   128
- (Bit*) bitToPlaceInHolder: (id<BitHolder>)holder
jens@3
   129
{
jens@3
   130
    if( holder.bit != nil || ! [holder isKindOfClass: [GoSquare class]] )
jens@3
   131
        return nil;
jens@10
   132
    else
jens@10
   133
        return [self pieceForPlayer: self.currentPlayer.index];
jens@3
   134
}
jens@3
   135
jens@3
   136
jens@7
   137
- (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)srcHolder
jens@7
   138
{
jens@7
   139
    return (srcHolder==nil);
jens@7
   140
}
jens@7
   141
jens@7
   142
jens@0
   143
- (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)srcHolder to: (id<BitHolder>)dstHolder
jens@0
   144
{
jens@7
   145
    if( srcHolder!=nil || ! [dstHolder isKindOfClass: [Square class]] )
jens@6
   146
        return NO;
jens@0
   147
    Square *dst=(Square*)dstHolder;
jens@0
   148
    
jens@0
   149
    // There should be a check here for a "ko" (repeated position) ... exercise for the reader!
jens@0
   150
    
jens@0
   151
    // Check for suicidal move. First an easy check for an empty adjacent space:
jens@0
   152
    NSArray *neighbors = dst.neighbors;
jens@0
   153
    for( GridCell *c in neighbors )
jens@0
   154
        if( c.empty )
jens@0
   155
            return YES;                     // there's an empty space
jens@0
   156
    // If the piece is surrounded, check the neighboring groups' liberties:
jens@0
   157
    for( GridCell *c in neighbors ) {
jens@0
   158
        int nLiberties;
jens@0
   159
        [c getGroup: &nLiberties];
jens@0
   160
        if( c.bit.unfriendly ) {
jens@0
   161
            if( nLiberties <= 1 )
jens@0
   162
                return YES;             // the move captures, so it's not suicidal
jens@0
   163
        } else {
jens@0
   164
            if( nLiberties > 1 )
jens@0
   165
                return YES;             // the stone joins a group with other liberties
jens@0
   166
        }
jens@0
   167
    }
jens@0
   168
    return NO;
jens@0
   169
}
jens@0
   170
jens@0
   171
jens@0
   172
- (void) bit: (Bit*)bit movedFrom: (id<BitHolder>)srcHolder to: (id<BitHolder>)dstHolder
jens@0
   173
{
jens@0
   174
    Square *dst=(Square*)dstHolder;
jens@10
   175
    int curIndex = self.currentPlayer.index;
jens@0
   176
    // Check for captured enemy groups:
jens@0
   177
    BOOL captured = NO;
jens@0
   178
    for( GridCell *c in dst.neighbors )
jens@0
   179
        if( c.bit.unfriendly ) {
jens@0
   180
            int nLiberties;
jens@0
   181
            NSSet *group = [c getGroup: &nLiberties];
jens@0
   182
            if( nLiberties == 0 ) {
jens@0
   183
                captured = YES;
jens@0
   184
                for( GridCell *capture in group )
jens@0
   185
                    [_captured[curIndex] addBit: capture.bit];  // Moves piece to POW camp!
jens@0
   186
            }
jens@0
   187
        }
jens@0
   188
    if( captured )
jens@1
   189
        PlaySound(@"Pop");
jens@10
   190
    
jens@10
   191
    [self.currentTurn addToMove: dst.name];
jens@10
   192
    [self endTurn];
jens@0
   193
}
jens@0
   194
jens@0
   195
jens@0
   196
// This sample code makes no attempt to detect the end of the game, or count score,
jens@0
   197
// both of which are rather complex to decide in Go.
jens@0
   198
jens@0
   199
jens@10
   200
#pragma mark -
jens@10
   201
#pragma mark STATE:
jens@10
   202
jens@10
   203
jens@10
   204
- (NSString*) stateString
jens@10
   205
{
jens@16
   206
    int n = _board.rows;
jens@10
   207
    unichar state[n*n];
jens@10
   208
    for( int y=0; y<n; y++ )
jens@10
   209
        for( int x=0; x<n; x++ ) {
jens@16
   210
            Bit *bit = [_board cellAtRow: y column: x].bit;
jens@10
   211
            unichar ch;
jens@10
   212
            if( bit==nil )
jens@10
   213
                ch = '-';
jens@10
   214
            else
jens@10
   215
                ch = '1' + bit.owner.index;
jens@10
   216
            state[y*n+x] = ch;
jens@10
   217
        }
jens@21
   218
    NSMutableString *stateString = [NSMutableString stringWithCharacters: state length: n*n];
jens@21
   219
    
jens@21
   220
    NSUInteger cap0=_captured[0].numberOfBits, cap1=_captured[1].numberOfBits;
jens@21
   221
    if( cap0 || cap1 )
jens@21
   222
        [stateString appendFormat: @",%i,%i", cap0,cap1];
jens@21
   223
    return stateString;
jens@10
   224
}
jens@10
   225
jens@10
   226
- (void) setStateString: (NSString*)state
jens@10
   227
{
jens@21
   228
    //NSLog(@"Go: setStateString: '%@'",state);
jens@21
   229
    NSArray *components = [state componentsSeparatedByString: @","];
jens@21
   230
    state = [components objectAtIndex: 0];
jens@16
   231
    int n = _board.rows;
jens@10
   232
    for( int y=0; y<n; y++ )
jens@10
   233
        for( int x=0; x<n; x++ ) {
jens@10
   234
            int i = y*n+x;
jens@10
   235
            Piece *piece = nil;
jens@10
   236
            if( i < state.length ) {
jens@10
   237
                int index = [state characterAtIndex: i] - '1';
jens@10
   238
                if( index==0 || index==1 )
jens@10
   239
                    piece = [self pieceForPlayer: index];
jens@10
   240
            }
jens@16
   241
            [_board cellAtRow: y column: x].bit = piece;
jens@10
   242
        }
jens@21
   243
    
jens@21
   244
    if( components.count < 3 )
jens@21
   245
        components = nil;
jens@21
   246
    for( int player=0; player<=1; player++ ) {
jens@21
   247
        NSUInteger nCaptured = [[components objectAtIndex: 1+player] intValue];
jens@21
   248
        NSUInteger curNCaptured = _captured[player].numberOfBits;
jens@21
   249
        if( nCaptured < curNCaptured )
jens@21
   250
           _captured[player].numberOfBits = nCaptured;
jens@21
   251
        else
jens@21
   252
            for( int i=curNCaptured; i<nCaptured; i++ )
jens@21
   253
                [_captured[player] addBit: [self pieceForPlayer: 1-player]];
jens@21
   254
    }
jens@10
   255
}
jens@10
   256
jens@10
   257
jens@10
   258
- (BOOL) applyMoveString: (NSString*)move
jens@10
   259
{
jens@21
   260
    //NSLog(@"Go: applyMoveString: '%@'",move);
jens@16
   261
    return [self animatePlacementIn: [_board cellWithName: move]];
jens@10
   262
}
jens@10
   263
jens@10
   264
jens@0
   265
@end
jens@10
   266
jens@10
   267
jens@10
   268
@implementation Go9Game
jens@10
   269
+ (NSString*) displayName   {return @"Go (9x9)";}
jens@10
   270
+ (int) dimensions          {return 9;}
jens@24
   271
+ (const GridCoord*) spotCoords
jens@24
   272
{
jens@24
   273
    static GridCoord const sSpots[6]= { {2,2}, {2,6}, {4,4}, {6,2}, {6,6}, {NSNotFound,NSNotFound} };
jens@24
   274
    return sSpots;
jens@24
   275
}
jens@10
   276
@end
jens@10
   277
jens@10
   278
jens@10
   279
@implementation Go13Game
jens@10
   280
+ (NSString*) displayName   {return @"Go (13x13)";}
jens@10
   281
+ (int) dimensions          {return 13;}
jens@24
   282
+ (const GridCoord*) spotCoords
jens@24
   283
{
jens@24
   284
    static GridCoord const sSpots[6] = { { 2,2}, { 2,10}, {6,6},
jens@24
   285
                                         {10,2}, {10,10}, {NSNotFound,NSNotFound} };
jens@24
   286
    return sSpots;
jens@24
   287
}
jens@10
   288
@end