From 595fd5371f2e6268bf4d2ef2c4f9afb5228a82ba Mon Sep 17 00:00:00 2001 From: Calcitem Date: Mon, 16 Nov 2020 00:45:02 +0800 Subject: [PATCH] =?UTF-8?q?flutter:=20=E7=9B=AE=E5=89=8D=E5=8F=AF=E4=BB=A5?= =?UTF-8?q?=E7=94=A8=E7=81=B0=E8=89=B2=E6=98=BE=E7=A4=BA=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E7=9A=84=E6=A3=8B=E5=AD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ui/flutter/lib/game/battle.dart | 3 +- src/ui/flutter/lib/mill/mill.dart | 7 +++ src/ui/flutter/lib/mill/position.dart | 16 +++-- src/ui/flutter/lib/routes/battle_page.dart | 69 +++++++++++----------- 4 files changed, 54 insertions(+), 41 deletions(-) diff --git a/src/ui/flutter/lib/game/battle.dart b/src/ui/flutter/lib/game/battle.dart index f4289510..5b187368 100644 --- a/src/ui/flutter/lib/game/battle.dart +++ b/src/ui/flutter/lib/game/battle.dart @@ -95,6 +95,7 @@ class Battle { newGame() { Battle.shared.position.init(); _focusIndex = _blurIndex = Move.invalidValue; + cmdlist = [""]; } select(int pos) { @@ -192,7 +193,7 @@ class Battle { String winner = position.winner; if (winner != Color.nobody) { - //resumeAiThreads(position.sideToMove); + //resumeAiThreads(position.sideToMove()); // TODO } else { // pauseThreads(); diff --git a/src/ui/flutter/lib/mill/mill.dart b/src/ui/flutter/lib/mill/mill.dart index f69fc0d2..2689d9ab 100644 --- a/src/ui/flutter/lib/mill/mill.dart +++ b/src/ui/flutter/lib/mill/mill.dart @@ -155,6 +155,9 @@ class Move { toFile = int.parse(move[1]); toRank = int.parse(move[3]); captured = Piece.noPiece; + } else if (move == "draw") { + // TODO + print("Computer request draw"); } else { assert(false); } @@ -198,6 +201,10 @@ class Move { } static bool validateEngineMove(String move) { + if (move == "draw") { + return true; // TODO + } + if (move == null || move.length > "(3,1)->(2,1)".length) return false; String sets = "0123456789(,)->"; diff --git a/src/ui/flutter/lib/mill/position.dart b/src/ui/flutter/lib/mill/position.dart index 1e9e2d77..7d629bce 100644 --- a/src/ui/flutter/lib/mill/position.dart +++ b/src/ui/flutter/lib/mill/position.dart @@ -71,7 +71,7 @@ class Position { Map score = {Color.black: 0, Color.white: 0, Color.draw: 0}; - int currentSquare; + int currentSquare = 0; int nPlayed = 0; String cmdline; @@ -478,14 +478,21 @@ class Position { } int setPosition(Rule newRule) { + result = GameResult.pending; + rule = new Rule(); // TODO gamePly = 0; rule50 = 0; + pliesFromNull = 0; - phase = Phase.ready; + gameOverReason = GameOverReason.noReason; + phase = Phase.placing; setSideToMove(Color.black); action = Act.place; + currentSquare = 0; + + cmdline = ""; for (int i = 0; i < _grid.length; i++) { _grid[i] = Piece.noPiece; @@ -796,7 +803,7 @@ class Position { return set_position(&RULES[ruleIndex - 1]) >= 0 ? true : false; } */ - if (cmd.substring(0, 5) == "Player") { + if (cmd.length > 6 && cmd.substring(0, 5) == "Player") { if (cmd[6] == '1') { return resign(Color.black); } else { @@ -1531,7 +1538,7 @@ class Position { rankNumber * fileNumber) return true; // Can fly - if (pieceCountOnBoard[sideToMove] <= rule.nPiecesAtLeast && + if (pieceCountOnBoard[sideToMove()] <= rule.nPiecesAtLeast && rule.allowFlyWhenRemainThreePieces) { return false; } @@ -1633,6 +1640,7 @@ class Position { void changeSideToMove() { them = _sideToMove; _sideToMove = Color.opponent(_sideToMove); + print("Change sideToMove to $_sideToMove"); } 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 3e2134e4..061cf000 100644 --- a/src/ui/flutter/lib/routes/battle_page.dart +++ b/src/ui/flutter/lib/routes/battle_page.dart @@ -124,7 +124,7 @@ class _BattlePageState extends State { break; case Act.remove: - if (position.remove_piece(sq)) { + if (position.removePiece(sq)) { // 播放音效 //playSound(GAME_SOUND_REMOVE, position.side_to_move()); result = true; @@ -174,13 +174,10 @@ class _BattlePageState extends State { // AI设置 // 如果还未决出胜负 if (position.winner == Color.nobody) { - //resumeAiThreads(position.sideToMove); + // Color.black is TODO + //resumeAiThreads(position.sideToMove()); engineToGo(); } - // 如果已经决出胜负 - else { - //pauseThreads(); - } } Battle.shared.sideToMove = position.sideToMove(); @@ -251,39 +248,40 @@ class _BattlePageState extends State { } engineToGo() async { - // - changeStatus('对方思考中...'); + while (Battle.shared.position.sideToMove() == Color.black) { + changeStatus('对方思考中...'); - final response = await widget.engine.search(Battle.shared.position); + final response = await widget.engine.search(Battle.shared.position); - if (response.type == 'move') { - // - Move mv = response.value; - final Move move = new Move(mv.move); + if (response.type == 'move') { + // + Move mv = response.value; + final Move move = new Move(mv.move); - //Battle.shared.move = move; - Battle.shared.command(move.move); + //Battle.shared.move = move; + Battle.shared.command(move.move); - final result = Battle.shared.scanBattleResult(); + final result = Battle.shared.scanBattleResult(); - switch (result) { - case GameResult.pending: - changeStatus('请走棋...'); - break; - case GameResult.win: - gotWin(); - break; - case GameResult.lose: - gotLose(); - break; - case GameResult.draw: - gotDraw(); - break; + switch (result) { + case GameResult.pending: + changeStatus('请走棋...'); + break; + case GameResult.win: + gotWin(); + break; + case GameResult.lose: + gotLose(); + break; + case GameResult.draw: + gotDraw(); + break; + } + // + } else { + // + changeStatus('Error: ${response.type}'); } - // - } else { - // - changeStatus('Error: ${response.type}'); } } @@ -308,9 +306,8 @@ class _BattlePageState extends State { context: context, builder: (BuildContext context) { return AlertDialog( - title: - Text('放弃对局?', style: TextStyle(color: Properties.primaryColor)), - content: SingleChildScrollView(child: Text('你确定要放弃当前的对局吗?')), + title: Text('新局?', style: TextStyle(color: Properties.primaryColor)), + content: SingleChildScrollView(child: Text('开始新局?')), actions: [ FlatButton(child: Text('确定'), onPressed: confirm), FlatButton(child: Text('取消'), onPressed: cancel),