flutter: 完成 inHowManyMills()
This commit is contained in:
parent
e87991c591
commit
f1fad8fa9c
|
@ -57,16 +57,17 @@ enum GameResult { pending, win, lose, draw }
|
||||||
|
|
||||||
class Color {
|
class Color {
|
||||||
//
|
//
|
||||||
static const unknown = '-';
|
static const none = '*';
|
||||||
static const black = '@';
|
static const black = '@';
|
||||||
static const white = 'O';
|
static const white = 'O';
|
||||||
static const ban = 'X';
|
static const ban = 'X';
|
||||||
|
static const nobody = '-';
|
||||||
|
|
||||||
static String of(String piece) {
|
static String of(String piece) {
|
||||||
if (black.contains(piece)) return black;
|
if (black.contains(piece)) return black;
|
||||||
if (white.contains(piece)) return white;
|
if (white.contains(piece)) return white;
|
||||||
if (ban.contains(piece)) return ban;
|
if (ban.contains(piece)) return ban;
|
||||||
return unknown;
|
return nobody;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool isSameColor(String p1, String p2) {
|
static bool isSameColor(String p1, String p2) {
|
||||||
|
|
|
@ -42,7 +42,7 @@ class StateInfo {
|
||||||
class Position {
|
class Position {
|
||||||
GameResult result = GameResult.pending;
|
GameResult result = GameResult.pending;
|
||||||
|
|
||||||
List<String> _board = List<String>(40);
|
List<String> board = List<String>(40);
|
||||||
List<String> _grid = List<String>(49); // 7 * 7
|
List<String> _grid = List<String>(49); // 7 * 7
|
||||||
|
|
||||||
MillRecorder _recorder;
|
MillRecorder _recorder;
|
||||||
|
@ -85,8 +85,8 @@ class Position {
|
||||||
_grid[i] ??= Piece.noPiece;
|
_grid[i] ??= Piece.noPiece;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (var i = 0; i < _board.length; i++) {
|
for (var i = 0; i < board.length; i++) {
|
||||||
_board[i] ??= Piece.noPiece;
|
board[i] ??= Piece.noPiece;
|
||||||
}
|
}
|
||||||
|
|
||||||
phase = Phase.placing;
|
phase = Phase.placing;
|
||||||
|
@ -103,15 +103,15 @@ class Position {
|
||||||
|
|
||||||
Position.boardToGrid() {
|
Position.boardToGrid() {
|
||||||
_grid = List<String>();
|
_grid = List<String>();
|
||||||
for (int sq = 0; sq < _board.length; sq++) {
|
for (int sq = 0; sq < board.length; sq++) {
|
||||||
_grid[squareToIndex[sq]] = _board[sq];
|
_grid[squareToIndex[sq]] = board[sq];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Position.gridToBoard() {
|
Position.gridToBoard() {
|
||||||
_board = List<String>();
|
board = List<String>();
|
||||||
for (int i = 0; i < _grid.length; i++) {
|
for (int i = 0; i < _grid.length; i++) {
|
||||||
_board[indexToSquare[i]] = _grid[i];
|
board[indexToSquare[i]] = _grid[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -119,8 +119,8 @@ class Position {
|
||||||
_grid = List<String>();
|
_grid = List<String>();
|
||||||
other._grid.forEach((piece) => _grid.add(piece));
|
other._grid.forEach((piece) => _grid.add(piece));
|
||||||
|
|
||||||
_board = List<String>();
|
board = List<String>();
|
||||||
other._board.forEach((piece) => _board.add(piece));
|
other.board.forEach((piece) => board.add(piece));
|
||||||
|
|
||||||
_recorder = other._recorder;
|
_recorder = other._recorder;
|
||||||
|
|
||||||
|
@ -151,7 +151,7 @@ class Position {
|
||||||
}
|
}
|
||||||
|
|
||||||
String pieceOnGrid(int index) => _grid[index];
|
String pieceOnGrid(int index) => _grid[index];
|
||||||
String pieceOn(int sq) => _board[sq];
|
String pieceOn(int sq) => board[sq];
|
||||||
|
|
||||||
bool empty(int sq) => pieceOn(sq) == Piece.noPiece;
|
bool empty(int sq) => pieceOn(sq) == Piece.noPiece;
|
||||||
|
|
||||||
|
@ -178,7 +178,7 @@ class Position {
|
||||||
|
|
||||||
if (phase == Phase.gameOver ||
|
if (phase == Phase.gameOver ||
|
||||||
action != Act.place ||
|
action != Act.place ||
|
||||||
_board[s] != Piece.noPiece) {
|
board[s] != Piece.noPiece) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -192,7 +192,7 @@ class Position {
|
||||||
pieceCountOnBoard[us]++;
|
pieceCountOnBoard[us]++;
|
||||||
|
|
||||||
_grid[index] = piece;
|
_grid[index] = piece;
|
||||||
_board[s] = piece;
|
board[s] = piece;
|
||||||
|
|
||||||
cmdline = "(" + fileOf(s).toString() + "," + rankOf(s).toString() + ")";
|
cmdline = "(" + fileOf(s).toString() + "," + rankOf(s).toString() + ")";
|
||||||
}
|
}
|
||||||
|
@ -464,21 +464,17 @@ class Position {
|
||||||
|
|
||||||
//final move = Move(m);
|
//final move = Move(m);
|
||||||
|
|
||||||
if (move.type == MoveType.remove) {
|
|
||||||
final captured = _grid[move.to];
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (move.type) {
|
switch (move.type) {
|
||||||
case MoveType.place:
|
case MoveType.place:
|
||||||
_grid[move.toIndex] = _board[move.to] = _sideToMove;
|
_grid[move.toIndex] = board[move.to] = _sideToMove;
|
||||||
break;
|
break;
|
||||||
case MoveType.remove:
|
case MoveType.remove:
|
||||||
_grid[move.toIndex] = _board[move.to] = Piece.noPiece;
|
_grid[move.toIndex] = board[move.to] = Piece.noPiece;
|
||||||
break;
|
break;
|
||||||
case MoveType.move:
|
case MoveType.move:
|
||||||
_grid[move.toIndex] = _grid[move.fromIndex];
|
_grid[move.toIndex] = _grid[move.fromIndex];
|
||||||
_board[move.to] = _board[move.from];
|
board[move.to] = board[move.from];
|
||||||
_grid[move.fromIndex] = _board[move.from] = Piece.noPiece;
|
_grid[move.fromIndex] = board[move.from] = Piece.noPiece;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
assert(false);
|
assert(false);
|
||||||
|
@ -509,8 +505,8 @@ class Position {
|
||||||
// 修改棋盘
|
// 修改棋盘
|
||||||
_grid[move.to] = _grid[move.from];
|
_grid[move.to] = _grid[move.from];
|
||||||
_grid[move.from] = Piece.noPiece;
|
_grid[move.from] = Piece.noPiece;
|
||||||
_board[move.to] = _board[move.from];
|
board[move.to] = board[move.from];
|
||||||
_board[move.from] = Piece.noPiece;
|
board[move.from] = Piece.noPiece;
|
||||||
|
|
||||||
// 交换走棋方
|
// 交换走棋方
|
||||||
if (turnSide) _sideToMove = Color.opponent(_sideToMove);
|
if (turnSide) _sideToMove = Color.opponent(_sideToMove);
|
||||||
|
@ -523,8 +519,8 @@ class Position {
|
||||||
|
|
||||||
_grid[lastMove.from] = _grid[lastMove.to];
|
_grid[lastMove.from] = _grid[lastMove.to];
|
||||||
_grid[lastMove.to] = lastMove.captured;
|
_grid[lastMove.to] = lastMove.captured;
|
||||||
_board[lastMove.from] = _board[lastMove.to];
|
board[lastMove.from] = board[lastMove.to];
|
||||||
_board[lastMove.to] = lastMove.captured;
|
board[lastMove.to] = lastMove.captured;
|
||||||
|
|
||||||
_sideToMove = Color.opponent(_sideToMove);
|
_sideToMove = Color.opponent(_sideToMove);
|
||||||
|
|
||||||
|
@ -592,9 +588,9 @@ class Position {
|
||||||
for (int f = 1; f < 3 + 2; f++) {
|
for (int f = 1; f < 3 + 2; f++) {
|
||||||
for (int r = 0; r < 8; r++) {
|
for (int r = 0; r < 8; r++) {
|
||||||
int s = f * 8 + r;
|
int s = f * 8 + r;
|
||||||
if (_board[s] == Piece.blackStone) {
|
if (board[s] == Piece.blackStone) {
|
||||||
pieceCountOnBoard[Color.black]++;
|
pieceCountOnBoard[Color.black]++;
|
||||||
} else if (_board[s] == Piece.whiteStone) {
|
} else if (board[s] == Piece.whiteStone) {
|
||||||
pieceCountOnBoard[Color.black]++;
|
pieceCountOnBoard[Color.black]++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -629,7 +625,7 @@ class Position {
|
||||||
|
|
||||||
for (int i = 0; i < _grid.length; i++) _grid[i] = Piece.noPiece;
|
for (int i = 0; i < _grid.length; i++) _grid[i] = Piece.noPiece;
|
||||||
|
|
||||||
for (int i = 0; i < _board.length; i++) _board[i] = Piece.noPiece;
|
for (int i = 0; i < board.length; i++) board[i] = Piece.noPiece;
|
||||||
|
|
||||||
if (piecesOnBoardCount() == -1) {
|
if (piecesOnBoardCount() == -1) {
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -638,7 +634,7 @@ class Position {
|
||||||
piecesInHandCount();
|
piecesInHandCount();
|
||||||
pieceCountNeedRemove = 0;
|
pieceCountNeedRemove = 0;
|
||||||
|
|
||||||
winner = Color.unknown;
|
winner = Color.nobody;
|
||||||
|
|
||||||
currentSquare = 0;
|
currentSquare = 0;
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -652,12 +648,12 @@ class Position {
|
||||||
setSideToMove(Color.black);
|
setSideToMove(Color.black);
|
||||||
action = Act.place;
|
action = Act.place;
|
||||||
|
|
||||||
winner = Color.unknown;
|
winner = Color.nobody;
|
||||||
gameOverReason = GameOverReason.noReason;
|
gameOverReason = GameOverReason.noReason;
|
||||||
|
|
||||||
for (int i = 0; i < _grid.length; i++) _grid[i] = Piece.noPiece;
|
for (int i = 0; i < _grid.length; i++) _grid[i] = Piece.noPiece;
|
||||||
|
|
||||||
for (int i = 0; i < _board.length; i++) _board[i] = Piece.noPiece;
|
for (int i = 0; i < board.length; i++) board[i] = Piece.noPiece;
|
||||||
|
|
||||||
pieceCountOnBoard[Color.black] = pieceCountOnBoard[Color.white] = 0;
|
pieceCountOnBoard[Color.black] = pieceCountOnBoard[Color.white] = 0;
|
||||||
pieceCountInHand[Color.black] =
|
pieceCountInHand[Color.black] =
|
||||||
|
@ -695,6 +691,8 @@ class Position {
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void createMillTable() {
|
void createMillTable() {
|
||||||
|
@ -1118,4 +1116,37 @@ class Position {
|
||||||
millTable = millTable_noObliqueLine;
|
millTable = millTable_noObliqueLine;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String colorOn(int sq) {
|
||||||
|
return board[sq];
|
||||||
|
}
|
||||||
|
|
||||||
|
int inHowManyMills(int s, String c, int squareSelected) {
|
||||||
|
int n = 0;
|
||||||
|
String locbak = Piece.noPiece;
|
||||||
|
|
||||||
|
assert(0 <= squareSelected && squareSelected < sqNumber);
|
||||||
|
|
||||||
|
if (c == Color.nobody) {
|
||||||
|
c = colorOn(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (squareSelected != 0) {
|
||||||
|
locbak = board[squareSelected];
|
||||||
|
board[squareSelected] = Piece.noPiece;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int l = 0; l < lineDirectionNumber; l++) {
|
||||||
|
// TODO: right?
|
||||||
|
if (c == board[millTable[s][l][0]] && c == board[millTable[s][l][1]]) {
|
||||||
|
n++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (squareSelected != 0) {
|
||||||
|
board[squareSelected] = locbak;
|
||||||
|
}
|
||||||
|
|
||||||
|
return n;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue