Beispiel #1
0
 public Grid(int cols, int rows)
 {
     this.ColsCount = cols;
     this.RowsCount = rows;
     gridMap        = new GridData(cols, rows);
 }
Beispiel #2
0
 /** Clear the grid content */
 public void Clear() => gridMap = new GridData(ColsCount, RowsCount);
Beispiel #3
0
        /** Merge the piece content (cells) in the grid */
        protected PlaceSt MergePiece(MergeType mode)
        {
            PlaceSt st = CheckBoundaries();

            if (st != PlaceSt.OK)
            {
                return(st);
            }

            Piece piece = currentPiece;

            int pieceX = piece.GetCol();
            int pieceY = piece.GetRow();

            // Copy cells data onto map_data in order to preserve it before merging
            GridData draftMap = gridMap.Clone();

            for (int y = 0; y < Piece.ROWS; ++y)
            {
                for (int x = 0; x < Piece.COLS; ++x)
                {
                    int gridCol = pieceX + x;
                    int gridRow = pieceY + y;

                    if (gridCol < 0 ||
                        gridCol >= ColsCount ||
                        gridRow < 0 ||
                        gridRow >= RowsCount)
                    {
                        continue;
                    }

                    int gridCell  = draftMap.GetRow(gridRow).Get[gridCol];
                    int pieceCell = piece.GetAt(x, y);

                    if (mode == MergeType.MERGE_PUT)
                    {
                        // In case of cell conflict, return TOUCH_DOWN or
                        // NO_ROOM_FOR_PIECE
                        // depending on top margin value
                        // (cells data modification will be not committed)
                        if (gridCell != 0 && pieceCell != 0)
                        {
                            return((pieceY - piece.GetTopMargin()) < 1 ?
                                   PlaceSt.NO_ROOM_FOR_PIECE : PlaceSt.TOUCH_DOWN);
                        }

                        // Copy piece (or delete) cell content into the draft data map
                        if (pieceCell != 0)
                        {
                            draftMap.GetRow(gridRow).Get[gridCol] = pieceCell;
                        }
                    }
                    else
                    {
                        // Copy piece (or delete) cell content into the draft data
                        // map
                        if (pieceCell != 0)
                        {
                            draftMap.GetRow(gridRow).Get[gridCol] = 0;
                        }
                    }
                }
            }

            // commit changes to the map data
            gridMap = draftMap;

            return(PlaceSt.OK);
        }