public static List <Square> GetPossibleMoves(Dictionary <Square, AiChessPiece> board, AiChessPiece piece, bool updateStrength, float[,] strength) { string owner = piece.Owner.Name; int direction = owner == "player1" ? 1 : -1; List <Square> possibleMoves = null; switch (piece.Type) { case "pawn": case "bishop": { possibleMoves = InfantryMoves(piece.Position, direction, updateStrength, strength); break; } case "king": case "queen": { possibleMoves = RoyaltyMoves(board, piece.Position, 3, owner, updateStrength, strength); break; } case "knight": { possibleMoves = RoyaltyMoves(board, piece.Position, 4, owner, updateStrength, strength); break; } case "rook": { possibleMoves = ArcherMoves(board, piece.Position, owner, updateStrength, strength); break; } } List <Square> cleaned = new List <Square>(); List <Square> surrounding = MovementUtil.SurroundingSquares(piece.Position); foreach (Square s in possibleMoves.Where(s => ChessGrid.ValidPosition(s))) { if (board.ContainsKey(s)) { if (piece.Type != "knight" && !s.AttackOnly && !surrounding.Contains(s)) { // remove this if statement if we want pieces to be able to attack any squares they can reach continue; } AiChessPiece target = board[s]; if (target.Owner == piece.Owner) { continue; } } cleaned.Add(s); } return(cleaned); }
// Movement for the Rook and its Attack range private static List <Square> ArcherMoves(Dictionary <Square, AiChessPiece> board, Square start, string owner, bool updateStrength, float[,] strength) { int direction = owner == "player1" ? 1 : -1; List <Square> moves = MovementUtil.SurroundingSquares(start); foreach (Square s in moves) { if (!board.ContainsKey(s)) { continue; } AiChessPiece target = board[s]; if (target.Owner.Name != owner) { s.AttackOnly = true; } } for (int i = -3; i <= 3; i++) { for (int j = -3; j <= 3; j++) { if (i == 0 && j == 0) { continue; } Square s = new Square(start.X + i, start.Y + j); if (updateStrength && ChessGrid.ValidPosition(s)) { strength[s.X, s.Y] += direction * EvaluationValues.PieceStrength["rook"]; } if (!board.ContainsKey(s)) { continue; } AiChessPiece target = board[s]; if (target.Owner.Name != owner) { moves.Add(new Square(s.X, s.Y, true)); } } } return(moves); }
// Validate that a square can be moved to, and then add it to the corresponding cache list private static bool ValidateSquare(Dictionary <Square, ChessPiece> board, ISet <Square> validMoves, ISet <Square> invalidMoves, Square current, string owner) { if (!ChessGrid.ValidPosition(current)) { return(false); } if (board.ContainsKey(current)) { if (board[current].Owner.Name == owner) { invalidMoves.Add(current); return(false); } validMoves.Add(current); return(true); } validMoves.Add(current); return(true); }
// Get all possible moves for a player based on the current state of the board public static List <Square> GetPossibleMoves(Dictionary <Square, ChessPiece> board, ChessPiece piece) { // first check if piece's division has already moved - if so, return an empty list if (piece.GetCommander().Moved) { piece.moveable = false; return(new List <Square>()); } _direction = piece.Owner.Name == "player1" ? 1 : -1; List <Square> possibleMoves = null; // Find moves based on type of piece switch (piece.type) { case "pawn": case "bishop": { possibleMoves = InfantryMoves(piece.Position, _direction); break; } case "king": case "queen": { possibleMoves = RoyaltyMoves(board, piece.Position, 3, piece.Owner.Name); break; } case "knight": { possibleMoves = RoyaltyMoves(board, piece.Position, 4, piece.Owner.Name); break; } case "rook": { possibleMoves = ArcherMoves(board, piece.Position, piece.Owner.Name); break; } } // Clean the squares based on if the positions are valid on the board + other rules List <Square> cleaned = new List <Square>(); List <Square> surrounding = SurroundingSquares(piece.Position); foreach (Square s in possibleMoves.Where(s => ChessGrid.ValidPosition(s))) { if (board.ContainsKey(s)) { if (piece.type != "knight" && !s.AttackOnly && !surrounding.Contains(s)) { // remove this if statement if we want pieces to be able to attack any squares they can reach continue; } ChessPiece target = board[s]; if (target.Owner == piece.Owner) { continue; } } cleaned.Add(s); } piece.moveable = cleaned.Count > 0; return(cleaned); }