flutter: refactor

This commit is contained in:
Calcitem 2020-11-08 18:48:21 +08:00
parent 2339e3c51f
commit 56280b24d4
4 changed files with 57 additions and 65 deletions

View File

@ -96,7 +96,7 @@ class PiecesPainter extends PainterBase {
// //
for (var col = 0; col < 7; col++) { for (var col = 0; col < 7; col++) {
// //
final piece = position.pieceAt(row * 7 + col); // final piece = position.pieceOn(row * 7 + col); //
if (piece == Piece.noPiece) continue; if (piece == Piece.noPiece) continue;

View File

@ -89,15 +89,48 @@ enum Rank { rank_1, rank_2, rank_3, rank_4, rank_5, rank_6, rank_7, rank_8 }
const rankNumber = 8; const rankNumber = 8;
Map<int, int> squareToIndex = {
8: 17,
9: 18,
10: 25,
11: 32,
12: 31,
13: 30,
14: 23,
15: 16,
16: 10,
17: 12,
18: 26,
19: 40,
20: 38,
21: 36,
22: 22,
23: 8,
24: 3,
25: 6,
26: 27,
27: 48,
28: 45,
29: 42,
30: 21,
31: 0
};
Map<int, int> indexToSquare = squareToIndex.map((k, v) => MapEntry(v, k));
int makeSquare(int file, int rank) {
return (file << 3) + rank - 1;
}
/// ///
enum GameResult { pending, win, lose, draw } enum GameResult { pending, win, lose, draw }
class Color { class Color {
// //
static const unknown = '-'; static const unknown = '-';
static const black = 'b'; static const black = '@';
static const white = 'w'; static const white = 'O';
static const ban = 'x'; static const ban = 'X';
static String of(String piece) { static String of(String piece) {
if (black.contains(piece)) return black; if (black.contains(piece)) return black;
@ -121,15 +154,15 @@ class Color {
class Piece { class Piece {
// //
static const noPiece = ' '; static const noPiece = '*';
// //
static const blackStone = 'b'; static const blackStone = '@';
static const whiteStone = 'w'; static const whiteStone = 'O';
static const ban = 'x'; static const ban = 'X';
static bool isBlack(String c) => 'b'.contains(c); static bool isBlack(String c) => '@'.contains(c);
static bool isWhite(String c) => 'w'.contains(c); static bool isWhite(String c) => 'O'.contains(c);
} }
class Move { class Move {

View File

@ -20,35 +20,6 @@
import '../mill/recorder.dart'; import '../mill/recorder.dart';
import 'mill.dart'; import 'mill.dart';
Map<int, int> sqToLoc = {
8: 17,
9: 18,
10: 25,
11: 32,
12: 31,
13: 30,
14: 23,
15: 16,
16: 10,
17: 12,
18: 26,
19: 40,
20: 38,
21: 36,
22: 22,
23: 8,
24: 3,
25: 6,
26: 27,
27: 48,
28: 45,
29: 42,
30: 21,
31: 0
};
Map<int, int> locToSq = sqToLoc.map((k, v) => MapEntry(v, k));
class Position { class Position {
GameResult result = GameResult.pending; GameResult result = GameResult.pending;
String _sideToMove = Color.black; String _sideToMove = Color.black;
@ -73,44 +44,32 @@ class Position {
_recorder = other._recorder; _recorder = other._recorder;
} }
/// fen() returns a FEN representation of the position.
String fen() { String fen() {
// TODO // TODO
var fen = ''; var ss = '';
for (var file = 1; file <= 3; file++) { for (var file = 1; file <= 3; file++) {
//
var emptyCounter = 0;
for (var rank = 1; rank <= 8; rank++) { for (var rank = 1; rank <= 8; rank++) {
// //
final piece = pieceAt((file - 1) * 8 + rank + 8); final piece = pieceOn((file - 1) * 8 + rank + 8);
if (piece == Piece.noPiece) { if (piece == Piece.noPiece) {
//
emptyCounter++;
//
} else { } else {
// ss += piece;
if (emptyCounter > 0) {
fen += emptyCounter.toString();
emptyCounter = 0;
}
fen += piece;
} }
} }
if (emptyCounter > 0) fen += emptyCounter.toString(); if (file < 9) ss += '/';
if (file < 9) fen += '/';
} }
fen += ' $side'; ss += ' $side';
// step counter // step counter
fen += '${_recorder?.halfMove ?? 0} ${_recorder?.fullMove ?? 0}'; ss += '${_recorder?.halfMove ?? 0} ${_recorder?.fullMove ?? 0}';
return fen; return ss;
} }
void putPiece(var pt, int index) { void putPiece(var pt, int index) {
@ -215,7 +174,7 @@ class Position {
changeSideToMove() => _sideToMove = Color.opponent(_sideToMove); changeSideToMove() => _sideToMove = Color.opponent(_sideToMove);
String pieceAt(int index) => _board[index]; String pieceOn(int index) => _board[index];
get halfMove => _recorder.halfMove; get halfMove => _recorder.halfMove;

View File

@ -73,23 +73,23 @@ class _BattlePageState extends State<BattlePage> {
//position //position
flag++; flag++;
position.putPiece(flag % 2 == 0 ? 'b' : 'w', index); position.putPiece(flag % 2 == 0 ? '@' : 'O', index);
// Position side // Position side
if (position.side != Color.black) return; if (position.side != Color.black) return;
final tapedPiece = position.pieceAt(index); final tapedPiece = position.pieceOn(index);
print("Tap piece $tapedPiece at <$index>"); print("Tap piece $tapedPiece at <$index>");
// //
if (Battle.shared.focusIndex != Move.invalidIndex && if (Battle.shared.focusIndex != Move.invalidIndex &&
Color.of(position.pieceAt(Battle.shared.focusIndex)) == Color.black) { Color.of(position.pieceOn(Battle.shared.focusIndex)) == Color.black) {
// //
// //
if (Battle.shared.focusIndex == index) return; if (Battle.shared.focusIndex == index) return;
// //
final focusPiece = position.pieceAt(Battle.shared.focusIndex); final focusPiece = position.pieceOn(Battle.shared.focusIndex);
if (Color.isSameColor(focusPiece, tapedPiece)) { if (Color.isSameColor(focusPiece, tapedPiece)) {
// //