flutter: 目前可以用灰色显示移除的棋子

This commit is contained in:
Calcitem 2020-11-16 00:45:02 +08:00
parent 43a9eff2d8
commit 595fd5371f
4 changed files with 54 additions and 41 deletions

View File

@ -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();

View File

@ -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(,)->";

View File

@ -71,7 +71,7 @@ class Position {
Map<String, int> 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;

View File

@ -124,7 +124,7 @@ class _BattlePageState extends State<BattlePage> {
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<BattlePage> {
// 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<BattlePage> {
}
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<BattlePage> {
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: <Widget>[
FlatButton(child: Text('确定'), onPressed: confirm),
FlatButton(child: Text('取消'), onPressed: cancel),