Source/CheckersGame.m
author Jens Alfke <jens@mooseyard.com>
Thu Jul 03 17:44:30 2008 -0700 (2008-07-03)
changeset 10 6c78cc6bd7a6
parent 9 a59acc683080
child 11 436cbdf56810
permissions -rw-r--r--
Lots of reworking. Completed support for game history, including Turn class. Changed Game API around quite a bit.
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 "CheckersGame.h"
jens@0
    24
#import "Grid.h"
jens@0
    25
#import "Piece.h"
jens@0
    26
#import "QuartzUtils.h"
jens@1
    27
#import "GGBUtils.h"
jens@0
    28
jens@0
    29
jens@0
    30
@implementation CheckersGame
jens@0
    31
jens@0
    32
jens@10
    33
- (id) init
jens@10
    34
{
jens@10
    35
    self = [super init];
jens@10
    36
    if (self != nil) {
jens@10
    37
        _cells = [[NSMutableArray alloc] init];
jens@10
    38
        [self setNumberOfPlayers: 2];
jens@10
    39
        
jens@10
    40
        PreloadSound(@"Tink");
jens@10
    41
        PreloadSound(@"Funk");
jens@10
    42
        PreloadSound(@"Blow");
jens@10
    43
        PreloadSound(@"Pop");
jens@10
    44
    }
jens@10
    45
    return self;
jens@10
    46
}
jens@10
    47
jens@10
    48
- (CGImageRef) iconForPlayer: (int)playerNum
jens@10
    49
{
jens@10
    50
    return GetCGImageNamed( playerNum==0 ?@"Green.png" :@"Red.png" );
jens@10
    51
}
jens@10
    52
jens@7
    53
- (Piece*) pieceForPlayer: (int)playerNum
jens@0
    54
{
jens@9
    55
    Piece *p = [[Piece alloc] initWithImageNamed: (playerNum==0 ?@"Green.png" :@"Red.png") 
jens@9
    56
                                           scale: floor(_grid.spacing.width * 1.0)];
jens@7
    57
    p.owner = [self.players objectAtIndex: playerNum];
jens@7
    58
    p.name = playerNum ?@"2" :@"1";
jens@7
    59
    return [p autorelease];
jens@0
    60
}
jens@0
    61
jens@10
    62
- (void) makeKing: (Piece*)piece
jens@10
    63
{
jens@10
    64
    piece.scale = 1.4;
jens@10
    65
    [piece setValue: @"King" forKey: @"King"];
jens@10
    66
    piece.name = piece.owner.index ?@"4" :@"3";
jens@10
    67
}
jens@10
    68
jens@10
    69
- (void) setUpBoard
jens@0
    70
{
jens@0
    71
    RectGrid *grid = [[[RectGrid alloc] initWithRows: 8 columns: 8 frame: _board.bounds] autorelease];
jens@7
    72
    _grid = grid;
jens@10
    73
    [_board addSublayer: _grid];
jens@7
    74
    CGPoint pos = _grid.position;
jens@0
    75
    pos.x = floor((_board.bounds.size.width-grid.frame.size.width)/2);
jens@0
    76
    grid.position = pos;
jens@0
    77
    grid.allowsMoves = YES;
jens@0
    78
    grid.allowsCaptures = NO;
jens@1
    79
    grid.cellColor = CreateGray(0.0, 0.25);
jens@1
    80
    grid.altCellColor = CreateGray(1.0, 0.25);
jens@0
    81
    grid.lineColor = nil;
jens@7
    82
jens@7
    83
    [grid addAllCells];
jens@10
    84
    [_cells removeAllObjects];
jens@7
    85
    for( int i=0; i<32; i++ ) {
jens@7
    86
        int row = i/4;
jens@7
    87
        [_cells addObject: [_grid cellAtRow: row column: 2*(i%4) + (row&1)]];
jens@7
    88
    }
jens@0
    89
}
jens@0
    90
jens@7
    91
- (void) dealloc
jens@7
    92
{
jens@7
    93
    [_cells release];
jens@7
    94
    [_grid release];
jens@7
    95
    [super dealloc];
jens@7
    96
}
jens@7
    97
jens@7
    98
jens@10
    99
- (NSString*) initialStateString
jens@10
   100
{
jens@10
   101
    return @"111111111111--------222222222222";
jens@10
   102
}
jens@10
   103
jens@7
   104
- (NSString*) stateString
jens@7
   105
{
jens@7
   106
    unichar state[_cells.count];
jens@7
   107
    int i = 0;
jens@7
   108
    for( GridCell *cell in _cells ) {
jens@7
   109
        NSString *ident = cell.bit.name;
jens@7
   110
        if( ident )
jens@7
   111
            state[i++] = [ident characterAtIndex: 0];
jens@7
   112
        else
jens@7
   113
            state[i++] = '-';
jens@7
   114
    }
jens@7
   115
    return [NSString stringWithCharacters: state length: i];
jens@7
   116
}
jens@7
   117
jens@7
   118
- (void) setStateString: (NSString*)state
jens@7
   119
{
jens@7
   120
    _numPieces[0] = _numPieces[1] = 0;
jens@7
   121
    int i = 0;
jens@7
   122
    for( GridCell *cell in _cells ) {
jens@7
   123
        Piece *piece;
jens@10
   124
        int which = [state characterAtIndex: i++] - '1';
jens@10
   125
        if( which >=0 && which < 4 ) {
jens@10
   126
            int player = (which & 1);
jens@10
   127
            piece = [self pieceForPlayer: player];
jens@10
   128
            _numPieces[player]++;
jens@10
   129
            if( which & 2 ) 
jens@10
   130
                [self makeKing: piece];
jens@10
   131
        } else
jens@10
   132
            piece = nil;
jens@7
   133
        cell.bit = piece;
jens@7
   134
    }    
jens@7
   135
}
jens@7
   136
jens@0
   137
jens@0
   138
- (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)srcHolder to: (id<BitHolder>)dstHolder
jens@0
   139
{
jens@0
   140
    Square *src=(Square*)srcHolder, *dst=(Square*)dstHolder;
jens@0
   141
    if( [bit valueForKey: @"King"] )
jens@0
   142
        if( dst==src.bl || dst==src.br || dst==src.l || dst==src.r
jens@0
   143
           || (src.bl.bit.unfriendly && dst==src.bl.bl) || (src.br.bit.unfriendly && dst==src.br.br) )
jens@0
   144
            return YES;    
jens@0
   145
    return dst==src.fl || dst==src.fr
jens@0
   146
        || (src.fl.bit.unfriendly && dst==src.fl.fl) || (src.fr.bit.unfriendly && dst==src.fr.fr);
jens@0
   147
}
jens@0
   148
jens@0
   149
- (void) bit: (Bit*)bit movedFrom: (id<BitHolder>)srcHolder to: (id<BitHolder>)dstHolder
jens@0
   150
{
jens@0
   151
    Square *src=(Square*)srcHolder, *dst=(Square*)dstHolder;
jens@0
   152
    int playerIndex = self.currentPlayer.index;
jens@7
   153
    
jens@10
   154
    Turn *turn = self.currentTurn;
jens@10
   155
    if( turn.move.length==0 )
jens@10
   156
        [turn addToMove: src.name];
jens@10
   157
    [turn addToMove: @"-"];
jens@10
   158
    [turn addToMove: dst.name];
jens@7
   159
    
jens@0
   160
    BOOL isKing = ([bit valueForKey: @"King"] != nil);
jens@1
   161
    PlaySound(isKing ?@"Funk" :@"Tink");
jens@0
   162
jens@0
   163
    // "King" a piece that made it to the last row:
jens@0
   164
    if( dst.row == (playerIndex ?0 :7) )
jens@0
   165
        if( ! isKing ) {
jens@1
   166
            PlaySound(@"Blow");
jens@10
   167
            [self makeKing: (Piece*)bit];
jens@10
   168
            [turn addToMove: @"*"];
jens@0
   169
            // don't set isKing flag - piece can't jump again after being kinged.
jens@0
   170
        }
jens@0
   171
jens@0
   172
    // Check for a capture:
jens@0
   173
    Square *capture = nil;
jens@0
   174
    if(dst==src.fl.fl)
jens@0
   175
        capture = src.fl;
jens@0
   176
    else if(dst==src.fr.fr)
jens@0
   177
        capture = src.fr;
jens@0
   178
    else if(dst==src.bl.bl)
jens@0
   179
        capture = src.bl;
jens@0
   180
    else if(dst==src.br.br)
jens@0
   181
        capture = src.br;
jens@0
   182
    
jens@0
   183
    if( capture ) {
jens@1
   184
        PlaySound(@"Pop");
jens@10
   185
        _numPieces[capture.bit.owner.index]--;
jens@10
   186
        [capture destroyBit];
jens@10
   187
        [turn addToMove: @"!"];
jens@0
   188
        
jens@0
   189
        // Now check if another capture is possible. If so, don't end the turn:
jens@0
   190
        if( (dst.fl.bit.unfriendly && dst.fl.fl.empty) || (dst.fr.bit.unfriendly && dst.fr.fr.empty) )
jens@0
   191
            return;
jens@0
   192
        if( isKing )
jens@0
   193
            if( (dst.bl.bit.unfriendly && dst.bl.bl.empty) || (dst.br.bit.unfriendly && dst.br.br.empty) )
jens@0
   194
                return;
jens@0
   195
    }
jens@0
   196
    
jens@0
   197
    [self endTurn];
jens@0
   198
}
jens@0
   199
jens@0
   200
- (Player*) checkForWinner
jens@0
   201
{
jens@0
   202
    // Whoever runs out of pieces loses:
jens@0
   203
    if( _numPieces[0]==0 )
jens@0
   204
        return [self.players objectAtIndex: 1];
jens@0
   205
    else if( _numPieces[1]==0 )
jens@0
   206
        return [self.players objectAtIndex: 0];
jens@0
   207
    else
jens@0
   208
        return nil;
jens@0
   209
}
jens@0
   210
jens@0
   211
jens@7
   212
- (BOOL) applyMoveString: (NSString*)move
jens@7
   213
{
jens@7
   214
    GridCell *src = nil;
jens@10
   215
    for( NSString *ident in [move componentsSeparatedByString: @"-"] ) {
jens@10
   216
        while( [ident hasSuffix: @"!"] || [ident hasSuffix: @"*"] )
jens@10
   217
            ident = [ident substringToIndex: ident.length-1];
jens@7
   218
        GridCell *dst = [_grid cellWithName: ident];
jens@10
   219
        if( dst == nil )
jens@10
   220
            return NO;
jens@10
   221
        if( src && ! [self animateMoveFrom: src to: dst] )
jens@10
   222
            return NO;
jens@7
   223
        src = dst;
jens@7
   224
    }
jens@7
   225
    return YES;
jens@7
   226
}
jens@7
   227
jens@7
   228
jens@0
   229
@end