public void SameKillerMoveWithHigherScoreReplacesSlotEntry() { const int Ply = 10; KillerMoves.Clear(); Piece piece = new Piece(); PiecePawn piecePawn = new PiecePawn(piece); piece.Top = piecePawn; Move move1 = new Move(0, 0, Move.MoveNames.Standard, piece, Board.GetSquare(0), Board.GetSquare(1), null, 0, 20); // Add a move KillerMoves.RecordPossibleKillerMove(Ply, move1); Assert.IsTrue(KillerMoves.RetrieveA(Ply) == move1); Assert.IsTrue(KillerMoves.RetrieveA(Ply).Score == 20); Assert.IsNull(KillerMoves.RetrieveB(Ply)); // Add same move AGAIN, but with higher score. Move should be replaced, using higher score. Move move2 = new Move(0, 0, Move.MoveNames.Standard, piece, Board.GetSquare(0), Board.GetSquare(1), null, 0, 30); KillerMoves.RecordPossibleKillerMove(Ply, move2); Assert.IsTrue(KillerMoves.RetrieveA(Ply) == move2); Assert.IsTrue(KillerMoves.RetrieveA(Ply).Score == 30); Assert.IsNull(KillerMoves.RetrieveB(Ply)); // Add same move AGAIN, but with LOWER score. No killer moves should be changed Move move3 = new Move(0, 0, Move.MoveNames.Standard, piece, Board.GetSquare(0), Board.GetSquare(1), null, 0, 10); KillerMoves.RecordPossibleKillerMove(Ply, move3); Assert.IsTrue(Move.MovesMatch(KillerMoves.RetrieveA(Ply), move2)); Assert.IsTrue(KillerMoves.RetrieveA(Ply).Score == 30); Assert.IsNull(KillerMoves.RetrieveB(Ply)); // Now add a different move, and check it goes in slot B Move move4 = new Move(0, 0, Move.MoveNames.Standard, piece, Board.GetSquare(2), Board.GetSquare(3), null, 0, 5); KillerMoves.RecordPossibleKillerMove(Ply, move4); Assert.IsTrue(Move.MovesMatch(KillerMoves.RetrieveA(Ply), move3)); Assert.IsTrue(KillerMoves.RetrieveA(Ply).Score == 30); Assert.IsTrue(Move.MovesMatch(KillerMoves.RetrieveB(Ply), move4)); Assert.IsTrue(KillerMoves.RetrieveB(Ply).Score == 5); // Now improve score of the move that is in slot B. // Slot B's score should be updated. Slot A should stay the same. // Slot's A & B should be SWAPPED. Move move5 = new Move(0, 0, Move.MoveNames.Standard, piece, Board.GetSquare(2), Board.GetSquare(3), null, 0, 100); KillerMoves.RecordPossibleKillerMove(Ply, move5); Assert.IsTrue(Move.MovesMatch(KillerMoves.RetrieveA(Ply), move5)); Assert.IsTrue(KillerMoves.RetrieveA(Ply).Score == 100); Assert.IsTrue(Move.MovesMatch(KillerMoves.RetrieveB(Ply), move3)); Assert.IsTrue(KillerMoves.RetrieveB(Ply).Score == 30); }
public ChessBoard() : base(8, 8) { ConsoleColor[] colors = { ConsoleColor.Black, ConsoleColor.White }; /* * "I'M SO FANCY, YOU ALREADY KNOW" * AN USELESS BUT COOL WAY OF INITIALIZING THE BOARD WITH PIECES USING FOR LOOPS */ for (int team = 0; team < 2; team++) { for (int y = 0; y < 2; y++) { int mirrorY = Math.Abs((7 * team) - y); for (int x = 0; x < 4; x++) { switch (y) { case 0: switch (x) { case 0: _tiles[x, mirrorY] = new PieceRook(colors[team], this, new Position(x, mirrorY)); _tiles[7 - x, mirrorY] = new PieceRook(colors[team], this, new Position(7 - x, mirrorY)); break; case 1: _tiles[x, mirrorY] = new PieceKnight(colors[team], this, new Position(x, mirrorY)); _tiles[7 - x, mirrorY] = new PieceKnight(colors[team], this, new Position(7 - x, mirrorY)); break; case 2: _tiles[x, mirrorY] = new PieceBishop(colors[team], this, new Position(x, mirrorY)); _tiles[7 - x, mirrorY] = new PieceBishop(colors[team], this, new Position(7 - x, mirrorY)); break; case 3: _tiles[x, mirrorY] = new PieceQueen(colors[team], this, new Position(x, mirrorY)); _tiles[7 - x, mirrorY] = new PieceKing(colors[team], this, new Position(7 - x, mirrorY)); break; } break; case 1: _tiles[x, mirrorY] = new PiecePawn(colors[team], this, new Position(x, mirrorY)); _tiles[7 - x, mirrorY] = new PiecePawn(colors[team], this, new Position(7 - x, mirrorY)); break; } } } } }
public void RecordPossibleKillerMoveTest() { int Ply = 10; KillerMoves.Clear(); Piece piece = new Piece(); PiecePawn piecePawn = new PiecePawn(piece); piece.Top = piecePawn; Move move1 = new Move(0, 0, Move.MoveNames.Standard, piece, Board.GetSquare(0), Board.GetSquare(1), null, 0, 20); KillerMoves.RecordPossibleKillerMove(Ply, move1); Assert.IsTrue(KillerMoves.RetrieveA(Ply) == move1); Assert.IsNull(KillerMoves.RetrieveB(Ply)); Move move2 = new Move(0, 0, Move.MoveNames.Standard, piece, Board.GetSquare(2), Board.GetSquare(3), null, 0, 10); KillerMoves.RecordPossibleKillerMove(Ply, move2); Assert.IsTrue(KillerMoves.RetrieveA(Ply) == move1); Assert.IsTrue(KillerMoves.RetrieveB(Ply) == move2); Move move3 = new Move(0, 0, Move.MoveNames.Standard, piece, Board.GetSquare(4), Board.GetSquare(5), null, 0, 15); KillerMoves.RecordPossibleKillerMove(Ply, move3); Assert.IsTrue(KillerMoves.RetrieveA(Ply) == move1); Assert.IsTrue(KillerMoves.RetrieveB(Ply) == move3); Move move4 = new Move(0, 0, Move.MoveNames.Standard, piece, Board.GetSquare(6), Board.GetSquare(7), null, 0, 30); KillerMoves.RecordPossibleKillerMove(Ply, move4); Assert.IsTrue(KillerMoves.RetrieveA(Ply) == move4); Assert.IsTrue(KillerMoves.RetrieveB(Ply) == move1); // Start again KillerMoves.Clear(); Move move5 = new Move(0, 0, Move.MoveNames.Standard, piece, Board.GetSquare(16), Board.GetSquare(17), null, 0, 200); KillerMoves.RecordPossibleKillerMove(Ply, move5); Assert.IsTrue(KillerMoves.RetrieveA(Ply) == move5); Assert.IsNull(KillerMoves.RetrieveB(Ply)); Move move6 = new Move(0, 0, Move.MoveNames.Standard, piece, Board.GetSquare(18), Board.GetSquare(19), null, 0, 300); KillerMoves.RecordPossibleKillerMove(Ply, move6); Assert.IsTrue(KillerMoves.RetrieveA(Ply) == move6); Assert.IsTrue(KillerMoves.RetrieveB(Ply) == move5); }
public Board Create() { try { int countNroSquare = 0; #region Square for (int i = 1; i < nroColumn + 1; i++) { if (countNroSquare == 0 || countNroSquare == 16 || countNroSquare == 32 || countNroSquare == 48 || countNroSquare == 64) { colorSquare = colorSquareD; } if (countNroSquare == 8 || countNroSquare == 24 || countNroSquare == 40 || countNroSquare == 56) { colorSquare = colorSquareL; } foreach (string item in Row) { Square squareiitem = new Square { Name = "square" + i + item, PositionY = i.ToString(), PositionX = item, Color = colorSquare, Empty = true }; squareList.Add(squareiitem); if (colorSquare == colorSquareD) { colorSquare = colorSquareL; } else { colorSquare = colorSquareD; } countNroSquare++; } } #endregion #region Piece #region Pawn for (int p = 1; p < ((nroColumn * 2) + 1); p++) { if (nroColumn >= p) { //create piece Pawn Black PiecePawn pawnpcolorB = new PiecePawn { Name = "pawn" + p + colorB, Color = colorB, Dead = false, Img = "♟", }; pieceList.Add(pawnpcolorB); } else { var aux = (p - nroColumn); //create piece Pawn White PiecePawn pawnauxcolorW = new PiecePawn { Name = "pawn" + aux + colorW, Color = colorW, Dead = false, Img = "♙", }; pieceList.Add(pawnauxcolorW); } } #endregion #region Tower //create piece Tower Black PieceTower tower1colorB = new PieceTower { Name = "tower1" + colorB, Color = colorB, Dead = false, Img = "♜", }; PieceTower tower2colorB = new PieceTower { Name = "tower2" + colorB, Color = colorB, Dead = false, Img = "♜", }; pieceList.Add(tower1colorB); pieceList.Add(tower2colorB); //create piece Tower White PieceTower tower1colorW = new PieceTower { Name = "tower1" + colorW, Color = colorW, Dead = false, Img = "♖", }; PieceTower tower2colorW = new PieceTower { Name = "tower2" + colorW, Color = colorW, Dead = false, Img = "♖", }; pieceList.Add(tower1colorW); pieceList.Add(tower2colorW); #endregion #region Bishop //create piece Bishop Black PieceBishop bishop1colorB = new PieceBishop { Name = "bishop1" + colorB, Color = colorB, Dead = false, Img = "♝", }; PieceBishop bishop2colorB = new PieceBishop { Name = "bishop2" + colorB, Color = colorB, Dead = false, Img = "♝", }; pieceList.Add(bishop1colorB); pieceList.Add(bishop2colorB); //create piece Bishop White PieceBishop bishop1colorW = new PieceBishop { Name = "bishop1" + colorW, Color = colorW, Dead = false, Img = "♗", }; PieceBishop bishop2colorW = new PieceBishop { Name = "bishop2" + colorW, Color = colorW, Dead = false, Img = "♗", }; pieceList.Add(bishop1colorW); pieceList.Add(bishop2colorW); #endregion Bishop #region Horse //create piece Horse Black PieceHorse horse1colorB = new PieceHorse { Name = "horse1" + colorB, Color = colorB, Dead = false, Img = "♞", }; PieceHorse horse2colorB = new PieceHorse { Name = "horse2" + colorB, Color = colorB, Dead = false, Img = "♞", }; pieceList.Add(horse1colorB); pieceList.Add(horse2colorB); //create piece Horse White PieceHorse horse1colorW = new PieceHorse { Name = "horse1" + colorW, Color = colorW, Dead = false, Img = "♘", }; PieceHorse horse2colorW = new PieceHorse { Name = "horse2" + colorW, Color = colorW, Dead = false, Img = "♘", }; pieceList.Add(horse1colorW); pieceList.Add(horse2colorW); #endregion #region Queen //create piece queen Black PieceQueen queencolorB = new PieceQueen { Name = "queen" + colorB, Color = colorB, Dead = false, Img = "♛", }; pieceList.Add(queencolorB); //create piece queen White PieceKing queencolorW = new PieceKing { Name = "queen" + colorW, Color = colorW, Dead = false, Img = "♕", }; pieceList.Add(queencolorW); #endregion #region King //create piece king Black PieceKing kingcolorB = new PieceKing { Name = "king" + colorB, Color = colorB, Dead = false, Img = "♚", }; pieceList.Add(kingcolorB); //create piece king White PieceKing kingcolorW = new PieceKing { Name = "king" + colorW, Color = colorW, Dead = false, Img = "♔", }; pieceList.Add(kingcolorW); #endregion #endregion newBoard.Squares = squareList; newBoard.Pieces = pieceList; newBoard = RandomPieces(newBoard); return(newBoard); } catch (Exception ex) { return(null); } }
/// <summary> /// 移动指定位置棋格的棋子 /// </summary> /// <param name="pair">指定的成对的位置,First值为源棋格,Second值是目标棋格</param> public void PieceMoveIn(PositionPair pair) { Enums.ActionCollection actions = new Enums.ActionCollection(); Piece piece; if (this.TryGetPiece(pair.First.Dot, out piece)) { //第1步,判断一下过路兵的格子的逻辑 if (piece is PiecePawn) { if (pair.First.Y == pair.Second.Y) { return; } } actions.Add(Enums.Action.General); //第2步,判断目标棋格中是否有棋子。 //如果有棋子,更改Action,调用PieceMoveOut方法。 if (this.ContainsPiece(pair.Second.Dot)) { actions.Add(Enums.Action.Capture); this.PieceMoveOut(pair.Second); } //第3步,调用内部方法PieceMoveIn,移动棋子,并更改FEN记录 this.PieceMoveIn(piece, actions, pair.First, pair.Second); //第4步,如上一步棋出现过“过路兵”的局面,而现在进行了新的一步棋了,故而取消上一步棋的“过路兵”局面。 if (_piecePawnByEnPassanted != null) { _piecePawnByEnPassanted.EnableEnPassanted = false; _piecePawnByEnPassanted = null; } //第5步,判断是否过路兵的局面 if (piece is PiecePawn) { #region 过路兵 //成为能被吃过路兵的“兵” if ((pair.First.Y == 1 && pair.Second.Y == 3) || (pair.First.Y == 6 && pair.Second.Y == 4)) { char pawnChar; Position tmpPos = pair.Second.ShiftWest(); if (tmpPos != null) { if (this.TryGetPiece(tmpPos.Dot, out pawnChar)) { Enums.GameSide side = char.IsLower(pawnChar) ? Enums.GameSide.Black : Enums.GameSide.White; if (this.GameSide == side) { _piecePawnByEnPassanted = piece as PiecePawn; _piecePawnByEnPassanted.EnableEnPassanted = true; } } } tmpPos = pair.Second.ShiftEast(); if (tmpPos != null) { if (this.TryGetPiece(tmpPos.Dot, out pawnChar)) { Enums.GameSide side = char.IsLower(pawnChar) ? Enums.GameSide.Black : Enums.GameSide.White; if (this.GameSide == side) { _piecePawnByEnPassanted = piece as PiecePawn; _piecePawnByEnPassanted.EnableEnPassanted = true; } } } } else { ((PiecePawn)piece).EnableEnPassanted = false; } #endregion } //第6步,移动棋子后,根据移动棋子后的局面生成Action,主要是判断另一战方的“王”是否被将军 actions.Add(this.IsCheckPieceKing(piece)); //第7步,注册棋子移动后事件 OnPieceMoveInEvent(piece, actions, pair); } }