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); }
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); }
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); }