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(ChessPieceClass.pawn, ChessColor.black); pos[ChessCoordinate(i, 2)] = ChessPiece(ChessPieceClass.pawn, ChessColor.white); } pos[ChessCoordinate(1, 7)] = ChessPiece(ChessPieceClass.pawn, ChessColor.white); pos[ChessCoordinate(2, 7)] = ChessPiece(ChessPieceClass.pawn, ChessColor.white); pos[ChessCoordinate(8, 4)] = ChessPiece(ChessPieceClass.king, ChessColor.white); pos[ChessCoordinate(8, 6)] = ChessPiece(ChessPieceClass.king, ChessColor.black); pos[ChessCoordinate(1, 8)] = ChessPiece(ChessPieceClass.rook, ChessColor.black); pos[ChessCoordinate(2, 8)] = ChessPiece(ChessPieceClass.knight, ChessColor.black); pos[ChessCoordinate(3, 8)] = ChessPiece(ChessPieceClass.bishop, ChessColor.black); pos[ChessCoordinate(4, 8)] = ChessPiece(ChessPieceClass.queen, ChessColor.black); pos[ChessCoordinate(5, 8)] = ChessPiece(ChessPieceClass.king, ChessColor.black); pos[ChessCoordinate(6, 8)] = ChessPiece(ChessPieceClass.bishop, ChessColor.black); pos[ChessCoordinate(7, 8)] = ChessPiece(ChessPieceClass.knight, ChessColor.black); pos[ChessCoordinate(8, 8)] = ChessPiece(ChessPieceClass.rook, ChessColor.black); pos[ChessCoordinate(1, 1)] = ChessPiece(ChessPieceClass.rook, ChessColor.white); pos[ChessCoordinate(2, 1)] = ChessPiece(ChessPieceClass.knight, ChessColor.white); pos[ChessCoordinate(3, 1)] = ChessPiece(ChessPieceClass.bishop, ChessColor.white); pos[ChessCoordinate(4, 1)] = ChessPiece(ChessPieceClass.queen, ChessColor.white); pos[ChessCoordinate(5, 1)] = ChessPiece(ChessPieceClass.king, ChessColor.white); pos[ChessCoordinate(6, 1)] = ChessPiece(ChessPieceClass.bishop, ChessColor.white); pos[ChessCoordinate(7, 1)] = ChessPiece(ChessPieceClass.knight, ChessColor.white); pos[ChessCoordinate(8, 1)] = ChessPiece(ChessPieceClass.rook, ChessColor.white); return pos; } ChessPiece? getPieceAt(ChessCoordinate coordinate) { return position[ChessCoordinate(coordinate.column, coordinate.row)]; } 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()}'); } } }