2022-12-13 02:43:05 +00:00
|
|
|
import 'dart:developer';
|
2023-08-22 02:24:30 +00:00
|
|
|
import 'dart:math' as math;
|
2022-12-13 02:43:05 +00:00
|
|
|
|
2022-11-12 21:55:45 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2022-11-13 00:03:47 +00:00
|
|
|
import 'package:mchess/chess_bloc/chess_bloc.dart';
|
2022-11-12 21:55:45 +00:00
|
|
|
|
|
|
|
import 'chess_square.dart';
|
2022-12-25 15:16:23 +00:00
|
|
|
import '../utils/chess_utils.dart';
|
2022-11-12 21:55:45 +00:00
|
|
|
|
2022-11-13 00:03:47 +00:00
|
|
|
class ChessBoard extends StatelessWidget {
|
|
|
|
final ChessBoardState bState;
|
|
|
|
final List<ChessSquare> squares;
|
2022-11-12 21:55:45 +00:00
|
|
|
|
2022-11-13 01:42:10 +00:00
|
|
|
const ChessBoard._({required this.bState, required this.squares});
|
2022-11-12 21:55:45 +00:00
|
|
|
|
2024-01-05 21:59:31 +00:00
|
|
|
factory ChessBoard(
|
|
|
|
{required ChessBoardState boardState, ChessCoordinate? tappedSquare}) {
|
2022-11-13 00:03:47 +00:00
|
|
|
List<ChessSquare> squares = List.empty(growable: true);
|
2022-11-12 21:55:45 +00:00
|
|
|
for (int i = 0; i < 64; i++) {
|
|
|
|
var column = (i % 8) + 1;
|
|
|
|
var row = (i ~/ 8) + 1;
|
|
|
|
|
2023-08-19 01:45:03 +00:00
|
|
|
final piece = boardState.position[ChessCoordinate(column, row)];
|
|
|
|
|
|
|
|
bool squareWasPartOfLastMove = false;
|
2023-08-25 10:28:58 +00:00
|
|
|
if ((boardState.lastMove.to == ChessCoordinate(column, row) ||
|
|
|
|
boardState.lastMove.from == ChessCoordinate(column, row)) &&
|
2024-01-09 18:07:36 +00:00
|
|
|
boardState.colorLastMove) {
|
2023-08-19 01:45:03 +00:00
|
|
|
squareWasPartOfLastMove = true;
|
|
|
|
}
|
2022-11-13 02:24:42 +00:00
|
|
|
|
2022-11-12 21:55:45 +00:00
|
|
|
squares.add(
|
|
|
|
ChessSquare(
|
2024-01-05 21:59:31 +00:00
|
|
|
ChessCoordinate(column, row),
|
|
|
|
piece,
|
|
|
|
squareWasPartOfLastMove,
|
|
|
|
tappedSquare == ChessCoordinate(column, row)),
|
2022-11-12 21:55:45 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-08-19 01:45:03 +00:00
|
|
|
return ChessBoard._(bState: boardState, squares: squares);
|
2022-11-13 00:03:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-12-13 02:43:05 +00:00
|
|
|
log("ChessBoard's build()");
|
2022-12-18 00:04:08 +00:00
|
|
|
|
2023-08-22 02:24:30 +00:00
|
|
|
return LayoutBuilder(builder: (context, constraints) {
|
|
|
|
/*We calculate the margins manually, because otherwise we would have
|
|
|
|
lines that are 1 pixel wide between the squares (which is ugly)*/
|
|
|
|
int smallerSize =
|
|
|
|
math.min(constraints.maxWidth.toInt(), constraints.maxHeight.toInt());
|
|
|
|
int desiredSize = smallerSize - smallerSize % 8;
|
|
|
|
int verticalMargin = constraints.maxHeight.toInt() - desiredSize;
|
|
|
|
int verticalMarginTop = verticalMargin ~/ 2;
|
|
|
|
int verticalMarginBottom = verticalMargin - verticalMarginTop;
|
|
|
|
int horizontalMargin = constraints.maxWidth.toInt() - desiredSize;
|
|
|
|
int horizontalMarginLeft = horizontalMargin ~/ 2;
|
|
|
|
int horizontalMarginRight = horizontalMargin - horizontalMarginLeft;
|
|
|
|
return Container(
|
|
|
|
margin: EdgeInsets.only(
|
|
|
|
left: horizontalMarginLeft.toDouble(),
|
|
|
|
right: horizontalMarginRight.toDouble(),
|
|
|
|
top: verticalMarginTop.toDouble(),
|
|
|
|
bottom: verticalMarginBottom.toDouble(),
|
|
|
|
),
|
|
|
|
child: FittedBox(
|
|
|
|
fit: BoxFit.contain,
|
|
|
|
child: Container(
|
|
|
|
decoration: const BoxDecoration(boxShadow: [
|
|
|
|
BoxShadow(color: Colors.black, offset: Offset(10, 10))
|
|
|
|
]),
|
|
|
|
child: _buildBoard(bState.bottomColor == ChessColor.black),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
});
|
2022-11-12 21:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Row _buildChessRow(int rowNo, bool flipped) {
|
|
|
|
if (!flipped) {
|
|
|
|
return Row(
|
|
|
|
children: [
|
|
|
|
for (int i = 8 * rowNo - 8; i < 8 * rowNo; i++) squares.elementAt(i)
|
|
|
|
],
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return Row(
|
|
|
|
children: [
|
|
|
|
for (int i = 8 * rowNo - 1; i >= 8 * rowNo - 8; i--)
|
|
|
|
squares.elementAt(i)
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-12 09:43:51 +00:00
|
|
|
Column _buildBoard(bool flipped) {
|
2022-11-12 21:55:45 +00:00
|
|
|
List<Row> chessBoard = <Row>[];
|
|
|
|
|
|
|
|
if (!flipped) {
|
|
|
|
for (int row = 8; row > 0; row--) {
|
|
|
|
chessBoard.add(_buildChessRow(row, flipped));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int row = 1; row <= 8; row++) {
|
|
|
|
chessBoard.add(_buildChessRow(row, flipped));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-12 09:43:51 +00:00
|
|
|
return Column(children: [...chessBoard]);
|
2022-11-12 21:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ChessSquare getSquareAtCoordinate(ChessCoordinate coord) {
|
|
|
|
/* Calculate index in squares[] from column and row */
|
|
|
|
int index = (coord.row - 1) * 8 + (coord.column - 1);
|
|
|
|
|
2022-12-13 02:43:05 +00:00
|
|
|
log("getSquareAtCoordinates: index calculated to $index");
|
2022-11-12 21:55:45 +00:00
|
|
|
|
|
|
|
return squares.elementAt(index);
|
|
|
|
}
|
|
|
|
}
|