Aylık arşivler: Mayıs 2014

Mayın Tarlası Örnek Uygulaması

Mayın tarlası örneği kodları:


Button[,] b = new Button[10, 10];
        int[,] mayin;
        int mayin_adet;

        private void Form1_Load(object sender, EventArgs e)
        {
           mayin = new int[b.GetLength(0), b.GetLength(1)];
            /// mayın üretme 
           mayin_adet = b.Length/4;
            Random r=new Random();
            for (int i = 0; i < mayin_adet; i++)
            {
                int sat=r.Next(mayin.GetLength(0));
                int sut=r.Next(mayin.GetLength(1));
                if(mayin[sat,sut]==-1)
                {
                    i--;
                continue;
                }
                mayin[sat,sut ] = -1;
                adet_yaz(sat,sut);
            }

            for (int i = 0; i < b.GetLength(0); i++)
            {
                for (int j = 0; j < b.GetLength(1); j++)
                {
                    b[i, j] = new Button();
                    b[i, j].Name = "b" + i + "_" + j;
                    //b[i, j].Text = mayin[i, j].ToString();
                    b[i, j].Width = 50;
                    b[i, j].Height = 50;
                    b[i, j].Left = j * b[i, j].Width;
                    b[i, j].Top = i * b[i, j].Height;
                    b[i, j].Click += new EventHandler(btn_tiklanidi);//olay tanimlama
                    b[i, j].MouseDown += b_tik;
                        Controls.Add(b[i, j]);
                }
            }
        }

        void b_tik(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right && (sender as Button).Text == "P") (sender as Button).Text = "";
            else if (e.Button == MouseButtons.Right) (sender as Button).Text = "P";
        }



        void btn_tiklanidi(object sender, EventArgs e)
        {
            string[] bno=(sender as Button).Name.Substring(1).Split('_');
            int i = Convert.ToInt32(bno[0]);
            int j = Convert.ToInt32(bno[1]);
            if (mayin[i, j] != -1)
            {
                if (mayin[i, j] != 0) b[i, j].Text = mayin[i, j].ToString();
                else etrafi_bosalt(i,j);
                b[i, j].Enabled = false;
            }
            else {
                hepsini_ac();
            }
        }

        void adet_yaz(int sat, int sut)
        {
            int sat_bas = (sat == 0) ? 0 : (sat - 1);
            int sat_bit = (sat == (mayin.GetLength(0) - 1)) ? (mayin.GetLength(0) - 1) : (sat + 1);
            int sut_bas = (sut == 0) ? 0 : (sut - 1);
            int sut_bit = (sut == (mayin.GetLength(1) - 1)) ? (mayin.GetLength(1) - 1) : (sut + 1);

            for (int i = sat_bas; i <= sat_bit; i++)
            {
                for (int j = sut_bas; j <= sut_bit; j++)
                {
                    if (mayin[i, j] != -1) mayin[i, j]++;
                }
            }
        }

        void hepsini_ac()
        {
            for (int i = 0; i < b.GetLength(0); i++)
            {
                for (int j = 0; j < b.GetLength(1); j++)
                {
                    if (mayin[i, j] == -1) b[i, j].Text = "*";
                    else if (mayin[i, j] != 0) b[i, j].Text = mayin[i, j].ToString();

                    b[i, j].Enabled = false;
                }
            }
        }

        void etrafi_bosalt(int sat,int sut)
        {
            b[sat, sut].Text = "x";
            int sat_bas = (sat == 0) ? 0 : (sat - 1);
            int sat_bit = (sat == (mayin.GetLength(0) - 1)) ? (mayin.GetLength(0) - 1) : (sat + 1);
            int sut_bas = (sut == 0) ? 0 : (sut - 1);
            int sut_bit = (sut == (mayin.GetLength(1) - 1)) ? (mayin.GetLength(1) - 1) : (sut + 1);

            for (int i = sat_bas; i <= sat_bit; i++)
            {
                for (int j = sut_bas; j <= sut_bit; j++)
                {
                    if (b[i,j].Text=="x") continue;
                    if (mayin[i, j] != 0) b[i, j].Text = mayin[i, j].ToString();
                    else etrafi_bosalt(i, j);
                    b[i, j].Enabled = false;

                }
            }
            b[sat, sut].Text = "";
        }