public Grid() { m_field = new Square[NumberOfRows, NumberOfColumns]; for (int r = 0; r < NumberOfRows; ++r) for (int c = 0; c < NumberOfColumns; ++c) m_field[r, c] = new Square(r, c); }
private Square[] GetSquares(Square startingSquare, int length, Orientation orientation) { List<Square> squares = new List<Square>(); int deltaR = 0; int deltaC = 0; switch (orientation) { case Orientation.Horizontal: deltaC = 1; break; case Orientation.Vertical: deltaR = 1; break; } int row = startingSquare.Row; int column = startingSquare.Column; for (int i = 0; i < length; ++i) { squares.Add(m_field[row, column]); row += deltaR; column += deltaC; } return squares.ToArray(); }
private Square GetSquare(Square[] squares, int row, int column) { foreach (Square square in squares) { if (square.Row == row && square.Column == column) return square; } return null; }
public Ship MakeShip(Square startingSquare, int length, Orientation orientation) { Square[] squares = GetSquares(startingSquare, length, orientation); OccupySquares(squares); return new Ship(squares); }
private void OccupySquares(Square[] squares) { int left = squares[0].Column - 1; if (left < 0) left = 0; int top = squares[0].Row - 1; if (top < 0) top = 0; int right = squares[squares.Length - 1].Column + 1; if (right >= Grid.NumberOfColumns) right = Grid.NumberOfColumns - 1; int bottom = squares[squares.Length - 1].Row + 1; if (bottom >= Grid.NumberOfRows) bottom = Grid.NumberOfRows - 1; for (int r = top; r <= bottom; ++r) for (int c = left; c <= right; ++c) m_field[r, c].Occupy(); }
public OrientationalFiring(EnemyGrid grid, Square[] squaresHit, int targetShipLength) { m_grid = grid; m_squaresHit = squaresHit; m_targetShipLength = targetShipLength; }
private static int SquareComparison(Square sq1, Square sq2) { if (sq1.Column == sq2.Column) return sq1.Row - sq2.Row; return sq1.Column - sq2.Column; }
public void AddNewHit(Square square) { throw new NotImplementedException(); }
public void AddNewHit(Square square) { m_squaresHit.Add(square); }
bool IsSquareInside(Square square, int top, int left, int bottom, int right) { return square.Row >= top && square.Row < bottom && square.Column >= left && square.Column < right; }
public Square NextTarget() { m_lastTarget = m_tactics.NextTarget(); m_enemyGrid.OccupySquare(m_lastTarget); return m_lastTarget; }
public void NextTargetTest() { // test for a horizontal ship in the middle of playground EnemyGrid grid = new EnemyGrid(); Square sq1 = grid.GetSquare(3, 3); Square sq2 = grid.GetSquare(3, 4); Square sq3 = grid.GetSquare(3, 5); Square[] squaresHit = new Square[] { sq1, sq2, sq3 }; int targetShipLength = 4; OrientationalFiring tactics = new OrientationalFiring(grid, squaresHit, targetShipLength); Square next = tactics.NextTarget(); Assert.IsTrue(next.Row == 3 && (next.Column == 2 || next.Column == 6)); // now occupy a square at one of ends grid.GetSquare(3, 2).Occupy(); next = tactics.NextTarget(); Assert.IsTrue(next.Row == 3 && next.Column == 6); // test for a vertical ship at the bottom of playground grid = new EnemyGrid(); sq1 = grid.GetSquare(7, 1); sq2 = grid.GetSquare(8, 1); sq3 = grid.GetSquare(9, 1); squaresHit = new Square[] { sq1, sq2, sq3 }; targetShipLength = 4; tactics = new OrientationalFiring(grid, squaresHit, targetShipLength); next = tactics.NextTarget(); Assert.IsTrue(next.Row == 6 && next.Column == 1); // test for a vertical ship in the middle of playground grid = new EnemyGrid(); sq1 = grid.GetSquare(3, 1); sq2 = grid.GetSquare(4, 1); sq3 = grid.GetSquare(5, 1); squaresHit = new Square[] { sq1, sq2, sq3 }; targetShipLength = 4; tactics = new OrientationalFiring(grid, squaresHit, targetShipLength); next = tactics.NextTarget(); Assert.IsTrue((next.Row == 6 || next.Row == 2) && next.Column == 1); // now occupy a square at one of ends grid.GetSquare(6, 1).Occupy(); next = tactics.NextTarget(); Assert.IsTrue(next.Row == 2 && next.Column == 1); // test for a vertical ship at the bottom of playground grid = new EnemyGrid(); sq1 = grid.GetSquare(7, 1); sq2 = grid.GetSquare(8, 1); sq3 = grid.GetSquare(9, 1); squaresHit = new Square[] { sq1, sq2, sq3 }; targetShipLength = 4; tactics = new OrientationalFiring(grid, squaresHit, targetShipLength); next = tactics.NextTarget(); Assert.IsTrue(next.Row == 6 && next.Column == 1); grid = new EnemyGrid(); sq1 = grid.GetSquare(3, 1); sq2 = grid.GetSquare(5, 1); sq3 = grid.GetSquare(4, 1); squaresHit = new Square[] { sq1, sq2, sq3 }; targetShipLength = 4; tactics = new OrientationalFiring(grid, squaresHit, targetShipLength); tactics.AddNewHit(grid.GetSquare(6, 1)); next = tactics.NextTarget(); Assert.IsTrue((next.Row == 7 || next.Row == 2) && next.Column == 1); }
public Ship(Square[] squares) { m_squares = squares; }
public AfterFirstHitFiring(EnemyGrid grid, Square firstSquareHit, int targetShipLength) { m_grid = grid; m_firstSquare = firstSquareHit; m_targetShipLength = targetShipLength; }