Source/Game.h
author Jens Alfke <jens@mooseyard.com>
Sun Mar 16 15:06:47 2008 -0700 (2008-03-16)
changeset 7 428a194e3e59
parent 4 d781b00f3ed4
child 8 45c82a071aca
permissions -rw-r--r--
Game class now tracks board state and moves, as strings, and can step through its history.
Fixed another bug in Go (you could drag your captured stones back to the board!)
     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     GGBLayer *_board;
    31     NSArray *_players;
    32     Player *_currentPlayer, *_winner;
    33     NSMutableString *_currentMove;
    34     NSMutableArray *_states, *_moves;
    35     unsigned _currentTurn;
    36 }
    37 
    38 /** Returns the human-readable name of this game.
    39     (By default it just returns the class name with the "Game" suffix removed.) */
    40 + (NSString*) displayName;
    41 
    42 + (BOOL) landscapeOriented;
    43 
    44 @property (readonly, copy) NSArray *players;
    45 @property (readonly) Player *currentPlayer, *winner;
    46 
    47 @property (readonly) NSArray *states, *moves;
    48 @property (readonly) unsigned maxTurn;
    49 @property unsigned currentTurn;
    50 @property (readonly) BOOL isLatestTurn;
    51 
    52 - (BOOL) animateMoveFrom: (BitHolder*)src to: (BitHolder*)dst;
    53 
    54 
    55 // Methods for subclasses to implement:
    56 
    57 /** Designated initializer. After calling the superclass implementation,
    58     it should add the necessary Grids, Pieces, Cards, Decks etc. to the board. */
    59 - (id) initWithBoard: (GGBLayer*)board;
    60 
    61 
    62 /** Should return YES if it is legal for the given bit to be moved from its current holder.
    63     Default implementation always returns YES. */
    64 - (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)src;
    65 
    66 /** Should return YES if it is legal for the given Bit to move from src to dst.
    67     Default implementation always returns YES. */
    68 - (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)src to: (id<BitHolder>)dst;
    69 
    70 
    71 /** Should handle any side effects of a Bit's movement, such as captures or scoring.
    72     Does not need to do the actual movement! That's already happened.
    73     It should end by calling -endTurn, if the player's turn is over.
    74     Default implementation just calls -endTurn. */
    75 - (void) bit: (Bit*)bit movedFrom: (id<BitHolder>)src to: (id<BitHolder>)dst;
    76 
    77 /** Called on mouse-down/touch of an *empty* BitHolder. Should return a Bit if
    78     it's OK to place a new Bit there; else nil. */
    79 - (Bit*) bitToPlaceInHolder: (id<BitHolder>)holder;
    80 
    81 /** Called instead of the above if a Bit is simply clicked, not dragged.
    82     Should return NO if the click is illegal (i.e. clicking an empty draw pile in a card game.)
    83     Default implementation always returns YES. */
    84 - (BOOL) clickedBit: (Bit*)bit;
    85 
    86 /** Should return the winning player, if the current position is a win, else nil.
    87     Default implementation returns nil. */
    88 - (Player*) checkForWinner;
    89 
    90 
    91 @property (copy) NSString* stateString;
    92 - (BOOL) applyMoveString: (NSString*)move;
    93 
    94 
    95 // Protected methods for subclasses to call:
    96 
    97 /** Sets the number of players in the game. Subclass initializers should call this. */
    98 - (void) setNumberOfPlayers: (unsigned)n;
    99 
   100 /** The current move in progress. Append text to it as the user makes moves. */
   101 @property (readonly) NSMutableString* currentMove;
   102 
   103 /** Advance to the next player, when a turn is over. */
   104 - (void) nextPlayer;
   105 
   106 /** Checks for a winner and advances to the next player. */
   107 - (void) endTurn;
   108 
   109 @end
   110 
   111 
   112 
   113 /** A mostly-passive object used to represent a player. */
   114 @interface Player : NSObject
   115 {
   116     Game *_game;
   117     NSString *_name;
   118 }
   119 
   120 - (id) initWithGame: (Game*)game;
   121 
   122 @property (readonly) Game *game;
   123 @property (copy) NSString *name;
   124 @property (readonly) int index;
   125 @property (readonly, getter=isCurrent) BOOL current;
   126 @property (readonly, getter=isFriendly) BOOL friendly;
   127 @property (readonly, getter=isUnfriendly) BOOL unfriendly;
   128 @property (readonly) Player *nextPlayer, *previousPlayer;
   129 
   130 @end
   131 
   132 
   133 
   134 @interface CALayer (Game)
   135 
   136 /** Called on any CALayer in the game's layer tree, will return the current Game object. */
   137 @property (readonly) Game *game;
   138 
   139 @end