/// <summary> /// Tries to shoot at a coordinate /// </summary> /// <param name="input">string coordinate in the format [a-j][0-10]</param> /// <returns>IsValid is a bool indicating whether or not it is possible to shoot at the location, message is the returning message</returns> public (bool isValid, string message) ShootAtEnemy(string input) { input = input.ToLower(); var validationResult = Validators.IsValidCoordinate(input); if (!validationResult.isValid) { return(false, validationResult.message); } //burde opdatere dette til next player eller enemyplayer properties/metoder Player enemyPlayer = Players[(Turns + 1) % 2]; FlatMap enemyShipMap = enemyPlayer.ShipMap; Player player = Players[GetPlayerTurn]; var coordinate = Converters.StringToCoordinate(input); //this is pretty ugly :/ if (ShipsAvailableInGame.Keys.Where(ship => ship.Icon == enemyPlayer.ShipMap.Map[coordinate.y, coordinate.x]).Count() == 1) { enemyShipMap.MarkCoordinate(input, 'x'); player.HitMap.MarkCoordinate(input, 'x'); return(true, "You hit a ship"); } else { enemyShipMap.MarkCoordinate(input, 'o'); player.HitMap.MarkCoordinate(input, 'o'); return(true, "You missed"); } }
/// <summary> /// Greeting message at the start of each round /// </summary> /// <returns>A greeting message</returns> public string StartRoundMsg() { Player player = Players[GetPlayerTurn]; string output = $"{player.Name} It is your turn, here is your maps: \n" + $"Placement of your ships: Your shots at the opponents board: \n"; string[] hitMap = FlatMap.CreateMap(player.HitMap); string[] shipMap = FlatMap.CreateMap(player.ShipMap); for (int i = 0; i < hitMap.Length; i++) { output += shipMap[i] + " " + hitMap[i]; output += "\n"; } output += "Enter a coordinate on the opponents map, by entering coordinates in the format 'c3' or 'd8'"; return(output); }
/// <summary> /// Creates a visual representation of map as an array of lines /// </summary> /// <returns>an array of lines in a multiline map</returns> public static string[] CreateMap(FlatMap map) { List <string> output = new List <string>(); string[,] workingMap = map.Map; output.Add(" | a | b | c | d | e | f | g | h | i | j |"); output.Add(" -----------------------------------------"); for (int y = 0; y < workingMap.GetLength(0); y++) { string line = y + " |"; for (int x = 0; x < workingMap.GetLength(1); x++) { line += $" {workingMap[y, x]} |"; } output.Add(line); output.Add(" -----------------------------------------"); } return(output.ToArray()); }