Skip to content

Commit

Permalink
Side to move is now optional and add asserts
Browse files Browse the repository at this point in the history
  • Loading branch information
veloce committed Oct 27, 2023
1 parent 666cc53 commit d5698a9
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 12 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ class _MyHomePageState extends State<MyHomePage> {
data: BoardData(
interactableSide: InteractableSide.none,
orientation: Side.white,
sideToMove: Side.white,
fen: fen,
),
),
Expand Down
1 change: 0 additions & 1 deletion example/lib/board_thumbnails.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class BoardThumbnailsPage extends StatelessWidget {
data: BoardData(
interactableSide: InteractableSide.none,
orientation: Side.white,
sideToMove: Side.white,
fen: fen,
),
);
Expand Down
14 changes: 9 additions & 5 deletions lib/src/board_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@ class BoardData {
required this.interactableSide,
required this.orientation,
required this.fen,
required this.sideToMove,
this.sideToMove,
this.premove,
this.lastMove,
this.validMoves,
this.isCheck = false,
this.isCheck,
this.shapes,
this.annotations,
});
}) : assert(
(isCheck == null && interactableSide == InteractableSide.none) ||
sideToMove != null,
'sideToMove must be set when isCheck is set, or when the board is interactable.',
);

/// Which color is allowed to move? It can be both, none, white or black
///
Expand All @@ -31,7 +35,7 @@ class BoardData {
final Side orientation;

/// Side which is to move.
final Side sideToMove;
final Side? sideToMove;

/// FEN string describing the position of the board.
final String fen;
Expand All @@ -46,7 +50,7 @@ class BoardData {
final ValidMoves? validMoves;

/// Highlight the king of current side to move
final bool isCheck;
final bool? isCheck;

/// Optional set of [Shape] to be drawn on the board.
final ISet<Shape>? shapes;
Expand Down
8 changes: 4 additions & 4 deletions lib/src/widgets/board.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class _BoardState extends State<Board> {
widget.settings.showValidMoves ? _premoveDests ?? {} : {};
final shapes = widget.data.shapes ?? _emptyShapes;
final annotations = widget.data.annotations ?? _emptyAnnotations;
final checkSquare = widget.data.isCheck ? _getKingSquare() : null;
final checkSquare = widget.data.isCheck == true ? _getKingSquare() : null;
final premove = widget.data.premove;
final Widget board = Stack(
children: [
Expand Down Expand Up @@ -295,12 +295,12 @@ class _BoardState extends State<Board> {
)
else
board,
if (_promotionMove != null)
if (_promotionMove != null && widget.data.sideToMove != null)
PromotionSelector(
pieceAssets: widget.settings.pieceAssets,
move: _promotionMove!,
squareSize: widget.squareSize,
color: widget.data.sideToMove,
color: widget.data.sideToMove!,
orientation: widget.data.orientation,
onSelect: _onPromotionSelect,
onCancel: _onPromotionCancel,
Expand Down Expand Up @@ -384,7 +384,7 @@ class _BoardState extends State<Board> {
_promotionMove = null;
if (widget.onPremove != null &&
widget.data.premove != null &&
widget.data.sideToMove.name == widget.data.interactableSide.name) {
widget.data.sideToMove?.name == widget.data.interactableSide.name) {
Timer.run(() {
if (mounted) _tryPlayPremove();
});
Expand Down
1 change: 0 additions & 1 deletion test/widgets/board_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ void main() {
interactableSide: InteractableSide.none,
orientation: Side.white,
fen: dc.kInitialFEN,
sideToMove: Side.white,
),
),
);
Expand Down

0 comments on commit d5698a9

Please sign in to comment.