Source/CheckersGame.m
author Jens Alfke <jens@mooseyard.com>
Mon Mar 10 17:30:57 2008 -0700 (2008-03-10)
changeset 1 3eb7be1dd7b6
parent 0 e9f7ba4718e1
child 7 428a194e3e59
permissions -rw-r--r--
Tic-tac-toe works on the iPhone simulator!
     1 /*  This code is based on Apple's "GeekGameBoard" sample code, version 1.0.
     2     http://developer.apple.com/samplecode/GeekGameBoard/
     3     Copyright © 2007 Apple Inc. Copyright © 2008 Jens Alfke. All Rights Reserved.
     4 
     5     Redistribution and use in source and binary forms, with or without modification, are permitted
     6     provided that the following conditions are met:
     7 
     8     * Redistributions of source code must retain the above copyright notice, this list of conditions
     9       and the following disclaimer.
    10     * Redistributions in binary form must reproduce the above copyright notice, this list of
    11       conditions and the following disclaimer in the documentation and/or other materials provided
    12       with the distribution.
    13 
    14     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
    15     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
    16     FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
    17     BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    18     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
    19     PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
    20     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
    21     THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    22 */
    23 #import "CheckersGame.h"
    24 #import "Grid.h"
    25 #import "Piece.h"
    26 #import "QuartzUtils.h"
    27 #import "GGBUtils.h"
    28 
    29 
    30 @implementation CheckersGame
    31 
    32 
    33 - (void) addPieces: (NSString*)imageName
    34             toGrid: (Grid*)grid
    35          forPlayer: (int)playerNum
    36               rows: (NSRange)rows
    37        alternating: (BOOL)alternating
    38 {
    39     Piece *prototype = [[Piece alloc] initWithImageNamed: imageName scale: floor(grid.spacing.width * 0.8)];
    40     prototype.owner = [self.players objectAtIndex: playerNum];
    41     unsigned cols=grid.columns;
    42     for( unsigned row=rows.location; row<NSMaxRange(rows); row++ )
    43         for( unsigned col=0; col<cols; col++ ) {
    44             if( !alternating || ((row+col) & 1) == 0 ) {
    45                 GridCell *cell = [grid cellAtRow: row column: col];
    46                 if( cell ) {
    47                     Piece *piece = [prototype copy];
    48                     cell.bit = piece;
    49                     [piece release];
    50                     //cell.bit.rotation = random() % 360; // keeps pieces from looking too samey
    51                     _numPieces[playerNum]++;
    52                 }
    53             }
    54         }
    55     [prototype release];
    56 }
    57 
    58 
    59 - (Grid*) x_makeGrid
    60 {
    61     RectGrid *grid = [[[RectGrid alloc] initWithRows: 8 columns: 8 frame: _board.bounds] autorelease];
    62     CGPoint pos = grid.position;
    63     pos.x = floor((_board.bounds.size.width-grid.frame.size.width)/2);
    64     [grid addAllCells];
    65     grid.position = pos;
    66     grid.allowsMoves = YES;
    67     grid.allowsCaptures = NO;
    68     grid.cellColor = CreateGray(0.0, 0.25);
    69     grid.altCellColor = CreateGray(1.0, 0.25);
    70     grid.lineColor = nil;
    71     [self addPieces: @"Green Ball.png" toGrid: grid forPlayer: 0 rows: NSMakeRange(0,3) alternating: YES];
    72     [self addPieces: @"Red Ball.png"   toGrid: grid forPlayer: 1 rows: NSMakeRange(5,3) alternating: YES];
    73     return grid;
    74 }
    75 
    76 
    77 - (id) initWithBoard: (GGBLayer*)board
    78 {
    79     self = [super initWithBoard: board];
    80     if (self != nil) {
    81         [self setNumberOfPlayers: 2];
    82         [board addSublayer: [self x_makeGrid]];
    83         [self nextPlayer];
    84     }
    85     return self;
    86 }
    87 
    88 
    89 - (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)srcHolder to: (id<BitHolder>)dstHolder
    90 {
    91     Square *src=(Square*)srcHolder, *dst=(Square*)dstHolder;
    92     if( [bit valueForKey: @"King"] )
    93         if( dst==src.bl || dst==src.br || dst==src.l || dst==src.r
    94            || (src.bl.bit.unfriendly && dst==src.bl.bl) || (src.br.bit.unfriendly && dst==src.br.br) )
    95             return YES;    
    96     return dst==src.fl || dst==src.fr
    97         || (src.fl.bit.unfriendly && dst==src.fl.fl) || (src.fr.bit.unfriendly && dst==src.fr.fr);
    98 }
    99 
   100 - (void) bit: (Bit*)bit movedFrom: (id<BitHolder>)srcHolder to: (id<BitHolder>)dstHolder
   101 {
   102     Square *src=(Square*)srcHolder, *dst=(Square*)dstHolder;
   103     int playerIndex = self.currentPlayer.index;
   104     BOOL isKing = ([bit valueForKey: @"King"] != nil);
   105     
   106     PlaySound(isKing ?@"Funk" :@"Tink");
   107 
   108     // "King" a piece that made it to the last row:
   109     if( dst.row == (playerIndex ?0 :7) )
   110         if( ! isKing ) {
   111             PlaySound(@"Blow");
   112             bit.scale = 1.4;
   113             [bit setValue: @"King" forKey: @"King"];
   114             // don't set isKing flag - piece can't jump again after being kinged.
   115         }
   116 
   117     // Check for a capture:
   118     Square *capture = nil;
   119     if(dst==src.fl.fl)
   120         capture = src.fl;
   121     else if(dst==src.fr.fr)
   122         capture = src.fr;
   123     else if(dst==src.bl.bl)
   124         capture = src.bl;
   125     else if(dst==src.br.br)
   126         capture = src.br;
   127     
   128     if( capture ) {
   129         PlaySound(@"Pop");
   130         Bit *bit = capture.bit;
   131         _numPieces[bit.owner.index]--;
   132         [bit destroy];
   133         
   134         // Now check if another capture is possible. If so, don't end the turn:
   135         if( (dst.fl.bit.unfriendly && dst.fl.fl.empty) || (dst.fr.bit.unfriendly && dst.fr.fr.empty) )
   136             return;
   137         if( isKing )
   138             if( (dst.bl.bit.unfriendly && dst.bl.bl.empty) || (dst.br.bit.unfriendly && dst.br.br.empty) )
   139                 return;
   140     }
   141     
   142     [self endTurn];
   143 }
   144 
   145 - (Player*) checkForWinner
   146 {
   147     // Whoever runs out of pieces loses:
   148     if( _numPieces[0]==0 )
   149         return [self.players objectAtIndex: 1];
   150     else if( _numPieces[1]==0 )
   151         return [self.players objectAtIndex: 0];
   152     else
   153         return nil;
   154 }
   155 
   156 
   157 @end