public int CountBattleships(char[,] board) { BattleBoard battleShip = new BattleBoard(board); int i = 0; //- Runner horizontally int j = 0; //-Runner vertitally int maxI = board.GetLength(0); //width int maxJ = board.GetLength(1); //height while (j < maxJ) { while (i < maxI) { if (char.ToLower(battleShip.Board[i, j]) == 'x') { if (battleShip.Ships.Count != 0) //if it's not empty, check for { Ship ship = CheckNeighbour(battleShip, i, j); Point point = new Point(i, j); if (ship == null) { ship = new Ship(point); battleShip.Ships.Add(ship); } else { ship.positions.Add(point); } } else { //it is empty, so here's the first ship Point point = new Point(i, j); Ship ship = new Ship(point); battleShip.Ships.Add(ship); } } i++; } i = 0; j++; } return(battleShip.Ships.Count()); }
private Ship CheckNeighbour(BattleBoard board, int i, int j) { if (i != 0) //make sure you don't go out of bounds { if (board.Board[i - 1, j] == 'x') { Ship ship = board.Ships.Single(x => x.positions.Where(y => y.i == i - 1 && y.j == j).Any()); return(ship); } } if (j != 0) { if (board.Board[i, j - 1] == 'x') { Ship ship = board.Ships.Single(x => x.positions.Where(y => y.i == i && y.j == j - 1).Any()); return(ship); } } return(null); }