Source/Game.h
author Jens Alfke <jens@mooseyard.com>
Mon Jul 21 17:32:21 2008 -0700 (2008-07-21)
changeset 21 2eb229411d73
parent 15 73f8c889f053
child 22 4cb50131788f
permissions -rw-r--r--
* Added API to Stack for removing bits.
* GoGame correctly saves/restores number of captured pieces.
* Improved positioning of captured-piece Stacks in GoGame.
* New Go piece icons.
* Added "Warn" function to GGBUtils.
     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 
    24 #import <Foundation/Foundation.h>
    25 @class GGBLayer, Bit, BitHolder, Player, Turn;
    26 @protocol BitHolder;
    27 
    28 
    29 /** Abstract superclass. Keeps track of the rules and turns of a game. */
    30 @interface Game : NSObject <NSCoding>
    31 {
    32     GGBLayer *_table;
    33     NSArray *_players;
    34     Player *_winner;
    35     NSMutableArray *_turns;
    36     unsigned _currentTurnNo;
    37     NSMutableDictionary *_extraValues;
    38     BOOL _requireConfirmation;
    39 }
    40 
    41 #pragma mark  Class properties:
    42 
    43 /** The name used to identify this class of game in URLs.
    44     (By default it just returns the class name with the "Game" suffix removed.) */
    45 + (NSString*) identifier;
    46 
    47 /** The human-readable name of this class of game.
    48     (By default it just returns the class name with the "Game" suffix removed.) */
    49 + (NSString*) displayName;
    50 
    51 /** Is this game's board wider than it's high? */
    52 + (BOOL) landscapeOriented;
    53 
    54 
    55 /** Designated initializer: override this if your subclass needs additional initialization. */
    56 - (id) init;
    57 
    58 /** Convenience initializer that calls -init, -setTable:, and -nextTurn. */
    59 - (id) initNewGameWithTable: (GGBLayer*)table;
    60 
    61 /** NSCoding initializer. Calls -init, but then restores saved payers, states, moves. */
    62 - (id) initWithCoder: (NSCoder*)decoder;
    63 
    64 /** NSCoding method to save Game to an archive. */
    65 - (void) encodeWithCoder: (NSCoder*)coder;
    66 
    67 
    68 @property (readonly, copy) NSArray *players;
    69 @property (readonly) Player *currentPlayer, *winner, *remotePlayer;
    70 @property (readonly, getter=isLocal) BOOL local;            // Are all players local?
    71 
    72 @property (retain) GGBLayer *table;                         // The root layer for the game.
    73 
    74 @property (readonly) NSArray *turns;
    75 @property (readonly) Turn *currentTurn, *latestTurn;
    76 @property (readonly) unsigned maxTurnNo;
    77 @property unsigned currentTurnNo;
    78 @property (readonly) BOOL isLatestTurn;
    79 
    80 /** Check this before the user begins a move action (mouse-down on a bit, etc.)
    81     It's YES if it's OK to move,  or NO if the current move is finished or it's another player's turn. */
    82 @property (readonly) BOOL okToMove;
    83 
    84 @property BOOL requireConfirmation;
    85 - (void) cancelCurrentTurn;
    86 - (void) confirmCurrentTurn;
    87 
    88 
    89 #pragma mark  Methods for subclasses to implement:
    90 
    91 /** An icon for a player (usually the same as the image of the player's pieces.) */
    92 - (CGImageRef) iconForPlayer: (int)playerIndex;
    93 
    94 
    95 /** Should return YES if it is legal for the given bit to be moved from its current holder.
    96     Default implementation always returns YES. */
    97 - (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)src;
    98 
    99 /** Should return YES if it is legal for the given Bit to move from src to dst.
   100     Default implementation always returns YES. */
   101 - (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)src to: (id<BitHolder>)dst;
   102 
   103 
   104 /** Should handle any side effects of a Bit's movement, such as captures or scoring.
   105     Does not need to do the actual movement! That's already happened.
   106     It should end by calling -endTurn, if the player's turn is over.
   107     Default implementation just calls -endTurn. */
   108 - (void) bit: (Bit*)bit movedFrom: (id<BitHolder>)src to: (id<BitHolder>)dst;
   109 
   110 /** Called on mouse-down/touch of an *empty* BitHolder. Should return a Bit if
   111     it's OK to place a new Bit there; else nil. */
   112 - (Bit*) bitToPlaceInHolder: (id<BitHolder>)holder;
   113 
   114 /** Called instead of the above if a Bit is simply clicked, not dragged.
   115     Should return NO if the click is illegal (i.e. clicking an empty draw pile in a card game.)
   116     Default implementation always returns YES. */
   117 - (BOOL) clickedBit: (Bit*)bit;
   118 
   119 @end