/// <summary> /// 初始化一个黑白棋棋盘, 大小为 8*8. 中间 4 个初始棋子为白黑白黑. /// </summary> private ReversiGame() { CurrentBoard = new ReversiPiece[BoardSize, BoardSize]; for (int i = 0; i < BoardSize; i++) { for (int j = 0; j < BoardSize; j++) { CurrentBoard[i, j] = ReversiPiece.Empty; } } CurrentBoard[BoardSize / 2 - 1, BoardSize / 2 - 1] = ReversiPiece.White; CurrentBoard[BoardSize / 2, BoardSize / 2 - 1] = ReversiPiece.Black; CurrentBoard[BoardSize / 2 - 1, BoardSize / 2] = ReversiPiece.Black; CurrentBoard[BoardSize / 2, BoardSize / 2] = ReversiPiece.White; // TODO: 删除用于调试的初始棋局 /*CurrentBoard[0, 0] = ReversiPiece.Black; * CurrentBoard[0, 1] = ReversiPiece.Black; * CurrentBoard[1, 0] = ReversiPiece.White; * CurrentBoard[1, 1] = ReversiPiece.White;*/ CurrentGame = this; CurrentPiece = ReversiPiece.Black; LastPiece = ReversiPiece.White; LastPosition = new ReversiPiecePosition(4, 4); TotalSteps = 0; reversiBuffer = new ReversiBuffer(); IsBusy = false; isRegretEnabled = false; MaxRegretSteps = 0; gameStateStack = new RecersiGameStateStack(); UpdateState(); ResetBuffer(); }
/// <summary> /// 初始化一个有特定棋局的黑白棋棋盘, 其棋局由参数确定. /// </summary> /// <param name="initialReversiBoard">是一个二维数组, 指定黑白棋棋局.</param> private ReversiGame(ReversiPiece[,] initialReversiBoard) { CurrentBoard = initialReversiBoard; CurrentGame = this; CurrentPiece = ReversiPiece.Black; LastPiece = ReversiPiece.White; LastPosition = new ReversiPiecePosition(4, 4); reversiBuffer = new ReversiBuffer(); IsBusy = false; UpdateState(); ResetBuffer(); }
/// <summary> /// 重建缓存 /// </summary> private void ResetBuffer() { reversiBuffer = null; if (!IsGameOver) { reversiBuffer = new ReversiBuffer(); ReversiPiecePosition tempPosition; List <ReversiPiecePosition> tempReverseList; int x, y; bool isThisPositionEnabled; bool haveDiffPiece; for (int i = 0; i < ReversiGame.BoardSize; i++) { for (int j = 0; j < ReversiGame.BoardSize; j++) { if (GetpieceInBoard(j, i) != ReversiPiece.Empty) { continue; } tempPosition = new ReversiPiecePosition(j, i); isThisPositionEnabled = false; for (int k = 0; k < 8; k++) { x = j; y = i; haveDiffPiece = false; tempReverseList = new List <ReversiPiecePosition>(); while (true) { x += ReversiDirections.Num(k).X; y += ReversiDirections.Num(k).Y; if (GetpieceInBoardWithoutException(x, y) == ReversiPiece.Empty) { break; } else if (GetpieceInBoardWithoutException(x, y) == GetReversepiece(CurrentPiece)) { tempReverseList.Add(new ReversiPiecePosition(x, y)); haveDiffPiece = true; } else { if (!haveDiffPiece) { break; } if (!isThisPositionEnabled) { reversiBuffer.AddNewEnabledPosition(tempPosition); } isThisPositionEnabled = true; foreach (ReversiPiecePosition rpp in tempReverseList) { reversiBuffer.AllBufferPositions[reversiBuffer.AllBufferPositions.Count - 1].AllReversePositions.Add(rpp); } break; } } } } } } }