Skip to content

Commit

Permalink
Major fixes to the board and piece view interaction, fix bugs in wind…
Browse files Browse the repository at this point in the history
…ow controller, more test coverage
  • Loading branch information
daylen committed Jan 13, 2014
1 parent d093e35 commit 0a4b706
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Stockfish.xcodeproj/project.pbxproj
Expand Up @@ -183,11 +183,11 @@
77050665187CE43300BA4635 = {
isa = PBXGroup;
children = (
77924AF8187E57740019C56E /* Constants.h */,
779925DA18828B6B00EAAEDD /* SFMBoardViewDelegate.h */,
773DE97018822DF8009156C3 /* Piece PDFs */,
7752FF4818810B34003E82B3 /* View */,
7715B8AD187E8981006F2366 /* Model */,
77924AF8187E57740019C56E /* Constants.h */,
7715B8AE187E89A8006F2366 /* UI */,
770506A8187CE52D00BA4635 /* Chess */,
77050677187CE43300BA4635 /* Stockfish */,
Expand Down
72 changes: 44 additions & 28 deletions Stockfish/SFMBoardView.mm
Expand Up @@ -46,7 +46,6 @@ @implementation SFMBoardView
- (void)setPosition:(Chess::Position *)position
{
_position = position;
NSLog(@"setting position");

assert(position->is_ok());

Expand Down Expand Up @@ -103,7 +102,6 @@ - (id)initWithFrame:(NSRect)frame
#pragma mark - Draw
- (void)drawRect:(NSRect)dirtyRect
{
NSLog(@"Draw rect");
// Draw a felt background
NSGraphicsContext *context = [NSGraphicsContext currentContext];
[context saveGraphicsState];
Expand Down Expand Up @@ -157,12 +155,10 @@ - (void)drawRect:(NSRect)dirtyRect

// Draw pieces
for (SFMPieceView *pieceView in self.pieces) {
// Only redraw if resizing or if we haven't drawn yet or if the board has been flipped
if (pieceView.frame.size.width == 0 || [self inLiveResize]) {
CGPoint coordinate = [self coordinatesForSquare:pieceView.square leftOffset:leftInset topOffset:topInset sideLength:squareSideLength];
[pieceView setFrame:NSMakeRect(coordinate.x, coordinate.y, squareSideLength, squareSideLength)];
[pieceView setNeedsDisplay:YES];
}
CGPoint coordinate = [self coordinatesForSquare:pieceView.square leftOffset:leftInset topOffset:topInset sideLength:squareSideLength];
[pieceView setFrame:NSMakeRect(coordinate.x, coordinate.y, squareSideLength, squareSideLength)];
[pieceView setNeedsDisplay:YES];

}

// Draw highlights
Expand Down Expand Up @@ -218,29 +214,48 @@ - (Square)squareForCoordinates:(NSPoint)point

- (void)mouseUp:(NSEvent *)theEvent
{
if (numHighlightedSquares > 0) {
NSPoint clickLocation = [self convertPoint:[theEvent locationInWindow] fromView:nil];
Square clickedSquare = [self squareForCoordinates:clickLocation leftOffset:leftInset topOffset:topInset sideLength:squareSideLength];
// Figure out which square you clicked on
NSPoint clickLocation = [self convertPoint:[theEvent locationInWindow] fromView:nil];
Square clickedSquare = [self squareForCoordinates:clickLocation leftOffset:leftInset topOffset:topInset sideLength:squareSideLength];

if (numHighlightedSquares == 0) {
// You haven't selected a valid piece, since there are no highlighted
// squares on the board.

if (clickedSquare == SQ_NONE) {
NSLog(@"Did not actually click a square!");
}
[self displayPossibleMoveHighlightsForPieceOnSquare:clickedSquare];

for (int i = 0; i < numHighlightedSquares; i++) {
if (highlightedSquares[i] == clickedSquare) {
assert(self.delegate != nil);
Move theMove = [self.delegate doMoveFrom:selectedSquare
to:clickedSquare]; // Update the model
} else {
// You previously selected a valid piece, and now you're trying to move it

if (clickedSquare != SQ_NONE) {

// Is it possible to move to the square you clicked on?
BOOL isValidMove = NO;
for (int i = 0; i < numHighlightedSquares; i++) {
if (highlightedSquares[i] == clickedSquare) {
isValidMove = YES;
break;
}
}

if (isValidMove) {
/* TODO
- castling
- en passant
- promotion
*/
Move theMove = [self.delegate doMoveFrom:selectedSquare to:clickedSquare];
UndoInfo u;
self.position->do_move(theMove, u); // Update view
self.position->do_move(theMove, u);
[self animatePieceOnSquare:selectedSquare to:clickedSquare];
}

}

// TODO Handle promotion
numHighlightedSquares = 0;
}
numHighlightedSquares = 0;

[self setNeedsDisplay:YES];

}

- (void)animatePieceOnSquare:(Square)fromSquare to:(Square)toSquare
Expand All @@ -250,7 +265,7 @@ - (void)animatePieceOnSquare:(Square)fromSquare to:(Square)toSquare

- (void)animatePieceOnSquare:(Square)fromSquare to:(Square)toSquare promotion:(PieceType)desiredPromotionPiece
{
// TODO handle promotion
// TODO handle promotion, castling, en passant, etc.
SFMPieceView *thePiece;
SFMPieceView *capturedPiece;
for (SFMPieceView *pieceView in self.pieces) {
Expand All @@ -263,12 +278,13 @@ - (void)animatePieceOnSquare:(Square)fromSquare to:(Square)toSquare promotion:(P
}

if (capturedPiece) {

} else {
[thePiece moveTo:[self coordinatesForSquare:toSquare leftOffset:leftInset topOffset:topInset sideLength:squareSideLength]];
[capturedPiece removeFromSuperview];
[self.pieces removeObject:capturedPiece];
}
thePiece.square = toSquare;
[thePiece moveTo:[self coordinatesForSquare:toSquare leftOffset:leftInset topOffset:topInset sideLength:squareSideLength]];


// TODO update internal data structure
}

- (void)displayPossibleMoveHighlightsForPieceOnSquare:(Chess::Square)sq
Expand Down
1 change: 1 addition & 0 deletions Stockfish/SFMChessGame.h
Expand Up @@ -42,5 +42,6 @@ using namespace Chess;

#pragma mark - Export
- (NSString *)pgnString; // PGN string for this game
- (NSString *)movesArrayAsString; // Just the move text

@end
14 changes: 10 additions & 4 deletions Stockfish/SFMChessGame.mm
Expand Up @@ -41,8 +41,10 @@ - (id)initWithWhite:(SFMPlayer *)p1 andBlack:(SFMPlayer *)p2
self.moveText = nil;
self.currentMoveIndex = 0;

self.startPosition = new Position([FEN_START_POSITION UTF8String]);
self.currPosition = new Position([FEN_START_POSITION UTF8String]);
self.startPosition = new Position;
self.currPosition = new Position;
self.startPosition->from_fen([FEN_START_POSITION UTF8String]);
self.currPosition->from_fen([FEN_START_POSITION UTF8String]);
assert(self.startPosition->is_ok());
assert(self.currPosition->is_ok());
}
Expand All @@ -69,8 +71,8 @@ - (void)populateMovesFromMoveText

- (void)convertToChessMoveObjects:(NSArray *)movesAsText
{
NSLog(@"Converting move text to chess move objects");
if (!movesAsText) {
NSLog(@"Called convert but no moves to convert!");
return;
}

Expand Down Expand Up @@ -110,7 +112,6 @@ - (void)convertToChessMoveObjects:(NSArray *)movesAsText
- (Move)doMoveFrom:(Square)fromSquare to:(Square)toSquare promotion:(PieceType)desiredPieceType
{
assert(self.currPosition->is_ok());
self.currPosition->print();
assert(square_is_ok(fromSquare));
assert(square_is_ok(toSquare));
assert(desiredPieceType == NO_PIECE_TYPE ||
Expand All @@ -125,6 +126,7 @@ - (Move)doMoveFrom:(Square)fromSquare to:(Square)toSquare promotion:(PieceType)d
move = mlist[i];
matches++;
}
NSLog(@"%d matches", matches);
assert(matches == 1);

// Update position
Expand Down Expand Up @@ -236,6 +238,10 @@ - (NSString *)movesArrayAsString
}
line[i] = MOVE_NONE;

NSLog(@"Printing the start position");
assert(self.startPosition->is_ok());
self.startPosition->print();

return [NSString stringWithUTF8String:line_to_san(*self.startPosition, line, 0, true, 1).c_str()];
}

Expand Down
2 changes: 1 addition & 1 deletion Stockfish/SFMDocument.mm
Expand Up @@ -47,7 +47,7 @@ - (id)initWithType:(NSString *)typeName error:(NSError *__autoreleasing *)outErr
{
self = [self init];
if (self) {
self.pgnFile = [SFMPGNFile new];
self.pgnFile = [[SFMPGNFile alloc] init];
}
return self;
}
Expand Down
5 changes: 0 additions & 5 deletions Stockfish/SFMPieceView.mm
Expand Up @@ -65,9 +65,4 @@ - (void)moveTo:(NSPoint)point
[NSAnimationContext endGrouping];
}

- (void)mouseUp:(NSEvent *)theEvent
{
[self.boardView displayPossibleMoveHighlightsForPieceOnSquare:self.square];
}

@end
6 changes: 5 additions & 1 deletion Stockfish/SFMWindowController.mm
Expand Up @@ -35,9 +35,13 @@ - (void)windowDidLoad
[super windowDidLoad];

SFMChessGame *firstGame = self.pgnFile.games[0];
[firstGame populateMovesFromMoveText];

self.boardView.position = firstGame.startPosition;
self.boardView.position->copy(*firstGame.startPosition);
[self.boardView setDelegate:self];

assert(firstGame.startPosition->is_ok());
assert(firstGame.currPosition->is_ok());

}

Expand Down
2 changes: 2 additions & 0 deletions StockfishTests/SFMChessGameTest.mm
Expand Up @@ -51,6 +51,7 @@ - (void)testSimpleGame
[game doMoveFrom:SQ_E7 to:SQ_E5];
XCTAssertEqual([game.moves count], 2);
XCTAssertTrue([game atEnd]);
XCTAssertEqualObjects([game movesArrayAsString], @"1. e4 e5 ");
}
- (void)testLoadedGame
{
Expand All @@ -68,6 +69,7 @@ - (void)testLoadedGame
[game doMoveFrom:SQ_A7 to:SQ_A6];
XCTAssertEqual([game.moves count], 6);
XCTAssertTrue([game atEnd]);
XCTAssertEqualObjects([game movesArrayAsString], @"1. e4 e5 2. Nf3 Nc6 3. Bb5 a6 ");
}

@end

0 comments on commit 0a4b706

Please sign in to comment.