public EnemyShip(IGridSquare seedSquare, EnemyBoard enemyBoard) { maxSize = enemyBoard.GetActiveShipMaxSize(); sizeDetected = 1; seed = Utils.ConvertGridSquareToPoint(seedSquare); bool addDown = true; bool addRight = true; bool addUp = true; bool addLeft = true; possibleTilesAroundSeed = new List <Point>(); for (int dist = 1; dist <= 4; dist++) { Point down = seed.Plus(new Point(0, dist)); if (IsPointOnGrid(down) && enemyBoard.IsTestedSquare(down)) { addDown = false; } if (addDown && IsPointOnGrid(down)) { possibleTilesAroundSeed.Add(down); } Point right = seed.Plus(new Point(dist, 0)); if (IsPointOnGrid(right) && enemyBoard.IsTestedSquare(right)) { addRight = false; } if (addRight && IsPointOnGrid(right)) { possibleTilesAroundSeed.Add(right); } Point up = seed.Plus(new Point(0, -dist)); if (IsPointOnGrid(up) && enemyBoard.IsTestedSquare(up)) { addUp = false; } if (addUp && IsPointOnGrid(up)) { possibleTilesAroundSeed.Add(up); } Point left = seed.Plus(new Point(-dist, 0)); if (IsPointOnGrid(left) && enemyBoard.IsTestedSquare(left)) { addLeft = false; } if (addLeft && IsPointOnGrid(left)) { possibleTilesAroundSeed.Add(left); } } }
/* * We can cover the whole board by only aiming at half of the squares * (In a chess-board pattern) * Pick even columns in rows A, C, E... * Pick odd columns in rows B, D, F... */ private IGridSquare PickRandomTarget() { int row = rng.Next(10); int column = rng.Next(5) * 2; if (row % 2 == 0) { column++; } int firstX = column; int firstY = row; int firstColumnInRow = column; while (enemyBoard.IsTestedSquare(new Point(column, row))) { column = (column + 2) % 10; if (column == firstColumnInRow) { row = (row + 1) % 10; column = (column + 1) % 10; firstColumnInRow = column; } //Complete loop, therefore shift to other coloured tiles //This will eventually be redundant, as matches will end before all options explored //But in the half-finished state, we need to avoid infinite loops if (column == firstX && row == firstY) { column = (column + 1) % 10; } } IGridSquare gridSquare = new GridSquare(GameBoard.GridRefs.ToCharArray()[row], column + 1); return(gridSquare); }