public Bitboard ComputeRayIntersections(Bitboard bitboardToIntersect, Position movingPiecePosition, bool includeIntersectionPosition) { // Recompute this bitboard after stopping all rays from the moving piece that are intersecting the passed bitboard Bitboard newBitboard = new Bitboard(); // Compute a bitboard containing all intersection points Bitboard intersectBitboard = this.ComplementBitboard(this.IntersectBitboard(bitboardToIntersect)); int row; for (row = 0; row < this.bitboard.Length; row++) { newBitboard.GetBitboard()[row] = (byte)this.GetBitboard()[row]; intersectBitboard.GetBitboard()[row] = (byte)(intersectBitboard.GetBitboard()[row] & newBitboard.GetBitboard()[row]); } // Now, for each intersect position, go along the unit vector connecting the moving position to this position and wipe out all points till the edge of the bitboard // These "vectors" can only be horizontal, vertical or diagonal foreach (Position intersectionPosition in intersectBitboard.GetPositions()) { Vector2 positionVector = new Vector2(intersectionPosition.GetColumn() - movingPiecePosition.GetColumn(), intersectionPosition.GetRow() - movingPiecePosition.GetRow()); if (positionVector.x > 1 || positionVector.x < -1) { positionVector.x = positionVector.x / Mathf.Abs(positionVector.x); } if (positionVector.y > 1 || positionVector.y < -1) { positionVector.y = positionVector.y / Mathf.Abs(positionVector.y); } Position clearPosition = new Position(intersectionPosition.GetColumn(), intersectionPosition.GetRow()); if (includeIntersectionPosition) { // Don't process the intersection position clearPosition.SetColumn(clearPosition.GetColumn() + (int)positionVector.x); clearPosition.SetRow(clearPosition.GetRow() + (int)positionVector.y); } while (clearPosition.GetColumn() >= Position.min && clearPosition.GetColumn() <= Position.max && clearPosition.GetRow() >= Position.min && clearPosition.GetRow() <= Position.max) { if (newBitboard.ValueAtPosition(clearPosition) > 0) { newBitboard.FlipPosition(clearPosition); } else { break; } clearPosition.SetColumn(clearPosition.GetColumn() + (int)positionVector.x); clearPosition.SetRow(clearPosition.GetRow() + (int)positionVector.y); } } return(newBitboard); }
public Bitboard GetSafeMovesForCurrentPosition() { Bitboard availableMoves = this.GetMovesForCurrentPosition(); foreach (Position position in availableMoves.GetPositions()) { if (!IsMoveSafe(position)) { availableMoves.FlipPosition(position); } } return(availableMoves); }
void OnMouseDown() { if (!GameObject.Find(Constants.PieceNames.ChessBoard).GetComponent <GameKeeper>().isSideControlledByAI(this.getPiece().side) && GameObject.Find(Constants.PieceNames.ChessBoard).GetComponent <GameKeeper>().hasGameStarted() && GameObject.Find(Constants.PieceNames.ChessBoard).GetComponent <GameKeeper>().chessBoard.CurrentMovingSide() == this.getPiece().side) { Bitboard moveBitboard = this.getPiece().GetSafeMovesForCurrentPosition(); GameObject.Find(Constants.PieceNames.ChessBoard).GetComponent <GameKeeper>().ClearTiles(); foreach (Position movePosition in moveBitboard.GetPositions()) { GameObject moveTile = Instantiate(GameObject.Find(Constants.PieceNames.ChessBoard).GetComponent <GameKeeper>().GetMoveIcon(this.getPiece().side)); moveTile.GetComponent <MoveIcon>().SetMovingPiece(this.getPiece()); moveTile.GetComponent <MoveIcon>().SetMovePosition(movePosition); } } }