Example #1
0
        //주어진 필드의 크기와 지뢰 갯수로 필드 생성
        public void InitMineField(int width = ConstantsPack.minWidthCells, int height = ConstantsPack.minHeightCells, int mines = ConstantsPack.minMines)
        {
            fieldState      = ConstantsPack.gameState.gameState_Init;
            fieldWidth      = width; fieldHeight = height; fieldMines = mines;
            remainSafeCells = (fieldWidth * fieldHeight) - fieldMines;
            fieldGrid       = new FieldCell[fieldHeight, fieldWidth];

            PcgRandom randObj = new PcgRandom();

            //주어진 지뢰 갯수로 필드에 지뢰를 임의로 배치시킨다.
            int remainMines = fieldMines;

            while (remainMines > 0)
            {
                int setX = randObj.Next(0, fieldWidth);
                int setY = randObj.Next(0, fieldHeight);

                if (fieldGrid[setY, setX].openValue == (sbyte)ConstantsPack.openCellValue.cellValue_mine)
                {
                    continue;
                }

                fieldGrid[setY, setX].openValue = (sbyte)ConstantsPack.openCellValue.cellValue_mine;
                remainMines--;
            }

            //배치가 끝나면 빈 칸들을 탐색하여 주변 지뢰가 있으면 그 갯수를 체크한다.
            int x = 0, y = 0, k = 0;

            for (y = 0; y < fieldHeight; ++y)
            {
                for (x = 0; x < fieldWidth; ++x)
                {
                    FieldCell curCell = fieldGrid[y, x];
                    if (curCell.openValue == (sbyte)ConstantsPack.openCellValue.cellValue_blank)
                    {
                        //빈 칸 주변 8셀에 지뢰가 있는지 체크하여 카운팅
                        sbyte totalAdjMines = 0;
                        for (k = 0; k < 8; ++k)
                        {
                            int adjX = x + adjDx[k], adjY = y + adjDy[k];
                            if (!CellRangeCheck(adjX, adjY))
                            {
                                continue;
                            }
                            if (fieldGrid[adjY, adjX].openValue == (sbyte)ConstantsPack.openCellValue.cellValue_mine)
                            {
                                totalAdjMines++;
                            }
                        }
                        //카운팅된 값을 셀에 대입
                        fieldGrid[y, x].openValue = totalAdjMines;
                    }
                }
            }

            //필드 생성 끝나면 인게임 상태로 전환
            fieldState = ConstantsPack.gameState.gameState_InGame;
        } //InitMineField 함수 끝
Example #2
0
        public FieldCell GetFieldCell(int getX, int getY)
        {
            FieldCell getCell = new FieldCell();

            if (CellRangeCheck(getX, getY))
            {
                getCell = fieldGrid[getY, getX];
            }

            return(getCell);
        }