import 'package:flutter/material.dart'; import 'package:flutter_svg/svg.dart'; import 'package:mchess/api/move.dart'; import 'package:quiver/core.dart'; enum ChessPieceName { none, whitePawn, whiteBishop, whiteKnight, whiteRook, whiteQueen, whiteKing, blackPawn, blackBishop, blackKnight, blackRook, blackQueen, blackKing, } enum ChessColor { black, white } Map chessPiecesAssets = { ChessPieceName.whitePawn: 'assets/pieces/white/pawn.svg', ChessPieceName.whiteBishop: 'assets/pieces/white/bishop.svg', ChessPieceName.whiteKnight: 'assets/pieces/white/knight.svg', ChessPieceName.whiteRook: 'assets/pieces/white/rook.svg', ChessPieceName.whiteQueen: 'assets/pieces/white/queen.svg', ChessPieceName.whiteKing: 'assets/pieces/white/king.svg', ChessPieceName.blackPawn: 'assets/pieces/black/pawn.svg', ChessPieceName.blackBishop: 'assets/pieces/black/bishop.svg', ChessPieceName.blackKnight: 'assets/pieces/black/knight.svg', ChessPieceName.blackRook: 'assets/pieces/black/rook.svg', ChessPieceName.blackQueen: 'assets/pieces/black/queen.svg', ChessPieceName.blackKing: 'assets/pieces/black/king.svg', ChessPieceName.none: 'assets/empty.svg', }; Map chessPiecesShortName = { ChessPieceName.whitePawn: 'P', ChessPieceName.whiteBishop: 'B', ChessPieceName.whiteKnight: 'N', ChessPieceName.whiteRook: 'R', ChessPieceName.whiteQueen: 'Q', ChessPieceName.whiteKing: 'K', ChessPieceName.blackPawn: 'p', ChessPieceName.blackBishop: 'b', ChessPieceName.blackKnight: 'n', ChessPieceName.blackRook: 'r', ChessPieceName.blackQueen: 'q', ChessPieceName.blackKing: 'k', ChessPieceName.none: 'X', }; class ChessCoordinate { final int column; final int row; ChessCoordinate(this.column, this.row); factory ChessCoordinate.fromString(String coordString) { var column = int.parse(coordString[0]); var row = int.parse(coordString[1]); return ChessCoordinate(column, row); } factory ChessCoordinate.fromApiCoordinate(ApiCoordinate apiCoordinate) { return ChessCoordinate(apiCoordinate.col, apiCoordinate.row); } ChessCoordinate.copyFrom(ChessCoordinate original) : column = original.column, row = original.row; static String columnIntToColumnString(int col) { String colStr; colStr = String.fromCharCode(col + 96); return colStr; } static int columnStringToColumnInt(String col) { int colInt; colInt = col.codeUnitAt(0) - 96; return colInt; } @override String toString() { String rowStr = row.toString(); String colStr = column.toString(); return '$colStr$rowStr'; } String toAlphabetical() { Map columnMap = { 1: "a", 2: "b", 3: "c", 4: "d", 5: "e", 6: "f", 7: "g", 8: "h" }; String rowStr = row.toString(); String colStr = columnMap[column]!; return '$colStr$rowStr'; } @override operator ==(other) { return other is ChessCoordinate && other.column == column && other.row == row; } @override int get hashCode { return hash2(column, row); } } class ChessPiece extends StatelessWidget { final ChessColor color; final ChessPieceName pieceName; final String shortName; final Widget? pieceImage; const ChessPiece._( this.pieceName, this.color, this.pieceImage, this.shortName); factory ChessPiece(ChessPieceName name, ChessColor color) { Widget? pieceImage; String pieceAssetUrl = chessPiecesAssets[name]!; String shortName = chessPiecesShortName[name]!; pieceImage = SvgPicture.asset(pieceAssetUrl); return ChessPiece._(name, color, pieceImage, shortName); } const ChessPiece.none({super.key}) : pieceName = ChessPieceName.none, color = ChessColor.white, pieceImage = null, shortName = "-"; @override Widget build(BuildContext context) { return SizedBox( child: pieceImage, ); } } class ChessMove { ChessCoordinate from; ChessCoordinate to; ChessMove({required this.from, required this.to}); factory ChessMove.fromApiMove(ApiMove apiMove) { final start = ChessCoordinate.fromApiCoordinate(apiMove.startSquare); final end = ChessCoordinate.fromApiCoordinate(apiMove.endSquare); return ChessMove(from: start, to: end); } @override operator ==(other) { return other is ChessMove && other.from == from && other.to == to; } @override int get hashCode { return hash2(from, to); } } class PieceMovedFrom { ChessCoordinate fromSquare; ChessPiece? movedPiece; PieceMovedFrom(this.fromSquare, this.movedPiece); }