2022-11-12 21:55:45 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_svg/svg.dart';
|
2022-11-13 02:24:42 +00:00
|
|
|
import 'package:quiver/core.dart';
|
2022-11-12 21:55:45 +00:00
|
|
|
|
|
|
|
enum ChessPieceName {
|
|
|
|
none,
|
|
|
|
whitePawn,
|
|
|
|
whiteBishop,
|
|
|
|
whiteKnight,
|
|
|
|
whiteRook,
|
|
|
|
whiteQueen,
|
|
|
|
whiteKing,
|
|
|
|
blackPawn,
|
|
|
|
blackBishop,
|
|
|
|
blackKnight,
|
|
|
|
blackRook,
|
|
|
|
blackQueen,
|
|
|
|
blackKing,
|
|
|
|
}
|
|
|
|
|
|
|
|
enum ChessColor { black, white }
|
|
|
|
|
|
|
|
Map<ChessPieceName, String> 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',
|
2022-11-13 02:24:42 +00:00
|
|
|
ChessPieceName.none: 'assets/empty.svg',
|
2022-11-12 21:55:45 +00:00
|
|
|
};
|
|
|
|
|
2023-01-30 21:39:13 +00:00
|
|
|
Map<ChessPieceName, String> 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',
|
|
|
|
};
|
|
|
|
|
2022-11-12 21:55:45 +00:00
|
|
|
class ChessCoordinate {
|
2022-11-13 13:28:09 +00:00
|
|
|
final int column;
|
|
|
|
final int row;
|
2022-11-12 21:55:45 +00:00
|
|
|
|
|
|
|
ChessCoordinate(this.column, this.row);
|
2022-11-13 02:24:42 +00:00
|
|
|
|
2022-12-14 22:17:31 +00:00
|
|
|
factory ChessCoordinate.fromString(String coordString) {
|
|
|
|
var column = int.parse(coordString[0]);
|
|
|
|
var row = int.parse(coordString[1]);
|
|
|
|
|
|
|
|
return ChessCoordinate(column, row);
|
|
|
|
}
|
|
|
|
|
2022-11-13 01:42:10 +00:00
|
|
|
ChessCoordinate.copyFrom(ChessCoordinate original)
|
|
|
|
: column = original.column,
|
|
|
|
row = original.row;
|
2022-11-12 21:55:45 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2022-11-13 02:24:42 +00:00
|
|
|
|
2022-11-13 13:28:09 +00:00
|
|
|
@override
|
|
|
|
String toString() {
|
2022-12-13 02:38:31 +00:00
|
|
|
String rowStr = row.toString();
|
|
|
|
String colStr = column.toString();
|
2022-11-13 13:28:09 +00:00
|
|
|
|
2022-12-13 02:38:31 +00:00
|
|
|
return '$colStr$rowStr';
|
2022-11-13 13:28:09 +00:00
|
|
|
}
|
|
|
|
|
2022-11-13 02:24:42 +00:00
|
|
|
@override
|
|
|
|
operator ==(other) {
|
|
|
|
return other is ChessCoordinate &&
|
|
|
|
other.column == column &&
|
|
|
|
other.row == row;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
int get hashCode {
|
|
|
|
return hash2(column, row);
|
|
|
|
}
|
2022-11-12 21:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class ChessPiece extends StatelessWidget {
|
|
|
|
final ChessColor color;
|
|
|
|
final ChessPieceName pieceName;
|
2023-01-30 21:39:13 +00:00
|
|
|
final String shortName;
|
2022-11-13 01:42:10 +00:00
|
|
|
final Widget? pieceImage;
|
2022-11-12 21:55:45 +00:00
|
|
|
|
2023-01-30 21:39:13 +00:00
|
|
|
const ChessPiece._(
|
|
|
|
this.pieceName, this.color, this.pieceImage, this.shortName);
|
2022-11-13 01:42:10 +00:00
|
|
|
|
|
|
|
factory ChessPiece(ChessPieceName name, ChessColor color) {
|
|
|
|
Widget? pieceImage;
|
|
|
|
String pieceAssetUrl = chessPiecesAssets[name]!;
|
2023-01-30 21:39:13 +00:00
|
|
|
String shortName = chessPiecesShortName[name]!;
|
2022-11-12 21:55:45 +00:00
|
|
|
|
|
|
|
pieceImage = SvgPicture.asset(pieceAssetUrl);
|
2023-01-30 21:39:13 +00:00
|
|
|
return ChessPiece._(name, color, pieceImage, shortName);
|
2022-11-12 21:55:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-13 01:42:10 +00:00
|
|
|
const ChessPiece.none({super.key})
|
|
|
|
: pieceName = ChessPieceName.none,
|
|
|
|
color = ChessColor.white,
|
2023-01-30 21:39:13 +00:00
|
|
|
pieceImage = null,
|
|
|
|
shortName = "-";
|
2022-11-13 01:42:10 +00:00
|
|
|
|
2022-11-12 21:55:45 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return SizedBox(
|
|
|
|
child: pieceImage,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-30 21:39:13 +00:00
|
|
|
class PieceMovedFrom {
|
|
|
|
ChessCoordinate fromSquare;
|
2022-11-12 21:55:45 +00:00
|
|
|
ChessPiece? movedPiece;
|
|
|
|
|
2023-01-30 21:39:13 +00:00
|
|
|
PieceMovedFrom(this.fromSquare, this.movedPiece);
|
2022-11-12 21:55:45 +00:00
|
|
|
}
|