From 8abb94b50109fe775f27e9d0df6eee1dedbbdcf3 Mon Sep 17 00:00:00 2001 From: Calcitem Date: Sun, 8 Nov 2020 20:45:05 +0800 Subject: [PATCH] =?UTF-8?q?flutter:=20=E5=A2=9E=E5=8A=A0=20board=20?= =?UTF-8?q?=E4=B8=94=E5=85=B6=E4=B8=8B=E6=A0=87=E4=B8=BA=20sq?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ui/flutter/lib/board/pieces_painter.dart | 2 +- src/ui/flutter/lib/game/battle.dart | 2 +- src/ui/flutter/lib/mill/position.dart | 76 +++++++++++++++++--- src/ui/flutter/lib/routes/battle_page.dart | 7 +- 4 files changed, 73 insertions(+), 14 deletions(-) diff --git a/src/ui/flutter/lib/board/pieces_painter.dart b/src/ui/flutter/lib/board/pieces_painter.dart index 8da9e185..60788ce2 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.pieceOn(row * 7 + col); // 初始状态无棋子 + final piece = position.pieceOnGrid(row * 7 + col); // 初始状态无棋子 if (piece == Piece.noPiece) continue; diff --git a/src/ui/flutter/lib/game/battle.dart b/src/ui/flutter/lib/game/battle.dart index 8bf23d22..c620f340 100644 --- a/src/ui/flutter/lib/game/battle.dart +++ b/src/ui/flutter/lib/game/battle.dart @@ -50,7 +50,7 @@ class Battle { bool move(int from, int to) { // - final captured = _position.move(from, to); + final captured = _position.doMove(from, to); if (captured == null) { //Audios.playTone('invalid.mp3'); diff --git a/src/ui/flutter/lib/mill/position.dart b/src/ui/flutter/lib/mill/position.dart index 054cf674..ef0d7b53 100644 --- a/src/ui/flutter/lib/mill/position.dart +++ b/src/ui/flutter/lib/mill/position.dart @@ -38,7 +38,9 @@ class StateInfo { class Position { GameResult result = GameResult.pending; - List _board = List(49); // 7 * 7 + List _board = List(40); + List _grid = List(49); // 7 * 7 + MillRecorder _recorder; int pieceCountInHandBlack = 12; @@ -72,6 +74,10 @@ class Position { //Move move; Position.init() { + for (var i = 0; i < _grid.length; i++) { + _grid[i] ??= Piece.noPiece; + } + for (var i = 0; i < _board.length; i++) { _board[i] ??= Piece.noPiece; } @@ -82,11 +88,55 @@ class Position { _recorder = MillRecorder(lastCapturedPosition: fen()); } + Position.boardToGrid() { + _grid = List(); + for (int sq = 0; sq < _board.length; sq++) { + _grid[squareToIndex[sq]] = _board[sq]; + } + } + + Position.gridToBoard() { + _board = List(); + for (int i = 0; i < _grid.length; i++) { + _board[indexToSquare[i]] = _grid[i]; + } + } + Position.clone(Position other) { + _grid = List(); + other._grid.forEach((piece) => _grid.add(piece)); + _board = List(); other._board.forEach((piece) => _board.add(piece)); - _sideToMove = other._sideToMove; + _recorder = other._recorder; + + pieceCountInHandBlack = other.pieceCountInHandBlack; + pieceCountInHandWhite = other.pieceCountInHandWhite; + pieceCountOnBoardBlack = other.pieceCountOnBoardBlack; + pieceCountOnBoardWhite = other.pieceCountOnBoardWhite; + pieceCountNeedRemove = other.pieceCountNeedRemove; + + gamePly = other.gamePly; + + _sideToMove = other._sideToMove; + + rule50 = other.rule50; + pliesFromNull = other.pliesFromNull; + + them = other.them; + winner = other.winner; + gameOverReason = other.gameOverReason; + + phase = other.phase; + action = other.action; + + scoreBlack = other.scoreBlack; + scoreWhite = other.scoreWhite; + scoreDraw = other.scoreDraw; + + currentSquare = other.currentSquare; + nPlayed = other.nPlayed; } /// fen() returns a FEN representation of the position. @@ -98,7 +148,7 @@ class Position { for (var file = 1; file <= 3; file++) { for (var rank = 1; rank <= 8; rank++) { // - final piece = pieceOn(squareToIndex[makeSquare(file, rank)]); + final piece = pieceOnGrid(squareToIndex[makeSquare(file, rank)]); ss += piece; } @@ -180,20 +230,23 @@ class Position { } void putPiece(var pt, int index) { - _board[index] = pt; + _grid[index] = pt; + _board[indexToSquare[index]] = pt; } - String move(int from, int to) { + String doMove(int from, int to) { // if (!validateMove(from, to)) return null; - final captured = _board[to]; + final captured = _grid[to]; final move = Move(from, to, captured: captured); //StepName.translate(this, move); _recorder.stepIn(move, this); // 修改棋盘 + _grid[to] = _grid[from]; + _grid[from] = Piece.noPiece; _board[to] = _board[from]; _board[from] = Piece.noPiece; @@ -216,6 +269,8 @@ class Position { void moveTest(Move move, {turnSide = false}) { // // 修改棋盘 + _grid[move.to] = _grid[move.from]; + _grid[move.from] = Piece.noPiece; _board[move.to] = _board[move.from]; _board[move.from] = Piece.noPiece; @@ -228,6 +283,8 @@ class Position { final lastMove = _recorder.removeLast(); if (lastMove == null) return false; + _grid[lastMove.from] = _grid[lastMove.to]; + _grid[lastMove.to] = lastMove.captured; _board[lastMove.from] = _board[lastMove.to]; _board[lastMove.to] = lastMove.captured; @@ -245,8 +302,8 @@ class Position { final moves = _recorder.reverseMovesToPrevCapture(); moves.forEach((move) { // - tempPosition._board[move.from] = tempPosition._board[move.to]; - tempPosition._board[move.to] = move.captured; + tempPosition._grid[move.from] = tempPosition._grid[move.to]; + tempPosition._grid[move.to] = move.captured; tempPosition._sideToMove = Color.opponent(tempPosition._sideToMove); }); @@ -281,7 +338,8 @@ class Position { changeSideToMove() => _sideToMove = Color.opponent(_sideToMove); - String pieceOn(int index) => _board[index]; + String pieceOnGrid(int index) => _grid[index]; + String pieceOn(int sq) => _board[sq]; 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 16c4e75d..74c3f054 100644 --- a/src/ui/flutter/lib/routes/battle_page.dart +++ b/src/ui/flutter/lib/routes/battle_page.dart @@ -78,18 +78,19 @@ class _BattlePageState extends State { // 仅 Position 中的 side 指示一方能动棋 if (position.side != Color.black) return; - final tapedPiece = position.pieceOn(index); + final tapedPiece = position.pieceOnGrid(index); print("Tap piece $tapedPiece at <$index>"); // 之前已经有棋子被选中了 if (Battle.shared.focusIndex != Move.invalidIndex && - Color.of(position.pieceOn(Battle.shared.focusIndex)) == Color.black) { + Color.of(position.pieceOnGrid(Battle.shared.focusIndex)) == + Color.black) { // // 当前点击的棋子和之前已经选择的是同一个位置 if (Battle.shared.focusIndex == index) return; // 之前已经选择的棋子和现在点击的棋子是同一边的,说明是选择另外一个棋子 - final focusPiece = position.pieceOn(Battle.shared.focusIndex); + final focusPiece = position.pieceOnGrid(Battle.shared.focusIndex); if (Color.isSameColor(focusPiece, tapedPiece)) { //