MORE CHANGES. Pieces can be moved now.

This commit is contained in:
Marco 2022-11-13 03:24:42 +01:00
parent b3a7418c5f
commit f5f89a070d
6 changed files with 42 additions and 19 deletions

View File

@ -29,15 +29,10 @@ class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
) {
Map<ChessCoordinate, ChessPiece> 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);
}
}
}

View File

@ -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(

View File

@ -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,
),
);
}
},
);
}

View File

@ -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<ChessPieceName, String> 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 {

View File

@ -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

View File

@ -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: