Form1.cs
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 TicTacToe_Oyunu_Yazilimcik_Com
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private OyunAlani oyunAlani;
private TicTacToeOyunu oyun;
private void Form1_Load(object sender, EventArgs e)
{
// www.yazilimcik.com
}
private void btnBilgisayar_Click(object sender, EventArgs e)
{
oyunBaslat(OyuncuTipi.Bilgisayar);
}
private void btnMyself_Click(object sender, EventArgs e)
{
oyunBaslat(OyuncuTipi.Insan);
}
private void oyunBaslat(OyuncuTipi ot)
{
bilgisayarPuan = 0;
oyuncuPuan = 0;
this.lblBilgisayarPuan.Text = bilgisayarPuan.ToString();
this.lblOyuncuPuan.Text = oyuncuPuan.ToString();
btnBilgisayar.Enabled = false;
btnMyself.Enabled = false;
oyunAlani = new OyunAlani(pb);
oyunAlani.Ciz();
oyun = new TicTacToeOyunu(oyunAlani, this);
oyun.OyunBitti += new TicTacToeOyunu.oyunBitti(oyun_OyunBitti);
oyun.baslat(ot);
}
void oyun_OyunBitti()
{
btnBilgisayar.Enabled = true;
btnMyself.Enabled = true;
int pc, ins;
pc = Convert.ToInt32(lblBilgisayarPuan.Text);
ins = Convert.ToInt32(lblOyuncuPuan.Text);
if (pc == ins)
{
MessageBox.Show("Berabere..!", "Sonuç:");
}
else if (pc > ins)
{
MessageBox.Show("Üzgünüm, Bilgisayar Kazandi..!", "Sonuç:");
}
else
{
MessageBox.Show("Tebrikler, Siz Kazandiniz..!", "Sonuç:");
titret();
}
}
int sol = 0;
int ust = 0;
int sayac = 0;
private void titret()
{
sol = this.Left;
ust = this.Top;
timer1.Start();
sayac = 0;
}
private void btnKapat_Click(object sender, EventArgs e)
{
DialogResult cevap;
cevap = MessageBox.Show("Programdan Çikmak Istediginizden Emin misiniz..?\nwww.yazilimcik.com", "Uyari:", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
if (cevap == DialogResult.Yes)
{
Application.Exit();
}
}
int bilgisayarPuan = 0;
int oyuncuPuan = 0;
public void puanArtir(KareDurumu kd, int p)
{
switch (kd)
{
case KareDurumu.X:
bilgisayarPuan += p;
this.lblBilgisayarPuan.Text = bilgisayarPuan.ToString();
break;
case KareDurumu.O:
oyuncuPuan += p;
this.lblOyuncuPuan.Text = oyuncuPuan.ToString();
break;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
switch (sayac % 2)
{
case 0:
this.Left += 10;
this.Top += 10;
break;
case 1:
this.Top = ust;
this.Left = sol;
break;
}
if (sayac >= 20)
{
timer1.Stop();
}
sayac++;
}
}
}
CLASS TANIMLARI
OyunAlani.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace TicTacToe_Oyunu_Yazilimcik_Com
{
class OyunAlani
{
private Pen cizgiKalemi = new Pen(Color.White, 1.0f);
private Font yaziFontu = new Font("Verdana", 48.0f);
private Brush OBrush = new SolidBrush(Color.Gainsboro);
private Brush XBrush = new SolidBrush(Color.Blue);
private Brush AktifBrush = new SolidBrush(Color.Green);
private Brush NormalBrush = new SolidBrush(Color.DarkGreen);
private PictureBox pb = null;
public delegate void birKareyeTiklandi(int x, int y);
public event birKareyeTiklandi BirKareyeTiklandi = null;
private OyunKaresi[,] kareler = new OyunKaresi[3, 3];
internal OyunKaresi[,] Kareler
{
get { return kareler; }
set { kareler = value; }
}
public OyunAlani(PictureBox pb)
{
for (int i = 0; i
for (int j = 0; j
kareler[i, j] = new OyunKaresi();
this.pb = pb;
pb.MouseMove += new MouseEventHandler(pb_MouseMove);
pb.MouseLeave += new EventHandler(pb_MouseLeave);
pb.MouseClick += new MouseEventHandler(pb_MouseClick);
}
void pb_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (e.X > 0 && e.X 0 && e.Y
{
int ax = (e.X - (e.X % 100)) / 100;
int ay = (e.Y - (e.Y % 100)) / 100;
if (BirKareyeTiklandi != null)
BirKareyeTiklandi(ax, ay);
}
}
}
void pb_MouseLeave(object sender, EventArgs e)
{
aktifKareDegistir(-1, -1);
}
void pb_MouseMove(object sender, MouseEventArgs e)
{
int x = e.X;
int y = e.Y;
int ax = -1;
int ay = -1;
if (x > 0 && x 0 && y
{
ax = (x - (x % 100)) / 100;
ay = (y - (y % 100)) / 100;
if (kareler[ax, ay].Durum != KareDurumu.Bos)
{
ax = -1;
ay = -1;
}
}
this.aktifKareDegistir(ax, ay);
}
public void Ciz()
{
Ciz(pb.Handle);
}
public void Ciz(IntPtr ptrGrafik)
{
Graphics gr = Graphics.FromHwnd(ptrGrafik);
gr.Clear(Color.DarkGreen);
gr.DrawRectangle(cizgiKalemi, 0, 0, 300, 300);
gr.DrawLine(cizgiKalemi, 100, 0, 100, 300);
gr.DrawLine(cizgiKalemi, 200, 0, 200, 300);
gr.DrawLine(cizgiKalemi, 0, 100, 300, 100);
gr.DrawLine(cizgiKalemi, 0, 200, 300, 200);
for (int i = 0; i
for (int j = 0; j
{
if (kareler[i, j].Durum == KareDurumu.O)
{
gr.FillRectangle(OBrush, i * 100 + 1, j * 100 + 1, 99, 99);
gr.DrawString("O", yaziFontu, Brushes.Black, i * 100 + 16, j * 100 + 10);
}
else if (kareler[i, j].Durum == KareDurumu.X)
{
gr.FillRectangle(XBrush, i * 100 + 1, j * 100 + 1, 99, 99);
gr.DrawString("X", yaziFontu, Brushes.Black, i * 100 + 16, j * 100 + 10);
}
}
if (aktifKareX >= 0 && aktifKareX = 0 && aktifKareY
{
gr.FillRectangle(AktifBrush, aktifKareX * 100 + 1, aktifKareY * 100 + 1, 99, 99);
}
}
private int aktifKareX = -1;
private int aktifKareY = -1;
public bool oyunAktif = false;
public void aktifKareDegistir(int x, int y)
{
if (oyunAktif)
{
if (aktifKareX != x || aktifKareY != y)
{
Graphics gr = Graphics.FromHwnd(pb.Handle);
if (aktifKareX >= 0 && aktifKareX = 0 && aktifKareY
gr.FillRectangle(NormalBrush, aktifKareX * 100 + 1, aktifKareY * 100 + 1, 99, 99);
aktifKareX = x;
aktifKareY = y;
if (aktifKareX >= 0 && aktifKareX = 0 && aktifKareY
gr.FillRectangle(AktifBrush, aktifKareX * 100 + 1, aktifKareY * 100 + 1, 99, 99);
}
}
}
}
}
OyunKaresi.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TicTacToe_Oyunu_Yazilimcik_Com
{
class OyunKaresi
{
private KareDurumu durum = KareDurumu.Bos;
internal KareDurumu Durum
{
get { return durum; }
set { durum = value; }
}
public OyunKaresi()
{
// www.yazilimcik.com
}
public OyunKaresi(KareDurumu ilkDurum)
{
this.durum = ilkDurum;
}
}
public enum KareDurumu
{
Bos, O, X
}
}
TicTacToeOyunu.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TicTacToe_Oyunu_Yazilimcik_Com
{
class TicTacToeOyunu
{
public delegate void oyunBitti();
public event oyunBitti OyunBitti;
private OyunAlani oyunAlani;
private Form1 oyunFormu;
private OyuncuTipi hamleSirasi;
internal OyunAlani OyunAlani
{
get { return oyunAlani; }
set { oyunAlani = value; }
}
public TicTacToeOyunu(OyunAlani oa, Form1 oyunFormu)
{
this.oyunAlani = oa;
oa.BirKareyeTiklandi += new OyunAlani.birKareyeTiklandi(oa_BirKareyeTiklandi);
this.oyunFormu = oyunFormu;
}
void oa_BirKareyeTiklandi(int x, int y)
{
if (hamleSirasi == OyuncuTipi.Insan && oyunAlani.Kareler[x, y].Durum == KareDurumu.Bos)
insan_oyna(x, y);
}
public void baslat(OyuncuTipi ilkOyuncu)
{
oyunAlani.oyunAktif = true;
this.hamleSirasi = ilkOyuncu;
if (hamleSirasi == OyuncuTipi.Bilgisayar)
bilgisayar_oyna();
}
private System.Collections.ArrayList bulunanHamleler;
private int kacPuanAlir(KareDurumu tip, int x, int y)
{
int ap = 0;
this.OyunAlani.Kareler[x, y].Durum = tip;
if (oyunAlani.Kareler[0, y].Durum == tip &&
oyunAlani.Kareler[1, y].Durum == tip &&
oyunAlani.Kareler[2, y].Durum == tip)
ap++;
if (oyunAlani.Kareler[x, 0].Durum == tip &&
oyunAlani.Kareler[x, 1].Durum == tip &&
oyunAlani.Kareler[x, 2].Durum == tip)
ap++;
if (x == y)
{
if (oyunAlani.Kareler[0, 0].Durum == tip &&
oyunAlani.Kareler[1, 1].Durum == tip &&
oyunAlani.Kareler[2, 2].Durum == tip)
ap++;
}
if ((x + y) == 2)
{
if (oyunAlani.Kareler[0, 2].Durum == tip &&
oyunAlani.Kareler[1, 1].Durum == tip &&
oyunAlani.Kareler[2, 0].Durum == tip)
ap++;
}
oyunAlani.Kareler[x, y].Durum = KareDurumu.Bos;
return ap;
}
private void bs_oyna()
{
bulunanHamleler = new System.Collections.ArrayList();
int bp = 0, kp = 0;
int gp = 0;
for (int i = 0; i
{
for (int j = 0; j
{
if (oyunAlani.Kareler[i, j].Durum == KareDurumu.Bos)
{
bp = kacPuanAlir(KareDurumu.X, i, j);
oyunAlani.Kareler[i, j].Durum = KareDurumu.X;
kp = 0;
gp = 0;
for (int ii = 0; ii
{
for (int jj = 0; jj
{
if (oyunAlani.Kareler[ii, jj].Durum == KareDurumu.Bos)
{
gp = kacPuanAlir(KareDurumu.O, ii, jj);
if (gp > kp)
kp = gp;
}
}
}
oyunAlani.Kareler[i, j].Durum = KareDurumu.Bos;
bulunanHamleler.Add(new HamleKaydi(i, j, bp, kp));
}
}
}
HamleKaydi oynanacakHamle = (HamleKaydi)bulunanHamleler[0];
foreach (HamleKaydi hk in bulunanHamleler)
{
if (hk.kazanc() > oynanacakHamle.kazanc())
oynanacakHamle = hk;
}
System.Collections.ArrayList esitUygunlar = new System.Collections.ArrayList();
foreach (HamleKaydi hk in bulunanHamleler)
{
if (hk.kazanc() == oynanacakHamle.kazanc())
esitUygunlar.Add(hk);
}
int hs = esitUygunlar.Count;
int shn = rnd.Next(hs);
oynanacakHamle = (HamleKaydi)esitUygunlar[shn];
oyunAlani.Kareler[oynanacakHamle.x, oynanacakHamle.y].Durum = KareDurumu.X;
oyunAlani.Ciz();
oyunFormu.puanArtir(KareDurumu.X, oynanacakHamle.bp);
}
private void bilgisayar_oyna()
{
bs_oyna();
if (!oynanabilir())
oyunSonu();
else
{
hamleSirasi = OyuncuTipi.Insan;
}
}
Random rnd = new Random();
private void insan_oyna(int ix, int iy)
{
int ip = kacPuanAlir(KareDurumu.O, ix, iy);
oyunFormu.puanArtir(KareDurumu.O, ip);
OyunAlani.Kareler[ix, iy].Durum = KareDurumu.O;
oyunAlani.aktifKareDegistir(-1, -1);
OyunAlani.Ciz();
if (!oynanabilir())
oyunSonu();
else
{
hamleSirasi = OyuncuTipi.Bilgisayar;
bilgisayar_oyna();
}
}
public void oyunSonu()
{
oyunAlani.oyunAktif = false;
if (OyunBitti != null)
OyunBitti();
}
public bool oynanabilir()
{
for (int i = 0; i
for (int j = 0; j
if (oyunAlani.Kareler[i, j].Durum == KareDurumu.Bos)
return true;
return false;
}
class HamleKaydi
{
public int x = 0, y = 0;
public int bp = 0, kp = 0;
public HamleKaydi(int _x, int _y, int _bp, int _kp)
{
x = _x;
y = _y;
bp = _bp;
kp = _kp;
}
public int kazanc()
{
return bp - kp;
}
}
}
enum OyuncuTipi
{
Insan, Bilgisayar
}
}