Compare commits

...

1 Commits

Author SHA1 Message Date
f5759cfbe6 Groundwork for move history 2023-09-04 22:00:58 +02:00
4 changed files with 124 additions and 47 deletions

View File

@ -23,6 +23,7 @@ class ChessPositionManager {
if (history.isEmpty) return null; if (history.isEmpty) return null;
return history.last; return history.last;
} }
ChessMoveHistory get allMoves => history;
ChessPosition fromPGNString(String pgn) { ChessPosition fromPGNString(String pgn) {
ChessPosition pos = {}; ChessPosition pos = {};

View File

@ -6,6 +6,7 @@ import 'package:mchess/chess_bloc/chess_bloc.dart';
import 'package:mchess/chess/chess_board.dart'; import 'package:mchess/chess/chess_board.dart';
import 'package:mchess/chess_bloc/promotion_bloc.dart'; import 'package:mchess/chess_bloc/promotion_bloc.dart';
import 'package:mchess/utils/chess_utils.dart'; import 'package:mchess/utils/chess_utils.dart';
import 'package:mchess/utils/widgets/move_history_widget.dart';
import 'package:mchess/utils/widgets/promotion_dialog.dart'; import 'package:mchess/utils/widgets/promotion_dialog.dart';
import 'package:uuid/uuid.dart'; import 'package:uuid/uuid.dart';
@ -43,8 +44,23 @@ class _ChessGameState extends State<ChessGame> {
}, },
child: BlocBuilder<ChessBloc, ChessBoardState>( child: BlocBuilder<ChessBloc, ChessBoardState>(
builder: (context, state) { builder: (context, state) {
return ChessBoard( return Row(
boardState: state, children: [
Expanded(
flex: 1,
child: Container(),
),
Expanded(
flex: 3,
child: ChessBoard(boardState: state),
),
const Expanded(
flex: 1,
child: Align(
alignment: Alignment.centerLeft,
child: MoveHistory()),
),
],
); );
}, },
), ),

View File

@ -115,30 +115,54 @@ Map<ChessPieceAssetKey, String> chessPiecesShortName = {
}; };
Map<ChessPieceAssetKey, String> pieceCharacter = { Map<ChessPieceAssetKey, String> pieceCharacter = {
ChessPieceAssetKey(pieceClass: ChessPieceClass.pawn, color: ChessColor.white):
"",
ChessPieceAssetKey(pieceClass: ChessPieceClass.rook, color: ChessColor.white):
"",
ChessPieceAssetKey( ChessPieceAssetKey(
pieceClass: ChessPieceClass.knight, color: ChessColor.white): "", pieceClass: ChessPieceClass.pawn,
color: ChessColor.white,
): "",
ChessPieceAssetKey( ChessPieceAssetKey(
pieceClass: ChessPieceClass.bishop, color: ChessColor.white): "", pieceClass: ChessPieceClass.rook,
ChessPieceAssetKey(pieceClass: ChessPieceClass.king, color: ChessColor.white): color: ChessColor.white,
"", ): "",
ChessPieceAssetKey( ChessPieceAssetKey(
pieceClass: ChessPieceClass.queen, color: ChessColor.white): "", pieceClass: ChessPieceClass.knight,
ChessPieceAssetKey(pieceClass: ChessPieceClass.pawn, color: ChessColor.black): color: ChessColor.white,
"♟︎", ): "",
ChessPieceAssetKey(pieceClass: ChessPieceClass.rook, color: ChessColor.black):
"",
ChessPieceAssetKey( ChessPieceAssetKey(
pieceClass: ChessPieceClass.knight, color: ChessColor.black): "", pieceClass: ChessPieceClass.bishop,
color: ChessColor.white,
): "",
ChessPieceAssetKey( ChessPieceAssetKey(
pieceClass: ChessPieceClass.bishop, color: ChessColor.black): "", pieceClass: ChessPieceClass.king,
ChessPieceAssetKey(pieceClass: ChessPieceClass.king, color: ChessColor.black): color: ChessColor.white,
"", ): "",
ChessPieceAssetKey( ChessPieceAssetKey(
pieceClass: ChessPieceClass.queen, color: ChessColor.black): "", pieceClass: ChessPieceClass.queen,
color: ChessColor.white,
): "",
ChessPieceAssetKey(
pieceClass: ChessPieceClass.pawn,
color: ChessColor.black,
): "♟︎",
ChessPieceAssetKey(
pieceClass: ChessPieceClass.rook,
color: ChessColor.black,
): "",
ChessPieceAssetKey(
pieceClass: ChessPieceClass.knight,
color: ChessColor.black,
): "",
ChessPieceAssetKey(
pieceClass: ChessPieceClass.bishop,
color: ChessColor.black,
): "",
ChessPieceAssetKey(
pieceClass: ChessPieceClass.king,
color: ChessColor.black,
): "",
ChessPieceAssetKey(
pieceClass: ChessPieceClass.queen,
color: ChessColor.black,
): "",
}; };
Map<String, ChessPiece> pieceFromShortname = { Map<String, ChessPiece> pieceFromShortname = {

View File

@ -12,42 +12,78 @@ class MoveHistory extends StatefulWidget {
} }
class _MoveHistoryState extends State<MoveHistory> { class _MoveHistoryState extends State<MoveHistory> {
late List<String> entries; ChessMoveHistory allMoves = [];
List<HistoryEntry> entries = [];
@override @override
void initState() { void initState() {
entries = [];
super.initState(); super.initState();
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return BlocListener<ChessBloc, ChessBoardState>( return BlocBuilder<ChessBloc, ChessBoardState>(builder: (context, state) {
listener: (context, state) { //We do not use the state, we just use the ChessBloc as a trigger
List<String> newEntries = [];
var positionManager = ChessPositionManager.getInstance(); var positionManager = ChessPositionManager.getInstance();
var allMoves = positionManager.allMoves; if (positionManager.lastMove == null) return Container();
var pieceMoved = positionManager.getPieceAt(positionManager.lastMove!.to);
var pieceChar = pieceCharacter[ChessPieceAssetKey(
//we take the opposite color because we use dark theme. White pieces appear black and vice versa.
pieceClass: pieceMoved!.pieceClass,
color: pieceMoved.color.getOpposite())];
for (ChessMove move in allMoves) { if (pieceMoved.color == ChessColor.white) {
var movedPiece = positionManager.getPieceAt(move.to); var entry = HistoryEntry();
var char = pieceCharacter[ChessPieceAssetKey( entry.setWhite(
pieceClass: movedPiece!.pieceClass, pieceChar!, positionManager.lastMove!.to.toAlphabetical());
color: movedPiece.color.getOpposite())]; entries.add(entry);
if (movedPiece.color == ChessColor.white) {
newEntries.add("$char ${move.to.toAlphabetical()}");
} else { } else {
newEntries.last = var entry = entries.last;
"${newEntries.last}\t\t$char${move.to.toAlphabetical()}"; entry.setBlack(
pieceChar!, positionManager.lastMove!.to.toAlphabetical());
} }
}
setState(() { return ListView.builder(
entries = newEntries; itemCount: entries.length,
}); itemBuilder: (context, index) {
}, return Text(
child: ListView(children: [ entries[index].toString(),
for (var entry in entries) Text(entry), style: const TextStyle(fontSize: 20),
]),
); );
},
);
});
}
}
class HistoryEntry {
String? wChar;
String? wTo;
String? bChar;
String? bTo;
HistoryEntry();
void setWhite(String char, to) {
wChar = char;
wTo = to;
}
void setBlack(String char, to) {
bChar = char;
bTo = to;
}
@override
String toString() {
String entry = "";
if (wChar != null) {
entry = "$entry$wChar$wTo";
}
if (bChar != null) {
entry = "$entry\t\t$bChar$bTo";
}
return entry;
} }
} }