MineSweeper - Mayın Tarlası

C# ile yapılmış örneklere devam... Burada da hepimizin bildiği mayın tarlası oyununun bir örneği bulunuyor. Tüm fonksiyonlarını içermemesine rağmen genel bir bilgi vermesi açısından faydalı olabilir...
Ve kodlar :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;

//public void baslangic();

namespace MineFarm
{
public partial class Form1 : Form
{

Button btnReplay;
int bayrakSayisi = 10;
Panel pnl;
Label lblMine,lblTime;
Timer timer;
int zaman=0;
int win = 0;
int trueBayrak = 0;


ArrayList flagList = new ArrayList();//Location (Points)
ArrayList buttonList = new ArrayList();//Buttons
ArrayList mineList = new ArrayList();//Location (Points)

void Win(object sender)
{
Button temp = (Button)sender;


foreach (Button o in buttonList)
{
if (!gotMine(o))
{
if (!inFlaglist(o))
{
if (!o.Enabled)
{

}
}
}

}


}

bool Sayi(object sender)
{
Button temp = (Button)sender;
int sayi = 0;


if (!gotMine(temp))
{
int x = temp.Location.X;
int y = temp.Location.Y;

int initialX = x;
int initialY = y;


for (int i = -1; i < 2; i++)
{

x = x - (i * 30);
temp.Location = new Point(x, y);
for (int j = -1; j < 2; j++)
{

y = y - (j * 30);
temp.Location = new Point(x, y);
if (gotMine(temp))
{
sayi++;
}
y = initialY;
temp.Location = new Point(x, y);

}
x = initialX;
temp.Location = new Point(x, y);
}


if (sayi != 0)
{
temp.Text = sayi.ToString();
return true;
}
else { return false; }


}
else return false;

}

/*
void Open(object sender)
{
Button temp = (Button)sender;
int x = temp.Location.X; int y = temp.Location.Y;
int initialX = x; int initialY = y;

if (gotMine(temp))
{
return;
}

else if (x == 0)
{
return;
}

else if(!gotMine(temp)&& x!=0)
{

temp.Enabled= false;
x = x - 30;
temp.Location = new Point(x, y);
Open(temp);

}


}
*/

bool gotMine(object sender)
{
Button temp = (Button)sender;

if (mineList.Contains(temp.Location))
{
return true;
}
else {
return false;
}

}

bool inFlaglist(object sender)
{
Button temp = (Button)sender;

if (flagList.Contains(temp.Location))
{
return true;
}
else
{
return false;
}
}


void random_Mine()
{
Random random = new Random();
for (int i = 0; i < 10; i++)
{

int random1 = random.Next(0, 10);
int random2 = random.Next(0, 10);
while (random1 == random2)
{
random1 = random.Next(0, 10);
}

Point a = new Point(random1*30, random2*30);
mineList.Add(a);



}
if (mineList.ToArray().Length != 10)
{
int a = random.Next(0, 10);
int b = random.Next(0, 10);

Point x = new Point(a * 30, b * 30);
mineList.Add(x);
}



}

void btnReplay_Click(object sender, MouseEventArgs replayArgs)
{


if (replayArgs.Button == MouseButtons.Right)
{
string liste="";
foreach(Point a in mineList){
Console.WriteLine(" "+a.ToString());
liste =liste+a.ToString();
}

MessageBox.Show(liste +"\n Eastern Egg!!! ");
}

btnReplay.Image = new Bitmap("GulenAdam.jpg");
temizle();
zaman = 0;
lblTime.Text = "Süre : " + zaman.ToString();
timer.Start();



}



//Paneldeki buttonlara týklandýðýnda
void btn1_Click(object sender, MouseEventArgs arg)
{
Console.WriteLine("hello");
Button btn = (Button)sender;
Console.WriteLine(btn.Location.ToString());

// Open(btn);
Sayi(btn);

if (arg.Button == MouseButtons.Right)
{
if (!inFlaglist(btn))
{
btn.Image = new Bitmap("flag.jpg");

flagList.Add(btn.Location);

bayrakSayisi = bayrakSayisi - 1;
lblMine.Text = "Flags Left: " + bayrakSayisi;
}
}
else
{
if (inFlaglist(btn))
{
//Flag konulan yere týklandýðýnda patlama!

}
else if (gotMine(btn))
{
btn.Image = new Bitmap("mine.jpg");
btnReplay.Image = new Bitmap("Bom.jpg");
timer.Stop();

//Diger mayýnlarý göster

foreach(Button b in buttonList)
{
if(gotMine(b))
{
b.Image = new Bitmap("mine.jpg");
}

if (flagList.Contains(b.Location)&&!gotMine(b))
{
b.Image = new Bitmap("flagWrong.jpg");
}
if (flagList.Contains(b.Location) && gotMine(b))
{
b.Image = new Bitmap("flag.jpg");
trueBayrak++;
}


}

pnl.Enabled = false; //buttons and panel are disabled
}
else
{
//btn.BackColor = Color.BurlyWood;
btn.Enabled = false;
win++;
int x=mineList.Count;
int a = flagList.Count;
if (win >= (100 - trueBayrak)) {
MessageBox.Show("You win!");
}


}
}

}

void Timer_Tick(object sender , EventArgs r)
{
zaman++;
lblTime.Text ="Süre : "+ zaman.ToString();


}



void GUI()
{

//Pencere Boyutu ayarlandý
this.Width = 350;
this.Height = 400;

//Menu Yaratýlmasý

// MenuStrip manu = new MenuStrip();



//this.Controls.Add(manu);


//Menu yaratýldý

pnl = new Panel();//Buttonlarýn yer aldýgý Yeni panel Oluþturuldu
//Buttonlarýn yer aldýðý Panel Boyutu ayarlandý
pnl.Width = 300;
pnl.Height = 300;
//Panelin üstündeki Yazýlar ve button oluþturuldu

lblMine = new Label();
lblMine.Text = "Flags Left: " + bayrakSayisi;
lblMine.Location = new Point(5, 10);

btnReplay = new Button();
btnReplay.Image = new Bitmap("GulenAdam.jpg");
btnReplay.Width = 40;
btnReplay.Height = 40;
btnReplay.Location = new Point(140, 5);
btnReplay.MouseDown += new MouseEventHandler(btnReplay_Click);




lblTime = new Label();
lblTime.Text = "Süre : "+zaman.ToString();
lblTime.Location = new Point(225, 10);

timer = new Timer();
timer.Interval = 1000;
timer.Start();
timer.Tick += new EventHandler(Timer_Tick);


this.Controls.Add(lblMine);
this.Controls.Add(btnReplay);
this.Controls.Add(lblTime);

int x = 30; // Buttonlarýn boyutu


//Button panel e 10 - 10 button oluþturuldu
for (int j = 0; j < 10; j++)
{
for (int i = 0; i < 10; i++)
{

Button btn1 = new Button();
btn1.Location = new Point(x * i, x * j);
btn1.Height = 30;
btn1.Width = 30;

buttonList.Add(btn1);
pnl.Controls.Add(btn1);

btn1.MouseDown += new MouseEventHandler(btn1_Click);
}//Button for dongusu iç dongu
} //Button for Dongusu sonu


pnl.Location = new Point(20, 50);
Controls.Add(pnl);



}

void temizle()
{
Console.WriteLine("Temizleniyor....");

mineList.Clear();
random_Mine();
flagList.Clear();
bayrakSayisi = 10;
lblMine.Text = "Flags Left: " + bayrakSayisi;



foreach (Button o in buttonList)
{
o.Image = new Bitmap("bos.jpg");
o.Enabled = true;
o.Text = "";
}
pnl.Enabled = true;

}


public Form1()
{

InitializeComponent();
}


private void Form1_Load(object sender, EventArgs e)
{

GUI();
random_Mine();



}
}
}



Resim dosyalarını ve projenin diğer bütün dosyalarını buradan bulabilirsiniz.

No comments: