From 0a4b7066412e6f80a877f872b5c0139433a13605 Mon Sep 17 00:00:00 2001 From: Daylen Yang Date: Sun, 12 Jan 2014 16:00:50 -0800 Subject: [PATCH] Major fixes to the board and piece view interaction, fix bugs in window controller, more test coverage --- Stockfish.xcodeproj/project.pbxproj | 2 +- Stockfish/SFMBoardView.mm | 72 ++++++++++++++++++----------- Stockfish/SFMChessGame.h | 1 + Stockfish/SFMChessGame.mm | 14 ++++-- Stockfish/SFMDocument.mm | 2 +- Stockfish/SFMPieceView.mm | 5 -- Stockfish/SFMWindowController.mm | 6 ++- StockfishTests/SFMChessGameTest.mm | 2 + 8 files changed, 64 insertions(+), 40 deletions(-) diff --git a/Stockfish.xcodeproj/project.pbxproj b/Stockfish.xcodeproj/project.pbxproj index cb5ff87..f6f4ed8 100644 --- a/Stockfish.xcodeproj/project.pbxproj +++ b/Stockfish.xcodeproj/project.pbxproj @@ -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 */, diff --git a/Stockfish/SFMBoardView.mm b/Stockfish/SFMBoardView.mm index 8372932..e982bf9 100644 --- a/Stockfish/SFMBoardView.mm +++ b/Stockfish/SFMBoardView.mm @@ -46,7 +46,6 @@ @implementation SFMBoardView - (void)setPosition:(Chess::Position *)position { _position = position; - NSLog(@"setting position"); assert(position->is_ok()); @@ -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]; @@ -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 @@ -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 @@ -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) { @@ -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 diff --git a/Stockfish/SFMChessGame.h b/Stockfish/SFMChessGame.h index c2539ae..f20c8cd 100644 --- a/Stockfish/SFMChessGame.h +++ b/Stockfish/SFMChessGame.h @@ -42,5 +42,6 @@ using namespace Chess; #pragma mark - Export - (NSString *)pgnString; // PGN string for this game +- (NSString *)movesArrayAsString; // Just the move text @end diff --git a/Stockfish/SFMChessGame.mm b/Stockfish/SFMChessGame.mm index 00c8e57..f3575f6 100644 --- a/Stockfish/SFMChessGame.mm +++ b/Stockfish/SFMChessGame.mm @@ -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()); } @@ -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; } @@ -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 || @@ -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 @@ -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()]; } diff --git a/Stockfish/SFMDocument.mm b/Stockfish/SFMDocument.mm index 7565b00..a0f1156 100644 --- a/Stockfish/SFMDocument.mm +++ b/Stockfish/SFMDocument.mm @@ -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; } diff --git a/Stockfish/SFMPieceView.mm b/Stockfish/SFMPieceView.mm index 23040b0..0f7b64c 100644 --- a/Stockfish/SFMPieceView.mm +++ b/Stockfish/SFMPieceView.mm @@ -65,9 +65,4 @@ - (void)moveTo:(NSPoint)point [NSAnimationContext endGrouping]; } -- (void)mouseUp:(NSEvent *)theEvent -{ - [self.boardView displayPossibleMoveHighlightsForPieceOnSquare:self.square]; -} - @end diff --git a/Stockfish/SFMWindowController.mm b/Stockfish/SFMWindowController.mm index 7dd3c8b..d7330b6 100644 --- a/Stockfish/SFMWindowController.mm +++ b/Stockfish/SFMWindowController.mm @@ -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()); } diff --git a/StockfishTests/SFMChessGameTest.mm b/StockfishTests/SFMChessGameTest.mm index 33e15b0..daeb003 100644 --- a/StockfishTests/SFMChessGameTest.mm +++ b/StockfishTests/SFMChessGameTest.mm @@ -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 { @@ -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