flutter: 调整 position 函数顺序

This commit is contained in:
Calcitem 2020-11-15 17:24:36 +08:00
parent a2d33e626e
commit ac7f2a565f
1 changed files with 92 additions and 130 deletions

View File

@ -376,136 +376,6 @@ class Position {
return legal(move);
}
/*
/// Position::do_move() makes a move, and saves all information necessary
/// to a StateInfo object. The move is assumed to be legal. Pseudo-legal
/// moves should be filtered out before this function is called.
void doMove(int move) {
bool ret = false;
MoveType mt = typeOf(move);
switch (mt) {
case MoveType.remove:
// Reset rule 50 counter
rule50 = 0;
ret = removePiece(toSq(move));
break;
case MoveType.move:
ret = movePiece(fromSq(move), toSq(move));
break;
case MoveType.place:
ret = putPieceSQ(toSq(move));
break;
default:
break;
}
if (!ret) {
return;
}
// Increment ply counters. In particular, rule50 will be reset to zero later on
// in case of a capture.
++gamePly;
++rule50;
++pliesFromNull;
_move = move;
}
*/
//
bool validateMove(int from, int to) {
//
//if (Color.of(_board[from]) != _sideToMove) return false;
return true;
//(StepValidate.validate(this, Move(from, to)));
}
//
//
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;
//
if (turnSide) _sideToMove = Color.opponent(_sideToMove);
}
bool regret() {
//
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;
_sideToMove = Color.opponent(_sideToMove);
final counterMarks = MillRecorder.fromCounterMarks(lastMove.counterMarks);
_recorder.halfMove = counterMarks.halfMove;
_recorder.fullMove = counterMarks.fullMove;
if (lastMove.captured != Piece.noPiece) {
//
// NativeEngine
final tempPosition = Position.clone(this);
final moves = _recorder.reverseMovesToPrevCapture();
moves.forEach((move) {
//
tempPosition._grid[move.from] = tempPosition._grid[move.to];
tempPosition._grid[move.to] = move.captured;
tempPosition._sideToMove = Color.opponent(tempPosition._sideToMove);
});
_recorder.lastCapturedPosition = tempPosition.fen();
}
result = GameResult.pending;
return true;
}
String movesSinceLastCaptured() {
//
var steps = '', posAfterLastCaptured = 0;
for (var i = _recorder.stepsCount - 1; i >= 0; i--) {
if (_recorder.stepAt(i).captured != Piece.noPiece) break;
posAfterLastCaptured = i;
}
for (var i = posAfterLastCaptured; i < _recorder.stepsCount; i++) {
steps += ' ${_recorder.stepAt(i).move}';
}
return steps.length > 0 ? steps.substring(1) : '';
}
get manualText => _recorder.buildManualText();
get side => _sideToMove;
changeSideToMove() => _sideToMove = Color.opponent(_sideToMove);
get halfMove => _recorder.halfMove;
get fullMove => _recorder.fullMove;
get lastMove => _recorder.last;
get lastCapturedPosition => _recorder.lastCapturedPosition;
void doMove(Move m) {
//
//if (!validateMove(m)) return null;
@ -1675,4 +1545,96 @@ class Position {
return (s == 16 || s == 18 || s == 20 || s == 22);
}
///////////////////////////////////////////////////////////////////////////////
//
bool validateMove(int from, int to) {
//
//if (Color.of(_board[from]) != _sideToMove) return false;
return true;
//(StepValidate.validate(this, Move(from, to)));
}
//
//
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;
//
if (turnSide) _sideToMove = Color.opponent(_sideToMove);
}
bool regret() {
//
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;
_sideToMove = Color.opponent(_sideToMove);
final counterMarks = MillRecorder.fromCounterMarks(lastMove.counterMarks);
_recorder.halfMove = counterMarks.halfMove;
_recorder.fullMove = counterMarks.fullMove;
if (lastMove.captured != Piece.noPiece) {
//
// NativeEngine
final tempPosition = Position.clone(this);
final moves = _recorder.reverseMovesToPrevCapture();
moves.forEach((move) {
//
tempPosition._grid[move.from] = tempPosition._grid[move.to];
tempPosition._grid[move.to] = move.captured;
tempPosition._sideToMove = Color.opponent(tempPosition._sideToMove);
});
_recorder.lastCapturedPosition = tempPosition.fen();
}
result = GameResult.pending;
return true;
}
String movesSinceLastCaptured() {
//
var steps = '', posAfterLastCaptured = 0;
for (var i = _recorder.stepsCount - 1; i >= 0; i--) {
if (_recorder.stepAt(i).captured != Piece.noPiece) break;
posAfterLastCaptured = i;
}
for (var i = posAfterLastCaptured; i < _recorder.stepsCount; i++) {
steps += ' ${_recorder.stepAt(i).move}';
}
return steps.length > 0 ? steps.substring(1) : '';
}
get manualText => _recorder.buildManualText();
get side => _sideToMove;
changeSideToMove() => _sideToMove = Color.opponent(_sideToMove);
get halfMove => _recorder.halfMove;
get fullMove => _recorder.fullMove;
get lastMove => _recorder.last;
get lastCapturedPosition => _recorder.lastCapturedPosition;
}