Ejemplo n.º 1
0
        /// <summary>
        /// Method which handles a shot being fired within a Game
        /// </summary>
        /// <param name="game"></param>
        /// <param name="shot"></param>
        /// <returns></returns>
        public Game FireShot(Game game, GridCoords shot)
        {
            var indices = ArrayMapper.GetArrayIndicesForCoords(shot);

            var target = game.GameGrid.Cells[indices.X, indices.Y];

            switch (target.Status)
            {
            case GridCellStatus.OpenSea:
                game = ShotMissed(game, indices);
                break;

            case GridCellStatus.ShipIntact:
                game = ShotLanded(game, indices);
                break;

            case GridCellStatus.ShotLanded:
                AlreadyAttempted(game);
                break;

            case GridCellStatus.ShotMissed:
                AlreadyAttempted(game);
                break;
            }
            ;

            return(game);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Calculates the max size boat that can be started vertically from this Grid Cell
        /// </summary>
        public void CalculateVerticalBoatCapacity(Grid grid)
        {
            // Get the array indices for the current Grid Cell
            var gridRef = new GridCoords(XRef, YRef);
            var indices = ArrayMapper.GetArrayIndicesForCoords(gridRef);
            var x       = indices.X;
            var y       = indices.Y;

            // Get the height of the grid
            var height = grid.Cells.GetLength(0);

            // Reset
            VerticalBoatCapacity = 0;

            // Calculate
            for (int i = x; i < height; i++)
            {
                if (grid.Cells[x, i].Status == GridCellStatus.OpenSea)
                {
                    VerticalBoatCapacity++;
                }
                else
                {
                    VerticalBoatCapacity = 0;
                    break;
                }
            }
        }
Ejemplo n.º 3
0
        public void GetArrayIndicesForCoords_WhenArgumentsAreValid_ExpectSuccessfulMapping()
        {
            // Arrange
            _gridCoordsArgument   = new GridCoords(5, "D");
            _arrayIndicesResponse = new Indices(4, 3);

            // Act
            var result = ArrayMapper.GetArrayIndicesForCoords(_gridCoordsArgument);

            // Assert
            Assert.AreEqual(this._arrayIndicesResponse.X, result.X);
            Assert.AreEqual(this._arrayIndicesResponse.Y, result.Y);
        }
Ejemplo n.º 4
0
        public void GetCoordsForArrayIndices_WhenArgumentsAreValid_ExpectSuccessMapping()
        {
            // Arrange
            _arrayIndicesArgument = new Indices(6, 3);
            _gridCoordsResponse   = new GridCoords(7, "D");

            // Act
            var result = ArrayMapper.GetCoordsForArrayIndices(_arrayIndicesArgument);

            // Assert
            Assert.IsInstanceOfType(result, typeof(GridCoords));
            Assert.AreEqual(this._gridCoordsResponse.X, result.X);
            Assert.AreEqual(this._gridCoordsResponse.Y, result.Y);
        }
Ejemplo n.º 5
0
        public void GetArrayIndicesForCoords_WhenCoordsArgumentIsNull_ExpectArgumentNullException()
        {
            // Arrange
            _gridCoordsArgument = null;

            // Act
            try
            {
                var result = ArrayMapper.GetArrayIndicesForCoords(_gridCoordsArgument);
            } catch (Exception ex)
            {
                // Assert
                Assert.IsInstanceOfType(ex, typeof(ArgumentNullException));
                throw;
            }
        }
Ejemplo n.º 6
0
        public void TestMapper()
        {
            var map = new Dictionary <int, int> {
                { 2, 0 }, { 1, 1 }, { 0, 2 }
            };

            var stream = new MemoryStream(Encoding.UTF8.GetBytes(_testData));
            var parser = new CsvParser(stream).UseHeaders();
            var mapper = new ArrayMapper(parser, map);

            while (parser.Read())
            {
                var y = parser.CurrentRecord;
                var x = mapper.CurrentRecord;
            }
        }
Ejemplo n.º 7
0
        public void GetArrayIndicesForCoords_WhenTheXCoordIsInvalid_ExpectArgumentException()
        {
            // Arrange
            _gridCoordsArgument = new GridCoords(-1, "E");

            // Act
            try
            {
                var result = ArrayMapper.GetArrayIndicesForCoords(_gridCoordsArgument);
            }
            catch (Exception ex)
            {
                // Assert
                Assert.IsInstanceOfType(ex, typeof(ArgumentOutOfRangeException));
                throw;
            }
        }
Ejemplo n.º 8
0
        public void GetCoordsForArrayIndices_WhenIndicesAreInvalid_ExpectArgumentException()
        {
            // Arrange
            _arrayIndicesArgument = null;

            // Act
            try
            {
                var result = ArrayMapper.GetCoordsForArrayIndices(_arrayIndicesArgument);
            }
            catch (Exception ex)
            {
                // Assert
                Assert.IsInstanceOfType(ex, typeof(ArgumentNullException));
                throw;
            }
        }