public void CreateandAssignShips(ShipType shipType, int width, int height, string coordinates) { var shipsCoordinates = coordinates.Split(' '); // Excpecting that we will have only 2 playser in player list for (int i = 0; i < shipsCoordinates.Length; i++) { var coordinate = shipsCoordinates[i]; // validate coordinate length should be 2 if (coordinate == null || (coordinate != null && coordinate.Length != 2)) { throw new Exception(ErrorMessage.ShipTypeMessage); } char row = BattleShipHelper.GetCharacter(coordinate[0].ToString()); int column = BattleShipHelper.GetNumber(coordinate[1].ToString()); if (i % 2 == 0) { players?[0].SetShipinBattleArea(shipType, row, column, width, height); } else { players?[1].SetShipinBattleArea(shipType, row, column, width, height); } } }
public Tuple <char, int> GetBattleAreaCoordinates(string input) { if (string.IsNullOrEmpty(input)) { throw new Exception(ErrorMessage.BattleAreaMessage); } var arr = input.Split(' '); if (arr != null && (arr.Length > 2 || arr.Length < 2 || !BattleShipHelper.IsNumber(arr[0]) || !BattleShipHelper.IsCharacter(arr[1]))) { throw new Exception(ErrorMessage.BattleAreaMessage); } return(new Tuple <char, int>(BattleShipHelper.GetCharacter(arr[1]), BattleShipHelper.GetNumber(arr[0]))); }
public Tuple <ShipType, int, int, string> GetShipType(string input) { if (string.IsNullOrEmpty(input)) { throw new Exception(ErrorMessage.ShipTypeMessage); } var arr = input.Split(' '); if (arr != null && (arr.Length < 5 || !BattleShipHelper.IsCharacter(arr[0]) || !BattleShipHelper.IsNumber(arr[1]) || !BattleShipHelper.IsNumber(arr[2]))) { throw new Exception(ErrorMessage.ShipTypeMessage); } var shipType = (ShipType)BattleShipHelper.GetCharacter(arr[0]); var numberOfShips = BattleShipHelper.GetNumber(arr[1]); var numberOfShips1 = BattleShipHelper.GetNumber(arr[2]); var shipAreas = string.Join(" ", arr.Skip(3)); return(new Tuple <ShipType, int, int, string>(shipType, numberOfShips, numberOfShips1, shipAreas)); }