//mine_form.cs
using Mine.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Mine
{
public partial class mine_form : Form
{
button_property[,] buttons_property;
Button[,] buttons;
mine_property[,] mines;
Button button_reset;
button_property button_reset_property;
TextBox left_to_win_textbox;
List<int> marked_button;
public static int N_row = 9, N_col = 11, N_mine = 20, unmarked_button_left = 0;
public mine_form()
{
InitializeComponent();
buttons_property = new button_property[N_row, N_col];
buttons = new Button[N_row, N_col];
mines = new mine_property[N_row, N_col];
marked_button = new List<int>();
unmarked_button_left = N_row * N_col - N_mine - unmarked_button_left;
//create mine
for (int i = 0; i < buttons.GetLength(0); i++)
{
for (int j = 0; j < buttons.GetLength(1); j++)
{
mines[i, j] = new mine_property()
{
row = i,
column = j,
is_mine = false
};
}
}
Random r = new Random();
for (int i = 0; i < N_mine;)
{
int M_row = r.Next(0, N_row);
int M_col = r.Next(0, N_col);
if (mines[M_row, M_col].is_mine == false)
{
mines[M_row, M_col].is_mine = true;
i++;
}
}
//create button
for (int i = 0; i < buttons.GetLength(0); i++)
{
for (int j = 0; j < buttons.GetLength(1); j++)
{
buttons[i, j] = new Button();
buttons_property[i, j] = new button_property
{
width = 30,
row = i,
column = j,
right_click = false,
is_mine_surrounded = false
};
buttons[i, j].Tag = i.ToString() + " " + j.ToString();
buttons[i, j].Image = buttons_property[i, j].flag;
buttons[i, j].ImageAlign = ContentAlignment.MiddleCenter;
buttons[i, j].Size = buttons_property[i, j].size;
buttons[i, j].Location = buttons_property[i, j].location;
//add button flag event
buttons[i, j].MouseDown += Button_MouseClick;
Controls.Add(buttons[i, j]);
}
}
//mark buttons surrounded by mine, used to decide when click on button, unveil surround buttons or not
for (int i = 0; i < buttons.GetLength(0); i++)
{
for (int j = 0; j < buttons.GetLength(1); j++)
{
if (mines[i, j].is_mine == true)
{
if (i > 0 && j > 0)
{
buttons_property[i - 1, j - 1].is_mine_surrounded = true;
}
if (i > 0)
{
buttons_property[i - 1, j].is_mine_surrounded = true;
}
if (i > 0 && j + 1 < N_col)
{
buttons_property[i - 1, j + 1].is_mine_surrounded = true;
}
if (j > 0)
{
buttons_property[i, j - 1].is_mine_surrounded = true;
}
if (j + 1 < N_col)
{
buttons_property[i, j + 1].is_mine_surrounded = true;
}
if (i + 1 < N_row && j > 0)
{
buttons_property[i + 1, j - 1].is_mine_surrounded = true;
}
if (i + 1 < N_row)
{
buttons_property[i + 1, j].is_mine_surrounded = true;
}
if (i + 1 < N_row && j + 1 < N_col)
{
buttons_property[i + 1, j + 1].is_mine_surrounded = true;
}
}
}
}
//reset button
button_reset = new Button();
button_reset_property = new button_property
{
width = 30,
row = N_row / 2,
column = 0
};
button_reset.Image = button_reset_property.smile;
button_reset.ImageAlign = ContentAlignment.MiddleCenter;
button_reset.Size = button_reset_property.size;
button_reset.Location = new Point(button_reset_property.row * button_reset_property.width, 0);
//add button reset event
button_reset.Click += Button_reset_Click;
Controls.Add(button_reset);
//create left to win text box
left_to_win_textbox = new TextBox();
left_to_win_textbox.Location = new Point((button_reset_property.row + 2) * button_reset_property.width, 0);
left_to_win_textbox.AutoSize = false;
left_to_win_textbox.Height = button_reset_property.width;
left_to_win_textbox.Width = button_reset_property.width * 5;
left_to_win_textbox.Font = new System.Drawing.Font("ArialBlack", 14, FontStyle.Regular);
Controls.Add(left_to_win_textbox);
}
//reset button click
private void Button_reset_Click(object sender, EventArgs e)
{
Application.Restart();
}
//blank buttons mouse click
private void Button_MouseClick(object sender, MouseEventArgs e)
{
Button current_button = (Button)sender;
int row = Convert.ToInt32(current_button.Tag.ToString().Split(' ')[0]);
int column = Convert.ToInt32(current_button.Tag.ToString().Split(' ')[1]);
if (e.Button == MouseButtons.Right)
{
buttons_property[row, column].right_click = !buttons_property[row, column].right_click;
buttons[row, column].Image = buttons_property[row, column].flag;
}
else if (e.Button == MouseButtons.Left)
{
//delete button mouse click event
buttons[row, column].MouseDown -= Button_MouseClick;
//if step on mine, unveil all mines, delete all button mouse click events
if (mines[row, column].is_mine == true)
{
button_reset.Image = button_reset_property.cry;
for (int i = 0; i < N_row; i++)
{
for (int j = 0; j < N_col; j++)
{
if (mines[i, j].is_mine == true)
{
buttons[i, j].Image = buttons_property[i, j].mine_explosion;
}
buttons[i, j].MouseDown -= Button_MouseClick;
}
}
left_to_win_textbox.Text = "Sorry, try again";
return;
}
//click on safe button, unveil surranding blanket, mark button with # of mines surrounding it,
//delete button mouse click event
else
{
var unmarked_button_list = new List<button_mark>();
var first_unmarked_button = new button_mark
{
row = row,
col = column
};
unmarked_button_list.Add(first_unmarked_button);
//recursive: click one button, unveil a patch of buttons
while (unmarked_button_list.Count > 0)
{
unmarked_button_list = mark_button(unmarked_button_list);
unmarked_button_left--;
}
}
//show how many click left to win
left_to_win_textbox.Text = unmarked_button_left + " left to win";
//check win or not
if (unmarked_button_left == 0)
{
button_reset.Image = button_reset_property.thumb_up;
left_to_win_textbox.Text = "You Win!!!";
}
}
}
//receives a list of blank button, mark first blank button in the list with # of surrounding mines, add more surround blank buttons to list
private List<button_mark> mark_button(List<button_mark> button_blank_list)
{
var first_button_blank = button_blank_list.First();
int _row = first_button_blank.row;
int _col = first_button_blank.col;
int mine_count = 0;
//surrounding button [x-1,y-1]
if (_row > 0 && _col > 0)
{
if (mines[_row - 1, _col - 1].is_mine == true)
{
mine_count++;
}
//if button_blank is surrounded by mine, don't unveil adjacent buttons
else if (buttons_property[_row, _col].is_mine_surrounded == false)
{
button_blank_list = add_button_blank(button_blank_list, _row - 1, _col - 1);
}
}
//surrounding button [x-1,y]
if (_row > 0)
{
if (mines[_row - 1, _col].is_mine == true)
{
mine_count++;
}
else if (buttons_property[_row, _col].is_mine_surrounded == false)
{
button_blank_list = add_button_blank(button_blank_list, _row - 1, _col);
}
}
//surrounding button [x-1,y+1]
if (_row > 0 && _col + 1 < N_col)
{
if (mines[_row - 1, _col + 1].is_mine == true)
{
mine_count++;
}
else if (buttons_property[_row, _col].is_mine_surrounded == false)
{
button_blank_list = add_button_blank(button_blank_list, _row - 1, _col + 1);
}
}
//surrounding button [x,y-1]
if (_col > 0)
{
if (mines[_row, _col - 1].is_mine == true)
{
mine_count++;
}
else if (buttons_property[_row, _col].is_mine_surrounded == false)
{
button_blank_list = add_button_blank(button_blank_list, _row, _col - 1);
}
}
//surrounding button [x,y+1]
if (_col + 1 < N_col)
{
if (mines[_row, _col + 1].is_mine == true)
{
mine_count++;
}
else if (buttons_property[_row, _col].is_mine_surrounded == false)
{
button_blank_list = add_button_blank(button_blank_list, _row, _col + 1);
}
}
//surrounding button [x+1,y-1]
if (_row + 1 < N_row && _col > 0)
{
if (mines[_row + 1, _col - 1].is_mine == true)
{
mine_count++;
}
else if (buttons_property[_row, _col].is_mine_surrounded == false)
{
button_blank_list = add_button_blank(button_blank_list, _row + 1, _col - 1);
}
}
//surrounding button [x+1,y]
if (_row + 1 < N_row)
{
if (mines[_row + 1, _col].is_mine == true)
{
mine_count++;
}
else if (buttons_property[_row, _col].is_mine_surrounded == false)
{
button_blank_list = add_button_blank(button_blank_list, _row + 1, _col);
}
}
//surrounding button [x+1,y+1]
if (_row + 1 < N_row && _col + 1 < N_col)
{
if (mines[_row + 1, _col + 1].is_mine == true)
{
mine_count++;
}
else if (buttons_property[_row, _col].is_mine_surrounded == false)
{
button_blank_list = add_button_blank(button_blank_list, _row + 1, _col + 1);
}
}
//check surrounding buttons finished, remove center button from list
button_blank_list.Remove(first_button_blank);
//mark the center button
switch (mine_count)
{
case 0:
buttons[_row, _col].Image = buttons_property[_row, _col].zero;
break;
case 1:
buttons[_row, _col].Image = buttons_property[_row, _col].one;
break;
case 2:
buttons[_row, _col].Image = buttons_property[_row, _col].two;
break;
case 3:
buttons[_row, _col].Image = buttons_property[_row, _col].three;
break;
case 4:
buttons[_row, _col].Image = buttons_property[_row, _col].four;
break;
case 5:
buttons[_row, _col].Image = buttons_property[_row, _col].five;
break;
case 6:
buttons[_row, _col].Image = buttons_property[_row, _col].six;
break;
case 7:
buttons[_row, _col].Image = buttons_property[_row, _col].seven;
break;
}
//button_blank is marked
if (!marked_button.Contains(first_button_blank.id))
{
marked_button.Add(first_button_blank.id);
}
//remove marded button mouse click event
buttons[_row, _col].MouseDown -= Button_MouseClick;
return button_blank_list;
}
//add new button_blank to list
private List<button_mark> add_button_blank(List<button_mark> button_blank_list, int _row, int _col)
{
button_mark new_button_blank = new button_mark()
{
row = _row,
col = _col
};
//if button is marked, don't add to button blank list
if (marked_button.Contains(new_button_blank.id))
{
return button_blank_list;
}
button_blank_list.Add(new_button_blank);
marked_button.Add(new_button_blank.id);
return button_blank_list;
}
}
}
-------------------------------------------------------------------
//settings.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Mine
{
public partial class setting : Form
{
public mine_form new_form;
public setting()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int row, col, mine, left_to_win;
try
{
row = Convert.ToInt32(ux_row.Text);
col = Convert.ToInt32(ux_column.Text);
mine = Convert.ToInt32(ux_mine.Text);
left_to_win = Convert.ToInt32(ux_button_left_to_win.Text);
}
catch(Exception e1)
{
error.Text = "invalid setting";
return;
}
if(mine>row*col/2)
{
error.Text = "max # of mine is half # of all buttons";
return;
}
mine_form.N_row = row;
mine_form.N_col = col;
mine_form.N_mine = mine;
mine_form.unmarked_button_left = left_to_win;
this.Hide();
new_form = new mine_form();
new_form.ShowDialog();
this.Close();
}
}
}
-------------------------------------------------------------------------
//button_property.cs
using Mine.Properties;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Mine
{
public class button_property
{
public int row { get; set; }
public int column { get; set; }
public int width { get; set; }
public Point location { get { return new Point(row * width, (column+1) * width); } }
//right click toggle flag on/off
public Image flag
{
get
{
if (right_click == true)
{
return resize_iamge(Resources.flag, width, width);
}
else
{
return resize_iamge(Resources.empty, width, width);
}
}
}
public Size size { get { return new Size(width, width); } }
public bool right_click { get; set; }
public Image mine_explosion { get { return resize_iamge(Resources.mine, width, width); } }
public Image zero { get { return resize_iamge(Resources._0, width, width); } }
public Image one { get { return resize_iamge(Resources._1, width, width); } }
public Image two { get { return resize_iamge(Resources._2, width, width); } }
public Image three { get { return resize_iamge(Resources._3, width, width); } }
public Image four { get { return resize_iamge(Resources._4, width, width); } }
public Image five { get { return resize_iamge(Resources._5, width, width); } }
public Image six { get { return resize_iamge(Resources._6, width, width); } }
public Image seven { get { return resize_iamge(Resources._7, width, width); } }
public Image eight { get { return resize_iamge(Resources._8, width, width); } }
public bool is_mine_surrounded { get; set; }
public Image smile { get { return resize_iamge(Resources.smile, width, width); } }
public Image thumb_up { get { return resize_iamge(Resources.thumb_up, width, width); } }
public Image cry { get { return resize_iamge(Resources.cry, width, width); } }
public Image question { get { return resize_iamge(Resources.question, width, width); } }
//resize button_image
private static Image resize_iamge(Image origin, int W, int H)
{
return (Image)(new Bitmap(origin, (new Size(W, H))));
}
}
}
------------------------------------------------------------------------------
//mine_property.cs
using Mine.Properties;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Mine
{
public class mine_property
{
public int row { get; set; }
public int column { get; set; }
public bool is_mine { get; set; }
}
}
--------------------------------------------------------
//button_mark.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Mine
{
public class button_mark
{
public int row { get; set; }
public int col { get; set; }
//used for identifying which button is marked
public int id { get { return row * 100 + col; } }
}
}
game download link:
http://stackoverflow.com/questions/7378661/how-to-get-a-trusted-verified-publisher
No comments:
Post a Comment