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 = {}; Map<ChessCoordinate, ChessPiece> newPosition = {};
newPosition[ChessCoordinate(5, 5)] = newPosition[event.endSquare] = state.position[event.startSquare]!;
ChessPiece(ChessPieceName.blackBishop, ChessColor.black);
ChessPiece? piece =
state.position[ChessCoordinate.copyFrom(event.startSquare)];
newPosition[event.endSquare] = piece!;
newPosition[event.startSquare] = const ChessPiece.none(); newPosition[event.startSquare] = const ChessPiece.none();
print('emitting new state with position $newPosition');
emit(ChessBoardState( emit(ChessBoardState(
state.flipped, state.flipped,
ChessColor.black, ChessColor.black,
@ -90,6 +85,10 @@ class ChessBoardState {
for (int col = 1; col <= 8; col++) { for (int col = 1; col <= 8; col++) {
position[ChessCoordinate(row, col)] = position[ChessCoordinate(row, col)] =
ChessPiece(ChessPieceName.none, ChessColor.white); 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 column = (i % 8) + 1;
var row = (i ~/ 8) + 1; var row = (i ~/ 8) + 1;
final piece = bState.position[ChessCoordinate(column, row)];
squares.add( squares.add(
ChessSquare( ChessSquare(
coordinate: ChessCoordinate(column, row), coordinate: ChessCoordinate(column, row),
containedPiece: piece,
), ),
); );
} }
@ -28,18 +31,11 @@ class ChessBoard extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
_placePieces();
return Column( return Column(
children: _buildBoard(bState.flipped), children: _buildBoard(bState.flipped),
); );
} }
void _placePieces() {
squares[0].containedPiece =
ChessPiece(ChessPieceName.blackBishop, ChessColor.black);
}
Row _buildChessRow(int rowNo, bool flipped) { Row _buildChessRow(int rowNo, bool flipped) {
if (!flipped) { if (!flipped) {
return Row( return Row(

View File

@ -57,10 +57,15 @@ class ChessSquare extends StatelessWidget {
}, },
onAccept: (move) { onAccept: (move) {
move.endSquare = coordinate; move.endSquare = coordinate;
ChessBloc().add(PieceMoved(
if (move.endSquare != move.startSquare) {
ChessBloc().add(
PieceMoved(
startSquare: move.startSquare, startSquare: move.startSquare,
endSquare: move.endSquare, endSquare: move.endSquare,
)); ),
);
}
}, },
); );
} }

View File

@ -1,5 +1,6 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart'; import 'package:flutter_svg/svg.dart';
import 'package:quiver/core.dart';
enum ChessPieceName { enum ChessPieceName {
none, none,
@ -32,7 +33,7 @@ Map<ChessPieceName, String> chessPiecesAssets = {
ChessPieceName.blackRook: 'assets/pieces/black/rook.svg', ChessPieceName.blackRook: 'assets/pieces/black/rook.svg',
ChessPieceName.blackQueen: 'assets/pieces/black/queen.svg', ChessPieceName.blackQueen: 'assets/pieces/black/queen.svg',
ChessPieceName.blackKing: 'assets/pieces/black/king.svg', ChessPieceName.blackKing: 'assets/pieces/black/king.svg',
ChessPieceName.none: '', ChessPieceName.none: 'assets/empty.svg',
}; };
class ChessCoordinate { class ChessCoordinate {
@ -40,6 +41,7 @@ class ChessCoordinate {
late int row; late int row;
ChessCoordinate(this.column, this.row); ChessCoordinate(this.column, this.row);
ChessCoordinate.copyFrom(ChessCoordinate original) ChessCoordinate.copyFrom(ChessCoordinate original)
: column = original.column, : column = original.column,
row = original.row; row = original.row;
@ -57,6 +59,18 @@ class ChessCoordinate {
colInt = col.codeUnitAt(0) - 96; colInt = col.codeUnitAt(0) - 96;
return colInt; 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 { class ChessPiece extends StatelessWidget {

View File

@ -187,6 +187,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "6.0.4" 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: sky_engine:
dependency: transitive dependency: transitive
description: flutter description: flutter

View File

@ -37,6 +37,7 @@ dependencies:
# Use with the CupertinoIcons class for iOS style icons. # Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2 cupertino_icons: ^1.0.2
flutter_bloc: ^8.1.1 flutter_bloc: ^8.1.1
quiver: ^3.1.0
dev_dependencies: dev_dependencies:
flutter_test: flutter_test: