mchess-client/lib/chess_bloc/chess_position.dart

106 lines
3.4 KiB
Dart

import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:mchess/utils/chess_utils.dart';
typedef ChessPositionType = Map<ChessCoordinate, ChessPiece>;
typedef ChessMoveHistory = List<ChessMove>;
class ChessPosition {
static final ChessPosition _instance = ChessPosition._internal();
final ChessPositionType position;
ChessMoveHistory history = ChessMoveHistory.empty(growable: true);
static ChessPosition getInstance() {
return _instance;
}
ChessPosition({required this.position});
factory ChessPosition._internal() {
ChessPositionType pos = {};
for (int i = 1; i <= 8; i++) {
pos[ChessCoordinate(i, 7)] =
ChessPiece(ChessPieceName.blackPawn, ChessColor.black);
pos[ChessCoordinate(i, 2)] =
ChessPiece(ChessPieceName.whitePawn, ChessColor.white);
}
pos[ChessCoordinate(1, 8)] =
ChessPiece(ChessPieceName.blackRook, ChessColor.black);
pos[ChessCoordinate(2, 8)] =
ChessPiece(ChessPieceName.blackKnight, ChessColor.black);
pos[ChessCoordinate(3, 8)] =
ChessPiece(ChessPieceName.blackBishop, ChessColor.black);
pos[ChessCoordinate(4, 8)] =
ChessPiece(ChessPieceName.blackQueen, ChessColor.black);
pos[ChessCoordinate(5, 8)] =
ChessPiece(ChessPieceName.blackKing, ChessColor.black);
pos[ChessCoordinate(6, 8)] =
ChessPiece(ChessPieceName.blackBishop, ChessColor.black);
pos[ChessCoordinate(7, 8)] =
ChessPiece(ChessPieceName.blackKnight, ChessColor.black);
pos[ChessCoordinate(8, 8)] =
ChessPiece(ChessPieceName.blackRook, ChessColor.black);
pos[ChessCoordinate(1, 1)] =
ChessPiece(ChessPieceName.whiteRook, ChessColor.white);
pos[ChessCoordinate(2, 1)] =
ChessPiece(ChessPieceName.whiteKnight, ChessColor.white);
pos[ChessCoordinate(3, 1)] =
ChessPiece(ChessPieceName.whiteBishop, ChessColor.white);
pos[ChessCoordinate(4, 1)] =
ChessPiece(ChessPieceName.whiteQueen, ChessColor.white);
pos[ChessCoordinate(5, 1)] =
ChessPiece(ChessPieceName.whiteKing, ChessColor.white);
pos[ChessCoordinate(6, 1)] =
ChessPiece(ChessPieceName.whiteBishop, ChessColor.white);
pos[ChessCoordinate(7, 1)] =
ChessPiece(ChessPieceName.whiteKnight, ChessColor.white);
pos[ChessCoordinate(8, 1)] =
ChessPiece(ChessPieceName.whiteRook, ChessColor.white);
return ChessPosition(position: pos);
}
ChessPositionType get currentPosition => position;
ChessMove get lastMove => history.last;
void recordMove(ChessCoordinate from, ChessCoordinate to) {
position[to] = position[from] ?? const ChessPiece.none();
position[from] = const ChessPiece.none();
history.add(ChessMove(from: from, to: to));
logPosition(position);
logHistory(history);
}
void logPosition(ChessPositionType p) {
String logString = '';
for (int row = 8; row > 0; row--) {
for (int col = 1; col <= 8; col++) {
var coord = ChessCoordinate(col, row);
if (p.containsKey(coord)) {
logString = '$logString ${p[coord]?.shortName}';
} else {
logString = '$logString .';
}
}
logString = '$logString\n';
}
log(logString);
}
void logHistory(ChessMoveHistory hist) {
hist.forEach((element) {
log('${element.from.toAlphabetical()} -> ${element.to.toAlphabetical()}');
});
}
}