Mayın Tarlası Oyunu – MineField Game — C#
Anasayfa
[code lang=”csharp”]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;//www.gorselprogramlama.com
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MineField_Game
{
public partial class frmGame : Form
{
public frmGame()
{
InitializeComponent();
}
#region MineField Properties
int totalField = 0;
private void CreateField(int height, int width)
{
int X = 1, Y = 1;
int btnNameIndis = 0;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
Button btn = new Button();//www.gorselprogramlama.com
btn.Left = X; btn.Top = Y;
btn.Height = 25; btn.Width = 25;
btn.Name = "btnField" + btnNameIndis;
//btn.Text = btnNameIndis.ToString();
btn.TabIndex = btnNameIndis;
btn.BackColor = Color.RoyalBlue;
btn.FlatStyle = FlatStyle.Popup;
btn.BackgroundImageLayout = ImageLayout.Stretch;
btn.Click += new EventHandler(MineField_Click);
btn.MouseEnter += new EventHandler(MineField_MouseEnter);
btn.MouseLeave += new EventHandler(MineField_MouseLeave);
pnlField.Controls.Add(btn);
X += btn.Width + 1;
btnNameIndis++;
}
Y += 25 + 1;
X = 1;
}
totalField = height * width;
lblFloor.Text = height + "x" + width;
lblMine.Text = totalMine.ToString();
pnlField.Height = 26 * height + 1;
pnlField.Width = 26 * width + 1;
this.Height = pnlField.Height + pnlField.Top + 29;
this.Width = pnlField.Width + pnlField.Left + 7;
PutMine();
}
private void DeleteField()
{
for (int i = 0; i < totalField; i++)
{
pnlField.Controls.Find("btnField" + i, true)[0].Dispose();
}
}
int totalMine = 0;
Random r = new Random();//www.gorselprogramlama.com
int[] Mines;
private void PutMine()
{
Mines = new int[totalMine];
for (int i = 0; i < totalMine; i++)
{
int rnd = r.Next(0, totalField);
while (Array.IndexOf(Mines,rnd) != -1)
{
rnd = r.Next(0, totalField);
}
Mines[i] = rnd;
}
}
private void OpenAllMine()
{
for (int i = 0; i < totalField; i++)
{
Button btn = (Button)pnlField.Controls.Find("btnField" + i, true)[0];
int NameIndis = Convert.ToInt32(btn.Name.Remove(0,8));
if (Array.IndexOf(Mines,NameIndis) != -1)
{
btn.BackgroundImage = Properties.Resources.mine;
}
}
}
#region Field Give Number
private int PutNumber(int indis)
{
int value = 0;
int line = 0;
if (totalMine == 9)
{
line = 10;//www.gorselprogramlama.com
}
else
{
line = 16;
}
if (indis % FieldLine == 0) // sol dikey
{
if (indis == (FieldLine * line) – FieldLine)
{
if (MineControl(indis – FieldLine)) value++;
if (MineControl((indis – FieldLine) + 1)) value++;
}
else if (indis == 0)
{
if (MineControl(indis + FieldLine)) value++;
if (MineControl((indis + FieldLine) + 1)) value++;
}
else
{
if (MineControl(indis – FieldLine)) value++;
if (MineControl((indis – FieldLine) + 1)) value++;
if (MineControl(indis + FieldLine)) value++;
if (MineControl((indis + FieldLine) + 1)) value++;
}
if (MineControl(indis + 1)) value++;
}
else if (indis >= 0 && indis < FieldLine) // üst
{
if (indis == (FieldLine – 1))
{
if (MineControl(indis – 1)) value++;//www.gorselprogramlama.com
if (MineControl(indis + FieldLine)) value++;
if (MineControl(indis + FieldLine – 1)) value++;
}
else
{
if (MineControl(indis – 1)) value++;
if (MineControl(indis + 1)) value++;
if (MineControl(indis + FieldLine)) value++;
if (MineControl(indis + FieldLine – 1)) value++;
if (MineControl(indis + FieldLine + 1)) value++;
}
}
else if (indis % FieldLine == (FieldLine – 1) && (indis – (FieldLine – 1)) / FieldLine <= (line – 1))
{ // sağ dikey
if (indis == ((FieldLine * line) – 1))
{
if (MineControl(indis – FieldLine)) value++;
if (MineControl(indis – FieldLine – 1)) value++;
if (MineControl(indis – 1)) value++;
}
else
{
if (MineControl(indis – 1)) value++;
if (MineControl(indis – FieldLine)) value++;
if (MineControl(indis + FieldLine)) value++;
if (MineControl(indis – FieldLine – 1)) value++;
if (MineControl(indis + FieldLine – 1)) value++;
}
}
else if (indis >= (FieldLine * line) – FieldLine && indis < (FieldLine * line)) // alt
{
if (MineControl(indis – FieldLine)) value++;
if (MineControl(indis – FieldLine – 1)) value++;
if (MineControl(indis – FieldLine + 1)) value++;
if (MineControl(indis – 1)) value++;
if (MineControl(indis + 1)) value++;
}
else // orta kısım
{
if (MineControl(indis – FieldLine)) value++;
if (MineControl(indis – FieldLine – 1)) value++;
if (MineControl(indis – FieldLine + 1)) value++;
if (MineControl(indis + FieldLine)) value++;
if (MineControl(indis + FieldLine – 1)) value++;
if (MineControl(indis + FieldLine + 1)) value++;
if (MineControl(indis – 1)) value++;
if (MineControl(indis + 1)) value++;
}
return value;
}
#endregion
private bool MineControl(int value)
{
if (Array.IndexOf(Mines,value) != -1)
{
return true;
}
return false;//www.gorselprogramlama.com
}
#region MineField Events
void MineField_MouseLeave(object sender, EventArgs e)
{
if (((Button)sender).BackColor != Color.Chartreuse)
{
((Button)sender).BackColor = Color.RoyalBlue;
}
}
void MineField_MouseEnter(object sender, EventArgs e)
{
if (((Button)sender).BackColor != Color.Chartreuse)
{
((Button)sender).BackColor = Color.DodgerBlue;
}
}
private void MineField_Click(object sender, EventArgs e)
{
int NameIndis = Convert.ToInt32((((Button)sender).Name).Remove(0, 8));
if (Array.IndexOf(Mines, NameIndis) != -1)
{
//((Button)sender).BackColor = Color.Red;
((Button)sender).BackgroundImage = Properties.Resources.mine;
OpenAllMine();//www.gorselprogramlama.com
MessageBox.Show("Üzgünüm, Oyunu kaybettiniz. Sonraki oyun için bol şans!", "Kaybettiniz");
pnlField.Enabled = false;
}
else
{
((Button)sender).BackColor = Color.Chartreuse;
((Button)sender).Text = PutNumber(NameIndis).ToString();
((Button)sender).Enabled = false;
ControlToWin();
}
}
private void ControlToWin()
{
bool win = true;
for (int i = 0; i < totalField; i++)
{
Button btn = (Button)(pnlField.Controls.Find("btnField" + i, true)[0]);
if (btn.Enabled)
{
if (!MineControl(Convert.ToInt32(btn.Name.Remove(0,8))))
{
win = false;
break;
}
}
}
if (win)
{
MessageBox.Show("Tebrikler, Oyunu kazandınız. Bütün mayınları tespit ettiniz.","Kazandınız");
OpenAllMine();
pnlField.Enabled = false;
}
}
#endregion
#endregion
#region MenuStrip Events
int FieldLine = 0;
private void başlangıçToolStripMenuItem_Click(object sender, EventArgs e)
{
totalMine = 9;
FieldLine = 10;
DeleteField();
CreateField(10, 10);
pnlField.Enabled = true;
}
private void ortaToolStripMenuItem_Click(object sender, EventArgs e)
{
totalMine = 40;
FieldLine = 16;
DeleteField();
CreateField(16, 16);//www.gorselprogramlama.com
pnlField.Enabled = true;
}
private void zorToolStripMenuItem_Click(object sender, EventArgs e)
{
totalMine = 99;
FieldLine = 30;
DeleteField();
CreateField(16, 30);
pnlField.Enabled = true;
}
private void hakkındaToolStripMenuItem_Click(object sender, EventArgs e)
{
frmAbout frmA = new frmAbout();
frmA.ShowDialog();
}
#endregion
private void Form1_Load(object sender, EventArgs e)
{
this.Size = new Size(267, 339);
pnlField.Size = new Size(261, 261);//www.gorselprogramlama.com
}
}
}
[/code]
Hakkında
[code lang=”csharp”]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;//www.gorselprogramlama.com
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MineField_Game
{
public partial class frmAbout : Form
{
public frmAbout()
{
InitializeComponent();
}
private void frmAbout_Load(object sender, EventArgs e)
{
}
}
}
[/code]
Ümit Demirtaş




Güzel çalışma olmuş tebrik ederim. ancak mayın tarlasının mantığı gereği
boluğa basıldığı zaman çevresindeki sayı bulunan tarla sınırlarına kadar açması gerekiyor.
benim de buna benzer bir uygulamam var. uygulama konsol da çalışıyor. form halini henüz yapmadım. ancak uygulamayı yapan arkadaşa benim uygulamamı inceleyerek burdaki kendi uygulamasını geliştirmesini öneririm.
Kenan :
Projeyi gönderebilirseniz yayınlarız.
gorselprogram@gmail.com
abi mine olmuyo MİNE’I ÇÖZDÜM ÇALISMIYOR 0 ALDIK MUTLU MUSUN? OLMADI İŞTE.. yok y 70 aldik sağol abi iyi günler kolay gelsin <3