To move forward
Code: Select all
CurrentPosition.MakeMove(VariationIndex);
Code: Select all
CurrentPosition.TakeBack;
So the question is, how do others implement a data structure for a PGN parser? Am I missing something obvious?
Thanks,
Steve
Moderator: Ras
Code: Select all
CurrentPosition.MakeMove(VariationIndex);
Code: Select all
CurrentPosition.TakeBack;
Code: Select all
@interface ChessMove : NSObject {
move_t move; // Low-level C move, actually just an int.
NSString *comment; // Comment for this move.
NSString *SANString; // SAN representation of this move.
int NAG; // Numeric annotation glyph.
}
Code: Select all
@interface GameNode : NSObject {
ChessPosition *position; // The position at this node.
ChessMove *move; // The move that led to this position, equal to
// 'nil' for the root node of a game.
GameNode *parent; // The parent node, 'nil' for the root node.
NSMutableArray *children; // Array of all child nodes, the main variation
// is the first entry.
}
Code: Select all
@interface Game : NSObject {
NSString *whitePlayer; // PGN tag
NSString *blackPlayer; // PGN tag
NSString *event; // PGN tag
NSString *site; // PGN tag
NSString *round; // PGN tag
NSString *date; // PGN tag
result_t result; // The result of the game
NSString *rootFEN; // FEN of the starting position for the game
GameNode *root; // The root node of the game
GameNode *currentNode; // The current node of the game
}
Code: Select all
// Add a new child node (i.e. start a new variation from this node):
-(void)insertMove:(ChessMove *)move {
[currentNode addChildNode: move];
currentNode = [[currentNode children] lastObject];
[self pushClock];
}
// Make a new move from the current position, remove the old move (if present)
// and all variations:
-(void)makeMove:(ChessMove *)move {
[currentNode removeAllChildNodes];
[self insertMove: move];
}
// Unmake the last move. No moves are deleted, we simply step one move back
// in the move list.
-(void)unmakeMove {
if(currentNode != root)
currentNode = [currentNode parent];
}
// Step forward one move, by picking the first child node of the current
// position (i.e. the main line):
-(void)stepForward {
if([[currentNode children] count] > 0)
currentNode = [currentNode firstChildNode];
}
// Go back to the beginning of the game:
-(void)goToBeginningOfGame {
while(currentNode != root)
currentNode = [currentNode parent];
}
// Go to the end of the game:
-(void)goToEndOfGame {
for(currentNode = root; [[currentNode children] count] > 0;
currentNode = [currentNode firstChildNode]);
}
// Delete the variation beginning with the current node:
-(void)deleteVariation {
if(currentNode != root) {
GameNode *parent = [currentNode parent];
[[parent children] removeObjectIdenticalTo: currentNode];
currentNode = parent;
}
}
// Are we at the beginning of the game?
-(BOOL)isAtBeginningOfGame {
if(currentNode == root) return YES;
else return NO;
}
// Are we at the end of the game?
-(BOOL)isAtEndOfGame {
GameNode *node;
if([[currentNode children] count] > 0) return NO;
for(node = root; [[node children] count] > 0; node = [node firstChildNode]);
if(node == currentNode) return YES;
else return NO;
}
// Are there any variations before the current variation from the parent node?
-(BOOL)previousVariationExists {
if(currentNode == root) return NO;
if([[[currentNode parent] children] indexOfObject: currentNode] == 0)
return NO;
else
return YES;
}
// Are there any variations after the current variation from the parent node?
-(BOOL)nextVariationExists {
NSMutableArray *siblings;
if(currentNode == root) return NO;
siblings = [[currentNode parent] children];
if([siblings indexOfObject: currentNode] < [siblings count] - 1)
return YES;
else
return NO;
}
// Go to the previous variation from the parent node:
-(void)goToPreviousVariation {
if([self previousVariationExists]) {
NSMutableArray *siblings = [[currentNode parent] children];
currentNode = [siblings objectAtIndex:
[siblings indexOfObject: currentNode] - 1];
}
}
// Go to the next variation from the parent node:
-(void)goToNextVariation {
if([self nextVariationExists]) {
NSMutableArray *siblings = [[currentNode parent] children];
currentNode = [siblings objectAtIndex:
[siblings indexOfObject: currentNode] + 1];
}
}
// Move the current variation one place up among the variations from the
// parent node:
-(void)moveVariationUp {
if([self previousVariationExists]) {
NSMutableArray *siblings = [[currentNode parent] children];
int index = [siblings indexOfObject: currentNode];
id tmp = [siblings objectAtIndex: index - 1];
[tmp retain];
[siblings replaceObjectAtIndex: index - 1 withObject: currentNode];
[siblings replaceObjectAtIndex: index withObject: tmp];
[tmp release];
}
}
// Move the current variation one place down among the variations from the
// parent node:
-(void)moveVariationDown {
if([self nextVariationExists]) {
NSMutableArray *siblings = [[currentNode parent] children];
int index = [siblings indexOfObject: currentNode];
id tmp = [siblings objectAtIndex: index + 1];
[tmp retain];
[siblings replaceObjectAtIndex: index + 1 withObject: currentNode];
[siblings replaceObjectAtIndex: index withObject: tmp];
[tmp release];
}
}
Thanks Tord and Reinhard,Tord Romstad wrote:Hello Gerd,
Reinhard is right: it's Objective-C. As strange as it may sound, Objective-C is a kind of hybrid between C and Smalltalk. Once you get used to the funny syntax, it's quite a decent language. I like it better than C++.
Tord
Code: Select all
Move := PGN_Game.Variation[i].Move[n];