public bool AddShip( IBoard board, BoardOrientation orientation, Point startingLocation, int length) { var coordinates = GetCoordinates(orientation, startingLocation, length) .ToList(); var maxRow = coordinates.Max(c => c.Location.X); var maxColumn = coordinates.Max(c => c.Location.Y); if (maxRow >= board.TotalRows || maxColumn >= board.TotalColumns) { return(false); } if (!board.IsVacant(coordinates.Select(c => c.Location))) { return(false); } var ship = new Ship(coordinates); return(board.AddShip(ship)); }
public static bool SelectAction(IBoard board) { Console.ForegroundColor = ConsoleColor.Blue; Console.WriteLine("\nPlease enter a command to perform an action:"); var input = Console.ReadLine(); if (string.IsNullOrWhiteSpace(input)) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Invalid input.\n "); SelectAction(board); } var args = input.Trim().Split(' '); try { switch (args[0].ToLower()) { case "--addship": var newShip = CreateShip(input); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine(newShip.ToString()); board.AddShip(newShip); Console.WriteLine("\nPress any key to redraw board..."); Console.ReadKey(); break; case "--takeattack": Console.ForegroundColor = ConsoleColor.White; var attackResult = board.TakeAttack(Mapper.MapCoordinates(input.ToLower().Replace("--takeattack", "").Trim())); Console.WriteLine($"{attackResult}\n"); if (attackResult == AttackResult.GameOver) { return(true); } Console.WriteLine("\nPress any key to redraw board..."); Console.ReadKey(); break; default: Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Invalid input.\n "); SelectAction(board); break; } } catch (Exception ex) { Console.ForegroundColor = ConsoleColor.Red; if (ex is ArgumentException || ex is FormatException) { Console.WriteLine($"{ex.Message}\n "); } else { Console.WriteLine($"{ex}\n "); } SelectAction(board); } return(false); }