1/*
2	File:		MBCBoard.h
3	Contains:	Fundamental move and board classes.
4	Copyright:	� 2002-2012 by Apple Inc., all rights reserved.
5
6	IMPORTANT: This Apple software is supplied to you by Apple Computer,
7	Inc.  ("Apple") in consideration of your agreement to the following
8	terms, and your use, installation, modification or redistribution of
9	this Apple software constitutes acceptance of these terms.  If you do
10	not agree with these terms, please do not use, install, modify or
11	redistribute this Apple software.
12
13	In consideration of your agreement to abide by the following terms,
14	and subject to these terms, Apple grants you a personal, non-exclusive
15	license, under Apple's copyrights in this original Apple software (the
16	"Apple Software"), to use, reproduce, modify and redistribute the
17	Apple Software, with or without modifications, in source and/or binary
18	forms; provided that if you redistribute the Apple Software in its
19	entirety and without modifications, you must retain this notice and
20	the following text and disclaimers in all such redistributions of the
21	Apple Software.  Neither the name, trademarks, service marks or logos
22	of Apple Inc. may be used to endorse or promote products
23	derived from the Apple Software without specific prior written
24	permission from Apple.  Except as expressly stated in this notice, no
25	other rights or licenses, express or implied, are granted by Apple
26	herein, including but not limited to any patent rights that may be
27	infringed by your derivative works or by other works in which the
28	Apple Software may be incorporated.
29
30	The Apple Software is provided by Apple on an "AS IS" basis.  APPLE
31	MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
32	THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND
33	FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS
34	USE AND OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
35
36	IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT,
37	INCIDENTAL OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
38	PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
39	PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE,
40	REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE,
41	HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING
42	NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
43	ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44*/
45
46#import <OpenGL/gl.h>
47#import <Cocoa/Cocoa.h>
48#import <stdio.h>
49
50enum MBCVariant {
51	kVarNormal,
52	kVarCrazyhouse,
53	kVarSuicide,
54	kVarLosers
55};
56
57extern NSString *   gVariantName[];
58extern const char   gVariantChar[];
59
60enum MBCPlayers {
61	kHumanVsHuman,
62	kHumanVsComputer,
63	kComputerVsHuman,
64	kComputerVsComputer,
65    kHumanVsGameCenter,
66};
67
68enum MBCSideCode {
69    kPlayWhite,
70    kPlayBlack,
71    kPlayEither
72};
73
74enum MBCUniqueCode {
75	kMatchingPieceExists 	= 1,
76	kMatchingPieceOnSameRow	= 2,
77	kMatchingPieceOnSameCol = 4,
78};
79typedef int MBCUnique;
80
81enum MBCPieceCode {
82	EMPTY = 0,
83	KING, QUEEN, BISHOP, KNIGHT, ROOK, PAWN,
84	kWhitePiece = 0,
85	kBlackPiece = 8,
86	kPromoted	= 16,
87	kPieceMoved	= 32
88};
89typedef unsigned char MBCPiece;
90
91inline MBCPiece White(MBCPieceCode code) { return kWhitePiece | code; }
92inline MBCPiece Black(MBCPieceCode code) { return kBlackPiece | code; }
93inline MBCPieceCode Piece(MBCPiece piece){ return (MBCPieceCode)(piece&7); }
94inline MBCPieceCode Color(MBCPiece piece){ return (MBCPieceCode)(piece&8); }
95inline MBCPieceCode What(MBCPiece piece) { return (MBCPieceCode)(piece&15);}
96inline MBCPiece Matching(MBCPiece piece, MBCPieceCode code)
97                                         { return (piece & 8) | code; }
98inline MBCPiece Opposite(MBCPiece piece) { return piece ^ 8; }
99inline MBCPieceCode Promoted(MBCPiece piece)
100                                         { return (MBCPieceCode)(piece & 16); }
101inline MBCPieceCode PieceMoved(MBCPiece piece)
102                                         { return (MBCPieceCode)(piece & 32); }
103
104enum MBCMoveCode {
105	kCmdNull,
106	kCmdMove, 		kCmdDrop, 		kCmdUndo,
107	kCmdWhiteWins, 	kCmdBlackWins, 	kCmdDraw,
108	kCmdPong, 		kCmdStartGame,
109	kCmdPMove,		kCmdPDrop,
110	kCmdMoveOK
111};
112
113typedef unsigned char MBCSquare;
114enum {
115	kSyntheticSquare	= 0x70,
116    kWhitePromoSquare	= 0x71,
117	kBlackPromoSquare	= 0x72,
118	kBorderRegion		= 0x73,
119	kInHandSquare  		= 0x80,
120	kInvalidSquare 		= 0xFF
121};
122
123inline unsigned 	Row(MBCSquare square)
124                       { return 1+(square>>3); 			}
125inline char 		Col(MBCSquare square)
126                       { return 'a'+(square&7);			}
127inline MBCSquare	Square(char col, unsigned row)
128                       { return ((row-1)<<3)|(col-'a'); }
129inline MBCSquare	Square(const char * colrow)
130                       { return ((colrow[1]-'1')<<3)|(colrow[0]-'a'); }
131
132enum MBCCastling {
133	kUnknownCastle, kCastleQueenside, kCastleKingside, kNoCastle
134};
135
136enum MBCSide {
137	kWhiteSide, kBlackSide, kBothSides, kNeitherSide
138};
139
140inline bool SideIncludesWhite(MBCSide side)     { return side==kWhiteSide || side==kBothSides; }
141inline bool SideIncludesBlack(MBCSide side)     { return side==kBlackSide || side==kBothSides; }
142
143extern const MBCSide gHumanSide[];
144extern const MBCSide gEngineSide[];
145
146//
147// A compact move has a very short existence and is only used in places
148// where the information absolutely has to be kept to 32 bits.
149//
150typedef unsigned MBCCompactMove;
151
152//
153// MBCMove - A move
154//
155@interface MBCMove : NSObject
156{
157@public
158    MBCMoveCode		fCommand;		// Command
159    MBCSquare		fFromSquare;	// Starting square of piece if move
160    MBCSquare		fToSquare;		// Finishing square if move or drop
161    MBCPiece		fPiece;			// Moved or dropped piece
162    MBCPiece		fPromotion;		// Pawn promotion piece
163    MBCPiece		fVictim;		// Captured piece, set by [board makeMove]
164    MBCCastling		fCastling;		// Castling move, set by [board makeMove]
165    BOOL			fEnPassant;		// En passant, set by [board makeMove]
166    BOOL           fCheck;        // Check, set by [board makeMove]
167    BOOL           fCheckMate;    // Checkmate, set asynchronously
168    BOOL 			fAnimate;		// Animate on board
169}
170
171- (id) initWithCommand:(MBCMoveCode)command;
172+ (id) newWithCommand:(MBCMoveCode)command;
173+ (id) moveWithCommand:(MBCMoveCode)command;
174- (id) initFromCompactMove:(MBCCompactMove)move;
175+ (id) newFromCompactMove:(MBCCompactMove)move;
176+ (id) moveFromCompactMove:(MBCCompactMove)move;
177- (id) initFromEngineMove:(NSString *)engineMove;
178+ (id) newFromEngineMove:(NSString *)engineMove;
179+ (id) moveFromEngineMove:(NSString *)engineMove;
180+ (BOOL) compactMoveIsWin:(MBCCompactMove)move;
181
182- (NSString *) localizedText;
183- (NSString *) engineMove;
184- (NSString *) origin;
185- (NSString *) operation;
186- (NSString *) destination;
187- (NSString *) check;
188
189@end
190
191//
192// MBCPieces - The full position representation
193//
194struct MBCPieces {
195	MBCPiece		fBoard[64];
196	char			fInHand[16];
197	MBCSquare		fEnPassant;	// Current en passant square, if any
198
199    bool            NoPieces(MBCPieceCode color);
200};
201
202//
203// MBCBoard - The game board
204//
205@interface MBCBoard : NSObject
206{
207	MBCPieces			fCurPos;
208	MBCPieces			fPrvPos;
209	int					fMoveClock;
210	MBCVariant			fVariant;
211	NSMutableArray *	fMoves;
212	MBCPiece			fPromotion[2];
213    NSMutableArray *    fObservers;
214    id                  fDocument;
215}
216
217- (void)        removeChessObservers;
218- (void) 		setDocument:(id)doc;
219- (void)		startGame:(MBCVariant)variant;
220- (MBCPiece)	curContents:(MBCSquare)square;	// Read contents of a square
221- (MBCPiece)	oldContents:(MBCSquare)square;	// Read contents of a square
222- (int)			curInHand:(MBCPiece)piece;		// # of pieces to drop
223- (int)			oldInHand:(MBCPiece)piece;		// # of pieces to drop
224- (void) 		makeMove:(MBCMove *)move; 		// Move pieces and record
225- (MBCCastling) tryCastling:(MBCMove *)move;
226- (void)		tryPromotion:(MBCMove *)move;
227- (MBCSide)    sideOfMove:(MBCMove *)move;
228- (MBCUnique) 	disambiguateMove:(MBCMove *)move;
229- (bool) 		undoMoves:(int)numMoves;
230- (void) 		commitMove;						// Save position
231- (NSString *)	fen;							// Position in FEN notation
232- (NSString *)	holding;                        // Pieces held
233- (NSString *) 	moves;							// Moves in engine format
234- (void)        setFen:(NSString *)fen holding:(NSString *)holding
235				moves:(NSString *)moves;
236- (BOOL)		saveMovesTo:(FILE *)f;
237- (BOOL) 		canPromote:(MBCSide)side;
238- (BOOL)	    canUndo;
239- (MBCMove *)   lastMove;
240- (int) 		numMoves;
241- (MBCMove *)   move:(int)index;
242- (MBCPieces *) curPos;
243- (MBCPiece)	defaultPromotion:(BOOL)white;
244- (void)		setDefaultPromotion:(MBCPiece)piece for:(BOOL)white;
245- (MBCMoveCode)outcome;
246- (NSString *) stringFromMove:(MBCMove *)move withLocalization:(NSDictionary *)localization;
247- (NSString *) extStringFromMove:(MBCMove *)move withLocalization:(NSDictionary *)localization;
248
249@end
250
251NSString * LocalizedString(NSDictionary * localization, NSString * key, NSString * fallback);
252
253#define LOC(key, fallback) LocalizedString(localization, key, fallback)
254
255
256// Local Variables:
257// mode:ObjC
258// End:
259