From f5f89a070d01f91a65deb9ed2310dc67775defdf Mon Sep 17 00:00:00 2001 From: Marco Date: Sun, 13 Nov 2022 03:24:42 +0100 Subject: [PATCH] MORE CHANGES. Pieces can be moved now. --- lib/chess_bloc/chess_bloc.dart | 13 ++++++------- lib/chessapp/chess_board.dart | 10 +++------- lib/chessapp/chess_square.dart | 13 +++++++++---- lib/chessapp/chess_utils.dart | 16 +++++++++++++++- pubspec.lock | 8 ++++++++ pubspec.yaml | 1 + 6 files changed, 42 insertions(+), 19 deletions(-) diff --git a/lib/chess_bloc/chess_bloc.dart b/lib/chess_bloc/chess_bloc.dart index aeb88e5..1354a35 100644 --- a/lib/chess_bloc/chess_bloc.dart +++ b/lib/chess_bloc/chess_bloc.dart @@ -29,15 +29,10 @@ class ChessBloc extends Bloc { ) { Map newPosition = {}; - newPosition[ChessCoordinate(5, 5)] = - ChessPiece(ChessPieceName.blackBishop, ChessColor.black); - - ChessPiece? piece = - state.position[ChessCoordinate.copyFrom(event.startSquare)]; - - newPosition[event.endSquare] = piece!; + newPosition[event.endSquare] = state.position[event.startSquare]!; newPosition[event.startSquare] = const ChessPiece.none(); + print('emitting new state with position $newPosition'); emit(ChessBoardState( state.flipped, ChessColor.black, @@ -90,6 +85,10 @@ class ChessBoardState { for (int col = 1; col <= 8; col++) { position[ChessCoordinate(row, col)] = ChessPiece(ChessPieceName.none, ChessColor.white); + if (col == 1 && row == 4) { + position[ChessCoordinate(col, row)] = + ChessPiece(ChessPieceName.blackKing, ChessColor.black); + } } } diff --git a/lib/chessapp/chess_board.dart b/lib/chessapp/chess_board.dart index 4732d29..8ffb47b 100644 --- a/lib/chessapp/chess_board.dart +++ b/lib/chessapp/chess_board.dart @@ -16,9 +16,12 @@ class ChessBoard extends StatelessWidget { var column = (i % 8) + 1; var row = (i ~/ 8) + 1; + final piece = bState.position[ChessCoordinate(column, row)]; + squares.add( ChessSquare( coordinate: ChessCoordinate(column, row), + containedPiece: piece, ), ); } @@ -28,18 +31,11 @@ class ChessBoard extends StatelessWidget { @override Widget build(BuildContext context) { - _placePieces(); - return Column( children: _buildBoard(bState.flipped), ); } - void _placePieces() { - squares[0].containedPiece = - ChessPiece(ChessPieceName.blackBishop, ChessColor.black); - } - Row _buildChessRow(int rowNo, bool flipped) { if (!flipped) { return Row( diff --git a/lib/chessapp/chess_square.dart b/lib/chessapp/chess_square.dart index 59d4bca..7882627 100644 --- a/lib/chessapp/chess_square.dart +++ b/lib/chessapp/chess_square.dart @@ -57,10 +57,15 @@ class ChessSquare extends StatelessWidget { }, onAccept: (move) { move.endSquare = coordinate; - ChessBloc().add(PieceMoved( - startSquare: move.startSquare, - endSquare: move.endSquare, - )); + + if (move.endSquare != move.startSquare) { + ChessBloc().add( + PieceMoved( + startSquare: move.startSquare, + endSquare: move.endSquare, + ), + ); + } }, ); } diff --git a/lib/chessapp/chess_utils.dart b/lib/chessapp/chess_utils.dart index 45ecf51..4d6ab40 100644 --- a/lib/chessapp/chess_utils.dart +++ b/lib/chessapp/chess_utils.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; import 'package:flutter_svg/svg.dart'; +import 'package:quiver/core.dart'; enum ChessPieceName { none, @@ -32,7 +33,7 @@ Map chessPiecesAssets = { ChessPieceName.blackRook: 'assets/pieces/black/rook.svg', ChessPieceName.blackQueen: 'assets/pieces/black/queen.svg', ChessPieceName.blackKing: 'assets/pieces/black/king.svg', - ChessPieceName.none: '', + ChessPieceName.none: 'assets/empty.svg', }; class ChessCoordinate { @@ -40,6 +41,7 @@ class ChessCoordinate { late int row; ChessCoordinate(this.column, this.row); + ChessCoordinate.copyFrom(ChessCoordinate original) : column = original.column, row = original.row; @@ -57,6 +59,18 @@ class ChessCoordinate { colInt = col.codeUnitAt(0) - 96; return colInt; } + + @override + operator ==(other) { + return other is ChessCoordinate && + other.column == column && + other.row == row; + } + + @override + int get hashCode { + return hash2(column, row); + } } class ChessPiece extends StatelessWidget { diff --git a/pubspec.lock b/pubspec.lock index c4be003..8ee2b58 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -187,6 +187,14 @@ packages: url: "https://pub.dev" source: hosted version: "6.0.4" + quiver: + dependency: "direct main" + description: + name: quiver + sha256: "93982981971e812c94d4a6fa3a57b89f9ec12b38b6380cd3c1370c3b01e4580e" + url: "https://pub.dev" + source: hosted + version: "3.1.0" sky_engine: dependency: transitive description: flutter diff --git a/pubspec.yaml b/pubspec.yaml index ee198a1..3a880ac 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -37,6 +37,7 @@ dependencies: # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.2 flutter_bloc: ^8.1.1 + quiver: ^3.1.0 dev_dependencies: flutter_test: