/// <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 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); }