mchess-client/lib/utils/chess_utils.dart

299 lines
7.0 KiB
Dart
Raw Normal View History

2023-07-01 07:29:43 +00:00
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
2023-06-02 21:28:40 +00:00
import 'package:mchess/api/move.dart';
2023-06-08 15:10:48 +00:00
import 'package:mchess/api/websocket_message.dart';
2022-11-13 02:24:42 +00:00
import 'package:quiver/core.dart';
2023-07-01 07:29:43 +00:00
enum ChessPieceClass {
none,
2023-07-01 07:29:43 +00:00
pawn,
bishop,
knight,
rook,
queen,
king,
}
class ChessPieceAssetKey {
final ChessPieceClass pieceClass;
final ChessColor color;
ChessPieceAssetKey({required this.pieceClass, required this.color});
@override
bool operator ==(Object other) {
return (other is ChessPieceAssetKey &&
(pieceClass == other.pieceClass) &&
(color == other.color));
}
@override
int get hashCode {
return hash2(pieceClass, color);
}
}
2023-06-08 15:10:48 +00:00
enum ChessColor {
black,
white;
static ChessColor fromApiColor(ApiColor color) {
if (color == ApiColor.black) {
return black;
} else {
return white;
}
}
}
2023-07-01 07:29:43 +00:00
Map<ChessPieceAssetKey, String> chessPiecesAssets = {
ChessPieceAssetKey(
pieceClass: ChessPieceClass.pawn,
color: ChessColor.white,
): 'assets/pieces/white/pawn.svg',
ChessPieceAssetKey(
pieceClass: ChessPieceClass.bishop,
color: ChessColor.white,
): 'assets/pieces/white/bishop.svg',
ChessPieceAssetKey(
pieceClass: ChessPieceClass.knight,
color: ChessColor.white,
): 'assets/pieces/white/knight.svg',
ChessPieceAssetKey(
pieceClass: ChessPieceClass.rook,
color: ChessColor.white,
): 'assets/pieces/white/rook.svg',
ChessPieceAssetKey(
pieceClass: ChessPieceClass.queen,
color: ChessColor.white,
): 'assets/pieces/white/queen.svg',
ChessPieceAssetKey(
pieceClass: ChessPieceClass.king,
color: ChessColor.white,
): 'assets/pieces/white/king.svg',
ChessPieceAssetKey(
pieceClass: ChessPieceClass.pawn,
color: ChessColor.black,
): 'assets/pieces/black/pawn.svg',
ChessPieceAssetKey(
pieceClass: ChessPieceClass.bishop,
color: ChessColor.black,
): 'assets/pieces/black/bishop.svg',
ChessPieceAssetKey(
pieceClass: ChessPieceClass.knight,
color: ChessColor.black,
): 'assets/pieces/black/knight.svg',
ChessPieceAssetKey(
pieceClass: ChessPieceClass.rook,
color: ChessColor.black,
): 'assets/pieces/black/rook.svg',
ChessPieceAssetKey(
pieceClass: ChessPieceClass.queen,
color: ChessColor.black,
): 'assets/pieces/black/queen.svg',
ChessPieceAssetKey(
pieceClass: ChessPieceClass.king,
color: ChessColor.black,
): 'assets/pieces/black/king.svg',
ChessPieceAssetKey(
pieceClass: ChessPieceClass.none,
color: ChessColor.black,
): 'assets/empty.svg',
};
2023-07-01 07:29:43 +00:00
Map<ChessPieceAssetKey, String> chessPiecesShortName = {
ChessPieceAssetKey(
pieceClass: ChessPieceClass.pawn,
color: ChessColor.white,
): 'P',
ChessPieceAssetKey(
pieceClass: ChessPieceClass.bishop,
color: ChessColor.white,
): 'B',
ChessPieceAssetKey(
pieceClass: ChessPieceClass.knight,
color: ChessColor.white,
): 'N',
ChessPieceAssetKey(
pieceClass: ChessPieceClass.rook,
color: ChessColor.white,
): 'R',
ChessPieceAssetKey(
pieceClass: ChessPieceClass.queen,
color: ChessColor.white,
): 'Q',
ChessPieceAssetKey(
pieceClass: ChessPieceClass.king,
color: ChessColor.white,
): 'K',
ChessPieceAssetKey(
pieceClass: ChessPieceClass.pawn,
color: ChessColor.black,
): 'p',
ChessPieceAssetKey(
pieceClass: ChessPieceClass.bishop,
color: ChessColor.black,
): 'b',
ChessPieceAssetKey(
pieceClass: ChessPieceClass.knight,
color: ChessColor.black,
): 'n',
ChessPieceAssetKey(
pieceClass: ChessPieceClass.rook,
color: ChessColor.black,
): 'r',
ChessPieceAssetKey(
pieceClass: ChessPieceClass.queen,
color: ChessColor.black,
): 'q',
ChessPieceAssetKey(
pieceClass: ChessPieceClass.king,
color: ChessColor.black,
): 'k',
ChessPieceAssetKey(
pieceClass: ChessPieceClass.none,
color: ChessColor.black,
): '-',
};
class ChessCoordinate {
final int column;
final int row;
ChessCoordinate(this.column, this.row);
2022-11-13 02:24:42 +00:00
factory ChessCoordinate.fromString(String coordString) {
var column = int.parse(coordString[0]);
var row = int.parse(coordString[1]);
return ChessCoordinate(column, row);
}
2023-06-02 21:28:40 +00:00
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;
}
2022-11-13 02:24:42 +00:00
@override
String toString() {
2022-12-13 02:38:31 +00:00
String rowStr = row.toString();
String colStr = column.toString();
2022-12-13 02:38:31 +00:00
return '$colStr$rowStr';
}
String toAlphabetical() {
Map<int, String> 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';
}
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);
}
}
class ChessPiece extends StatelessWidget {
final ChessColor color;
2023-07-01 07:29:43 +00:00
final ChessPieceClass pieceClass;
final String shortName;
final Widget? pieceImage;
const ChessPiece._(
2023-07-01 07:29:43 +00:00
this.pieceClass, this.color, this.pieceImage, this.shortName);
2023-07-01 07:29:43 +00:00
factory ChessPiece(ChessPieceClass pieceClass, ChessColor color) {
Widget? pieceImage;
2023-07-01 07:29:43 +00:00
String pieceAssetUrl = chessPiecesAssets[
ChessPieceAssetKey(pieceClass: pieceClass, color: color)]!;
String shortName = chessPiecesShortName[
ChessPieceAssetKey(pieceClass: pieceClass, color: color)]!;
pieceImage = SvgPicture.asset(pieceAssetUrl);
2023-07-01 07:29:43 +00:00
return ChessPiece._(pieceClass, color, pieceImage, shortName);
}
const ChessPiece.none({super.key})
2023-07-01 07:29:43 +00:00
: pieceClass = ChessPieceClass.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});
2023-06-02 21:28:40 +00:00
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);
}
}
2023-07-01 07:29:43 +00:00
class PieceDragged {
ChessCoordinate fromSquare;
2023-07-01 07:29:43 +00:00
ChessCoordinate toSquare;
ChessPiece? movedPiece;
2023-07-01 07:29:43 +00:00
PieceDragged(this.fromSquare, this.toSquare, this.movedPiece);
}