Conventional Minesweeper:
Class: UserInfo: User Information
button: Buttons
Topscore: Points
Main: Master Class
Use a sound file: lei.wav
----------------------------------------------------part of the reference code is as follows--------------------
Main.cs
Using system;using system.collections.generic;using system.componentmodel;using system.data;using System.Drawing; Using system.text;using system.windows.forms;using system.io;namespace bigbangbomb{public partial class frmmain:form{ public static Timer time;static int stat;static int run=0;static int Tim=0;int timertim = 0; static int status = 0;public static void End () {run = 1;time. Enabled = False;frmmain.showwin ();} public static void Blast () {run = 2; Time. Enabled = false; Frmmain.showlose ();} public static void Change_stat () {stat = 0;} static int width = 9;static int height = 9;static int mine=10;static string min1 = mine. ToString (); static button[,] But1;public frmmain () {InitializeComponent ();} public static button exp (int x, int y) {return (but1[x, y]);} private void Button1_Click (object sender, EventArgs e) {frmmain frm = new Frmmain (); frmMain.time.Enabled = False;run = 0;fr M.show (); Visible = False;this. Enabled = false;} public static void Setatt (int x, int y, inT m) {width = X;height = Y;mine = m;} public static void Setrun () {run = 0; }public static bool Isbomb (int x, int y) {if (x >= 0 && x < width) {if (y >= 0 && y < height) {RE Turn but1[x, y]. Minded;}} return false;} public static void Enabltime () {time. Enabled = true;} public static void Distime () {time. Enabled = true;} public void Start () {Panel1. width = width * + 8;panel1. Height = height * + 5;this. Width = Panel1. Width + 13;this. Height = Panel1. Height + 215;but1 = new Button[width, Height];button1. left = this. Width/2-28;int x = 0;int y = -19;time = new Timer (); time. Interval = 1000;time. Tick + = new EventHandler (Time_tick), for (int i = 0, i < width; i++) {for (int i1 = 0; i1 < height; i1++) {x = x + 19;i F (I1 = = 0) {x = 0;y = y + 19;} Button B = New button (panel1, width, height, x, y,i,i1,mine); but1[i, i1] = b;}} int b4 = 0; Random r = new Random (), while (B4 < mine) {int x1 = r.next (0,width); int y1 = R.next (0,height); Button B3 = But1[x1, Y1];if (!b3. Minded) {B3. minded = true;b4++;}}} static void Reset () {min1 = mine. ToString ();} private void Form1_Load (object sender, EventArgs e) {stat = 1;tim = 0;timertim = 0;timer2. Enabled = True;frmmain.setatt (width,height,mine), reset (), start ();p Anel1. Visible = True;label2. Text = mine. ToString (); Button5. Text = "Pause"; Button5. Visible = true;} private void Startertoolstripmenuitem_click (object sender, EventArgs e) {Frmmain.setatt (9, 9, 10);} private void Customizedtoolstripmenuitem_click (object sender, EventArgs e) {Attributes at = new Attributes (); at = new Attri Butes (); at. Show ();} private void Timer1_Tick (object sender, EventArgs e) {}public static void Chmine () {min1 = Convert.ToString ( Convert.ToInt32 (min1)-1);} private void Time_tick (object sender, EventArgs e) {if (run = = 0) {Label1. Text = convert.tostring (Timertim + 1); Timertim = Timertim + 1;tim = Timertim;}} public static void Showlose () {MessageBox.Show ("Game End Time" + convert.tostring (Tim) + "seconds."); PublIC static void Showwin () {MessageBox.Show ("Victory Time" + convert.tostring (Tim) + "seconds."); if (status = = 0) {bool ans; ans = checkfile (); if (ans = = true) readcomparefilecontents (); else {StreamWriter SW = new StreamWriter ("Test.txt", false); Frmuserinfo frm = new Frmuserinfo (); frm. ShowDialog (); SW. WriteLine (Frmuserinfo.username); SW. WriteLine (Convert.ToString (Tim)); SW. Close (); }}} private static bool Checkfile () {StreamReader SR; try {SR = new StreamReader ("test.txt"); } catch (FileNotFoundException a) {return false; } SR. Close (); return true; } private static void ReadcompaRefilecontents () {StreamReader SR = new StreamReader ("test.txt"); String str = SR. ReadLine (); str = SR. ReadLine (); SR. Close (); int i = Convert.ToInt32 (str); If (Tim < i) {StreamWriter SW = new StreamWriter ("Test.txt", false); Frmuserinfo frm = new Frmuserinfo (); frm. ShowDialog (); SW. WriteLine (Frmuserinfo.username); SW. WriteLine (Convert.ToString (Tim)); SW. Close (); }} private void Exittoolstripmenuitem_click (object sender, EventArgs e) {}private void Fo Rm1_formclosing (object sender, FormClosingEventArgs e) {application.exit ();} private void Panel1_mousedown (object sender, MouseEventArgs e) {if (button5. Text = "Resume") {MessageBox.Show ("game Stop"); }} private void Button2_Click (object sender, EventArgs e) {status = 0; This. Visible = false; Frmmain.setatt (9, 9, 10); Frmmain frm = new Frmmain (); frmMain.time.Enabled = false; frm. Show (); Run = 0; } private void Button3_Click (object sender, EventArgs e) {status = 1; Attributes at; at = new Attributes (this); At. Show (); } private void Button4_Click (object sender, EventArgs e) {help.showhelp (this, "helpbigbangbomb.chm"); The private void Button5_click (object sender, EventArgs e) {if (button5. Text = = "Pause") {time. Enabled = false; for (int yo = 0, yo < width; yo++) {for (int wo = 0; wo < height; wo++) {but1[yo, wo].b1. Enabled = false; }} button5. text= "Resume"; }else {time. Enabled = true; for (int yo = 0, yo < width; yo++) {for (int wo = 0; wo < height; wo++) {but1[yo, wo].b1. Enabled = true; }} button5. Text = "Pause"; }} private void Panel1_paint (object sender, PaintEventArgs e) {}private void Tim Er2_tick (object sender, EventArgs e) {Label2. Text = min1;if (Run > 0) {button1. Visible = True;time. Enabled = False;timer2. Enabled = false;}} private void Button6_click (object sender, EventArgs e) {Form frm = new Frmtopscore (); frm. Show (); } }}
Minesweeper Button Button.cs
Using system;using system.collections.generic;using system.text;using system.windows.forms;using System.Drawing; namespace Bigbangbomb{public class Button{public button b1;public EventHandler expl; Panel p;bool mined;bool gover;bool disman;bool open;int posx, Posy;int nposx, nposy;int width;int height;int mines;int det Min=0;public button (Panel p1,int w, int h,int x, int y,int pos, int posy1,int mine) {width = W;height = H;mines = Mine;posx = Pos;posy = POSY1;B1 = new Button (); B1. FlatStyle = flatstyle.flat;int bw = 19;int BH = 19;b1. Width = bw;b1. BackColor = systemcolors.controldark;b1. Height = bh;b1. Top = x;b1. left = Y;b1. Click + = new EventHandler (click); B1. MouseDown + = new MouseEventHandler (down);p 1. Controls.Add (b1); mined = False;disman = False;open = false;} public bool Minded{get {return (this.mined);} set {this.mined = value;}} public bool Dismant{get {return (This.disman);}} private void down (object sender, MouseEventArgs e) {if (e.button==mousebuttons.right) {if (dismant) {Disman = fAlse;b1. BackColor = Systemcolors.controldark;} Else{disman = true;b1. BackColor = Color.orange;if (Frmmain.isbomb (posx,posy)) {check ();} Frmmain.chmine ();}}} public void Check () {int win = 2;int coumin = 0;for (int i = 0, i < width; i++) {for (int y = 0; y < height; y++) {if ( Frmmain.exp (i, y). Disman = = True && frmmain.exp (i,y). mined==true) {coumin = coumin + 1;} if (coumin = = mines) {button BH = frmmain.exp (i, Y), if (Bh.open = = False) {if (Bh.disman = = False) {win = 1;} Else{win = 0;}}}} if (win = = 0) {detmin = Detmin + 1;frmmain.end (), for (int i = 0, i < width; i++) {for (int y = 0; y < height; y++) {frm Main.exp (i, y). B1. Enabled = false;}}} private void Show () {if (open = = False && disman==false) {int c = 0;open = True;if (Frmmain.isbomb (posx-1, Posy- 1)) {C + +;} if (Frmmain.isbomb (posx-0, posy-1)) {C + +;} if (Frmmain.isbomb (Posx + 1, posy-1)) {C + +;} if (Frmmain.isbomb (Posx-1, posy-0)) {C + +;} if (Frmmain.isbomb (posx-0, posy-0)) {C + +;} if (Frmmain.isbomb (Posx + 1, posy-0)) {C + +;} if (Frmmain.isbomb (posx-1, Posy + 1)) {C + +;} if (Frmmain.isbomb (posx-0, Posy + 1)) {C + +;} if (Frmmain.isbomb (Posx + 1, posy + 1)) {C + +;} if (c = = 0) {B1. Text = "";} if (C > 0) {b1. Text = (c.tostring ()); B1. BackColor = color.cornsilk;b1. Enabled = false;} Else{b1. BackColor = color.cornsilk;b1. Enabled = False;show1 (Posx-1, Posy-1); Show1 (Posx-0, Posy-1); Show1 (Posx + 1, Posy-1); Show1 (Posx-1, Posy-0); sh Ow1 (Posx-0, Posy-0); Show1 (Posx + 1, Posy-0), Show1 (Posx-1, Posy + 1), Show1 (Posx-0, Posy + 1); Show1 (Posx + 1, Posy + 1);}}} public int Posx{get {return (THIS.POSX);}} public int Posy{get {return (this.posy);}} private void Playfile (String url) {wmplib.windowsmediaplayer Player = new Wmplib.windowsmediaplayer (); Player.url = URL; Player.controls.play (); }private void Expld () {for (int i = 0; i < width; i++) {for (int y = 0; y < height; y++) {button BUT1 = Frmmain.exp (i, Y), if (Frmmain.isbomb (i,y)) {if (but1.b1.BackColor = = Color.orange) {but1.b1.BackColor = Color.green;} Else{but1.b1.backcolor = Color.red;but1.b1.backgroundimage = Image.FromFile ("dead.jpg"); Playfile (@ "Expl.wav");}} else if (Frmmain.exp (i,y). Disman==true) {frmmain.exp (i, y). B1. BackColor = color.red;} but1.b1.Enabled = false;}} Gover = True;frmmain.blast ();} public bool Gstat () {return (gover);} private void Dispau (Button B12) {B12. Enabled = false; }private void Show1 (int x,int y) {if (x >= 0 && x < width) {if (y >= 0 && y < height) {button bot = Frmmain.exp (x, y); Bot.show ();}}} private void Click (object sender, System.EventArgs e) {frmmain.enabltime (); Dismant) {if (minded) {EXPLD (); Frmmain.distime ();} Else{show (); check ();}}}}
C # 50 Minesweeper