Source/Game.h
author Jens Alfke <jens@mooseyard.com>
Fri Mar 07 11:43:02 2008 -0800 (2008-03-07)
changeset 0 e9f7ba4718e1
child 1 3eb7be1dd7b6
permissions -rw-r--r--
Initial check-in into Mercurial. Branched from 1.0 release of Apple's sample code. No longer requires garbage collection. Fixed some memory leaks of CG objects. Fixed a bug when advancing to the 8th row in the Checkers game.
     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 <Cocoa/Cocoa.h>
    24 @class Bit, Player;
    25 @protocol BitHolder;
    26 
    27 
    28 /** Abstract superclass. Keeps track of the rules and turns of a game. */
    29 @interface Game : NSObject
    30 {
    31     CALayer *_board;
    32     NSArray *_players;
    33     Player *_currentPlayer, *_winner;
    34 }
    35 
    36 /** Returns the human-readable name of this game.
    37     (By default it just returns the class name with the "Game" suffix removed.) */
    38 + (NSString*) displayName;
    39 
    40 @property (readonly, copy) NSArray *players;
    41 @property (readonly) Player *currentPlayer, *winner;
    42 
    43 
    44 // Methods for subclasses to implement:
    45 
    46 /** Designated initializer. After calling the superclass implementation,
    47     it should add the necessary Grids, Pieces, Cards, Decks etc. to the board. */
    48 - (id) initWithBoard: (CALayer*)board;
    49 
    50 /** Should return YES if it is legal for the given bit to be moved from its current holder.
    51     Default implementation always returns YES. */
    52 - (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)src;
    53 
    54 /** Should return YES if it is legal for the given Bit to move from src to dst.
    55     Default implementation always returns YES. */
    56 - (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)src to: (id<BitHolder>)dst;
    57 
    58 /** Should handle any side effects of a Bit's movement, such as captures or scoring.
    59     Does not need to do the actual movement! That's already happened.
    60     It should end by calling -endTurn, if the player's turn is over.
    61     Default implementation just calls -endTurn. */
    62 - (void) bit: (Bit*)bit movedFrom: (id<BitHolder>)src to: (id<BitHolder>)dst;
    63 
    64 /** Called instead of the above if a Bit is simply clicked, not dragged.
    65     Should return NO if the click is illegal (i.e. clicking an empty draw pile in a card game.)
    66     Default implementation always returns YES. */
    67 - (BOOL) clickedBit: (Bit*)bit;
    68 
    69 /** Should return the winning player, if the current position is a win, else nil.
    70     Default implementation returns nil. */
    71 - (Player*) checkForWinner;
    72 
    73 
    74 // Protected methods for subclasses to call:
    75 
    76 /** Sets the number of players in the game. Subclass initializers should call this. */
    77 - (void) setNumberOfPlayers: (unsigned)n;
    78 
    79 /** Advance to the next player, when a turn is over. */
    80 - (void) nextPlayer;
    81 
    82 /** Checks for a winner and advances to the next player. */
    83 - (void) endTurn;
    84 
    85 @end
    86 
    87 
    88 
    89 /** A mostly-passive object used to represent a player. */
    90 @interface Player : NSObject
    91 {
    92     Game *_game;
    93     NSString *_name;
    94 }
    95 
    96 - (id) initWithGame: (Game*)game;
    97 
    98 @property (readonly) Game *game;
    99 @property (copy) NSString *name;
   100 @property (readonly) int index;
   101 @property (readonly, getter=isCurrent) BOOL current;
   102 @property (readonly, getter=isFriendly) BOOL friendly;
   103 @property (readonly, getter=isUnfriendly) BOOL unfriendly;
   104 @property (readonly) Player *nextPlayer, *previousPlayer;
   105 
   106 @end
   107 
   108 
   109 
   110 @interface CALayer (Game)
   111 
   112 /** Called on any CALayer in the game's layer tree, will return the current Game object. */
   113 @property (readonly) Game *game;
   114 
   115 @end