import 'dart:developer'; import 'package:mchess/utils/chess_utils.dart'; typedef ChessPositionType = Map; typedef ChessMoveHistory = List; class ChessPosition { static ChessPosition _instance = ChessPosition._internal(); static ChessMoveHistory history = ChessMoveHistory.empty(growable: true); final ChessPositionType position; static ChessPosition getInstance() { return _instance; } ChessPosition({required this.position}); factory ChessPosition._internal() { return ChessPosition(position: _getStartingPosition()); } ChessPositionType get currentPosition => position; ChessPositionType get copyOfCurrentPosition => Map.from(position); ChessMove? get lastMove { if (history.isEmpty) return null; return history.last; } static ChessPositionType _getStartingPosition() { 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 pos; } void resetToStartingPosition() { history = ChessMoveHistory.empty(growable: true); _instance = ChessPosition(position: _getStartingPosition()); } 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) { for (var element in hist) { log('${element.from.toAlphabetical()} -> ${element.to.toAlphabetical()}'); } } }