private MineButton CreateMineButton(int column, int row) { MineButton button = new MineButton(column, row); button.Dock = System.Windows.Forms.DockStyle.Fill; //button.Location = new System.Drawing.Point(267, 149); button.Name = "button"; //button.Size = new System.Drawing.Size(258, 140); button.TabIndex = 0; button.Text = gameMap[column, row] ? "" : ""; button.Margin = new Padding(0, 0, 0, 0); button.UseVisualStyleBackColor = true; button.Click += DebugButtons; button.MouseUp += MineButton_Click; return(button); }
private void AddMineButtons(TableLayoutPanel table) { for (int column = 1; column < table.ColumnCount - 1; column++) { for (int row = 1; row < table.RowCount - 1; row++) { MineButton mineButton = CreateMineButton(column, row); table.Controls.Add(mineButton, mineButton.x, mineButton.y); spawnedMineButtons.Add(mineButton); //Making sure to remove these buttons from their lists when they're disposed mineButton.Disposed += GetDelegateToRemoveFromSequence(spawnedMineButtons); if (gameMap[mineButton.x, mineButton.y]) { // Button has mine under it explosiveButtons.Add(mineButton); mineButton.Disposed += GetDelegateToRemoveFromSequence(explosiveButtons); } } } }
private void Initalize() { // Initalize game variables Config config; XmlSerializer xs = new XmlSerializer(typeof(Config)); using (Stream s = File.OpenRead(configFilePath)) config = (Config)xs.Deserialize(s); tableColumnCount = config.TableColumnCount; tableRowCount = config.TableRowCount; mineCount = config.MineCount; playerWon = false; isFirstClick = true; //System.Diagnostics.Process.Start("Config.txt"); InitializeComponent(); gameTable = this.tableLayoutPanel1; InitalizeTable(gameTable, tableColumnCount, tableRowCount); gameMap = new MineMap(tableColumnCount, tableRowCount, mineCount); spawnedLabels = new List <Label>(); spawnedMineButtons = new List <MineButton>(); explosiveButtons = new List <MineButton>(); MineButton.ClearFlagCount(); // Make the first row a little thicker than the last row gameTable.RowStyles[0].Height = 100f / (float)tableRowCount * 2; gameTable.RowStyles[tableRowCount - 1].Height = 100f / (float)tableRowCount / 2; AddMineButtons(gameTable); int restartButtonColumnSpan = tableColumnCount % 2 == 0 ? 2 : 3; //If even columns then two else three restartButton = AddRestartButton(restartButtonColumnSpan, 0); flagCountLabel = AddFlagCountLabel(); flagCountLabel.ScaleFont(); }
public void DebugButtons(object sender, EventArgs eventArgs) { MineButton b = (MineButton)sender; System.Diagnostics.Debug.WriteLine(b.x + " " + b.y); }
private void MineButton_Click(object sender, EventArgs e) { MineButton mineButton = (MineButton)sender; MouseEventArgs args = e as MouseEventArgs; if (mineButton.IsDisposed) { // Not sure if this will come into effect // It's just incase of weirdness with this method and its button's events // causing this being called twice on the same button return; } int PosX = mineButton.x, PosY = mineButton.y; switch (args?.Button) { case null: // The case of the automatic click :/ case MouseButtons.Left: if (mineButton.IsFlagged) { // Can't click flagged buttons return; } // Making it so your first click can't be a mine if (isFirstClick) { isFirstClick = false; while (gameMap[PosX, PosY]) { gameMap.ReGenerateMines(); } } int minesAroundCount = gameMap.MinesAroundPosition(PosX, PosY); if (gameMap[PosX, PosY] == true) { // Lose; Clicked on mine foreach (MineButton button in explosiveButtons) { button.BackColor = Color.Black; button.ForeColor = Color.White; button.Text = "💣"; } mineButton.BackColor = Color.Red; mineButton.ForeColor = Color.Black; MakeAllButtonsUnclickable(); restartButton.Text = "💀"; restartButton.ForeColor = Color.Red; } else if (minesAroundCount == 0) { mineButton.Dispose(); spawnedMineButtons.Remove(mineButton); // No mines around button, Going to click them automatically List <MineButton> mineButtons = GetControlsAroundPoint <MineButton>(PosX, PosY); // Creating a list of buttons to click // Click Each of the mineButtons around me foreach (MineButton button in mineButtons) { // Unflagging these buttons because It's 100% true that these have no mines under them button.IsFlagged = false; // Doing the weird thing with adding and removing from the event // because PerformClick only works with the Click event // And I am using this because this method is usually in // the MouseUp event, and that is because the Click event // won't f*****g trigger on right clicks! FFFUUUUUUUU button.Click += MineButton_Click; button.PerformClick(); // Clicks with EventArgs not MouseEventArgs button.Click -= MineButton_Click; } } else if (minesAroundCount != 0) { // It has some mines around it, so tell it to just put a label with the number of mines mineButton.Dispose(); Label label = SpawnLabel(PosX, PosY, minesAroundCount); label.ScaleFont(); label.ForeColor = NumberColors[minesAroundCount]; label.MouseUp += Label_Click; } if (spawnedMineButtons.SequenceEqual(explosiveButtons)) { // Player Won! Do winning stuff MakeAllButtonsUnclickable(); restartButton.BackColor = Color.LightYellow; restartButton.ForeColor = Color.DarkOrange; restartButton.Text = "😎"; if (!playerWon) { MessageBox.Show("A Winnar Is You!", "WIN!", MessageBoxButtons.OK); System.Diagnostics.Debug.WriteLine("Player Won!"); playerWon = true; } } break; case MouseButtons.Right: // Do Flag Stuff mineButton.IsFlagged = !mineButton.IsFlagged; UpdateFlagLabelDisplay(); break; } }