diff --git a/src/ui/flutter/lib/board/pieces_painter.dart b/src/ui/flutter/lib/board/pieces_painter.dart index 3b2a62d7..8da9e185 100644 --- a/src/ui/flutter/lib/board/pieces_painter.dart +++ b/src/ui/flutter/lib/board/pieces_painter.dart @@ -96,7 +96,7 @@ class PiecesPainter extends PainterBase { // 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; diff --git a/src/ui/flutter/lib/mill/mill.dart b/src/ui/flutter/lib/mill/mill.dart index cd34159d..863b485a 100644 --- a/src/ui/flutter/lib/mill/mill.dart +++ b/src/ui/flutter/lib/mill/mill.dart @@ -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; +Map 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 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 } class Color { // static const unknown = '-'; - static const black = 'b'; - static const white = 'w'; - static const ban = 'x'; + static const black = '@'; + static const white = 'O'; + static const ban = 'X'; static String of(String piece) { if (black.contains(piece)) return black; @@ -121,15 +154,15 @@ class Color { class Piece { // - static const noPiece = ' '; + static const noPiece = '*'; // - static const blackStone = 'b'; - static const whiteStone = 'w'; - static const ban = 'x'; + static const blackStone = '@'; + static const whiteStone = 'O'; + 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 { diff --git a/src/ui/flutter/lib/mill/position.dart b/src/ui/flutter/lib/mill/position.dart index e19a8843..c8322ab4 100644 --- a/src/ui/flutter/lib/mill/position.dart +++ b/src/ui/flutter/lib/mill/position.dart @@ -20,35 +20,6 @@ import '../mill/recorder.dart'; import 'mill.dart'; -Map 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 locToSq = sqToLoc.map((k, v) => MapEntry(v, k)); - class Position { GameResult result = GameResult.pending; String _sideToMove = Color.black; @@ -73,44 +44,32 @@ class Position { _recorder = other._recorder; } + /// fen() returns a FEN representation of the position. + String fen() { // TODO - var fen = ''; + var ss = ''; for (var file = 1; file <= 3; file++) { - // - var emptyCounter = 0; - 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) { - // - emptyCounter++; - // } else { - // - if (emptyCounter > 0) { - fen += emptyCounter.toString(); - emptyCounter = 0; - } - - fen += piece; + ss += piece; } } - if (emptyCounter > 0) fen += emptyCounter.toString(); - - if (file < 9) fen += '/'; + if (file < 9) ss += '/'; } - fen += ' $side'; + ss += ' $side'; // 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) { @@ -215,7 +174,7 @@ class Position { changeSideToMove() => _sideToMove = Color.opponent(_sideToMove); - String pieceAt(int index) => _board[index]; + String pieceOn(int index) => _board[index]; get halfMove => _recorder.halfMove; diff --git a/src/ui/flutter/lib/routes/battle_page.dart b/src/ui/flutter/lib/routes/battle_page.dart index 7287c4ed..16c4e75d 100644 --- a/src/ui/flutter/lib/routes/battle_page.dart +++ b/src/ui/flutter/lib/routes/battle_page.dart @@ -73,23 +73,23 @@ class _BattlePageState extends State { //position flag++; - position.putPiece(flag % 2 == 0 ? 'b' : 'w', index); + position.putPiece(flag % 2 == 0 ? '@' : 'O', index); // 仅 Position 中的 side 指示一方能动棋 if (position.side != Color.black) return; - final tapedPiece = position.pieceAt(index); + final tapedPiece = position.pieceOn(index); print("Tap piece $tapedPiece at <$index>"); // 之前已经有棋子被选中了 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; // 之前已经选择的棋子和现在点击的棋子是同一边的,说明是选择另外一个棋子 - final focusPiece = position.pieceAt(Battle.shared.focusIndex); + final focusPiece = position.pieceOn(Battle.shared.focusIndex); if (Color.isSameColor(focusPiece, tapedPiece)) { //