2023-01-30 21:39:13 +00:00
|
|
|
import 'dart:developer';
|
|
|
|
import 'package:mchess/utils/chess_utils.dart';
|
|
|
|
|
|
|
|
typedef ChessPositionType = Map<ChessCoordinate, ChessPiece>;
|
2023-01-30 22:40:46 +00:00
|
|
|
typedef ChessMoveHistory = List<ChessMove>;
|
2023-01-30 21:39:13 +00:00
|
|
|
|
|
|
|
class ChessPosition {
|
2023-06-08 18:23:00 +00:00
|
|
|
static ChessPosition _instance = ChessPosition._internal();
|
|
|
|
static ChessMoveHistory history = ChessMoveHistory.empty(growable: true);
|
2023-06-09 11:19:03 +00:00
|
|
|
final ChessPositionType position;
|
2023-01-30 21:39:13 +00:00
|
|
|
|
|
|
|
static ChessPosition getInstance() {
|
|
|
|
return _instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
ChessPosition({required this.position});
|
|
|
|
|
|
|
|
factory ChessPosition._internal() {
|
2023-06-08 18:23:00 +00:00
|
|
|
return ChessPosition(position: _getStartingPosition());
|
|
|
|
}
|
|
|
|
|
|
|
|
ChessPositionType get currentPosition => position;
|
2023-06-28 10:37:59 +00:00
|
|
|
ChessPositionType get copyOfCurrentPosition => Map.from(position);
|
2023-06-08 18:23:00 +00:00
|
|
|
ChessMove? get lastMove {
|
|
|
|
if (history.isEmpty) return null;
|
|
|
|
return history.last;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ChessPositionType _getStartingPosition() {
|
2023-01-30 21:39:13 +00:00
|
|
|
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);
|
|
|
|
|
2023-06-08 18:23:00 +00:00
|
|
|
return pos;
|
2023-01-30 21:39:13 +00:00
|
|
|
}
|
|
|
|
|
2023-06-08 18:23:00 +00:00
|
|
|
void resetToStartingPosition() {
|
|
|
|
history = ChessMoveHistory.empty(growable: true);
|
|
|
|
_instance = ChessPosition(position: _getStartingPosition());
|
2023-06-02 21:28:40 +00:00
|
|
|
}
|
2023-01-30 21:39:13 +00:00
|
|
|
|
|
|
|
void recordMove(ChessCoordinate from, ChessCoordinate to) {
|
|
|
|
position[to] = position[from] ?? const ChessPiece.none();
|
|
|
|
position[from] = const ChessPiece.none();
|
2023-01-30 22:40:46 +00:00
|
|
|
|
|
|
|
history.add(ChessMove(from: from, to: to));
|
|
|
|
|
2023-01-30 21:39:13 +00:00
|
|
|
logPosition(position);
|
2023-01-30 22:40:46 +00:00
|
|
|
|
|
|
|
logHistory(history);
|
2023-01-30 21:39:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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';
|
|
|
|
}
|
|
|
|
|
2023-06-28 10:37:59 +00:00
|
|
|
print(logString);
|
2023-01-30 21:39:13 +00:00
|
|
|
}
|
2023-01-30 22:40:46 +00:00
|
|
|
|
|
|
|
void logHistory(ChessMoveHistory hist) {
|
2023-05-28 12:56:01 +00:00
|
|
|
for (var element in hist) {
|
2023-01-30 22:40:46 +00:00
|
|
|
log('${element.from.toAlphabetical()} -> ${element.to.toAlphabetical()}');
|
2023-05-28 12:56:01 +00:00
|
|
|
}
|
2023-01-30 22:40:46 +00:00
|
|
|
}
|
2023-01-30 21:39:13 +00:00
|
|
|
}
|