Kod:
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 sos
{
public partial class Sos_Oyunu : Form
{
public Sos_Oyunu()
{
InitializeComponent();
}
TextBox[,] alanlar;
int birinci_oyuncu = 0, ikinci_oyuncu = 0;
int satır_sayısı = 0, sütun_sayısı = 0;
private **** Form_oyun_Load(object sender, EventArgs e)
{
/// Sos oyun alanı 3 x 3 matriksi çeklinde olsun
///
satır_sayısı = 3;
sütun_sayısı = 3;
alanlar = new TextBox[satır_sayısı, sütun_sayısı];
/// iç içe for döngüsü kullanarak sos oyun alanını textbox lar kullanarak oluşturalım
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.******** = 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;
}
}
}
private **** yenitextBox_KeyUp(object sender, KeyEventArgs e)
{
///Hangi textbox kutucuğuna karakter girilmiş ise o kutuyu referans alalım
TextBox text = sender as TextBox;
label1.Focus();
///Eğer textbox kutucuguna birden fazla karakter girilmiş ise sadece ilk karakteri
///dikkate alalım
if (text.TextLength > 1)
{
text.Text = text.Text.Substring(0, 1);
}
text.Text = text.Text.ToUpper();
///Eğergirilen karakter S veya o değilse textbox kutucuğunu temizleyelim
if (text.Text != "S" && text.Text != "O")
{
text.Text = "";
}
///Oyun da SOS oluştu mu kontrol edelim
for (int i = 0; i < satır_sayısı; i++)
{
for (int j = 0; j < sütun_sayısı; j++)
{
bool sosOlduMu = false;
try
{
if (alanlar[i, j].Text == "S" && alanlar[i, j + 1].Text == "O" && alanlar[i, j + 2].Text == "S")
sosOlduMu = true;
}
catch (Exception) { }
try
{
if (alanlar[i, j].Text == "S" && alanlar[i, j - 1].Text == "O" && alanlar[i, j - 2].Text == "S")
sosOlduMu = true;
}
catch (Exception) { }
try
{
if (alanlar[i, j].Text == "S" && alanlar[i + 1, j].Text == "O" && alanlar[i + 2, j].Text == "S")
sosOlduMu = true;
}
catch (Exception) { }
try
{
if (alanlar[i, j].Text == "S" && alanlar[i - 1, j].Text == "O" && alanlar[i - 2, j].Text == "S")
sosOlduMu = true;
}
catch (Exception) { }
try
{
if (alanlar[i, j].Text == "S" && alanlar[i + 1, j + 1].Text == "O" && alanlar[i + 2, j + 2].Text == "S")
sosOlduMu = true;
}
catch (Exception) { }
try
{
if (alanlar[i, j].Text == "S" && alanlar[i + 1, j - 1].Text == "O" && alanlar[i + 2, j - 2].Text == "S")
sosOlduMu = true;
}
catch (Exception) { }
try
{
if (alanlar[i, j].Text == "S" && alanlar[i - 1, j - 1].Text == "O" && alanlar[i - 2, j - 2].Text == "S")
sosOlduMu = true;
}
catch (Exception) { }
try
{
if (alanlar[i, j].Text == "S" && alanlar[i - 1, j + 1].Text == "O" && alanlar[i - 2, j + 2].Text == "S")
sosOlduMu = true;
}
catch (Exception) { }
if (sosOlduMu)
{
MessageBox.Show("SOS :" + text.Text + " OYUNCUSU OYUNU KAZANDI.", "TEBRİKLER", MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
}
}
}
}
private **** buttonYeniOyun_Click(object sender, EventArgs e)
{
foreach (Control item in this.Controls)
{
if (item is TextBox)
{
(item as TextBox).Text = "";
}
}
}
}
}