Exemplo n.º 1
0
        public string GetUserInput(string userInput)
        {
            string message = string.Empty;

            if (userInput.Length > 3 || userInput.Length == 1 || string.IsNullOrEmpty(userInput))
            {
                throw new ArgumentException();
            }

            string userInputX = userInput.Substring(0, Index).ToUpper();

            char.TryParse(userInputX, out char x);

            string userInputY = userInput.Substring(Index, userInput.Length - Index);

            int.TryParse(userInputY, out int y);

            if (!BattleshipExtensions.IsSegmentWithInGridRange(x, y))
            {
                throw new ArgumentException();
            }

            var segment = segmentation.GetSegment(x, y);

            if (segment.Character == Miss || segment.Character == Hit)
            {
                return(CoordinateTriedMessage);
            }

            Coordinate coordinate = new Coordinate(x, y);

            if (segment.Ship == null)
            {
                segment.IsEmpty   = false;
                segment.Character = Miss;
                segmentation.UpdateSegment(coordinate, segment);
                message = MissMessage;
                playerStats.Miss++;
            }

            if (segment.Ship != null)
            {
                segment.IsEmpty   = false;
                segment.Character = Hit;
                segment.Ship.ShipHit++;
                segmentation.UpdateSegment(coordinate, segment);
                message = HitMessage;
                playerStats.Hit++;
                if (segment.Ship.IsShipSunk)
                {
                    playerStats.Sunk++;
                    message = SunkMessage;
                }
            }

            return(message);
        }