public MoveResult ProcessAMove(string move, Player currentPlayer) { try { var cells = move.Trim().Split(' '); if (cells.Length != 2) { throw new IndexOutOfRangeException(); } //Parse and Validate starting cell var(x, y) = ParseCellNum(cells[0]); From = MainBoard.GetCell(x, y); if (From != null && !From.IsOccupied()) { return(MoveResult.EmptyBeginningCell); } if (From != null && From.IsOccupied() && !From.Piece.IsSameSide(currentPlayer)) { return(MoveResult.WrongBeginningCell); } //Parse and Validate destination cell (x, y) = ParseCellNum(cells[1]); To = MainBoard.GetCell(x, y); if (To != null && To.IsOccupied() && To.Piece.IsSameSide(currentPlayer)) { return(MoveResult.WrongDestinationCell); } var possibleMoves = GetPossibleMoves(From); if (!possibleMoves.Contains(To)) { return(MoveResult.ImpossibleMove); } return(PerformAMove()); } catch { return(MoveResult.WrongInputFormat); } }
private List <Cell> GetPossibleMoves(Cell Start) { var possibleMoves = new List <Cell>(); if (Start?.Piece == null) { return(possibleMoves); } var x = Start.X; var y = Start.Y; var player = Start.Piece.Player; var isContinouos = Start.Piece.IsContinuous; foreach (var direction in Start.Piece.MoveDirections.Where(md => !md.FirstMove || (md.FirstMove && IsFirstMove(Start.Piece)))) { for (var i = 1; i < (isContinouos ? MainBoard.Rank : 2); i++) { var cell = MainBoard.GetCell(x + direction.X * i, y + direction.Y * i); if (cell == null) { break; } if (cell.IsOccupied()) { if (!cell.Piece.IsSameSide(player)) { possibleMoves.Add(cell); } break; } if (!direction.OnlyWithAttack) { possibleMoves.Add(cell); } } } return(possibleMoves); }