private Square[,] m_field; //dvodimenzijalno polje #endregion Fields #region Constructors //konstruktor klase public Grid() { m_field = new Square[NumberOfRows, NumberOfColumns]; for (int r = 0; r < 10; ++r) //rows for (int c = 0; c < 10; ++c) //columns m_field[r, c] = new Square(r, c); //inicijalizacija }
public Ship(Square[] squares) { m_squares = squares; //polja }
public Ship MakeShip(Square startSquare, int lenght, Orientation orientation) { Square[] squares = GetSquares(startSquare, lenght, orientation); OccupySquares(squares); return new Ship(squares); }
private void OccupySquares(Square[] squraes) { int left = squraes[0].Column - 1; if (left < 0) left = 0; int right = squraes[squraes.Length - 1].Column + 1; if (right >= Grid.NumberOfColumns) right = Grid.NumberOfColumns - 1; int bottom = squraes[squraes.Length - 1].Row + 1; if (bottom >= Grid.NumberOfRows) bottom = Grid.NumberOfRows - 1; int top = squraes[0].Row - 1; if (top < 0) top = 0; for (int r = top; r <= bottom; ++r) { for (int c = left; c <= right; ++c) m_field[r, c].Occupy(); } }
private Square[] GetSquares(Square startSquare, int lenght, 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 = startSquare.Row; int column = startSquare.Column; for (int i = 0; i < lenght; ++i) { squares.Add(m_field[row, column]); row += deltaR; column += deltaC; } return squares.ToArray(); }