public void AttackOnBoard_Should_Result_With_Correct_AttackResultStatus(ShipPointLocationModel attackingPosition, AttackResult attackResult , bool needemptyBoard, bool configureBoard, bool isSunk, bool shouldGameOver) { //arrange if (configureBoard) { StateTrackingManager.ShipsOnBoard = SetupShipsOnBoard(); } if (needemptyBoard) { StateTrackingManager.ShipsOnBoard = new List <IBattleship>(); } var isGameOver = false; //act var result = _sutStateTrackingManager.AttackOnBoard(attackingPosition, out isGameOver); var shipUnderAttack = StateTrackingManager.ShipsOnBoard.FirstOrDefault(ship => ship.PointsOccupied.Any(p => p.XCoordinate == attackingPosition.XCoordinate && p.YCoordinate == attackingPosition.YCoordinate)); //assert Assert.Equal(attackResult, result); if (isSunk && !shouldGameOver) //After the last ship sunk, game will be reset with no ships on board. so shipUnderAttack would not be available { Assert.Equal(ShipHealth.Sunk, shipUnderAttack.Health); } else if (attackResult == AttackResult.Hit) { Assert.Equal(ShipHealth.Damaged, shipUnderAttack.Health); } Assert.Equal(shouldGameOver, isGameOver); //if(shouldGameOver) //Assert.Equal(0, StateTrackingManager.ShipsOnBoard.Count); }
public AttackResult AttackOnBoard(ShipPointLocationModel attackingPosition, out bool isGameOver) { isGameOver = false; //Check if there is any ship on the board if (!ShipsOnBoard.Any()) { return(AttackResult.Invalid); } var attackStatus = AttackResult.Miss; var shipUnderAttack = ShipsOnBoard.FirstOrDefault(ship => ship.PointsOccupied.Any(p => p.XCoordinate == attackingPosition.XCoordinate && p.YCoordinate == attackingPosition.YCoordinate)); if (shipUnderAttack != null) { shipUnderAttack.PointsAttacked = shipUnderAttack.PointsAttacked ?? new List <ShipPoint>(); //Check to see if the same point is already attacked previously if (shipUnderAttack.PointsAttacked.Any(p => p.XCoordinate == attackingPosition.XCoordinate && p.YCoordinate == attackingPosition.YCoordinate)) { attackStatus = AttackResult.RepeatAttack; } else { var pointUnderAttack = Mapper.Map <ShipPoint>(attackingPosition); shipUnderAttack.PointsAttacked.Add(pointUnderAttack); if (shipUnderAttack.Size == shipUnderAttack.PointsAttacked.Count) { shipUnderAttack.Health = ShipHealth.Sunk; attackStatus = AttackResult.Sunk; } else { shipUnderAttack.Health = ShipHealth.Damaged; attackStatus = AttackResult.Hit; } } } //Game will not be over untill all the ship on board are are SUNK status isGameOver = !ShipsOnBoard.Any(ship => ship.Health == ShipHealth.Undamaged || ship.Health == ShipHealth.Damaged); //Reset the board if all the placed ships are sunk if (isGameOver) { ResetBoard(); } return(attackStatus); }
public IHttpActionResult Attack(ShipPointLocationModel pointUnderAttack) { if (ModelState.IsValid) { var isGameOver = false; var attackResult = _stateTrackingManager.AttackOnBoard(pointUnderAttack, out isGameOver); return(Ok(new { IsGameOver = isGameOver, AttackStatus = attackResult.ToString(), Message = isGameOver ? "Board is reset for a new game as this game is over!" : attackResult.GetDescription() })); } return(BadRequest(ModelState)); }