Exemplo n.º 1
0
        }   //END (FormMineSweeper_Activated)

#if DEBUG   //debug section start
        /// <summary>
        /// Shows additional form with fully opened mine field
        /// </summary>
        private void DebugMineField()
        {
            if (formDebug == null)                                                              //if Debug form is not created yet
                formDebug = new Form();                                                             //create it

            //setting the Debug form properties
            formDebug.AutoSizeMode = AutoSizeMode.GrowAndShrink;                                //form grows and shrinks according to its contents
                                                                                                //  (no manual resize)
            formDebug.BackColor = SystemColors.Control;                                         //back color as set in OS for controls
            formDebug.FormBorderStyle = FormBorderStyle.FixedSingle;                            //border is fixed single line
            formDebug.MaximizeBox = false;                                                      //no maximize box
            formDebug.MinimizeBox = true;                                                       //no minimize box
            formDebug.ShowIcon = false;                                                         //do not show icon in the form's header
            formDebug.ShowInTaskbar = false;                                                    //do not show in taskbar
            formDebug.Size = new Size(Width, Height);                                           //size same as of the main form
            formDebug.StartPosition = FormStartPosition.CenterScreen;                           //show form in the center of the screen
            formDebug.Text = "Minesweeper DEBUG";                                               //header text
            formDebug.FormClosing += (object s, FormClosingEventArgs e) => formDebug = null;    //form closing event handler (set form's instance to null)
            formDebug.Controls.Clear();                                                         //clear controls

            GroupBox gbField = new GroupBox();                                                  //create new group box for Debug form
            //setting GroupBox properties
            gbField.Anchor = AnchorStyles.None;                                                 //no anchoring
            gbField.AutoSizeMode = AutoSizeMode.GrowAndShrink;                                  //grows and shrinks according to its contents
            gbField.BackColor = SystemColors.Control;                                           //back color as set in OS for controls
            gbField.Location = new Point(gbMineField.Location.X, gbMineField.Location.Y);       //location is same as of the mine field in the main form
            gbField.Size = new Size(gbMineField.Size.Width, gbMineField.Size.Height);           //size is same as of the mine field in the main form

            //generating cells of the debug mine field
            MineFieldLabel[][] labels = null;                                                   //debug-mine-field's labels (cells)
            Array.Resize(ref labels, MS.FieldWidth);                                            //get memory for the debug mine field
            for (int i = 0; i < MS.FieldWidth; i++)                                             //moving through all the columns of the mine field
            {
                Array.Resize(ref labels[i], MS.FieldHeight);                                        //get memory for the current debug-mine-field's column
                for (int j = 0; j < MS.FieldHeight; j++)                                            //moving through the cells of the current column
                {
                    labels[i][j] = flPrototype.GetNew(CellSize, i, j);                                  //generate the new cell for current column
                    labels[i][j].BackColor = OpenedColor;                                               //set cell's back color to the opened-cell-color
                    if (ME[i, j].cell == -1)                                                            //if current cell has mine
                        labels[i][j].ImageIndex = 0;                                                        //show mine in it
                    else                                                                                //if current cell has NO mine
                    {
                        labels[i][j].ForeColor = NumberColors[ME[i, j].cell];                               //set its fore color (text color) 
                                                                                                            //  according to the number in it
                        labels[i][j].Text = ME[i, j].cell.ToString();                                       //write its number
                    }   //ENDELSE (NO mine)
                }   //ENDFOR (cells in column)
                gbField.Controls.AddRange(labels[i]);                                               //add current column to the debug mine field
            }   //ENDFOR (columns)
            formDebug.Controls.Add(gbField);                                                    //add debug mine field to the Debug form
            formDebug.Show();                                                                   //show Debug mine field
        }   //END (DebugMineField)
Exemplo n.º 2
0
        }   //END (ctor)

        /// <summary>
        /// Main form load handler
        /// </summary>
        /// <param name="sender">Sender of the form load event</param>
        /// <param name="e">Form load event args</param>
        private void FormMineSweeper_Load(object sender, EventArgs e)
        {
            //loading game settings
            try                                     //trying to load settings from the file
            {
                FileStream fs = new FileStream("minesettings.soap", FileMode.Open);
                //using SOAP-serialization
                SoapFormatter formatter = new SoapFormatter();
                MS = (MinesSettings)formatter.Deserialize(fs);
                //disposing the file-stream
                fs.Dispose();
                fs = null;
            }   //ENDTRY
            catch (FileNotFoundException)           //if game-settings file wasn't found
            {
                MS = MinesSettings.setSettings();   //loading default settings
            }   //ENDCATCH (File not found)
            catch (SerializationException)          //if something went wrong with serialization
            {
                MS = MinesSettings.setSettings();   //loading default settings
            }   //ENDCATCH (Serialization)

            //loading game stats
            try                                         //trying to load stats from the file
            {
                FileStream fs = new FileStream("minestats.soap", FileMode.Open);
                //using SOAP-serialization
                SoapFormatter formatter = new SoapFormatter();
                MStats = (MinesStatistics)formatter.Deserialize(fs);
                //disposing the file-stream
                fs.Dispose();
                fs = null;
            }   //ENDTRY
            catch (FileNotFoundException)               //if game-stats file wasn't found
            {
                MStats = MinesStatistics.getInstance(); //create empty stats
            }   //ENDCATCH (File not found
            catch (SerializationException)              //if something went wrong with serialization
            {
                MStats = MinesStatistics.getInstance(); //create empty stats
            }   //ENDCATCH (Serialization)
            
            //setting styles for the form controls
            SetStyle(ControlStyles.UserPaint, true);                //controls must be drawn by themselves (not by OS)
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);     //controls must ignore WM_ERASEBKGND message (reduces flicker)
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);    //controls must be pre-drawn in buffer (reduces flicker)
            SetStyle(ControlStyles.ResizeRedraw, true);             //controls must be re-drawn when their size changes
            UpdateStyles();                                         //force apply the new styles

            //creating prototype mine-field label (will be used in cloning all the minefield cells)
            flPrototype = new MineFieldLabel(
                new Size(CellSize, CellSize),   //label size
                ilIconsField,                   //image list for label
                ClosedColor,                    //back color of the label
                Cell_Down,                      //mouse-down event handler
                Cell_Up,                        //mouse-up event handler
                MineFieldLabel_DoubleClick);    //double-click event handler

            //generating all the cells for the maximal-field-dimensions by using prototype label
            Array.Resize(ref FL, MinesSettings.MaxWidth);       //get memory for the field
            for(int i = 0; i < MinesSettings.MaxWidth; i++)     //moving through all the columns
            {
                Array.Resize(ref FL[i], MinesSettings.MaxHeight);   //get memory for the current field column
                for (int j = 0; j < MinesSettings.MaxHeight; j++)   //moving through all the cells in current column
                    FL[i][j] = flPrototype.GetNew(CellSize, i, j);      //create new label for the current cell
                gbMineField.Controls.AddRange(FL[i]);               //add current column of cells into the field
            }   //ENDFOR (columns)

            //initialising the mine field
            InitialiseField();
        }   //END (FormMineSweeper_Load)