Skip to content

Commit

Permalink
Added support for multiple games for each PGN file
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-bovet committed May 12, 2021
1 parent 769a477 commit fa84584
Show file tree
Hide file tree
Showing 18 changed files with 254 additions and 62 deletions.
26 changes: 24 additions & 2 deletions BChess.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@
A77F66DC25A832270030E61D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A77F66AF25A832270030E61D /* Assets.xcassets */; };
A78E754C202C080E00445360 /* MovesTests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A78E754B202C080E00445360 /* MovesTests.cpp */; };
A78E7551202C110200445360 /* UnitTestHelper.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A78E754F202C110200445360 /* UnitTestHelper.cpp */; };
A791117C264B923600F97FA7 /* FEngineGame.mm in Sources */ = {isa = PBXBuildFile; fileRef = A791117B264B923600F97FA7 /* FEngineGame.mm */; };
A791117D264B923600F97FA7 /* FEngineGame.mm in Sources */ = {isa = PBXBuildFile; fileRef = A791117B264B923600F97FA7 /* FEngineGame.mm */; };
A791117F264B92E500F97FA7 /* FEngineGame+Private.h in Sources */ = {isa = PBXBuildFile; fileRef = A791117E264B92E500F97FA7 /* FEngineGame+Private.h */; };
A7911180264B92E500F97FA7 /* FEngineGame+Private.h in Sources */ = {isa = PBXBuildFile; fileRef = A791117E264B92E500F97FA7 /* FEngineGame+Private.h */; };
A7911181264B98DC00F97FA7 /* FEngineGame.mm in Sources */ = {isa = PBXBuildFile; fileRef = A791117B264B923600F97FA7 /* FEngineGame.mm */; };
A7911183264B98E600F97FA7 /* FEngineMoveNode.mm in Sources */ = {isa = PBXBuildFile; fileRef = A757614E264A50E8006242F9 /* FEngineMoveNode.mm */; };
A7911184264B998900F97FA7 /* FEngineMoveNode.mm in Sources */ = {isa = PBXBuildFile; fileRef = A757614E264A50E8006242F9 /* FEngineMoveNode.mm */; };
A7911185264B998900F97FA7 /* FEngineGame.mm in Sources */ = {isa = PBXBuildFile; fileRef = A791117B264B923600F97FA7 /* FEngineGame.mm */; };
A7911186264B998900F97FA7 /* FEngineMove.mm in Sources */ = {isa = PBXBuildFile; fileRef = A7C92AB31FF86BF200160D2E /* FEngineMove.mm */; };
A79514EC25AAE2C400AEA95F /* FENgineInfo+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = A75359831FDC51B2008D1DEE /* FENgineInfo+Extension.swift */; };
A79514F925AAE2C500AEA95F /* FENgineInfo+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = A75359831FDC51B2008D1DEE /* FENgineInfo+Extension.swift */; };
A795159E25ABE1D700AEA95F /* PiecesFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = A795159D25ABE1D700AEA95F /* PiecesFactory.swift */; };
Expand Down Expand Up @@ -113,7 +122,6 @@
A7BC72E42636003F008FBBB4 /* NewGameView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7BC72E32636003F008FBBB4 /* NewGameView.swift */; };
A7BC72E52636003F008FBBB4 /* NewGameView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7BC72E32636003F008FBBB4 /* NewGameView.swift */; };
A7C92AAB1FF6F19100160D2E /* Openings.pgn in Resources */ = {isa = PBXBuildFile; fileRef = A7C92AA91FF6EE5500160D2E /* Openings.pgn */; };
A7C92AB41FF86BF200160D2E /* FEngineMove.mm in Sources */ = {isa = PBXBuildFile; fileRef = A7C92AB31FF86BF200160D2E /* FEngineMove.mm */; };
A7C92AB51FF86C4300160D2E /* FEngineMove.mm in Sources */ = {isa = PBXBuildFile; fileRef = A7C92AB31FF86BF200160D2E /* FEngineMove.mm */; };
A7C92AB91FF86F6800160D2E /* FEngineUtility.mm in Sources */ = {isa = PBXBuildFile; fileRef = A7C92AB81FF86F6800160D2E /* FEngineUtility.mm */; };
A7C92ABB1FF8712800160D2E /* FEngineUtility.mm in Sources */ = {isa = PBXBuildFile; fileRef = A7C92AB81FF86F6800160D2E /* FEngineUtility.mm */; };
Expand Down Expand Up @@ -270,6 +278,9 @@
A78E754B202C080E00445360 /* MovesTests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = MovesTests.cpp; sourceTree = "<group>"; };
A78E754F202C110200445360 /* UnitTestHelper.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = UnitTestHelper.cpp; sourceTree = "<group>"; };
A78E7550202C110200445360 /* UnitTestHelper.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = UnitTestHelper.hpp; sourceTree = "<group>"; };
A791117A264B923600F97FA7 /* FEngineGame.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FEngineGame.h; sourceTree = "<group>"; };
A791117B264B923600F97FA7 /* FEngineGame.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = FEngineGame.mm; sourceTree = "<group>"; };
A791117E264B92E500F97FA7 /* FEngineGame+Private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "FEngineGame+Private.h"; sourceTree = "<group>"; };
A795159D25ABE1D700AEA95F /* PiecesFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PiecesFactory.swift; sourceTree = "<group>"; };
A79515A825ABE31100AEA95F /* SquareView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SquareView.swift; sourceTree = "<group>"; };
A79515B325ABE34300AEA95F /* TopInformationView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TopInformationView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -362,6 +373,9 @@
A7C92AB31FF86BF200160D2E /* FEngineMove.mm */,
A75683271FCD28A000CF1408 /* FEngine.h */,
A75683281FCD28A000CF1408 /* FEngine.mm */,
A791117A264B923600F97FA7 /* FEngineGame.h */,
A791117E264B92E500F97FA7 /* FEngineGame+Private.h */,
A791117B264B923600F97FA7 /* FEngineGame.mm */,
A757614D264A50E8006242F9 /* FEngineMoveNode.h */,
A7576151264A51BC006242F9 /* FEngineMoveNode+Private.h */,
A757614E264A50E8006242F9 /* FEngineMoveNode.mm */,
Expand Down Expand Up @@ -843,6 +857,7 @@
A78E754C202C080E00445360 /* MovesTests.cpp in Sources */,
A7712D341FC895D100E7E802 /* UCI.swift in Sources */,
A70A61DA1FD4D49500AFDF0E /* ChessBoard.cpp in Sources */,
A7911183264B98E600F97FA7 /* FEngineMoveNode.mm in Sources */,
A75CB25D1FED8F82005487BD /* EvaluationTests.cpp in Sources */,
A7E490EC1FEA23DD00970EAD /* PGNTests.cpp in Sources */,
A7E490ED1FEA29B900970EAD /* GamesTests.swift in Sources */,
Expand All @@ -854,6 +869,7 @@
A75359851FDC994E008D1DEE /* FENgineInfo+Extension.swift in Sources */,
A7C92AB51FF86C4300160D2E /* FEngineMove.mm in Sources */,
A70A61D51FD4D42100AFDF0E /* FEngineInfo.mm in Sources */,
A7911181264B98DC00F97FA7 /* FEngineGame.mm in Sources */,
A7688F64204B73BF004B1E9E /* StateTests.cpp in Sources */,
A7E490F21FEA2E7E00970EAD /* GoogleTests.mm in Sources */,
A7EF55C91FF1CF77004CF2DA /* BestMoveTests.cpp in Sources */,
Expand Down Expand Up @@ -884,6 +900,8 @@
A795160C25ABE55E00AEA95F /* Square.swift in Sources */,
A7FE322A25AA96D100A75936 /* ChessBoardHash.cpp in Sources */,
A77F66D725A832270030E61D /* ChessDocument.swift in Sources */,
A791117C264B923600F97FA7 /* FEngineGame.mm in Sources */,
A791117F264B92E500F97FA7 /* FEngineGame+Private.h in Sources */,
A795162225ABE59900AEA95F /* PlayAgainst.swift in Sources */,
A746997C2637CCF7007E0058 /* NavigationView.swift in Sources */,
A7FE324E25AAD61200A75936 /* FEngineUtility.mm in Sources */,
Expand Down Expand Up @@ -936,6 +954,8 @@
A7FE31CA25AA96A800A75936 /* FEngineInfo.mm in Sources */,
A7FE31DA25AA96B800A75936 /* MoveList.cpp in Sources */,
A795162325ABE59900AEA95F /* PlayAgainst.swift in Sources */,
A791117D264B923600F97FA7 /* FEngineGame.mm in Sources */,
A7911180264B92E500F97FA7 /* FEngineGame+Private.h in Sources */,
A7FE31CB25AA96A800A75936 /* FEngineUtility.mm in Sources */,
A746997D2637CCF7007E0058 /* NavigationView.swift in Sources */,
A79515F725ABE44000AEA95F /* PiecesView.swift in Sources */,
Expand Down Expand Up @@ -999,13 +1019,14 @@
A7712D2F1FC7C4CD00E7E802 /* UCI.swift in Sources */,
A77372011FE320010001A90F /* FPGN.cpp in Sources */,
A746C2492001707F001F4437 /* GameHistory.cpp in Sources */,
A7911186264B998900F97FA7 /* FEngineMove.mm in Sources */,
A70A61BB1FD132D200AFDF0E /* FFEN.cpp in Sources */,
A75359841FDC51B2008D1DEE /* FENgineInfo+Extension.swift in Sources */,
A756832D1FD0B3FE00CF1408 /* magicmoves.c in Sources */,
A7712D471FCBC13100E7E802 /* ChessBoard.cpp in Sources */,
A7911184264B998900F97FA7 /* FEngineMoveNode.mm in Sources */,
A77371FE1FE31FEF0001A90F /* ChessGame.cpp in Sources */,
A7688F60204B6E91004B1E9E /* ChessState.cpp in Sources */,
A7C92AB41FF86BF200160D2E /* FEngineMove.mm in Sources */,
A70A61CA1FD46E3C00AFDF0E /* ChessEvaluater.cpp in Sources */,
A70A61C41FD458DE00AFDF0E /* ChessMoveGenerator.cpp in Sources */,
A70A61D11FD4AA9600AFDF0E /* FEngineInfo.mm in Sources */,
Expand All @@ -1014,6 +1035,7 @@
A7C92AB91FF86F6800160D2E /* FEngineUtility.mm in Sources */,
A731BD392000A513004C13EF /* TournamentEngines.swift in Sources */,
A731BD3720009856004C13EF /* Tournament.swift in Sources */,
A7911185264B998900F97FA7 /* FEngineGame.mm in Sources */,
A746C244200148E9001F4437 /* ChessBoardHash.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
1 change: 1 addition & 0 deletions BChess/BChess-Bridging-Header.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
#import "FEngineMove.h"
#import "FEngineInfo.h"
#import "FEngineMoveNode.h"
#import "FEngineGame.h"
4 changes: 2 additions & 2 deletions BChess/UCI/Tournament.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation

typealias Callback = () -> Void

protocol Player : class {
protocol Player : AnyObject {

var state: String { get set }

Expand All @@ -37,7 +37,7 @@ class PlayerChessEngine: Player {
engine.setPGN(newValue)
}
get {
return engine.pgn()
return engine.getPGNCurrentGame()
}
}

Expand Down
2 changes: 1 addition & 1 deletion BChessTests/FEngineTests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// FEngine.swift
// FEngineTests.swift
// BChessTests
//
// Created by Jean Bovet on 5/10/21.
Expand Down
10 changes: 5 additions & 5 deletions Shared/Actions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ struct Actions {
func newGame() {
engine.setFEN(StartPosFEN)
document.info = nil
document.pgn = engine.pgn()
document.pgn = engine.pgnAllGames()

document.selection = Selection.empty()
document.lastMove = nil
Expand All @@ -74,7 +74,7 @@ struct Actions {
}

engine.move(to: to)
document.pgn = document.engine.pgn()
document.pgn = document.engine.pgnAllGames()
}
}

Expand Down Expand Up @@ -102,7 +102,7 @@ struct Actions {
#if os(macOS)
let pb = NSPasteboard.general
pb.declareTypes([.string], owner: nil)
pb.setString(engine.pgn(), forType: .string)
pb.setString(engine.getPGNCurrentGame(), forType: .string)
#else
UIPasteboard.general.string = engine.pgn()
#endif
Expand All @@ -119,7 +119,7 @@ struct Actions {
}
#endif
if engine.setFEN(content) {
document.pgn = document.engine.pgn()
document.pgn = document.engine.pgnAllGames()
return true
} else {
return false
Expand All @@ -137,7 +137,7 @@ struct Actions {
}
#endif
if engine.setPGN(content) {
document.pgn = document.engine.pgn()
document.pgn = document.engine.pgnAllGames()
return true
} else {
return false
Expand Down
14 changes: 14 additions & 0 deletions Shared/ActionsToolbar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@ struct CopyPasteMenu: View {
}
}

struct GameSelectionMenu: View {
@Binding var document: ChessDocument

var body: some View {
Picker(selection: $document.currentGameIndex, label: Text("Games")) {
ForEach(document.engine.games, id:\.self) { game in
Text(game.name).tag(game.index)
}
}
}
}

struct ActionsToolbar: ToolbarContent {

@Binding var document: ChessDocument
Expand All @@ -132,6 +144,8 @@ struct ActionsToolbar: ToolbarContent {
NewGameButton(document: $document, showNewGameSheet: $showNewGameSheet, newGameSheetEditMode: $newGameSheetEditMode)
EditGameButton(document: $document, showNewGameSheet: $showNewGameSheet, newGameSheetEditMode: $newGameSheetEditMode)

GameSelectionMenu(document: $document)

Divider()

AnalyzeBoard(document: $document)
Expand Down
8 changes: 7 additions & 1 deletion Shared/Bridge/FEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
@class FEngineMove;
@class FEngineInfo;
@class FEngineMoveNode;
@class FEngineGame;

typedef void(^FEngineSearchCallback)(FEngineInfo * _Nonnull info, BOOL completed);
typedef void(^FEngineDidUpdateCallback)();
Expand All @@ -36,6 +37,9 @@ typedef NS_ENUM(NSInteger, Direction){

@property (nonatomic, strong, readonly) NSString * _Nonnull state;

@property (nonatomic, strong, readonly) NSArray<FEngineGame*>* _Nonnull games;
@property (nonatomic, assign) NSUInteger currentGameIndex;

- (id _Nonnull)init;

- (BOOL)loadOpening:(NSString* _Nonnull)pgn;
Expand All @@ -45,8 +49,10 @@ typedef NS_ENUM(NSInteger, Direction){
- (BOOL)setFEN:(NSString* _Nonnull)FEN;
- (NSString* _Nonnull)FEN;

- (BOOL)loadAllGames:(NSString* _Nonnull)PGN;
- (BOOL)setPGN:(NSString* _Nonnull)PGN;
- (NSString* _Nonnull)PGN;
- (NSString* _Nonnull)pgnAllGames;
- (NSString* _Nonnull)getPGNCurrentGame;

- (NSString* _Nonnull)PGNFormattedForDisplay;
- (NSArray<FEngineMoveNode*>* _Nonnull)moveNodesTree;
Expand Down
49 changes: 42 additions & 7 deletions Shared/Bridge/FEngine.mm
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import "FEngine.h"
#import "FEngineInfo+Private.h"
#import "FEngineMove.h"
#import "FEngineGame+Private.h"
#import "FEngineMoveNode.h"
#import "FEngineMoveNode+Private.h"
#import "FEngineInfo.h"
Expand All @@ -34,6 +35,9 @@ @interface FEngine () {

@implementation FEngine

@synthesize games;
@synthesize currentGameIndex;

+ (void)initialize {
if (self == [FEngine class]) {
ChessEngine::initialize();
Expand Down Expand Up @@ -89,12 +93,43 @@ - (NSString*)FEN {
return NSStringFromString(engine.getFEN());
}

- (NSArray<FEngineGame*>*)games {
NSMutableArray *engineGames = [NSMutableArray array];
auto games = engine.games;
for (int index=0; index<games.size(); index++) {
auto game = games[index];
FEngineGame *engineGame = [[FEngineGame alloc] initWithGame:game andIndex:index];
[engineGames addObject:engineGame];
}
return engineGames;
}

- (void)setCurrentGameIndex:(NSUInteger)index {
engine.gameIndex = (unsigned int)index;
}

- (NSUInteger)currentGameIndex {
return engine.gameIndex;
}

- (BOOL)loadAllGames:(NSString* _Nonnull)PGN {
return engine.loadAllGames(StringFromNSString(PGN));
}

- (BOOL)setPGN:(NSString *)PGN {
return engine.setPGN(StringFromNSString(PGN));
}

- (NSString*)PGN {
return NSStringFromString(engine.getPGN());
- (NSString*)getPGN:(BOOL)currentGame {
return NSStringFromString(engine.getPGN(currentGame));
}

- (NSString* _Nonnull)pgnAllGames {
return [self getPGN:false];
}

- (NSString* _Nonnull)getPGNCurrentGame {
return [self getPGN:true];
}

- (NSString*)PGNFormattedForDisplay {
Expand Down Expand Up @@ -126,7 +161,7 @@ - (void)moveNodesFromNode:(ChessGame::MoveNode)node
}

- (NSUInteger)currentMoveNodeUUID {
return engine.game.getCurrentMoveUUID();
return engine.game().getCurrentMoveUUID();
}

#pragma mark -
Expand All @@ -137,7 +172,7 @@ - (NSString*)state {

- (NSArray<FEngineMove*>* _Nonnull)allMoves {
NSMutableArray *moves = [NSMutableArray array];
for (Move move : engine.game.allMoves()) {
for (Move move : engine.game().allMoves()) {
[moves addObject:[self engineMoveFromMove:move]];
}
return moves;
Expand Down Expand Up @@ -185,13 +220,13 @@ - (void)move:(NSString*)from to:(NSString*)to {
}
}
- (BOOL)canMoveTo:(Direction)direction {
return engine.game.canMoveTo([self gameDirection:direction]);
return engine.game().canMoveTo([self gameDirection:direction]);
}

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

Expand Down Expand Up @@ -221,7 +256,7 @@ - (BOOL)canPlay {
- (FEngineInfo*)infoFor:(ChessEvaluation)info {
FEngineInfo *ei = [[FEngineInfo alloc] init];
ei.info = info;
ei.game = engine.game;
ei.game = engine.game();
return ei;
}

Expand Down
21 changes: 21 additions & 0 deletions Shared/Bridge/FEngineGame+Private.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// FEngineGame+Private.m
// BChess
//
// Created by Jean Bovet on 5/11/21.
// Copyright © 2021 Jean Bovet. All rights reserved.
//

#import "FEngineGame.h"
#import "ChessGame.hpp"

@interface FEngineGame () {
ChessGame game;
}

@property (nonatomic, assign, readwrite) NSUInteger index;
@property (nonatomic, strong, readwrite) NSString * _Nonnull name;

- (instancetype _Nonnull )initWithGame:(ChessGame)game andIndex:(NSUInteger)index;

@end
20 changes: 20 additions & 0 deletions Shared/Bridge/FEngineGame.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// FEngineGame.h
// BChess
//
// Created by Jean Bovet on 5/11/21.
// Copyright © 2021 Jean Bovet. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface FEngineGame: NSObject

@property (nonatomic, assign, readonly) NSUInteger index;
@property (nonatomic, strong, readonly) NSString * _Nonnull name;

@end

NS_ASSUME_NONNULL_END
Loading

0 comments on commit fa84584

Please sign in to comment.