using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace C_Sharp_Sos_5x5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Random rastgele = null;
int satır_sayısı = 5;
int sütun_sayısı = 5;
TextBox[,] alanlar;
private void Form1_Load(object sender, EventArgs e)
{
rastgele = new Random();
alanlar = new TextBox[satır_sayısı, sütun_sayısı];
for (int i = 0; i < satır_sayısı; i++)
{
for (int j = 0; j < sütun_sayısı; j++)
{
TextBox yenitextBox = new TextBox();
yenitextBox.Location = new System.Drawing.Point(50 + j * 25, i * 25 + 50);
yenitextBox.Name = i + " * " + j;
yenitextBox.Size = new System.Drawing.Size(20, 20);
yenitextBox.TabIndex = 1;
yenitextBox.BorderStyle = BorderStyle.FixedSingle;
yenitextBox.KeyUp += new KeyEventHandler(yenitextBox_KeyUp);
this.Controls.Add(yenitextBox);
alanlar[i, j] = yenitextBox;
}
}
}
void yenitextBox_KeyUp(object sender, KeyEventArgs e)
{
(sender as TextBox).Text = (sender as TextBox).Text.ToUpper();
if ((sender as TextBox).Text.Length > 1)
(sender as TextBox).Text = (sender as TextBox).Text.Substring(0,1);
else
{
bool oyunbitti = false;
oyunbitti = SosOlanlariBoya(oyunbitti);
}
}
private bool SosOlanlariBoya(bool oyunbitti)
{
for (int i = 0; i < satır_sayısı; i++)
{
for (int j = 0; j < sütun_sayısı; j++)
{
if (alanlar[i, j].Text.ToUpper() == "S")
{
try
{
if (alanlar[i, j + 1].Text.ToUpper() == "O" && alanlar[i, j + 2].Text.ToUpper() == "S")
{
oyunbitti = true;
alanlar[i, j].BackColor = Color.Lime;
alanlar[i, j + 1].BackColor = Color.Lime;
alanlar[i, j + 2].BackColor = Color.Lime;
}
}
catch (Exception)
{
}
try
{
if (alanlar[i + 1, j + 1].Text.ToUpper() == "O" && alanlar[i + 2, j + 2].Text.ToUpper() == "S")
{
oyunbitti = true;
alanlar[i, j].BackColor = Color.Lime;
alanlar[i + 1, j + 1].BackColor = Color.Lime;
alanlar[i + 2, j + 2].BackColor = Color.Lime;
}
}
catch (Exception)
{
}
try
{
if (alanlar[i + 1, j].Text.ToUpper() == "O" && alanlar[i + 2, j].Text.ToUpper() == "S")
{
oyunbitti = true;
alanlar[i, j].BackColor = Color.Lime;
alanlar[i + 1, j].BackColor = Color.Lime;
alanlar[i + 2, j].BackColor = Color.Lime;
}
}
catch (Exception)
{
}
try
{
if (alanlar[i + 1, j - 1].Text.ToUpper() == "O" && alanlar[i + 2, j - 2].Text.ToUpper() == "S")
{
oyunbitti = true;
alanlar[i, j].BackColor = Color.Lime;
alanlar[i + 1, j - 1].BackColor = Color.Lime;
alanlar[i + 2, j - 2].BackColor = Color.Lime;
}
}
catch (Exception)
{
}
}
}
}
return oyunbitti;
}
private void btnYeniOyun_Click(object sender, EventArgs e)
{
for (int i = 0; i < satır_sayısı; i++)
{
for (int j = 0; j < sütun_sayısı; j++)
{
alanlar[i, j].Text = "";
alanlar[i, j].BackColor = Color.LightGray;
if (rastgele.Next(2) == 0)
{
alanlar[i, j].Text = "S";
}
else
{
alanlar[i, j].Text = "O";
}
}
}
SosOlanlariBoya(false);
}
}
}
Hiç yorum yok :
Yorum Gönder