C#の50掃雷


通常の掃雷:
クラス:UserInfo:ユーザー情報
Button:ボタン
TopScore:ポイント
Main:マスタークラス
音声ファイル:lei.wav
--------------------------------------------------------------------------------------------------------------------
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;				
				frm.Show();
				this.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)
				{
					return 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 * 19 + 8;
			panel1.Height = height * 19 + 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;
					if (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();
			panel1.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 Attributes();
				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("  " + Convert.ToString(tim) + " seconds.");
		}
		public static void showwin()
		{			
			MessageBox.Show(" " + 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 Form1_FormClosing(object sender, FormClosingEventArgs e)
		{
			Application.Exit();
		}

		private void panel1_MouseDown(object sender, MouseEventArgs e)
		{
            if (button5.Text == "Resume")
            {
                MessageBox.Show(" ");
            }            
		}

        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");
        }

        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 timer2_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();
        }
    }
}

掃雷ボタン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 detmin=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);
			p1.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++)
						{
							frmMain.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);
					show1(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();
			if (!Dismant)
			{
				if (Minded)
				{
					expld();
					frmMain.distime();
				}
				else
				{
					show();
					check();
				}
			}
		}
		}
	}