mchess-client/lib/chessapp/chess_board.dart

84 lines
1.8 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
import 'chess_square.dart';
import 'chess_utils.dart';
class ChessBoard extends StatefulWidget {
final bool flipped;
const ChessBoard({required this.flipped, super.key});
@override
State<StatefulWidget> createState() => ChessBoardState();
}
class ChessBoardState extends State<ChessBoard> {
var squares = <ChessSquare>[];
ChessColor turnColor = ChessColor.white;
@override
Widget build(BuildContext context) {
for (int i = 0; i < 64; i++) {
var column = (i % 8) + 1;
var row = (i ~/ 8) + 1;
squares.add(
ChessSquare(
coordinate: ChessCoordinate(column, row),
),
);
}
_placePieces();
return Column(
children: _buildBoard(widget.flipped),
);
}
void _placePieces() {}
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)
],
);
}
}
List<Row> _buildBoard(bool flipped) {
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));
}
}
return chessBoard;
}
ChessSquare getSquareAtCoordinate(ChessCoordinate coord) {
/* Calculate index in squares[] from column and row */
int index = (coord.row - 1) * 8 + (coord.column - 1);
print("getSquareAtCoordinates: index calculated to $index");
return squares.elementAt(index);
}
}