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