public void GetHitResult_HitsShipFourTimes_ReturnsSink() { int panelSize = 10; IShipManager shipManager = Substitute.For <IShipManager>(); var destroyerType = new ShipType("Destroyer", "D", 4); var shipsByType = new Dictionary <ShipType, int> { { destroyerType, 1 } }; var ship = new Ship(new Point(3, 2), new Point(3, 5), destroyerType); var shipList = new List <Ship> { ship }; shipManager.GetShipsByType().Returns(shipsByType); shipManager.GetShipList(shipsByType, panelSize).Returns(shipList); GameService target = new GameService(shipManager); target.InitializeGame(panelSize); target.Hit(new Point(3, 2)); target.Hit(new Point(3, 3)); target.Hit(new Point(3, 4)); var result = target.Hit(new Point(3, 5)); result.Should().Be(HitTypeEnum.Sink); }
public void GameOver_WithOneShipSinked_ReturnsTrue() { int panelSize = 10; IShipManager shipManager = Substitute.For <IShipManager>(); var destroyerType = new ShipType("Destroyer", "D", 4); var shipsByType = new Dictionary <ShipType, int> { { destroyerType, 1 } }; var ship = new Ship(new Point(3, 2), new Point(3, 5), destroyerType); var shipList = new List <Ship> { ship }; shipManager.GetShipsByType().Returns(shipsByType); shipManager.GetShipList(shipsByType, panelSize).Returns(shipList); GameService target = new GameService(shipManager); target.InitializeGame(panelSize); target.Hit(new Point(3, 2)); target.Hit(new Point(3, 3)); target.Hit(new Point(3, 4)); target.Hit(new Point(3, 5)); target.GameOver().Should().BeTrue(); }
public void GetHitList_WithFourHits_ReturnsListWithAllOfThem() { int panelSize = 10; IShipManager shipManager = Substitute.For <IShipManager>(); var destroyerType = new ShipType("Destroyer", "D", 4); var shipsByType = new Dictionary <ShipType, int> { { destroyerType, 1 } }; var ship = new Ship(new Point(3, 2), new Point(3, 5), destroyerType); var shipList = new List <Ship> { ship }; shipManager.GetShipsByType().Returns(shipsByType); shipManager.GetShipList(shipsByType, panelSize).Returns(shipList); GameService target = new GameService(shipManager); target.InitializeGame(panelSize); var missPoint = new Point(0, 0); var hitPoint1 = new Point(3, 2); var hitPoint2 = new Point(3, 3); var hitPoint3 = new Point(3, 4); var hitList = new List <Point> { missPoint, hitPoint1, hitPoint2, hitPoint3 }; target.Hit(missPoint); target.Hit(hitPoint1); target.Hit(hitPoint2); target.Hit(hitPoint3); target.GetHitList().Should().Contain(hitList); }
public ShotManager( IShipManager shipManager, IFireShotResponse fireShotResponse, IBoard board, ILog log) { _shipManager = shipManager ?? throw new ArgumentNullException(nameof(shipManager)); _fireShotResponse = fireShotResponse ?? throw new ArgumentNullException(nameof(fireShotResponse)); _board = board ?? throw new ArgumentNullException(nameof(board)); _log = log ?? throw new ArgumentNullException(nameof(log)); }
public void StartTransmittingData(IPlayerManager playerManager, IShipManager shipManager , IBulletManager bulletManager, BulletCollision bulletCollision, ShipCollision shipCollision) { playerManager.SendPlayerPosition += shipManager.ReceivePlayerPosition; shipManager.SendShipsPositions += bulletManager.ReceiveShipsPositions; shipManager.SendNewWeapons += bulletManager.SubscribeForNewWeapons; bulletCollision.bulletCollision += bulletManager.OnBulletCollision; bulletCollision.bulletCollidesWithAShip += shipManager.OnBulletCollision; bulletCollision.bulletCollidesWithAShip += playerManager.OnBulletCollision; shipCollision.shipCollidesWithAnotherShip += shipManager.OnShipCollision; shipManager.SendMessageWhenShipDestroyed += playerManager.UpdateScore; shipManager.ShipCollidesWithPlayer += playerManager.UpdateHealth; }
public Game() { this.satellite = new Satellite(); this.playerManager = new PlayerManager(); this.bulletManager = new BulletManager(); this.shipManager = new ShipManager(); this.bulletCollision = new BulletCollision(); this.shipCollision = new ShipCollision(); this.satellite.StartTransmittingData(playerManager, shipManager, bulletManager, bulletCollision, shipCollision); this.shipManager.GenerateShips(); this.playerManager.CreatePlayer(shipManager.BuildShip(ShipType.PlayerShip)); }
public SetupManager( IOutputManager outputManager, IInputManager inputManager, IGameConfig gameConfig, IRandomManager randomManager, IShipManager shipManager, ICoordinateManager coordinateManager, IShotManager shotManager, ILog log) { _outputManager = outputManager ?? throw new ArgumentNullException(nameof(outputManager)); _inputManager = inputManager ?? throw new ArgumentNullException(nameof(outputManager)); _gameConfig = gameConfig ?? throw new ArgumentNullException(nameof(gameConfig)); _randomManager = randomManager ?? throw new ArgumentNullException(nameof(gameConfig)); _shipManager = shipManager ?? throw new ArgumentNullException(nameof(shipManager)); _coordinateManager = coordinateManager ?? throw new ArgumentNullException(nameof(coordinateManager)); _shotManager = shotManager ?? throw new ArgumentNullException(nameof(shotManager)); _log = log ?? throw new ArgumentNullException(nameof(log)); }
private void CreateShips() { switch (shipConfiguration.ShipManagerType) { case ShipManagerType.ShipManager: shipManager = new GameObject("ShipManager").AddComponent <ShipManager>(); break; case ShipManagerType.ShipManagerJobs: shipManager = new GameObject("ShipManagerJobs").AddComponent <ShipManagerJobs>(); break; case ShipManagerType.ShipManagerECS: shipManager = new GameObject("ShipManagerECS").AddComponent <ShipManagerECS>(); break; } shipManager.Setup(terrainGenerator, shipConfiguration); shipManager.GenerateShips(); }
public void InitializeGame_WithOneShip_ReturnsItCorrectly() { int panelSize = 10; IShipManager shipManager = Substitute.For <IShipManager>(); var destroyerType = new ShipType("Destroyer", "D", 4); var shipsByType = new Dictionary <ShipType, int> { { destroyerType, 1 } }; var ship = new Ship(new Point(3, 2), new Point(3, 5), destroyerType); var shipList = new List <Ship> { ship }; shipManager.GetShipsByType().Returns(shipsByType); shipManager.GetShipList(shipsByType, panelSize).Returns(shipList); GameService target = new GameService(shipManager); target.InitializeGame(panelSize); target.GetShipList().Should().Contain(shipList); }
public ShipController(IShipManager shipManager) { this.ShipManager = shipManager; }
public static void Main() { #region [on] 0. Instance var battlefield = new Battlefield(10, 10); positionParser = new PositionParser(); positionValidator = new PositionValidator(); randomManager = new RandomManager(); shipManager = new ShipManager(); shootManager = new ShootManager(); placeManager = new PlaceManager(); var battleBgm = new WindowsMediaPlayer(); var winnerBgm = new WindowsMediaPlayer(); #endregion #region [off] 1. Welcome Screen //GraphicManager.WelcomeScreen(); #endregion #region [on] 1.1. BGM SoundEffects.BattleBgmPlayer(battleBgm); #endregion #region [on] 2. Ships Topf // instance two players var player = new Player(); player.Positions = new List <Position>(); player.Hits = new List <Hit>(); player.Ships = new List <Ship>(); var computer = new Player(); computer.Positions = new List <Position>(); computer.Hits = new List <Hit>(); computer.Ships = new List <Ship>(); #endregion #region [on] 3. Place PlayerShips and ComputerShips // create player Ships player.Ships = InitializeShips(); // place player ships // [!!] placeManager.PlacePlayerShips(player.Ships, positionParser, positionValidator, shipManager,battlefield); placeManager.PlaceComputerShips(player.Ships, randomManager, shipManager, positionValidator, battlefield); // create ships for pc computer.Ships = InitializeShips(); // place pc ships placeManager.PlaceComputerShips(computer.Ships, randomManager, shipManager, positionValidator, battlefield); // Add positions into Instances foreach (var playerShip in player.Ships) { player.Positions.AddRange(playerShip.Positions); } var allPlayerShipsPositions = player.Positions; foreach (var computerShip in computer.Ships) { computer.Positions.AddRange(computerShip.Positions); } var allComputerShipsPositions = computer.Positions; #endregion #region + [on] display pcShips for Testing Console.Clear(); Console.WriteLine(); Console.WriteLine(); Console.WriteLine(" God Mode"); Console.WriteLine(" PC Ships:"); Console.WriteLine(); Console.WriteLine(GraphicManager.DisplayBattlefield(battlefield.ColumnSize, battlefield.RowSize, computer.Ships).PadRight(20)); Console.ReadKey(); SoundEffects.SetShipSoundPlayer(); #endregion #region + [off] display Win view for Testing //battleBgm.close(); //EndGameManager.WhoWin(player, computer, winnerBgm, shootManager); //EndGameManager.RestartGame(winnerBgm); #endregion #region [on] 4. Game begin view GraphicManager.GameStartView(player, computer, battlefield); #endregion #region [on] 5. Player || PC shots do // End the Game { do // Player == * ==> PC { var iShootPosition = ShootManager.PlayerShoot(player, computer, battlefield, positionParser, positionValidator); if (allComputerShipsPositions.Contains(iShootPosition)) // Hit Ship { computer.Hits.Add(new Hit(HitType.Ship, iShootPosition)); shootManager.HitShip(player, computer, iShootPosition, computer.Ships, battlefield); } else // Hit Water { computer.Hits.Add(new Hit(HitType.Water, iShootPosition)); shootManager.HitWater(player, computer, iShootPosition, battlefield, positionParser); } // Is all Player Ships Sunken if (shootManager.IsAllShipsSunken(computer.Ships)) { break; } } while (computer.Hits.Last().HitType == HitType.Ship); // one more time shoot do // PC == * ==> Player { // if All player ships have Sunked skip all those code if (shootManager.IsAllShipsSunken(computer.Ships)) { break; } var pcShootPosition = ShootManager.ComputerShoot(player, computer, battlefield, randomManager, positionParser); if (allPlayerShipsPositions.Contains(pcShootPosition)) // Hit Ship { player.Hits.Add(new Hit(HitType.Ship, pcShootPosition)); shootManager.HitShip(player, computer, pcShootPosition, player.Ships, battlefield); } else //Hit Water { player.Hits.Add(new Hit(HitType.Water, pcShootPosition)); shootManager.HitWater(player, computer, pcShootPosition, battlefield, positionParser); } // Check if all Player Ship Sunken if (shootManager.IsAllShipsSunken(player.Ships)) { break; } } while (player.Hits.Last().HitType == HitType.Ship); // one more time shoot } while (shootManager.IsAllShipsSunken(computer.Ships) == false && shootManager.IsAllShipsSunken(player.Ships) == false); // End the game #endregion #region [on] 6. Game End View battleBgm.close(); EndGameManager.WhoWin(player, computer, winnerBgm, shootManager); EndGameManager.RestartGame(winnerBgm); #endregion }
private static List <Position> ValidatePlayerPositions(List <Ship> playerShips, IPositionParser positionParser, IPositionValidator positionValidator, IShipManager shipManager, List <Position> shipPositions, Ship ship, Battlefield battlefield) { Position position; Direction direction; for (int i = 0; i < shipPositions.Count; i++) { while (positionValidator.IsValidPosition(shipPositions[i], battlefield.ColumnSize, battlefield.RowSize, playerShips) == false) { Console.WriteLine(); do { Console.Write(" Position is not validated, try again >"); position = positionParser.Parse(Console.ReadLine()); } while (position == null); do { direction = PlaceManager.GetDirection(); } while (direction == Direction.None); shipPositions = shipManager.WholeShipPositions(position, ship.Size, direction); i = -1; break; } } return(shipPositions); }
public void PlaceComputerShips(List <Ship> computerShips, IRandomManager randomManager, IShipManager shipManager, IPositionValidator positionValidator, Battlefield battlefield) { foreach (var ship in computerShips) { var randomPosition = randomManager.RandomPosition(battlefield.ColumnSize, battlefield.RowSize); var randomDirection = randomManager.RandomDirection(); var shipPositions = shipManager.WholeShipPositions(randomPosition, ship.Size, randomDirection); for (int i = 0; i < shipPositions.Count; i++) { while (positionValidator.IsValidPosition(shipPositions[i], battlefield.ColumnSize, battlefield.RowSize, computerShips) == false) { randomPosition = randomManager.RandomPosition(battlefield.ColumnSize, battlefield.RowSize); randomDirection = randomManager.RandomDirection(); shipPositions = shipManager.WholeShipPositions(randomPosition, ship.Size, randomDirection); i = -1; break; } } ship.Positions.AddRange(shipPositions); } }
public void PlacePlayerShips(List <Ship> playerShips, IPositionParser positionParser, IPositionValidator positionValidator, IShipManager shipManager, Battlefield battlefield) { foreach (var ship in playerShips) { Console.Clear(); Console.WriteLine(); //display battle field Console.WriteLine(GraphicManager.DisplayBattlefield(battlefield.ColumnSize, battlefield.RowSize, playerShips)); //display place table Console.WriteLine(GraphicManager.DisplayPlaceTable(playerShips, ship.ShipType)); //get the StartPosition Position position; do { Console.Write(" Enter position for {0} > ", ship.ShipType); position = positionParser.Parse(Console.ReadLine()); } while (position == null); //get the direction Direction direction; do { direction = GetDirection(); } while (direction == Direction.None); //get whole ship positions var shipPositions = shipManager.WholeShipPositions(position, ship.Size, direction); //validate player position shipPositions = ValidatePlayerPositions(playerShips, positionParser, positionValidator, shipManager, shipPositions, ship, battlefield); ship.Positions.AddRange(shipPositions); SoundEffects.SetShipSoundPlayer(); } }
public GameService(IShipManager shipManager) { _hits = new List <Point>(); _shipManager = shipManager; }
/// <summary> /// Initializes a new instance of the <see cref="ShipController"/> class. /// </summary> /// <param name="shipManager">The ship manager.</param> public ShipController(IShipManager shipManager) { this.shipManager = shipManager; }