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 void Play() { // Expecting only 2 players if (players != null && players.Any() && players.Count == 2) { var playerOne = players[0]; var playerTwo = players[1]; int player_one_targetCount = playerOne.GetTargetsCount(); int player_two_targetCount = playerTwo.GetTargetsCount(); var playerTurn = playerOne; var targetPlayer = playerTwo; for (int i = 0; i < player_one_targetCount + player_two_targetCount; i++) { var target = playerTurn.GetTarget(); if (target == null) { Console.WriteLine($"{playerTurn.GetName()} has no more missiles left to launch"); var playersResult = this.GetCurrentandTargetPlayer(playerTurn, targetPlayer, playerOne, playerTwo); playerTurn = playersResult.Item1; targetPlayer = playersResult.Item2; continue; } // validate target char row = target[0]; int column = BattleShipHelper.GetNumber(target[1].ToString()); var response = playerTurn.Attack(targetPlayer, row, column); if (response == Missile.NoMissile) { Console.WriteLine($"{playerTurn.GetName()} has no more missiles left to launch"); } else if (response == Missile.Hit) { Console.WriteLine($"{playerTurn.GetName()} fires a missile with target {target} which got hit"); continue; } else if (response == Missile.Miss) { Console.WriteLine($"{playerTurn.GetName()} fires a missile with target {target} which got miss"); } // compare 2 player objects var players = this.GetCurrentandTargetPlayer(playerTurn, targetPlayer, playerOne, playerTwo); playerTurn = players.Item1; targetPlayer = players.Item2; } int player_one_shipsCount = playerOne.ShipsCount(); int player_two_shipsCount = playerTwo.ShipsCount(); DisplayResult(player_one_shipsCount, player_two_shipsCount, playerOne.GetName(), playerTwo.GetName()); } }
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)); }
public int GetNumberofPlayers(string input) { if (string.IsNullOrEmpty(input)) { throw new Exception(ErrorMessage.NumberofPlayerMessage); } if (!BattleShipHelper.IsNumber(input)) { throw new Exception(ErrorMessage.NumberofPlayerMessage); } int number = BattleShipHelper.GetNumber(input); if (number != 2) { throw new Exception(ErrorMessage.NumberofPlayerMessage); } return(number); }