} //END (Cell_Up) /// <summary> /// Double click the mine field cell event handler /// </summary> /// <param name="sender">Event sender</param> /// <param name="e">Event arguments</param> private void MineFieldLabel_DoubleClick(object sender, EventArgs e) { if (ME.CurrentGameState.State != MinesEngine.GameState.NewGame && //if current game state is NOT NewGame ME.CurrentGameState.State != MinesEngine.GameState.InProgress) //AND current game state is NOT InProgress return; //just do nothing and return MineFieldLabel Cell = (MineFieldLabel)sender; //get the cell upon which mouse acted MinesEngine.CurrentGameStateInfo GS = ME.OpenMany(Cell.Column, Cell.Row); //open the current cell and cells around it switch (GS.State) //switch between the current game states { case MinesEngine.GameState.InProgress: //game's in progress RedrawOpened(); //redraw already opened cells break; case MinesEngine.GameState.Loose: //game's lost tTime.Stop(); //stop game timer RedrawOpened(); //redraw already opened cells OpenLoose(GS); //open field as for the lost game butNewGame.ImageIndex = 3; //set the NewGame button's image to LOST MStats.CalcStats(ME, MS, GameSeconds, AskForUserName); //calculate game stats break; case MinesEngine.GameState.Win: //game's won tTime.Stop(); //stop game timer RedrawOpened(); //redraw already opened cells butNewGame.ImageIndex = 4; //set the NewGame button's image to WON MStats.CalcStats(ME, MS, GameSeconds, AskForUserName); //calculate game stats break; case MinesEngine.GameState.Stopped: //game's stopped break; //do nothing } //ENDSWITCH (game states) } //END (MineFieldLabel_DoubleClick)
} //END (RedrawOpened) /// <summary> /// Open field when game is lost (shows only mines location and mistakes) /// </summary> /// <param name="GS">Current game state</param> private void OpenLoose(MinesEngine.CurrentGameStateInfo GS) { //show mines for (int i = 0; i < ME.Width; i++) //moving through all the columns for (int j = 0; j < ME.Height; j++) //moving through all the cells in current column if (ME[i, j].cell == -1) //if cell contains mine FL[i][j].ImageIndex = 0; //show mine //highlight mistakes for (int k = 0; k < GS.NumMinesBombed; k++) //moving through all the mines that bombed FL[GS.BombedMines[k].Column][GS.BombedMines[k].Row].BackColor = MistakeColor; //and highlighting them with mistake color for (int k = 0; k < GS.NumWrongFlags; k++) //moving through all the wrongly placed flags { FL[GS.WrongFlags[k].Column][GS.WrongFlags[k].Row].BackColor = MistakeColor; //highlight flag with mistake color FL[GS.WrongFlags[k].Column][GS.WrongFlags[k].Row].ImageIndex = 1; //show flag on the cell } //ENDFOR (wrong flags) } //END (OpenLoose)
} //END (Cell_Down) /// <summary> /// Mouse button goes up upon the minefield cell (event handler) /// </summary> /// <param name="sender">Event sender</param> /// <param name="e">Event arguments</param> private void Cell_Up(object sender, MouseEventArgs e) { if (ME.CurrentGameState.State != MinesEngine.GameState.NewGame && //if current game state is NOT NewGame ME.CurrentGameState.State != MinesEngine.GameState.InProgress) //AND current game state is NOT InProgress //which means that game is stopped return; //just return (mine field doesn't work while game is stopped) //if we're here then game is NOT stopped MineFieldLabel Cell = (MineFieldLabel)sender; //get the cell upon mouse button acted MouseEventArgs curButton = //create arguments for the current mouse button new MouseEventArgs(e.Button, //which button acted (left or right) e.Clicks, //number of clicks (not used) Cell.Column, //current cell's column number Cell.Row, //current cell's row number e.Delta); //delta (not used) //check whether it is a new game if (!GameStart) //if game wasn't started by now (it was the first click on the mine field) { if (SameButton(LBDown, curButton) || SameButton(curButton, RBDown)) //if button goes up on the same cell where it went down { GameStart = true; //start game GameSeconds = 0; //clear game time tTime.Start(); //start game timer } //ENDIF (same cell) } //ENDIF (GameStart) //check the combinations of the currently acted buttons if (SameButton(LBDown, RBDown)) //Left+Right buttons acted together { //we will work with the current cell and all the cells around it for (int dI = -1; dI <= 1; dI++) //moving through the previous, current and next columns { if (Cell.Column + dI < 0 || Cell.Column + dI >= ME.Width) //if current dI gets us outside the mine field borders continue; //just skip current dI for (int dJ = -1; dJ <= 1; dJ++) //moving through the previous, current and next cells in current column { if (Cell.Row + dJ < 0 || Cell.Row + dJ >= ME.Height) //if current dJ gets us outside the mine field borders continue; //just skip current dJ FL[Cell.Column + dI][Cell.Row + dJ].BackColor = ClosedColor; //set current cell's color to the closed-cell-color } //ENDFOR (cells in column) } //ENDFOR (columns) MinesEngine.CurrentGameStateInfo GS = ME.OpenMany(Cell.Column, Cell.Row); //open the clicked cell and cells around it switch (GS.State) //switch between the game states { case MinesEngine.GameState.InProgress: //game's in progress RedrawOpened(); //redraw already opened cells break; case MinesEngine.GameState.Loose: //game's lost tTime.Stop(); //stop the game timer RedrawOpened(); //redraw already opened cells OpenLoose(GS); //open field as for the lost game butNewGame.ImageIndex = 3; //set the NewGame button's image to the LOOSE MStats.CalcStats(ME, MS, GameSeconds, AskForUserName); //calculate stats break; case MinesEngine.GameState.Win: //game's won tTime.Stop(); //stop the game timer RedrawOpened(); //redraw already opened cells butNewGame.ImageIndex = 4; //set the NewGame button's image to the WIN MStats.CalcStats(ME, MS, GameSeconds, AskForUserName); //calculate stats break; case MinesEngine.GameState.Stopped: //game's stopped break; //do nothing } //ENDSWITCH (game state) } //ENDIF (left+right buttons) else if (SameButton(LBDown, curButton)) //same left button upped as was down (left-click on the cell) { FL[Cell.Column][Cell.Row].BackColor = ClosedColor; //set current cell's back color to the closed-cell-color MinesEngine.CurrentGameStateInfo GS = ME.OpenCell(Cell.Column, Cell.Row); //open the clicked cell switch (GS.State) //switch between the game states { case MinesEngine.GameState.InProgress: //game's in progress RedrawOpened(); //redraw already opened cells break; case MinesEngine.GameState.Loose: //game's lost tTime.Stop(); //stop the game timer RedrawOpened(); //redraw already opened cells OpenLoose(GS); //open field as for the lost game butNewGame.ImageIndex = 3; //set the NewGame button's image to the LOOSE MStats.CalcStats(ME, MS, GameSeconds, AskForUserName); //calculate stats break; case MinesEngine.GameState.Win: //game's won tTime.Stop(); //stop the game timer RedrawOpened(); //redraw already opened cells butNewGame.ImageIndex = 4; //set the NewGame button's image to the WIN MStats.CalcStats(ME, MS, GameSeconds, AskForUserName); //calculate stats break; case MinesEngine.GameState.Stopped: //game's stopped break; //do nothing } //ENDSWITCH (game state) } //ENDELSEIF (left click) else if (SameButton(curButton, RBDown)) //same right button upped as was down (right-click on the cell) { ME.ChangeMarker(Cell.Column, Cell.Row); //change marker on the current cell if (ME[Cell.Column, Cell.Row].marker > 0) //if there is marker set on the current cell FL[Cell.Column][Cell.Row].ImageIndex = ME[Cell.Column, Cell.Row].marker; //draw marker on the current cell else //otherwise (marker not set) FL[Cell.Column][Cell.Row].ImageIndex = -1; //hide marker on the current cell lMines.Text = ((int)ME.NumMines - (int)ME.NumFlags).ToString(); //change number of mines in the mines-textfield } //ENDELSEIF (right click) LBDown = null; //clear the information about the pushed left button of the mouse RBDown = null; //clear the information about the pushed right button of the mouse } //END (Cell_Up)