Exemplo n.º 1
0
        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
        }
Exemplo n.º 2
0
 public Ship(Square[] squares)
 {
     m_squares = squares; //polja
 }
Exemplo n.º 3
0
 public Ship MakeShip(Square startSquare, int lenght, Orientation orientation)
 {
     Square[] squares = GetSquares(startSquare, lenght, orientation);
     OccupySquares(squares);
     return new Ship(squares);
 }
Exemplo n.º 4
0
        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();
            }
        }
Exemplo n.º 5
0
        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();
        }