/// <summary> /// Clears the piece from the board on the parameter square. /// </summary> /// <param name="sq"> The position of the piece </param> /// <param name="board"> The board to remove from </param> public static void ClearPiece(int sq, Board.Board board) { Debug.Assert(Validators.IsSq(sq), String.Format("Invalid Square {0}", Io.SqToString(sq))); int pce = board[sq]; int colour = (int)Data.PIECE_COLOURS[pce]; int t_pceNum = -1; Debug.Assert(Validators.PieceValid(pce), String.Format("Invalid Piece {0} at square {1}", (Piece)pce, Io.SqToString(sq))); MakeMove.HashPiece(board, pce, sq); board[sq] = (int)Piece.EMPTY; // Subtract the value of the piece from the material total. board.Material[colour] -= Data.PIECE_VAL[pce]; if (Data.IS_PIECE_BIG[pce]) { board.BigPieces[colour]--; if (Data.IS_PIECE_MAJ[pce]) { board.MajPieces[colour]--; } else // else piece is minor piece { board.MinPieces[colour]--; } } else // else piece is pawn. { int sq64 = Conversion.getSq120ToSq64(sq); BitBoard.ClearBit(ref board.Pawns[colour], sq64); BitBoard.ClearBit(ref board.Pawns[(int)Colour.BOTH], sq64); } for (int i = 0; i < board.PceNum[pce]; ++i) { // Find the correct piece by comparing squares. if (board.PList[pce, i] == sq) { t_pceNum = i; break; } } // Ensure that the piece has been located in the PList. Debug.Assert(t_pceNum != -1, String.Format("Piece {0} at square {1} wasn't found in the Board's PList", (Piece)pce, Io.SqToString(sq))); // Decrement the value of the PList. board.PceNum[pce]--; board.PList[pce, t_pceNum] = board.PList[pce, board.PceNum[pce]]; }
/// <summary> /// Moves the piece at the from square to the to square /// </summary> /// <param name="from"> The starting square for the piece </param> /// <param name="to"> The new square for the piece </param> /// <param name="board"> The board to operate on </param> /// <exception cref="Exception"> Throws an exception if the from or to Square is invalid </exception> public static void MovePiece(int from, int to, Board.Board board) { Debug.Assert(Validators.IsSq(from), String.Format("Invalid From Square {0}", Io.SqToString(from))); Debug.Assert(Validators.IsSq(to), String.Format("Invalid To Square {0}", Io.SqToString(from))); int pce = board[from]; Debug.Assert(Validators.PieceValid(pce), String.Format( "Invalid Pce represented by integer {0} On Sq {0}", pce, Io.SqToString(from))); int colour = (int)Data.PIECE_COLOURS[pce]; int t_pceNum = -1; MakeMove.HashPiece(board, pce, from); // Removes the piece from the from square. board[from] = (int)Piece.EMPTY; MakeMove.HashPiece(board, pce, to); // Adds the piece to the to square. board[to] = pce; if (!Data.IS_PIECE_BIG[pce]) // If piece is pawn. { int sq64From = Conversion.getSq120ToSq64(from); int sq64To = Conversion.getSq120ToSq64(to); // Clear from sq BitBoard.ClearBit(ref board.Pawns[colour], sq64From); BitBoard.ClearBit(ref board.Pawns[(int)Colour.BOTH], sq64From); // Sets to sq BitBoard.SetBit(ref board.Pawns[colour], sq64To); BitBoard.SetBit(ref board.Pawns[(int)Colour.BOTH], sq64To); } // Now we move the piece in the piece list: for (int i = 0; i < board.PceNum[pce]; ++i) { // Find the correct piece by comparing squares. if (board.PList[pce, i] == from) { board.PList[pce, i] = to; t_pceNum = i; break; } } // If t_pceNum is unchanged. Debug.Assert(t_pceNum != -1, String.Format("Piece {0} at square {1} wasn't found in the Board's PList", (Piece)pce, Io.SqToString(from))); }
public void BitBoardTest_64Squares() { BitBoard bb1 = new BitBoard(64); // test bit count - should be 0 Assert.AreEqual(0, bb1.BitCount); // set a bit and make sure it is set and bit count is 1 bb1.SetBit(3); Assert.AreEqual(1, bb1.GetBit(3)); Assert.AreEqual(1, bb1.BitCount); // clear the bit and ensure it and bit count are 0 bb1.ClearBit(3); Assert.AreEqual(0, bb1.GetBit(3)); Assert.AreEqual(0, bb1.BitCount); // set a bunch of bits and make sure we extract them properly bb1.SetBit(0); bb1.SetBit(3); bb1.SetBit(12); bb1.SetBit(18); bb1.SetBit(19); bb1.SetBit(33); bb1.SetBit(49); bb1.SetBit(63); Assert.AreEqual(8, bb1.BitCount); Assert.AreEqual(0, bb1.ExtractLSB()); Assert.AreEqual(7, bb1.BitCount); Assert.AreEqual(3, bb1.ExtractLSB()); Assert.AreEqual(6, bb1.BitCount); Assert.AreEqual(12, bb1.ExtractLSB()); Assert.AreEqual(5, bb1.BitCount); Assert.AreEqual(18, bb1.ExtractLSB()); Assert.AreEqual(4, bb1.BitCount); Assert.AreEqual(19, bb1.ExtractLSB()); Assert.AreEqual(3, bb1.BitCount); Assert.AreEqual(33, bb1.ExtractLSB()); Assert.AreEqual(2, bb1.BitCount); Assert.AreEqual(49, bb1.ExtractLSB()); Assert.AreEqual(1, bb1.BitCount); Assert.AreEqual(63, bb1.ExtractLSB()); Assert.AreEqual(0, bb1.BitCount); }