Source/Game.h
author Jens Alfke <jens@mooseyard.com>
Thu May 29 15:04:06 2008 -0700 (2008-05-29)
changeset 9 a59acc683080
parent 7 428a194e3e59
child 10 6c78cc6bd7a6
permissions -rw-r--r--
Finally fixed the slow animation performance of board games; all it took was changing the board's z index from 1 to 0, somehow. Games working well now.
     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, BitHolder, 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     NSString *_uniqueID;
    31     GGBLayer *_board;
    32     NSArray *_players;
    33     Player *_currentPlayer, *_winner;
    34     NSMutableString *_currentMove;
    35     NSMutableArray *_states, *_moves;
    36     unsigned _currentTurn;
    37 }
    38 
    39 /** Returns the name used to identify this game in URLs.
    40      (By default it just returns the class name with the "Game" suffix removed.) */
    41 + (NSString*) identifier;
    42 
    43 /** Returns the human-readable name of this game.
    44     (By default it just returns the class name with the "Game" suffix removed.) */
    45 + (NSString*) displayName;
    46 
    47 + (BOOL) landscapeOriented;
    48 
    49 @property (readonly, copy) NSArray *players;
    50 @property (readonly) Player *currentPlayer, *winner;
    51 
    52 @property (readonly) NSArray *states, *moves;
    53 @property (readonly) unsigned maxTurn;
    54 @property unsigned currentTurn;
    55 @property (readonly) BOOL isLatestTurn;
    56 
    57 
    58 /** A globally-unique string assigned to this game instance, to help networked players identify it. */
    59 @property (readonly) NSString* uniqueID;
    60 
    61 - (BOOL) animateMoveFrom: (BitHolder*)src to: (BitHolder*)dst;
    62 
    63 
    64 - (id) initWithUniqueID: (NSString*)uniqueID;
    65 - (id) init;
    66 
    67 
    68 // Methods for subclasses to implement:
    69 
    70 /** Designated initializer. After calling the superclass implementation,
    71     it should add the necessary Grids, Pieces, Cards, Decks etc. to the board. */
    72 - (id) initWithBoard: (GGBLayer*)board;
    73 
    74 
    75 /** Should return YES if it is legal for the given bit to be moved from its current holder.
    76     Default implementation always returns YES. */
    77 - (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)src;
    78 
    79 /** Should return YES if it is legal for the given Bit to move from src to dst.
    80     Default implementation always returns YES. */
    81 - (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)src to: (id<BitHolder>)dst;
    82 
    83 
    84 /** Should handle any side effects of a Bit's movement, such as captures or scoring.
    85     Does not need to do the actual movement! That's already happened.
    86     It should end by calling -endTurn, if the player's turn is over.
    87     Default implementation just calls -endTurn. */
    88 - (void) bit: (Bit*)bit movedFrom: (id<BitHolder>)src to: (id<BitHolder>)dst;
    89 
    90 /** Called on mouse-down/touch of an *empty* BitHolder. Should return a Bit if
    91     it's OK to place a new Bit there; else nil. */
    92 - (Bit*) bitToPlaceInHolder: (id<BitHolder>)holder;
    93 
    94 /** Called instead of the above if a Bit is simply clicked, not dragged.
    95     Should return NO if the click is illegal (i.e. clicking an empty draw pile in a card game.)
    96     Default implementation always returns YES. */
    97 - (BOOL) clickedBit: (Bit*)bit;
    98 
    99 /** Should return the winning player, if the current position is a win, else nil.
   100     Default implementation returns nil. */
   101 - (Player*) checkForWinner;
   102 
   103 /** A string describing the current state of the game (the positions of all pieces,
   104     orderings of cards, player scores, ... */
   105 @property (copy) NSString* stateString;
   106 
   107 /** Add a move to the game based on the contents of the string. */
   108 - (BOOL) applyMoveString: (NSString*)move;
   109 
   110 
   111 // Protected methods for subclasses to call:
   112 
   113 /** Sets the number of players in the game. Subclass initializers should call this. */
   114 - (void) setNumberOfPlayers: (unsigned)n;
   115 
   116 /** The current move in progress. Append text to it as the user makes moves. */
   117 @property (readonly) NSMutableString* currentMove;
   118 
   119 /** Advance to the next player, when a turn is over. */
   120 - (void) nextPlayer;
   121 
   122 /** Checks for a winner and advances to the next player. */
   123 - (void) endTurn;
   124 
   125 @end
   126 
   127 
   128 
   129 /** A mostly-passive object used to represent a player. */
   130 @interface Player : NSObject <NSCoding>
   131 {
   132     Game *_game;
   133     NSString *_name;
   134 }
   135 
   136 - (id) initWithGame: (Game*)game;
   137 
   138 @property (readonly) Game *game;
   139 @property (copy) NSString *name;
   140 @property (readonly) int index;
   141 @property (readonly, getter=isCurrent) BOOL current;
   142 @property (readonly, getter=isFriendly) BOOL friendly;
   143 @property (readonly, getter=isUnfriendly) BOOL unfriendly;
   144 @property (readonly) Player *nextPlayer, *previousPlayer;
   145 
   146 @end
   147 
   148 
   149 
   150 @interface CALayer (Game)
   151 
   152 /** Called on any CALayer in the game's layer tree, will return the current Game object. */
   153 @property (readonly) Game *game;
   154 
   155 @end