コード例 #1
0
ファイル: Form1.cs プロジェクト: eugenerf/Minesweeper
        }   //END (tsmiStats_Click)
        #endregion

        #region Additional
        /// <summary>
        /// Initialises the new minefield and redraws the form for it
        /// </summary>
        public void InitialiseField()
        {

#if DEBUG   //debug section start
            if (formDebug != null) formDebug.Hide();    //hide the debug form if it is created
#endif      //debug section end

            ME = MinesEngine.getEngine(MS); //getting the game engine

            //setting game related fields
            GameStart = false;  //game not started yet
            GameSeconds = 0;    //game time equals zero by now

            //setting form controls to the initial state
            tTime.Stop();                                       //game timer stopped
            butNewGame.ImageIndex = 2;                          //set in-game image for the NewGame button
            lMines.Text = MS.NumMines.ToString();               //show number of mines
            lTime.Text = "";                                    //clear timer textfield

            //redrawing mine field cells
            gbMineField.SuspendLayout();                        //suspend layout of the mine field
            for (int i = 0; i < MS.FieldWidth; i++)             //moving through all the mine field columns
                for (int j = 0; j < MS.FieldHeight; j++)        //moving through all the cells in the current column
                    FL[i][j].ChangeLabel(ClosedColor, -1, "");  //setting current mine field cell to its initial state
            gbMineField.ResumeLayout();                         //resume layout of the mine field

            //changing size of the main form
            Size = new Size(
                MS.FieldWidth * CellSize + 30,                  //width
                MS.FieldHeight * CellSize + 115);               //height

            //changing size of the mine field
            gbMineField.Size = new Size(
                MS.FieldWidth * CellSize + 2,                   //width
                MS.FieldHeight * CellSize + 8);                 //height

            //setting easter egg 2 controls to the initial state
            pbHeartBig.Visible = false;                         //hide the big heart
            pbHeartBig.Size = gbMineField.Size;                 //set size of the big heart equal to size of the mine field
            pbHeartBigger.Visible = false;                      //hide even bigge5r heart
            pbHeartBigger.Size = gbMineField.Size;              //set its size equal to size of the mine field

#if DEBUG   //debug section start
            DebugMineField();   //show debug mine field
#endif      //debug section end
        }   //END (InitialiseField)
コード例 #2
0
        }                //END (ctor)

        /// <summary>
        /// Gets unique instance of the MinesEngine with specified settings
        /// </summary>
        /// <param name="MS">Settings for the engine</param>
        /// <returns>Unique instance of the MinesEngine class</returns>
        public static MinesEngine getEngine(MinesSettings MS)
        {
            if (Instance == null)                       //if class instance is not created
            {
                lock (multyThreadLock)                  //begin lock section (to nobody could create instance at the same time)
                {
                    if (Instance == null)               //again: if class instance is not created
                    {
                        Instance = new MinesEngine(MS); //create unique class instance
                    }
                }                                       //ENDLOCK (lock section)
            }
            else                                        //if class instance is already created
            {
                Instance.NewGame(MS);                   //just begin the new game for it
            }
            return(Instance);                           //return unique class instance
        }                                               //END (getEngine)
コード例 #3
0
        }                     //END (getInstance)

        /// <summary>
        /// Calculate stats for the current game
        /// </summary>
        /// <param name="me">Engine of the current game</param>
        /// <param name="ms">Settings of the current game</param>
        /// <param name="askPN">Handler of the method that asks for a player name</param>
        public void CalcStats(MinesEngine me, MinesSettings ms, uint gameTime, AskPlayerName askPN)
        {
            DateTime curTime = DateTime.Now;                                //current date and time
            int      index   = -1;                                          //index of the current preset in the stats array

            if (me.CurrentGameState.State != MinesEngine.GameState.Loose && //if game is not lost
                me.CurrentGameState.State != MinesEngine.GameState.Win)     //AND game is not won
            {
                return;                                                     //do nothing and just return
            }
            uint level3BV = me.Level3BV;                                    //get difficulty level of the game from the engine

            switch (ms.CurrentPreset)                                       //switch between game presets
            {
            case MinesSettings.Preset.Newbie: index = 0; break;             //for Newbie index is zero

            case MinesSettings.Preset.Advanced: index = 1; break;           //for Advanced index is 1

            case MinesSettings.Preset.Professional: index = 2; break;       //for Professional index is 2

            case MinesSettings.Preset.Custom:                               //for Custom we will not add stats by preset
                                                                            //  but we will try to add a record to the top by difficulty level
                if (me.CurrentGameState.State == MinesEngine.GameState.Win) //if game is won
                {
                    AddToTop(3, curTime, gameTime, level3BV, askPN);        //try to add record to top list
                }
                return;                                                     //return

            default: return;                                                //if somehow preset in game settings is invalid do nothing and return
            }                                                               //ENDSWITCH (preset)
            StatsByPreset[index].TotalGames++;                              //increase total games played for the current preset
            if (me.CurrentGameState.State == MinesEngine.GameState.Win)     //if game is won
            {
                if (gameTime > StatsByPreset[index].LongestGameTime)        //if current game duration is more than the saved in stats one
                {
                    StatsByPreset[index].LongestGameTime = gameTime;        //save current game duration
                }
                StatsByPreset[index].WinGames++;                            //increase won games counter
                StatsByPreset[index].WinPercent =
                    StatsByPreset[index].WinGames /
                    (double)StatsByPreset[index].TotalGames *
                    100.0;                                                          //calculate the new win-percentage
                StatsByPreset[index].WinStreak++;                                   //increase win streak counter
                if (StatsByPreset[index].LooseStreak >
                    StatsByPreset[index].MaxLooseStreak)                            //if current loose streak is longer than the saved one
                {
                    StatsByPreset[index].MaxLooseStreak =
                        StatsByPreset[index].LooseStreak;                //save the current loose streak as the maximal
                }
                StatsByPreset[index].LooseStreak     = 0;                //reset current loose streak
                StatsByPreset[index].FastestGameTime =
                    AddToTop(index, curTime, gameTime, level3BV, askPN); //try to add current game to top
            }                                                            //ENDIF (game is won)
            else                                                         //if game is LOST
            {
                if (StatsByPreset[index].WinStreak >
                    StatsByPreset[index].MaxWinStreak)                              //if current win streak is longer than the saved one
                {
                    StatsByPreset[index].MaxWinStreak =
                        StatsByPreset[index].WinStreak; //save current win streak as the maximal
                }
                StatsByPreset[index].WinStreak = 0;     //reset current win streak
                StatsByPreset[index].LooseStreak++;     //increase the loose streak counter
            }                                           //ENDELSE (game is lost)
        }                                               //END (CalcStats)