jens@0: /* This code is based on Apple's "GeekGameBoard" sample code, version 1.0. jens@0: http://developer.apple.com/samplecode/GeekGameBoard/ jens@0: Copyright © 2007 Apple Inc. Copyright © 2008 Jens Alfke. All Rights Reserved. jens@0: jens@0: Redistribution and use in source and binary forms, with or without modification, are permitted jens@0: provided that the following conditions are met: jens@0: jens@0: * Redistributions of source code must retain the above copyright notice, this list of conditions jens@0: and the following disclaimer. jens@0: * Redistributions in binary form must reproduce the above copyright notice, this list of jens@0: conditions and the following disclaimer in the documentation and/or other materials provided jens@0: with the distribution. jens@0: jens@0: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR jens@0: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND jens@0: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI- jens@0: BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES jens@0: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR jens@0: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN jens@0: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF jens@0: THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. jens@0: */ jens@0: #import "HexchequerGame.h" jens@0: #import "HexGrid.h" jens@0: #import "Piece.h" jens@0: #import "QuartzUtils.h" jens@1: #import "GGBUtils.h" jens@0: jens@0: jens@0: @implementation HexchequerGame jens@0: jens@0: jens@0: - (Grid*) x_makeGrid jens@0: { jens@7: HexGrid *grid = [[HexGrid alloc] initWithRows: 9 columns: 9 frame: _board.bounds]; jens@7: _grid = grid; jens@0: CGPoint pos = grid.position; jens@0: pos.x = floor((_board.bounds.size.width-grid.frame.size.width)/2); jens@0: grid.position = pos; jens@0: grid.allowsMoves = YES; jens@0: grid.allowsCaptures = NO; // no land-on captures, that is jens@1: grid.cellColor = CreateGray(1.0, 0.25); jens@0: grid.lineColor = kTranslucentLightGrayColor; jens@0: jens@7: [grid addCellsInHexagon]; jens@7: for( int y=0; y<9; y++ ) { jens@7: for( int x=0; x<9; x++ ) { jens@7: GridCell *cell = [_grid cellAtRow: y column: x]; jens@7: if( cell ) jens@7: [_cells addObject: cell]; jens@7: } jens@7: } jens@7: self.stateString = @"111111111111111111-------------------------222222222222222222"; jens@7: jens@7: [self performSelector: @selector(applyMoveString:) withObject: @"C4D4" afterDelay: 2.0]; jens@7: [self performSelector: @selector(applyMoveString:) withObject: @"G3F3" afterDelay: 5.0]; jens@7: [self performSelector: @selector(applyMoveString:) withObject: @"D4E4" afterDelay: 8.0]; jens@7: [self performSelector: @selector(applyMoveString:) withObject: @"F3D4" afterDelay: 11.0]; jens@7: jens@0: return grid; jens@0: } jens@0: jens@0: jens@0: - (BOOL) canBit: (Bit*)bit moveFrom: (id)srcHolder to: (id)dstHolder jens@0: { jens@0: Hex *src=(Hex*)srcHolder, *dst=(Hex*)dstHolder; jens@0: if( [bit valueForKey: @"King"] ) jens@0: if( dst==src.bl || dst==src.br || dst==src.l || dst==src.r jens@0: || (src.bl.bit.unfriendly && dst==src.bl.bl) || (src.br.bit.unfriendly && dst==src.br.br) jens@0: || (src.l.bit.unfriendly && dst==src.l.l) || (src.r.bit.unfriendly && dst==src.r.r) ) jens@0: return YES; jens@0: return dst==src.fl || dst==src.fr jens@0: || (src.fl.bit.unfriendly && dst==src.fl.fl) || (src.fr.bit.unfriendly && dst==src.fr.fr); jens@0: } jens@0: jens@0: - (void) bit: (Bit*)bit movedFrom: (id)srcHolder to: (id)dstHolder jens@0: { jens@0: Hex *src=(Hex*)srcHolder, *dst=(Hex*)dstHolder; jens@0: int playerIndex = self.currentPlayer.index; jens@7: jens@7: if( self.currentMove.length==0 ) jens@7: [self.currentMove appendString: src.name]; jens@7: [self.currentMove appendString: dst.name]; jens@0: jens@7: BOOL isKing = ([bit valueForKey: @"King"] != nil); jens@1: PlaySound(isKing ?@"Funk" :@"Tink"); jens@0: jens@0: // "King" a piece that made it to the last row: jens@0: if( dst.row == (playerIndex ?0 :8) ) jens@0: if( ! isKing ) { jens@1: PlaySound(@"Blow"); jens@0: bit.scale = 1.4; jens@0: [bit setValue: @"King" forKey: @"King"]; jens@0: // don't set isKing flag - piece can't capture again after being kinged. jens@0: } jens@0: jens@0: // Check for a capture: jens@0: Hex *capture = nil; jens@0: if(dst==src.fl.fl) jens@0: capture = src.fl; jens@0: else if(dst==src.fr.fr) jens@0: capture = src.fr; jens@0: else if(dst==src.bl.bl) jens@0: capture = src.bl; jens@0: else if(dst==src.br.br) jens@0: capture = src.br; jens@0: else if(dst==src.l.l) jens@0: capture = src.l; jens@0: else if(dst==src.r.r) jens@0: capture = src.r; jens@0: jens@0: if( capture ) { jens@1: PlaySound(@"Pop"); jens@0: Bit *bit = capture.bit; jens@0: _numPieces[bit.owner.index]--; jens@0: [bit destroy]; jens@0: jens@0: // Now check if another capture is possible. If so, don't end the turn: jens@0: if( (dst.fl.bit.unfriendly && dst.fl.fl.empty) || (dst.fr.bit.unfriendly && dst.fr.fr.empty) ) jens@0: return; jens@0: if( isKing ) jens@0: if( (dst.bl.bit.unfriendly && dst.bl.bl.empty) || (dst.br.bit.unfriendly && dst.br.br.empty) jens@0: || (dst.l.bit.unfriendly && dst.l.l.empty) || (dst.r.bit.unfriendly && dst.r.r.empty)) jens@0: return; jens@0: } jens@0: jens@0: [self endTurn]; jens@0: } jens@0: jens@0: jens@0: @end