Skip to content

Commit

Permalink
Partly refactor position.dart
Browse files Browse the repository at this point in the history
Change-Id: Idb9245b99f95fbe91712b1642190f8c7e48f2d60
  • Loading branch information
calcitem committed Apr 29, 2023
1 parent 34cf90f commit 20920bd
Showing 1 changed file with 73 additions and 29 deletions.
102 changes: 73 additions & 29 deletions src/ui/flutter_app/lib/services/mill/src/position.dart
Original file line number Diff line number Diff line change
Expand Up @@ -401,14 +401,77 @@ class Position {

///////////////////////////////////////////////////////////////////////////////
bool isWithinBoard(int s) {
return sqBegin <= s && s < sqEnd;
}

bool isSquareEmpty(int s) {
return _board[s] == PieceColor.none;
}

void increasePieceOnBoardCount(PieceColor color) {
if (pieceOnBoardCount[color] != null) {
pieceOnBoardCount[color] = pieceOnBoardCount[color]! + 1;
}
}

void decreasePieceInHandCount(PieceColor color) {
if (pieceInHandCount[color] != null) {
pieceInHandCount[color] = pieceInHandCount[color]! - 1;
}
}

bool canPlacePiece(int s) {
if (phase == Phase.gameOver ||
action != Act.place ||
!isWithinBoard(s) ||
!isSquareEmpty(s)) {
return false;
}
return true;
}

bool canMovePiece(int from, int to) {
int md;

if (phase != Phase.moving ||
(pieceOnBoardCount[sideToMove]! > DB().ruleSettings.flyPieceCount &&
!DB().ruleSettings.mayFly)) {
return false;
}

for (md = 0; md < moveDirectionNumber; md++) {
if (to == _adjacentSquares[from][md]) {
break;
}
}

// Not in moveTable
if (md == moveDirectionNumber) {
logger.i(
"[position] putPiece: [$to] is not in [$from]'s move table.",
);
return false;
}

return true;
}

bool isGameOverMaterialConditionMet() {
if (pieceOnBoardCount[_them]! + pieceInHandCount[_them]! <
DB().ruleSettings.piecesAtLeastCount) {
return true;
}
return false;
}

//////////////////////////////////////////////////////////////////////////////
bool _putPiece(int s) {
PieceColor piece = PieceColor.none;
final PieceColor us = _sideToMove;

if (phase == Phase.gameOver ||
action != Act.place ||
!(sqBegin <= s && s < sqEnd) ||
_board[s] != PieceColor.none) {
if (!canPlacePiece(s)) {
return false;
}

Expand All @@ -423,12 +486,10 @@ class Position {
.showSnackBarClear("FEN: ${MillController().position.fen}");
return false;
}
pieceInHandCount[us] = pieceInHandCount[us]! - 1;
decreasePieceInHandCount(us);
}

if (pieceOnBoardCount[us] != null) {
pieceOnBoardCount[us] = pieceOnBoardCount[us]! + 1;
}
increasePieceOnBoardCount(us);

_grid[squareToIndex[s]!] = piece;
_board[s] = piece;
Expand Down Expand Up @@ -485,8 +546,7 @@ class Position {

if (DB().ruleSettings.mayOnlyRemoveUnplacedPieceInPlacingPhase &&
pieceInHandCount[_them] != null) {
pieceInHandCount[_them] =
pieceInHandCount[_them]! - 1; // Or pieceToRemoveCount?
decreasePieceInHandCount(_them); // Or pieceToRemoveCount?

if (pieceInHandCount[_them]! < 0) {
pieceInHandCount[_them] = 0;
Expand Down Expand Up @@ -523,23 +583,8 @@ class Position {
}

// If illegal
if (pieceOnBoardCount[sideToMove]! > DB().ruleSettings.flyPieceCount ||
!DB().ruleSettings.mayFly) {
int md;

for (md = 0; md < moveDirectionNumber; md++) {
if (s == _adjacentSquares[_currentSquare][md]) {
break;
}
}

// Not in moveTable
if (md == moveDirectionNumber) {
logger.i(
"[position] putPiece: [$s] is not in [$_currentSquare]'s move table.",
);
return false;
}
if (!canMovePiece(_currentSquare, s)) {
return false;
}

_record = ExtMove(
Expand Down Expand Up @@ -646,8 +691,7 @@ class Position {
pieceOnBoardCount[_them] = pieceOnBoardCount[_them]! - 1;
}

if (pieceOnBoardCount[_them]! + pieceInHandCount[_them]! <
DB().ruleSettings.piecesAtLeastCount) {
if (isGameOverMaterialConditionMet()) {
_setGameOver(sideToMove, GameOverReason.loseLessThanThree);
Audios().playTone(Sound.remove);
return const MillResponseOK();
Expand Down

0 comments on commit 20920bd

Please sign in to comment.