本文整理汇总了C#中System.Coordinate.IsInBoardBounds方法的典型用法代码示例。如果您正苦于以下问题:C# Coordinate.IsInBoardBounds方法的具体用法?C# Coordinate.IsInBoardBounds怎么用?C# Coordinate.IsInBoardBounds使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Coordinate
的用法示例。
在下文中一共展示了Coordinate.IsInBoardBounds方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DetermineTranslationalMovements
private HashSet<ChessMove> DetermineTranslationalMovements(List<int[]> tranlations, Coordinate fromCoordinate, ChessPiece fromPiece)
{
Coordinate potentialCoordinate;
var legalMoves = new HashSet<ChessMove>();
foreach (int[] translation in tranlations)
{
potentialCoordinate = new Coordinate(fromCoordinate, translation[0], translation[1]);
if (potentialCoordinate.IsInBoardBounds())
{
ChessPiece potentialCoordinatePiece = GetPieceAtCoordinate(potentialCoordinate);
bool blocked = potentialCoordinatePiece.Type != ChessPieceType.None;
bool capture = blocked && potentialCoordinatePiece.Color != TurnColor;
// Only move to blocked square if it is to be captured.
if (!blocked || capture)
{
// Check and checkmate values will only be verified after a move is chosen, to avoid the heavy processing for each move.
legalMoves.Add(new ChessMove(fromCoordinate, potentialCoordinate, fromPiece.Type, capture, false, false, false, false, false, false));
}
}
}
return legalMoves;
}
示例2: InitializePossibleMoves
// Note that this considers moving one's self into a checked position possible, even though it is not legal. This is to avoid the excessive computation of determining if this is the case. The check will be performed on tryApplyMove instead.
private HashSet<ChessMove> InitializePossibleMoves(Coordinate fromCoordinate)
{
var legalMoves = new HashSet<ChessMove>();
ChessPiece fromPiece = GetPieceAtCoordinate(fromCoordinate);
// No valid moves for empty squares or if the color of the piece does not match whose turn it is.
if (fromPiece.Type == ChessPieceType.None || fromPiece.Color == ChessPieceColor.None || fromPiece.Color != TurnColor)
{
// Empty at this point
return legalMoves;
}
Coordinate potentialCoordinate;
switch (fromPiece.Type)
{
case ChessPieceType.Rook:
legalMoves.UnionWith(DetermineIndefiniteDirectionalMovements(RookDirections, fromCoordinate, fromPiece));
break;
case ChessPieceType.Knight:
legalMoves.UnionWith(DetermineTranslationalMovements(KnightTranslations, fromCoordinate, fromPiece));
break;
case ChessPieceType.Bishop:
legalMoves.UnionWith(DetermineIndefiniteDirectionalMovements(BishopDirections, fromCoordinate, fromPiece));
break;
case ChessPieceType.Queen:
legalMoves.UnionWith(DetermineIndefiniteDirectionalMovements(QueenDirectionsKingTranslations, fromCoordinate, fromPiece));
break;
case ChessPieceType.King:
legalMoves.UnionWith(DetermineTranslationalMovements(QueenDirectionsKingTranslations, fromCoordinate, fromPiece));
if (TurnColor == ChessPieceColor.White)
{
if (WhiteKingsideCastling && BoardGrid[0, 5].Type == ChessPieceType.None && BoardGrid[0, 6].Type == ChessPieceType.None)
{
// Check and checkmate values will only be verified after a move is chosen, to avoid the heavy processing for each move.
legalMoves.Add(new ChessMove(fromCoordinate, new Coordinate(0, 6), ChessPieceType.King, false, false, false, false, false, true, false));
}
if (WhiteQueensideCastling && BoardGrid[0, 1].Type == ChessPieceType.None && BoardGrid[0, 2].Type == ChessPieceType.None && BoardGrid[0, 3].Type == ChessPieceType.None)
{
// Check and checkmate values will only be verified after a move is chosen, to avoid the heavy processing for each move.
legalMoves.Add(new ChessMove(fromCoordinate, new Coordinate(0, 2), ChessPieceType.King, false, false, false, false, false, false, true));
}
}
else
{
if (BlackKingsideCastling && BoardGrid[7, 5].Type == ChessPieceType.None && BoardGrid[7, 6].Type == ChessPieceType.None)
{
// Check and checkmate values will only be verified after a move is chosen, to avoid the heavy processing for each move.
legalMoves.Add(new ChessMove(fromCoordinate, new Coordinate(7, 6), ChessPieceType.King, false, false, false, false, false, true, false));
}
if (BlackQueensideCastling && BoardGrid[7, 1].Type == ChessPieceType.None && BoardGrid[7, 2].Type == ChessPieceType.None && BoardGrid[7, 3].Type == ChessPieceType.None)
{
legalMoves.Add(new ChessMove(fromCoordinate, new Coordinate(7, 2), ChessPieceType.King, false, false, false, false, false, false, true));
}
}
break;
case ChessPieceType.Pawn:
// 1 if white, -1 if black.
int pawnDirection;
// How many steps foeward this pawn is allowed to move.
int pawnForwardAmountAllowed = 1;
if (TurnColor == ChessPieceColor.White)
{
pawnDirection = 1;
// Yet-to-move pawns can move forward two spaces.
if (fromCoordinate.Row == 1)
{
pawnForwardAmountAllowed = 2;
}
}
else
{
pawnDirection = -1;
// Yet-to-move pawns can move forward two spaces.
if (fromCoordinate.Row == 6)
{
pawnForwardAmountAllowed = 2;
}
}
int forwardMoved = 1;
// Continue checking the next forward step until the max amount of steps have been taken, or a piece has been encountered.
while (forwardMoved <= pawnForwardAmountAllowed)
{
potentialCoordinate = new Coordinate(fromCoordinate, forwardMoved * pawnDirection, 0);
// If the potential coordinate does not go off bounds and does not occupy the location of another piece, allow a move there
if (potentialCoordinate.IsInBoardBounds() && GetPieceAtCoordinate(potentialCoordinate).Type == ChessPieceType.None)
{
// If a white pawn moves to row 7 or a black pawn moves to row 0, a prootion is in order.
bool isPromotionToQueen = (potentialCoordinate.Row == (TurnColor == ChessPieceColor.White ? 7 : 0));
// Check and checkmate values will only be verified after a move is chosen, to avoid the heavy processing for each move.
legalMoves.Add(new ChessMove(fromCoordinate, potentialCoordinate, fromPiece.Type, false, isPromotionToQueen, false, false, false, false, false));
}
else
{
break;
}
forwardMoved++;
//.........这里部分代码省略.........
示例3: DetermineIndefiniteDirectionalMovements
// Determine legal moves for pieces that can move in specific directions until they reach the end of the board or another piece.
private List<ChessMove> DetermineIndefiniteDirectionalMovements(List<int[]> directions, Coordinate fromCoordinate, ChessPiece fromPiece)
{
var legalMoves = new List<ChessMove>();
Coordinate potentialCoordinate;
foreach (int[] direction in directions)
{
int moved = 1;
while (true)
{
potentialCoordinate = new Coordinate(fromCoordinate, moved * direction[0], moved * direction[1]);
// If the potential coordinate does not go off bounds, allow a move there.
if (potentialCoordinate.IsInBoardBounds())
{
ChessPiece potentialCoordinatePiece = GetPieceAtCoordinate(potentialCoordinate);
bool blocked = potentialCoordinatePiece.Type != ChessPieceType.None;
bool capture = blocked && potentialCoordinatePiece.Color != TurnColor;
// Only move to blocked square if it is to be captured.
if (!blocked || capture)
{
// Check and checkmate values will only be verified after a move is chosen, to avoid the heavy processing for each move.
legalMoves.Add(new ChessMove(fromCoordinate, potentialCoordinate, fromPiece.Type, capture, false, false, false, false, false, false));
}
// Cannot move past another piece.
if (blocked) { break; }
}
else
{
break;
}
// Check further in this direction.
moved++;
}
}
return legalMoves;
}