Skip to content

Commit

Permalink
load fen fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
anon committed Sep 19, 2024
1 parent 61355c6 commit da4756f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
21 changes: 19 additions & 2 deletions lib/src/model/board_editor/board_editor_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,25 @@ class BoardEditorController extends _$BoardEditorController {
);
}

void loadFen(String fen) {
_updatePosition(readFen(fen).lock);
void loadFen(
String fen, {
bool loadSideToPlay = false,
bool loadCastling = false,
}) {
final splits = fen.split(' ');
_updatePosition(readFen(splits[0]).lock);

if (loadSideToPlay && splits.length >= 2) {
setSideToPlay(splits[1].toLowerCase() == 'w' ? Side.white : Side.black);
}

if(loadCastling && splits.length >= 3) {
final castling = splits[2];
setCastling(Side.white, CastlingSide.king, castling.contains('K'));
setCastling(Side.white, CastlingSide.queen, castling.contains('Q'));
setCastling(Side.black, CastlingSide.king, castling.contains('k'));
setCastling(Side.black, CastlingSide.queen, castling.contains('q'));
}
}

/// Calculates the squares where an en passant capture could be possible.
Expand Down
12 changes: 8 additions & 4 deletions lib/src/view/board_editor/board_editor_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,21 @@ class BoardEditorMenu extends ConsumerWidget {
ChoiceChip(
onSelected: (_) {
ref.read(editorController.notifier).loadFen(
'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR b KQkq - 0 1',
'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq',
loadCastling: true,
loadSideToPlay: true,
);
},
selected: false,
label: const Text('Starting Position'),
),
ChoiceChip(
onSelected: (_) {
ref
.read(editorController.notifier)
.loadFen('8/8/8/8/8/8/8/8 b KQkq - 0 1');
ref.read(editorController.notifier).loadFen(
'8/8/8/8/8/8/8/8',
loadCastling: true,
loadSideToPlay: true,
);
},
label: const Text('Clear Board'),
selected: false,
Expand Down

0 comments on commit da4756f

Please sign in to comment.