}                                                                //END (ctor)

        /// <summary>
        /// Get unique instance of the MinesStatistics class
        /// </summary>
        /// <returns>Unique instance of MinesStatistics</returns>
        public static MinesStatistics getInstance()
        {
            if (Instance == null)                   //if instance of class is not created
            {
                lock (multyThreadLock)              //lock static elements of the class
                    if (Instance == null)           //if instance of class is still not created
                    {
                        Instance =
                            new MinesStatistics();
                    }         //create it
            }
            return(Instance); //return unique instance of the class
        }                     //END (getInstance)
Beispiel #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)