Skip to content

Commit

Permalink
Support navigating to the start or end of the game
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-bovet committed May 12, 2021
1 parent 92735b8 commit 769a477
Show file tree
Hide file tree
Showing 12 changed files with 130 additions and 147 deletions.
12 changes: 6 additions & 6 deletions BChess.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
A72E2B57200496CD006CBB1C /* BoardHashTests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A72E2B56200496CC006CBB1C /* BoardHashTests.cpp */; };
A731BD3720009856004C13EF /* Tournament.swift in Sources */ = {isa = PBXBuildFile; fileRef = A731BD3620009856004C13EF /* Tournament.swift */; };
A731BD392000A513004C13EF /* TournamentEngines.swift in Sources */ = {isa = PBXBuildFile; fileRef = A731BD382000A513004C13EF /* TournamentEngines.swift */; };
A746997C2637CCF7007E0058 /* AnalyzeActionsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A746997B2637CCF7007E0058 /* AnalyzeActionsView.swift */; };
A746997D2637CCF7007E0058 /* AnalyzeActionsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A746997B2637CCF7007E0058 /* AnalyzeActionsView.swift */; };
A746997C2637CCF7007E0058 /* NavigationView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A746997B2637CCF7007E0058 /* NavigationView.swift */; };
A746997D2637CCF7007E0058 /* NavigationView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A746997B2637CCF7007E0058 /* NavigationView.swift */; };
A746C244200148E9001F4437 /* ChessBoardHash.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A746C242200148E9001F4437 /* ChessBoardHash.cpp */; };
A746C245200148ED001F4437 /* ChessBoardHash.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A746C242200148E9001F4437 /* ChessBoardHash.cpp */; };
A746C2492001707F001F4437 /* GameHistory.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A746C2472001707F001F4437 /* GameHistory.cpp */; };
Expand Down Expand Up @@ -209,7 +209,7 @@
A72E2B56200496CC006CBB1C /* BoardHashTests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = BoardHashTests.cpp; sourceTree = "<group>"; };
A731BD3620009856004C13EF /* Tournament.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Tournament.swift; sourceTree = "<group>"; };
A731BD382000A513004C13EF /* TournamentEngines.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TournamentEngines.swift; sourceTree = "<group>"; };
A746997B2637CCF7007E0058 /* AnalyzeActionsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnalyzeActionsView.swift; sourceTree = "<group>"; };
A746997B2637CCF7007E0058 /* NavigationView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NavigationView.swift; sourceTree = "<group>"; };
A746C242200148E9001F4437 /* ChessBoardHash.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = ChessBoardHash.cpp; sourceTree = "<group>"; };
A746C243200148E9001F4437 /* ChessBoardHash.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = ChessBoardHash.hpp; sourceTree = "<group>"; };
A746C2472001707F001F4437 /* GameHistory.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = GameHistory.cpp; sourceTree = "<group>"; };
Expand Down Expand Up @@ -561,7 +561,7 @@
A7A30EDC25AEBD7C00729432 /* LabelsView.swift */,
A77F66AE25A832240030E61D /* ContentView.swift */,
A7A30F2E25B2BAFC00729432 /* PromotionView.swift */,
A746997B2637CCF7007E0058 /* AnalyzeActionsView.swift */,
A746997B2637CCF7007E0058 /* NavigationView.swift */,
);
path = Views;
sourceTree = "<group>";
Expand Down Expand Up @@ -885,7 +885,7 @@
A7FE322A25AA96D100A75936 /* ChessBoardHash.cpp in Sources */,
A77F66D725A832270030E61D /* ChessDocument.swift in Sources */,
A795162225ABE59900AEA95F /* PlayAgainst.swift in Sources */,
A746997C2637CCF7007E0058 /* AnalyzeActionsView.swift in Sources */,
A746997C2637CCF7007E0058 /* NavigationView.swift in Sources */,
A7FE324E25AAD61200A75936 /* FEngineUtility.mm in Sources */,
A79515F625ABE44000AEA95F /* PiecesView.swift in Sources */,
A7BC72E42636003F008FBBB4 /* NewGameView.swift in Sources */,
Expand Down Expand Up @@ -937,7 +937,7 @@
A7FE31DA25AA96B800A75936 /* MoveList.cpp in Sources */,
A795162325ABE59900AEA95F /* PlayAgainst.swift in Sources */,
A7FE31CB25AA96A800A75936 /* FEngineUtility.mm in Sources */,
A746997D2637CCF7007E0058 /* AnalyzeActionsView.swift in Sources */,
A746997D2637CCF7007E0058 /* NavigationView.swift in Sources */,
A79515F725ABE44000AEA95F /* PiecesView.swift in Sources */,
A758BB0725AC119C0092714E /* Actions.swift in Sources */,
A716976D262BFF8C00156BD6 /* SettingsView.swift in Sources */,
Expand Down
25 changes: 8 additions & 17 deletions Shared/Actions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@ struct Actions {
document.lastMove = nil
}

func undoMove() {
guard engine.canUndoMove() else {
func canMove(to: Direction) -> Bool {
return engine.canMove(to: to);
}

func move(to: Direction) {
guard engine.canMove(to: to) else {
return
}

Expand All @@ -68,21 +72,8 @@ struct Actions {
if engine.isAnalyzing() {
engine.cancel()
}
engine.undoMove()
document.pgn = document.engine.pgn()
}
}

func redoMove() {
guard engine.canRedoMove() else {
return
}

withAnimation {
document.selection = Selection.empty()
document.lastMove = nil

engine.redoMove()

engine.move(to: to)
document.pgn = document.engine.pgn()
}
}
Expand Down
28 changes: 1 addition & 27 deletions Shared/ActionsToolbar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,24 +79,6 @@ struct RotateBoard: View {
}
}

struct UndoMoveButton: View {
@Binding var document: ChessDocument
var body: some View {
Button(action: { Actions(document: $document).undoMove() }) {
Label("Undo Move", systemImage: "arrow.uturn.backward.square")
}.disabled(!document.engine.canUndoMove())
}
}

struct RedoMoveButton: View {
@Binding var document: ChessDocument
var body: some View {
Button(action: { Actions(document: $document).redoMove() }) {
Label("Redo Move", systemImage: "arrow.uturn.forward.square")
}.disabled(!document.engine.canRedoMove())
}
}

struct CopyFENButton: View {
@Binding var document: ChessDocument
var body: some View {
Expand Down Expand Up @@ -154,17 +136,9 @@ struct ActionsToolbar: ToolbarContent {

AnalyzeBoard(document: $document)
TrainButton(document: $document)
}
label: {
Label("Game", systemImage: "ellipsis.circle")
}

Menu {
UndoMoveButton(document: $document)
RedoMoveButton(document: $document)

Divider()

RotateBoard(document: $document)

Divider()
Expand Down
14 changes: 9 additions & 5 deletions Shared/Bridge/FEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
typedef void(^FEngineSearchCallback)(FEngineInfo * _Nonnull info, BOOL completed);
typedef void(^FEngineDidUpdateCallback)();

typedef NS_ENUM(NSInteger, Direction){
start = 0,
end,
backward,
forward
};

/** This class is the interface between the C++ engine and the Objective-C/Swift world*/
@interface FEngine : NSObject

Expand Down Expand Up @@ -49,11 +56,8 @@ typedef void(^FEngineDidUpdateCallback)();
- (NSArray<FEngineMove*>* _Nonnull)movesAt:(NSUInteger)rank file:(NSUInteger)file;
- (void)move:(NSUInteger)move;

- (BOOL)canUndoMove;
- (BOOL)canRedoMove;

- (void)undoMove;
- (void)redoMove;
- (BOOL)canMoveTo:(Direction)direction;
- (void)moveTo:(Direction)direction;

- (void)move:(NSString* _Nonnull)from to:(NSString* _Nonnull)to;

Expand Down
28 changes: 15 additions & 13 deletions Shared/Bridge/FEngine.mm
Original file line number Diff line number Diff line change
Expand Up @@ -172,24 +172,26 @@ - (void)move:(NSString*)from to:(NSString*)to {
[self fireUpdate:self.stateIndex];
}

- (BOOL)canUndoMove {
return engine.canUndoMove();
- (ChessGame::Direction)gameDirection:(Direction)direction {
switch (direction) {
case start:
return ChessGame::Direction::start;
case end:
return ChessGame::Direction::end;
case backward:
return ChessGame::Direction::backward;
case forward:
return ChessGame::Direction::forward;
}
}

- (BOOL)canRedoMove {
return engine.canRedoMove();
- (BOOL)canMoveTo:(Direction)direction {
return engine.game.canMoveTo([self gameDirection:direction]);
}

- (void)undoMove {
- (void)moveTo:(Direction)direction {
// TODO: handle the cancel with a callback when the cancel actually really happened
[self cancel];
engine.undoMove();
[self fireUpdate:self.stateIndex];
}

- (void)redoMove {
[self cancel];
engine.redoMove();
engine.game.moveTo([self gameDirection:direction]);
[self fireUpdate:self.stateIndex];
}

Expand Down
18 changes: 1 addition & 17 deletions Shared/Engine/Engine/ChessEngine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,7 @@ class ChessEngine {
void move(std::string from, std::string to) {
game.move(from, to);
}

bool canUndoMove() {
return game.canUndoMove();
}

bool canRedoMove() {
return game.canRedoMove();
}

void undoMove() {
game.undoMove();
}

void redoMove() {
game.redoMove();
}


void stop() {
iterativeSearch.stop();
}
Expand Down
42 changes: 27 additions & 15 deletions Shared/Engine/Engine/ChessGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,23 +130,35 @@ void ChessGame::move(std::string from, std::string to) {
}
}

bool ChessGame::canUndoMove() {
return moveIndexes.moveCursor > 0;
}

bool ChessGame::canRedoMove() {
return moveIndexes.moveCursor < moveIndexes.moves.size();
}

void ChessGame::undoMove() {
assert(canUndoMove());
moveIndexes.moveCursor--;
replayMoves();
bool ChessGame::canMoveTo(Direction direction) {
switch (direction) {
case Direction::start:
return moveIndexes.moveCursor > 0;
case Direction::end:
return moveIndexes.moveCursor < moveIndexes.moves.size();
case Direction::backward:
return moveIndexes.moveCursor > 0;
case Direction::forward:
return moveIndexes.moveCursor < moveIndexes.moves.size();
}
}

void ChessGame::redoMove() {
assert(canRedoMove());
moveIndexes.moveCursor++;
void ChessGame::moveTo(Direction direction) {
assert(canMoveTo(direction));
switch (direction) {
case Direction::start:
moveIndexes.moveCursor = 0;
break;
case Direction::end:
moveIndexes.moveCursor = (int)moveIndexes.moves.size();
break;
case Direction::backward:
moveIndexes.moveCursor--;
break;
case Direction::forward:
moveIndexes.moveCursor++;
break;
}
replayMoves();
}

Expand Down
9 changes: 5 additions & 4 deletions Shared/Engine/Engine/ChessGame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,12 @@ class ChessGame {
void move(Move move, std::string comment, bool replace);
void move(std::string from, std::string to);

bool canUndoMove();
bool canRedoMove();
enum class Direction {
start, end, backward, forward
};

void undoMove();
void redoMove();
bool canMoveTo(Direction direction);
void moveTo(Direction direction);

std::string getState();

Expand Down
41 changes: 0 additions & 41 deletions Shared/Views/AnalyzeActionsView.swift

This file was deleted.

2 changes: 1 addition & 1 deletion Shared/Views/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ struct ContentView: View {

if (showInfo) {
VStack(alignment: .leading) {
AnalyzeActionsView(document: $document)
NavigationView(document: $document)

InformationView(document: document)
}
Expand Down
1 change: 0 additions & 1 deletion Shared/Views/InformationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ struct InformationView: View {
}
List(document.moveNodes.moveNodes, children: \FullMoveItem.children) { item in
FullMoveItemView(item: item, currentMoveUUID: document.engine.currentMoveNodeUUID())
.font(.system(.body, design: .monospaced))
}
Spacer()
if document.mode.value == .play {
Expand Down
Loading

0 comments on commit 769a477

Please sign in to comment.