Exemple #1
0
        public void IsShipDrowned_Should_return_true_or_false_based_on_ship_status(bool isHit, bool expected)
        {
            // Arrange
            var battleBoard = new BattleBoard()
            {
                BoardCells = new BoardCell[6, 6], Length = 3, BoardSize = 5
            };

            battleBoard.BoardCells[1, 2] = new BoardCell {
                IsEmpty = false, IsHit = isHit
            };
            battleBoard.BoardCells[1, 3] = new BoardCell {
                IsEmpty = false, IsHit = isHit
            };
            battleBoard.BoardCells[1, 4] = new BoardCell {
                IsEmpty = false, IsHit = isHit
            };

            var response = Tuple.Create <bool, BattleBoard>(true, battleBoard);

            cacheService.Setup(x => x.GetDataFromCache <BattleBoard>(It.IsAny <string>())).Returns(response);

            // Act
            var actual = validationService.IsShipDrowned();

            // Assert
            Assert.IsNotNull(actual);
            Assert.AreEqual(expected, actual);
        }
Exemple #2
0
        public override List <Hit> GenerateHits(BattleBoard board, Point target)
        {
            var grid = GenerateImpactGrid();
            var hits = new List <Hit>();

            for (var x = 0; x < grid.Size.Width; x++)
            {
                for (var y = 0; y < grid.Size.Height; y++)
                {
                    if (grid.Weight[x, y] < 1)
                    {
                        continue;
                    }

                    var currentTarget = new Point(target.X - 12 + x, target.Y - 12 + y);

                    var combatant = board.GetCharacterAt(currentTarget);

                    if (combatant == null || !CanTarget(combatant.Faction))
                    {
                        continue;
                    }

                    hits.Add(new Hit {
                        Target   = currentTarget,
                        Critical = 50,
                        Damage   = (int)(7 * (grid.Weight[x, y] / 255.0)),
                    });
                }
            }

            return(hits);
        }
        private async Task <Ship> FillCells(BattleBoard board, Point startPoint, ShipDirection direction)
        {
            var ship = new Ship
            {
                Direction = direction,
                Cells     = new List <BoardCell>()
            };

            if (direction == ShipDirection.Horizontal)
            {
                for (int i = startPoint.X; i < startPoint.X + board.ShipLength; i++)
                {
                    board.Cells[i, startPoint.Y].Status = CellStatus.HasShip;
                    ship.Cells.Add(board.Cells[i, startPoint.Y]);
                }

                board.Ships.Add(ship);
                _logger.LogInformation($"Filled in the cells from cell ({startPoint.X},{startPoint.Y}) to cell ({startPoint.X + board.ShipLength},{startPoint.Y})");
            }
            else
            {
                for (int j = startPoint.Y; j < startPoint.Y + board.ShipLength; j++)
                {
                    board.Cells[startPoint.X, j].Status = CellStatus.HasShip;
                    ship.Cells.Add(board.Cells[startPoint.X, j]);
                }
                board.Ships.Add(ship);
                _logger.LogInformation($"Filled in the cells from cell ({startPoint.X},{startPoint.Y}) to cell ({startPoint.X },{startPoint.Y + board.ShipLength})");
            }
            return(await Task.FromResult(ship));
        }
Exemple #4
0
        public void IsInRangeToAttack_Should_return_true_or_false_based_on_input(int xPosition, int yPosition, bool expected)
        {
            // Arrange
            var battleBoard = new BattleBoard()
            {
                BoardCells = new BoardCell[6, 6], Length = 2, BoardSize = 5
            };

            battleBoard.BoardCells[1, 2] = new BoardCell {
                IsEmpty = false, IsHit = false
            };
            battleBoard.BoardCells[1, 3] = new BoardCell {
                IsEmpty = false, IsHit = false
            };
            battleBoard.BoardCells[1, 4] = new BoardCell {
                IsEmpty = false, IsHit = false
            };

            var response = Tuple.Create <bool, BattleBoard>(true, battleBoard);

            cacheService.Setup(x => x.GetDataFromCache <BattleBoard>(It.IsAny <string>())).Returns(response);

            // Act
            var actual = validationService.IsInRangeToAttack(xPosition, yPosition);

            // Assert
            Assert.IsNotNull(actual);
            Assert.AreEqual(expected, actual);
        }
Exemple #5
0
        public void Attack_Should_return_hit_or_miss_based_on_input_matches_ship_coordinates_or_not(int xPosition,
                                                                                                    int yPosition, string expected)
        {
            // Arrange
            var battleBoard = new BattleBoard()
            {
                BoardCells = new BoardCell[6, 6], Length = 2
            };

            battleBoard.BoardCells[3, 2] = new BoardCell {
                IsEmpty = false, IsHit = false
            };
            battleBoard.BoardCells[4, 2] = new BoardCell {
                IsEmpty = false, IsHit = false
            };
            var response = Tuple.Create <bool, BattleBoard>(true, battleBoard);

            cacheService.Setup(x => x.GetDataFromCache <BattleBoard>(It.IsAny <string>())).Returns(response);
            validationService.Setup(x => x.IsInRangeToAttack(It.IsAny <int>(), It.IsAny <int>())).Returns(true);

            // Act
            var actual = trackerService.Attack(xPosition, yPosition);

            // Assert
            Assert.AreEqual(expected, actual);
            validationService.Verify(x => x.IsInRangeToAttack(It.IsAny <int>(), It.IsAny <int>()), Times.Once);
            cacheService.Verify(x => x.GetDataFromCache <BattleBoard>(It.IsAny <string>()), Times.Once);
        }
Exemple #6
0
        public override List<Hit> GenerateHits(BattleBoard board, Point target)
        {
            var grid = GenerateImpactGrid();
            var hits = new List<Hit>();

            for(var x = 0; x < grid.Size.Width; x++)
            {
                for(var y = 0; y < grid.Size.Height; y++)
                {
                    if (grid.Weight[x, y] < 1) continue;

                    var currentTarget = new Point(target.X - 12 + x, target.Y - 12 + y);

                    var combatant = board.GetCharacterAt(currentTarget);

                    if(combatant == null || ! CanTarget(combatant.Faction)) continue;

                    hits.Add(new Hit {
                            Target = currentTarget,
                            Critical = 50,
                            Damage = (int)(7 * (grid.Weight[x, y] / 255.0)),
                    });
                }
            }

            return hits;
        }
        private bool IsValidPoint(BattleBoard board, Point startPoint)
        {
            if (startPoint.X >= 0 && startPoint.X < board.Size && startPoint.Y >= 0 && startPoint.Y < board.Size)
            {
                return(true);
            }

            return(false);
        }
Exemple #8
0
        public void ShouldNotAddShipWhenShipIsNull()
        {
            // Arrange
            int         width  = 45;
            int         height = 34;
            BattleBoard board  = new BattleBoard(width, height);

            // Act
            board.OnBoardShips(null, "A1");
            // Assert
            Assert.IsTrue(board.HaveAllShipDestroyed());
        }
Exemple #9
0
 public override List<Hit> GenerateHits(BattleBoard board, Point target)
 {
     return new List<Hit>
         {
             new Hit
                 {
                     Critical = 50,
                     Damage = 10,
                     Delay = 500,
                     Target = target
                 }
         };
 }
        public void SetBoard(BattleBoard board)
        {
            _board = board;

            foreach (var character in _board.Characters)
            {
                character.Avatar.Sprite.X         = (int)(character.Avatar.Location.X * 50 + 25 - character.Avatar.Sprite.Width / 2.0) + OffsetX();
                character.Avatar.Sprite.Y         = (int)(character.Avatar.Location.Y * 50 + 25 - character.Avatar.Sprite.Height + character.Avatar.GetFeet().Height / 2.0) + OffsetY();
                character.Avatar.Sprite.DrawOrder = (int)character.Avatar.Sprite.Y;
                _characters.Add(character.Name, character.Avatar.Sprite);
                Components.Add(character.Avatar.Sprite);
            }
        }
Exemple #11
0
        public void ShouldAddShipWhichHaveValidLocations()
        {
            // Arrange
            int         width  = 5;
            int         height = 5;
            BattleBoard board  = new BattleBoard(width, height);

            // Act
            board.OnBoardShips(new Ship(), "A1");
            board.OnBoardShips(new Ship(), "E3");
            // Assert
            Assert.IsFalse(board.HaveAllShipDestroyed());
        }
Exemple #12
0
 public override List <Hit> GenerateHits(BattleBoard board, Point target)
 {
     return(new List <Hit>
     {
         new Hit
         {
             Critical = 50,
             Damage = 10,
             Delay = 500,
             Target = target
         }
     });
 }
Exemple #13
0
        public void ShouldNotAddShipWhichHaveInvalidLocations()
        {
            // Arrange
            int         width  = 5;
            int         height = 5;
            BattleBoard board  = new BattleBoard(width, height);

            // Act
            board.OnBoardShips(new Ship(), "Q2");
            board.OnBoardShips(new Ship(), "P2");
            // Assert
            Assert.IsTrue(board.HaveAllShipDestroyed());
        }
Exemple #14
0
        public void ShouldNotOnBoardShipsHavingInvalidDimensions()
        {
            // Arrange
            int         width  = 5;
            int         height = 5;
            BattleBoard board  = new BattleBoard(width, height);
            Player      player = new Player();

            player.Board = board;
            // Act
            player.FormateFleet(23, 23, "A1", ShipType.TYPE_P);
            // Assert
            Assert.IsTrue(player.Board.HaveAllShipDestroyed());
        }
Exemple #15
0
        public void ShouldReturnValidShipFieldsWhenCordinatesAreValid()
        {
            // Arrange
            int         width  = 5;
            int         height = 5;
            BattleBoard board  = new BattleBoard(width, height);

            // Act
            board.OnBoardShips(new Ship(), "A1");
            var a = board.GetCordinates("A1");

            // Assert
            Assert.IsTrue(a.Shot() == ShotResult.DESTROYED);
        }
Exemple #16
0
        public override List<Hit> GenerateHits(BattleBoard board, Point target)
        {
            if (!CanHit(board.Sandbag, target)) return new List<Hit>();

            return new List<Hit>
                {
                    new Hit
                        {
                            Critical = 0,
                            Damage = -12,
                            Delay = 200,
                            Target = target
                        }
                };
        }
Exemple #17
0
        public void ShouldCleanValidShipFields()
        {
            // Arrange
            int         width  = 5;
            int         height = 5;
            BattleBoard board  = new BattleBoard(width, height);

            // Act
            board.OnBoardShips(new Ship(), "A1");
            board.OnBoardShips(new Ship(), "E3");
            board.ClearShipField("A1");
            board.ClearShipField("E3");
            // Assert
            Assert.IsTrue(board.HaveAllShipDestroyed());
        }
Exemple #18
0
        public void ShouldNotAddMissileTargetsWhenNotValid()
        {
            // Arrange
            int         width  = 5;
            int         height = 5;
            BattleBoard board  = new BattleBoard(width, height);
            Player      player = new Player();

            player.Board = board;
            // Act
            player.AddMissiles(new List <string> {
                "P1", "K2", "B6"
            });

            // Assert
            Assert.IsTrue(player.HaveMissileFinished());
        }
Exemple #19
0
        public override List <Hit> GenerateHits(BattleBoard board, Point target)
        {
            if (!CanHit(board.Sandbag, target))
            {
                return(new List <Hit>());
            }

            return(new List <Hit>
            {
                new Hit
                {
                    Critical = 0,
                    Damage = -12,
                    Delay = 200,
                    Target = target
                }
            });
        }
 private bool IsInsideBoard(BattleBoard board, Point startPoint, ShipDirection direction)
 {
     if (direction == ShipDirection.Horizontal)
     {
         if ((startPoint.X + board.ShipLength) < board.Size)
         {
             return(true);
         }
     }
     else
     {
         if ((startPoint.Y + board.ShipLength) < board.Size)
         {
             return(true);
         }
     }
     return(false);
 }
Exemple #21
0
        public void ShouldHitTheDeckIfMissileCordinatesAreValid()
        {
            // Arrange
            int         width   = 5;
            int         height  = 5;
            BattleBoard board   = new BattleBoard(width, height);
            Player      playerA = new Player();

            playerA.Board = board;
            playerA.AddMissiles(new List <string> {
                "A1"
            });


            BattleBoard boardB  = new BattleBoard(width, height);
            Player      playerB = new Player();

            playerB.Board = boardB;
            playerB.FormateFleet(1, 1, "A1", ShipType.TYPE_P);
            //Act
            playerA.LaunchMissileToTarget(playerB);
            //Assert
            Assert.IsTrue(playerA.HaveMissileFinished());
        }
Exemple #22
0
        public void IsBoardCellEmpty_Should_return_true_or_false_based_on_cell_state(Orientation orientation,
                                                                                     int xPosition, int yPostiion, int endPosition, bool isEmpty, bool expected)
        {
            // Arrange
            var battleBoard = new BattleBoard()
            {
                BoardCells = new BoardCell[6, 6], Length = 2, BoardSize = 5
            };

            battleBoard.BoardCells[xPosition, yPostiion] = new BoardCell {
                IsEmpty = isEmpty, IsHit = false
            };

            var response = Tuple.Create <bool, BattleBoard>(true, battleBoard);

            cacheService.Setup(x => x.GetDataFromCache <BattleBoard>(It.IsAny <string>())).Returns(response);

            // Act
            var actual = validationService.IsBoardCellEmpty(xPosition, yPostiion, endPosition, orientation);

            // Assert
            Assert.IsNotNull(actual);
            Assert.AreEqual(expected, actual);
        }
Exemple #23
0
        public void ShouldClearBattleAreaToAfterAllHit()
        {
            // Arrange
            int         width   = 5;
            int         height  = 5;
            BattleBoard board   = new BattleBoard(width, height);
            Player      playerA = new Player();

            playerA.Board = board;
            playerA.AddMissiles(new List <string> {
                "A1", "A1"
            });


            BattleBoard boardB  = new BattleBoard(width, height);
            Player      playerB = new Player();

            playerB.Board = boardB;
            playerB.FormateFleet(1, 1, "A1", ShipType.TYPE_Q);
            //Act
            playerA.LaunchMissileToTarget(playerB);
            //Assert
            Assert.IsTrue(playerB.Board.HaveAllShipDestroyed());
        }
 private bool AreCellsEmpty(BattleBoard board, Point startPoint, ShipDirection direction)
 {
     if (direction == ShipDirection.Horizontal)
     {
         for (int i = startPoint.X; i < startPoint.X + board.ShipLength; i++)
         {
             if (board.Cells[i, startPoint.Y].Status != CellStatus.Empty)
             {
                 return(false);
             }
         }
     }
     else
     {
         for (int j = startPoint.Y; j < startPoint.Y + board.ShipLength; j++)
         {
             if (board.Cells[startPoint.X, j].Status != CellStatus.Empty)
             {
                 return(false);
             }
         }
     }
     return(true);
 }
Exemple #25
0
        /// <summary>
        /// Initialize a battle sequence by name. This call happens at the start of a battle and is required
        /// for this scene to not die a horrible death.
        /// </summary>
        /// <param name="battleName">Name of the battle to be initialized</param>
        public void SetBattle(string battleName)
        {
            // set up defaults

            BattleBoard = new BattleBoard();

            RoundNumber = 0;
            FactionTurn = 0;
            _state      = BattleState.PlayerTurn;
            var partyGrid = new List <Point>();

            _battleBoardLayer = new BattleBoardLayer(this, null);
            _battleBoardLayer.CharacterSelected += SelectCharacter;

            Components.Add(_battleBoardLayer);

            switch (battleName)
            {
            case "coliseum/halls":
                _battleBoardLayer.SetBackground("Zones/Coliseum/Halls/north");
                _battleBoardLayer.SetGrid("Zones/Coliseum/Halls/battle");
                BattleBoard.Sandbag = Grid.FromBitmap(Game.Services, "Zones/Coliseum/Halls/battle");
                _battleBoardLayer.AddImage(new ImageObject(Game, _battleBoardLayer, "Zones/Coliseum/Halls/north"));
                _battleBoardLayer.AddImage(new ImageObject(Game, _battleBoardLayer, "Zones/Coliseum/pillar")
                {
                    X = 393, Y = 190, DrawOrder = 248
                });
                _battleBoardLayer.AddImage(new ImageObject(Game, _battleBoardLayer, "Zones/Coliseum/pillar")
                {
                    X = 393, Y = 390, DrawOrder = 448
                });
                _battleBoardLayer.AddImage(new ImageObject(Game, _battleBoardLayer, "Zones/Coliseum/pillar")
                {
                    X = 393, Y = 590, DrawOrder = 648
                });
                _battleBoardLayer.AddImage(new ImageObject(Game, _battleBoardLayer, "Zones/Coliseum/pillar")
                {
                    X = 593, Y = 190, DrawOrder = 248
                });
                _battleBoardLayer.AddImage(new ImageObject(Game, _battleBoardLayer, "Zones/Coliseum/pillar")
                {
                    X = 593, Y = 390, DrawOrder = 448
                });
                _battleBoardLayer.AddImage(new ImageObject(Game, _battleBoardLayer, "Zones/Coliseum/pillar")
                {
                    X = 593, Y = 590, DrawOrder = 648
                });

                partyGrid.Add(new Point(10, 13));
                partyGrid.Add(new Point(11, 13));
                partyGrid.Add(new Point(9, 13));
                partyGrid.Add(new Point(10, 12));
                partyGrid.Add(new Point(11, 12));
                partyGrid.Add(new Point(9, 12));

                BattleBoard.Characters.Add(GenerateCombatant("Guard 1", "coliseum/guard", new Vector2(9, 3)));
                BattleBoard.Characters.Add(GenerateCombatant("Guard 2", "coliseum/guard", new Vector2(11, 3)));
                BattleBoard.Characters.Add(GenerateCombatant("Guard Captain", "coliseum/guard", new Vector2(10, 3)));

                BattleBoard.Characters.Add(GenerateCombatant("Guard 3", "coliseum/guard", new Vector2(9, 10)));
                BattleBoard.Characters.Add(GenerateCombatant("Guard 4", "coliseum/guard", new Vector2(11, 10)));
                BattleBoard.Characters.Add(GenerateCombatant("Guard 5", "coliseum/guard", new Vector2(10, 10)));

                _startingDialog = Dialog.Fetch("coliseum", "guards");

                break;

            default:
                throw new Exception("unknown battle " + battleName);
            }

            // add party to the party grid for this battle, in order
            for (var i = 0; i < ((SRPGGame)Game).Party.Count; i++)
            {
                var character = ((SRPGGame)Game).Party[i];
                character.Avatar.Location.X = partyGrid[i].X;
                character.Avatar.Location.Y = partyGrid[i].Y;
                BattleBoard.Characters.Add(character);
            }

            _battleBoardLayer.SetBoard(BattleBoard);
            _commander.BattleBoard = BattleBoard;

            // center camera on partyGrid[0]
            UpdateCamera(
                0 - partyGrid[0].X * 50 + Game.GraphicsDevice.Viewport.Width / 2,
                0 - partyGrid[0].Y * 50 + Game.GraphicsDevice.Viewport.Height / 2
                );

            ChangeFaction(0);
        }
Exemple #26
0
 /// <summary>
 /// Indicate whether or not the character is being threatened. Being threatened is defined as being within attack
 /// range of any regular attacks. Regular attacks do not include active or passive abilities and are strictly limited
 /// to weapon attacks.
 /// </summary>
 /// <param name="board">The game board for the current battle. This will be asked about enemy positioning.</param>
 /// <returns></returns>
 public bool IsThreatened(BattleBoard board)
 {
     throw new NotImplementedException();
 }
        public void SetBoard(BattleBoard board)
        {
            _board = board;

            foreach (var character in _board.Characters)
            {
                character.Avatar.Sprite.X = (int)(character.Avatar.Location.X * 50 + 25 - character.Avatar.Sprite.Width / 2.0) + OffsetX();
                character.Avatar.Sprite.Y = (int)(character.Avatar.Location.Y * 50 + 25 - character.Avatar.Sprite.Height + character.Avatar.GetFeet().Height / 2.0) + OffsetY();
                character.Avatar.Sprite.DrawOrder = (int)character.Avatar.Sprite.Y;
                _characters.Add(character.Name, character.Avatar.Sprite);
                Components.Add(character.Avatar.Sprite);
            }
        }
 private bool IsShipCreatable(BattleBoard board, Point startPoint, ShipDirection direction)
 {
     return(IsInsideBoard(board, startPoint, direction) && AreCellsEmpty(board, startPoint, direction));
 }
Exemple #29
0
        /// <summary>
        /// Initialize a battle sequence by name. This call happens at the start of a battle and is required
        /// for this scene to not die a horrible death.
        /// </summary>
        /// <param name="battleName">Name of the battle to be initialized</param>
        public void SetBattle(string battleName)
        {
            // set up defaults

            BattleBoard = new BattleBoard();

            RoundNumber = 0;
            FactionTurn = 0;
            _state = BattleState.PlayerTurn;
            var partyGrid = new List<Point>();

            _battleBoardLayer = new BattleBoardLayer(this, null);
            _battleBoardLayer.CharacterSelected += SelectCharacter;

            Components.Add(_battleBoardLayer);

            switch(battleName)
            {
                case "coliseum/halls":
                    _battleBoardLayer.SetBackground("Zones/Coliseum/Halls/north");
                    _battleBoardLayer.SetGrid("Zones/Coliseum/Halls/battle");
                    BattleBoard.Sandbag = Grid.FromBitmap(Game.Services, "Zones/Coliseum/Halls/battle");
                    _battleBoardLayer.AddImage(new ImageObject(Game, _battleBoardLayer, "Zones/Coliseum/Halls/north"));
                    _battleBoardLayer.AddImage(new ImageObject(Game, _battleBoardLayer, "Zones/Coliseum/pillar") { X = 393, Y = 190, DrawOrder = 248 });
                    _battleBoardLayer.AddImage(new ImageObject(Game, _battleBoardLayer, "Zones/Coliseum/pillar") { X = 393, Y = 390, DrawOrder = 448 });
                    _battleBoardLayer.AddImage(new ImageObject(Game, _battleBoardLayer, "Zones/Coliseum/pillar") { X = 393, Y = 590, DrawOrder = 648 });
                    _battleBoardLayer.AddImage(new ImageObject(Game, _battleBoardLayer, "Zones/Coliseum/pillar") { X = 593, Y = 190, DrawOrder = 248 });
                    _battleBoardLayer.AddImage(new ImageObject(Game, _battleBoardLayer, "Zones/Coliseum/pillar") { X = 593, Y = 390, DrawOrder = 448 });
                    _battleBoardLayer.AddImage(new ImageObject(Game, _battleBoardLayer, "Zones/Coliseum/pillar") { X = 593, Y = 590, DrawOrder = 648 });

                    partyGrid.Add(new Point(10,13));
                    partyGrid.Add(new Point(11,13));
                    partyGrid.Add(new Point(9,13));
                    partyGrid.Add(new Point(10,12));
                    partyGrid.Add(new Point(11,12));
                    partyGrid.Add(new Point(9,12));

                    BattleBoard.Characters.Add(GenerateCombatant("Guard 1", "coliseum/guard", new Vector2(9, 3)));
                    BattleBoard.Characters.Add(GenerateCombatant("Guard 2", "coliseum/guard", new Vector2(11, 3)));
                    BattleBoard.Characters.Add(GenerateCombatant("Guard Captain", "coliseum/guard", new Vector2(10, 3)));

                    BattleBoard.Characters.Add(GenerateCombatant("Guard 3", "coliseum/guard", new Vector2(9, 10)));
                    BattleBoard.Characters.Add(GenerateCombatant("Guard 4", "coliseum/guard", new Vector2(11, 10)));
                    BattleBoard.Characters.Add(GenerateCombatant("Guard 5", "coliseum/guard", new Vector2(10, 10)));

                    _startingDialog = Dialog.Fetch("coliseum", "guards");

                    break;
                default:
                    throw new Exception("unknown battle " + battleName);
            }

            // add party to the party grid for this battle, in order
            for(var i = 0; i < ((SRPGGame)Game).Party.Count; i++)
            {
                var character = ((SRPGGame) Game).Party[i];
                character.Avatar.Location.X = partyGrid[i].X;
                character.Avatar.Location.Y = partyGrid[i].Y;
                BattleBoard.Characters.Add(character);
            }

            _battleBoardLayer.SetBoard(BattleBoard);
            _commander.BattleBoard = BattleBoard;

            // center camera on partyGrid[0]
            UpdateCamera(
                0 - partyGrid[0].X*50 + Game.GraphicsDevice.Viewport.Width/2,
                0 - partyGrid[0].Y*50 + Game.GraphicsDevice.Viewport.Height/2
            );

            ChangeFaction(0);
        }