private Battleship FindLargestHorizontalShip() { //the algorithm: 1iterate through each row cell by cell. //once empty cell is found move to the righ until the the cell is not empty of the board edge is reached //keep track of sizes. If the size is larger than the previous one create new battleship //return largest battleship int size = 0; Battleship battleship = null; for (int y = 0; y < Height; y++) { for (int x = 0; x < Width; x++) { if (Squares.Where(s => s.X == x && s.Y == y).First().IsEmpty) { for (int i = x + 1; i < Width; i++) { if (Squares.Where(s => s.X == i && s.Y == y).First().IsEmpty&& size < i - x) { battleship = new Battleship(); battleship.AddSquares(Squares.Where(s => s.Y == y && s.X <= i)); size = battleship.Squares.Count; } } } } } return(battleship); }
public void BattleshipTest1() { Battleship ship = new Battleship(4); ship.SetupDeployment(new List <(int, int)> { (2, 3), (2, 5), (2, 6), (2, 7) });
private Battleship FindLargestVerticalShip() { int size = 0; Battleship battleship = null; for (int x = 0; x < Width; x++) { for (int y = 0; y < Height; y++) { if (Squares.Where(s => s.X == x && s.Y == y).First().IsEmpty) { for (int i = y + 1; i < Height; i++) { if (Squares.Where(s => s.Y == i && s.X == x).First().IsEmpty&& size <= i - y) { battleship = new Battleship(); battleship.AddSquares(Squares.Where(s => s.X == x && s.Y >= y && s.Y <= i)); size = battleship.Squares.Count; } } } } } return(battleship); }
public void AddAShipToBoardTest() { BattleshipBoard board = new BattleshipBoard(); Battleship ship = new Battleship(6); ship.SetupDeployment(new List <(int, int)> { (1, 3), (1, 4), (1, 5), (1, 6) });
public Boolean ToCheckShipIsSunk(Battleship ship) { if (!ship.IsSunk && ship.Deployment?.Count > 0) { foreach (Coordinate location in ship.Deployment) { if (Board[location.X, location.Y].State != CoordinateState.HIT) { return(false); // ship.IsSunk didn't change } } ship.IsSunk = true; // A battleship is sunk if it has been hit on all the squares it occupies } return(ship.IsSunk); // A ghost ship occupies zero squares, it was sunk! }
public Boolean AddAShipToBoard(Battleship ship) { if (IsBoardReadyToPlay || ship.ShipNumber <= 0) { return(false); } if (BattleShips.ContainsKey(ship.ShipNumber)) { ReportTool.WriteLine("ShipNumber is already in board."); return(false); } if (ship.Deployment.Count > boardSize || ship.Deployment.Count <= 0) { ReportTool.WriteLine("Ship must fit entirely on the board"); return(false); } if (!ship.IsValidDeployment()) { ReportTool.WriteLine("The ship should be 1-by-n sized"); return(false); } // check if ship position had been occupied by another ship foreach (Coordinate location in ship.Deployment) { if (Board[location.X, location.Y].State == CoordinateState.OCCUPIED) { ReportTool.WriteLine("Ship can't overlap another ship. {location}"); return(false); } } // put ship on the board foreach (Coordinate location in ship.Deployment) { BoardCell cell = Board[location.X, location.Y]; cell.State = CoordinateState.OCCUPIED; cell.ShipNumber = ship.ShipNumber; } BattleShips.Add(ship.ShipNumber, ship); return(true); }
static void Main(string[] args) { Player player = new Player(); player.ReportPlayBoardState(); player.CreateBoard(); player.ReportPlayBoardState(); player.AddShipsFrom = new AddShipsToBoardForLostTest(); player.ReportPlayBoardState(); player.AddShipsToBoard(); player.ReportPlayBoardState(); var result = player.PlayBoard.IsBoardReadyToPlay; if (result) { // [2-5,4] should be shipNumber 4 result = player.TakeAnAttack(2, 4) == 4 && player.TakeAnAttack(3, 4) == 4 && player.TakeAnAttack(4, 4) == 4 && player.TakeAnAttack(5, 4) == 4; } player.ReportPlayBoardState(); if (player.IsLostGame()) { player.ReportTool.WriteLine("Game over!!!"); } player.ReportTool.WriteLine("--------------------"); player.ReportTool.WriteLine("####################"); player.ReportTool.WriteLine("Start new Game"); player.PlayBoard.CreateBattleshipBoard(); var shipNumber = 100; Battleship ship = new Battleship(shipNumber); ship.SetupDeployment(new List <(int, int)> { (7, 3), (8, 3), (9, 3) });
public HitResult TryHit(int x, int y) { Battleship destroyed = null; var result = HitResult.Missed; foreach (var bs in Battleships) { result = bs.TryToHit(x, y); if (result == HitResult.Destroyed) { destroyed = bs; } if (result != HitResult.Missed) { break; } } if (destroyed != null) { Battleships.Remove(destroyed); } return(result); }
private Battleship BuildBattleship(Alingment alingment, int size, int x, int y) { var bs = new Battleship(); if (Squares.Where(s => s.X == x && s.Y == y).FirstOrDefault() == null || Squares.Where(s => (s.X == x + size - 1 && s.Y == y && alingment == Alingment.Horizontal) || (s.Y == y + size - 1 && s.X == x && alingment == Alingment.Vertical)).FirstOrDefault() == null) { bs = null; } else { if (alingment == Alingment.Horizontal) { if (Squares.Any(s => (s.Y == y && (s.X >= x && s.X <= x + size - 1)) && !s.IsEmpty)) { bs = null; } else { bs.AddSquares(Squares.Where(s => (s.Y == y && (s.X >= x && s.X <= x + size)))); } } else { if (Squares.Any(s => (s.X == x && (s.Y >= y && s.Y <= y + size - 1)) && !s.IsEmpty)) { bs = null; } else { bs.AddSquares(Squares.Where(s => (s.X == x && (s.Y >= y && s.Y <= y + size - 1)))); } } } return(bs); }