/// <summary> /// Gets move, if there is no figure in the place moving to, null otherwise /// </summary> /// <param name="playerColor">Moving player</param> /// <param name="from">Position to move from</param> /// <param name="to">Position to move to</param> private static GameMove CanMove(ChessboardModel chessboard, PlayerColor playerColor, ChessPosition from, ChessPosition to) { if (!to.IsValid()) { return(null); } var moveTo = chessboard.GetFigureOnPosition(to); if (moveTo != null) { return(null); } return(new GameMove(from, to, chessboard.GetFigureOnPosition(from).Type, chessboard.GetFigureOnPosition(to)?.Type)); }
/// <summary> /// Gets all possible moves of pawns of given player /// </summary> private static List <GameMove> GetMovesForPawns(ChessboardModel chessboard, PlayerColor playerColor) { var acumulator = new List <GameMove>(); var moveVector = playerColor == PlayerColor.White ? new GameMoveVector(0, 1) : new GameMoveVector(0, -1); var captureVector1 = playerColor == PlayerColor.White ? new GameMoveVector(1, 1) : new GameMoveVector(1, -1); var captureVector2 = playerColor == PlayerColor.White ? new GameMoveVector(-1, 1) : new GameMoveVector(-1, -1); for (int x = 0; x < 8; x++) { for (int y = 0; y < 8; y++) { var position = new ChessPosition(x, y); var figure = chessboard.GetFigureOnPosition(position); if ( figure != null && figure.Type == FigureType.Pawn && figure.Color == playerColor ) { acumulator.Add(CanAttack(chessboard, playerColor, position, position.Add(captureVector1))); acumulator.Add(CanAttack(chessboard, playerColor, position, position.Add(captureVector2))); var moveOnce = CanMove(chessboard, playerColor, position, position.Add(moveVector)); acumulator.Add(moveOnce); if (moveOnce != null && !figure.Moved) { acumulator.Add(CanMove(chessboard, playerColor, position, moveOnce.To.Add(moveVector))); } } } } acumulator.RemoveAll(x => x == null); return(acumulator); }
/// <summary> /// Gets all possible moves of bishops of given player /// </summary> private static List <GameMove> GetMovesForBishops(ChessboardModel chessboard, PlayerColor playerColor) { var acumulator = new List <GameMove>(); var upLeft = new GameMoveVector(-1, 1); var upRight = new GameMoveVector(1, 1); var downLeft = new GameMoveVector(-1, -1); var downRight = new GameMoveVector(1, -1); for (int x = 0; x < 8; x++) { for (int y = 0; y < 8; y++) { var position = new ChessPosition(x, y); var figure = chessboard.GetFigureOnPosition(position); if ( figure != null && figure.Type == FigureType.Bishop && figure.Color == playerColor ) { acumulator.AddRange(CanMoveOrAttackIterative(chessboard, playerColor, position, upLeft)); acumulator.AddRange(CanMoveOrAttackIterative(chessboard, playerColor, position, upRight)); acumulator.AddRange(CanMoveOrAttackIterative(chessboard, playerColor, position, downLeft)); acumulator.AddRange(CanMoveOrAttackIterative(chessboard, playerColor, position, downRight)); } } } acumulator.RemoveAll(x => x == null); return(acumulator); }
/// <summary> /// Gets all possible moves of king of given player /// </summary> private static List <GameMove> GetMovesForKing(ChessboardModel chessboard, PlayerColor playerColor) { var acumulator = new List <GameMove>(); var up = new GameMoveVector(0, 1); var left = new GameMoveVector(-1, 0); var down = new GameMoveVector(0, -1); var right = new GameMoveVector(1, 0); var upLeft = new GameMoveVector(-1, 1); var upRight = new GameMoveVector(1, 1); var downLeft = new GameMoveVector(-1, -1); var downRight = new GameMoveVector(1, -1); for (int x = 0; x < 8; x++) { for (int y = 0; y < 8; y++) { var position = new ChessPosition(x, y); var figure = chessboard.GetFigureOnPosition(position); if ( figure != null && figure.Type == FigureType.King && figure.Color == playerColor ) { acumulator.Add(CanMoveOrAttack(chessboard, playerColor, position, position.Add(up))); acumulator.Add(CanMoveOrAttack(chessboard, playerColor, position, position.Add(left))); acumulator.Add(CanMoveOrAttack(chessboard, playerColor, position, position.Add(down))); acumulator.Add(CanMoveOrAttack(chessboard, playerColor, position, position.Add(right))); acumulator.Add(CanMoveOrAttack(chessboard, playerColor, position, position.Add(upLeft))); acumulator.Add(CanMoveOrAttack(chessboard, playerColor, position, position.Add(upRight))); acumulator.Add(CanMoveOrAttack(chessboard, playerColor, position, position.Add(downLeft))); acumulator.Add(CanMoveOrAttack(chessboard, playerColor, position, position.Add(downRight))); } } } acumulator.RemoveAll(x => x == null); return(acumulator); }