Exemple #1
0
        public void UpdateSegmentRange(SortedDictionary <Coordinate, Segment> segments)
        {
            foreach (var segment in segments)
            {
                if (!BattleshipExtensions.IsSegmentWithInGridRange(segment.Key.X, segment.Key.Y))
                {
                    throw new IndexOutOfRangeException();
                }

                Segment item = segmentations.FirstOrDefault(q => q.Key.X == segment.Key.X && q.Key.Y == segment.Key.Y).Value;

                if (item != null)
                {
                    if (item.IsEmpty & segment.Value.IsEmpty)
                    {
                        throw new ArgumentException();
                    }

                    item.IsEmpty = false;
                    if (segment.Value.Ship != null)
                    {
                        item.Ship          = segment.Value.Ship;
                        item.ShipDirection = segment.Value.ShipDirection;
                    }
                }
            }
        }
Exemple #2
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);
        }
Exemple #3
0
        public void AddSegment(Coordinate coordinate, Segment segment)
        {
            if (!BattleshipExtensions.IsSegmentWithInGridRange(coordinate.X, coordinate.Y))
            {
                throw new IndexOutOfRangeException();
            }

            this.segmentation.Add(coordinate, new Segment(segment.Character));
        }
Exemple #4
0
        public void IsSegmentWithInGridRange_WhenYAxisIsWithinDimension_ReturnTrue()
        {
            // Arrange
            int x = XInitialPoint;
            int y = GridDimension;

            // Act
            bool result = BattleshipExtensions.IsSegmentWithInGridRange(x, y);

            // Assert
            Assert.IsTrue(result);
        }
Exemple #5
0
        public void IsSegmentWithInGridRange_WhenYAxisIsLessThanYAxisIndex_ReturnFalse()
        {
            // Arrange
            int x = XInitialPoint;
            int y = 0;

            // Act
            bool result = BattleshipExtensions.IsSegmentWithInGridRange(x, y);

            // Assert
            Assert.IsFalse(result);
        }
Exemple #6
0
        public void IsSegmentWithInGridRange_WhenXAxisIsGreaterThanDimension_ReturnFalse()
        {
            // Arrange
            int x = XInitialPoint + GridDimension + Index;
            int y = Index;

            // Act
            bool result = BattleshipExtensions.IsSegmentWithInGridRange(x, y);

            // Assert
            Assert.IsFalse(result);
        }
Exemple #7
0
        private bool AddToXAxis(int currentXPosition, int currentYPosition, IShip ship, ref SortedDictionary <Coordinate, Segment> temporarySegments)
        {
            bool result = false;

            // If the current segment position is valid add to the temporary list, otherwise clear the list and start again
            if (segments.IsSegmentAvailable(currentXPosition, currentYPosition) &&
                BattleshipExtensions.IsSegmentWithInGridRange(currentXPosition, currentYPosition))
            {
                temporarySegments.Add(new Coordinate(currentXPosition, currentYPosition), new Segment(ShipDirection.Horizontal, ship));
                result = true;
            }
            else
            {
                this.Clear(temporarySegments);
            }

            return(result);
        }
Exemple #8
0
        public async Task <string> CreatePlayer(string name, string surname, int numberOfShips)
        {
            if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(surname))
            {
                throw new ArgumentException();
            }

            List <IShip> getRandomShips = BattleshipExtensions.GetRandomShips(numberOfShips);
            SortedDictionary <Coordinate, Segment> ships =
                this.shipRandomiser.GetRandomisedShipCoordinates(getRandomShips);

            // serialise to save into database

            string shipCoordinates =
                JsonConvert.SerializeObject(ships.ToArray(), Formatting.Indented, this.jsonSerializerSettings);

            return(await this.gameRepository.CreatePlayer(name, surname, shipCoordinates));
        }
Exemple #9
0
        public void UpdateSegment(Coordinate coordinate, Segment segment)
        {
            if (!BattleshipExtensions.IsSegmentWithInGridRange(coordinate.X, coordinate.Y))
            {
                throw new IndexOutOfRangeException();
            }

            Segment item = segmentations.FirstOrDefault(q => q.Key.X == coordinate.X && q.Key.Y == coordinate.Y).Value;

            if (item != null)
            {
                item.IsEmpty   = false;
                item.Character = segment.Character;

                if (segment.Ship != null)
                {
                    item.Ship          = segment.Ship;
                    item.ShipDirection = segment.ShipDirection;
                }
            }
        }